/** * Any implementing HTTP providers should send a request to the provided endpoint with the parameters. * They should return, in string form, the response body and throw an exception on error. * * @param UriInterface $endpoint * @param mixed $requestBody * @param array $extraHeaders * @param string $method * * @return string * * @throws TokenResponseException * @throws \InvalidArgumentException */ public function retrieveResponse(UriInterface $endpoint, $requestBody, array $extraHeaders = array(), $method = 'POST') { // Normalize method name $method = strtoupper($method); $this->normalizeHeaders($extraHeaders); if ($method === 'GET' && !empty($requestBody)) { throw new \InvalidArgumentException('No body expected for "GET" request.'); } if (!isset($extraHeaders['Content-type']) && $method === 'POST' && is_array($requestBody)) { $extraHeaders['Content-type'] = 'Content-type: application/x-www-form-urlencoded'; } $host = 'Host: ' . $endpoint->getHost(); // Append port to Host if it has been specified if ($endpoint->hasExplicitPortSpecified()) { $host .= ':' . $endpoint->getPort(); } $extraHeaders['Host'] = $host; $extraHeaders['Connection'] = 'Connection: close'; if (is_array($requestBody)) { $requestBody = http_build_query($requestBody, '', '&'); } $extraHeaders['Content-length'] = 'Content-length: ' . strlen($requestBody); $context = $this->generateStreamContext($requestBody, $extraHeaders, $method); $level = error_reporting(0); $response = file_get_contents($endpoint->getAbsoluteUri(), false, $context); error_reporting($level); if (false === $response) { $lastError = error_get_last(); if (is_null($lastError)) { throw new TokenResponseException('Failed to request resource.'); } throw new TokenResponseException($lastError['message']); } return $response; }
/** * Any implementing HTTP providers should send a request to the provided endpoint with the parameters. * They should return, in string form, the response body and throw an exception on error. * * @param UriInterface $endpoint * @param mixed $requestBody * @param array $extraHeaders * @param string $method * * @return string * * @throws TokenResponseException * @throws \InvalidArgumentException */ public function retrieveResponse(UriInterface $endpoint, $requestBody, array $extraHeaders = array(), $method = 'POST') { // Normalize method name $method = strtoupper($method); $this->normalizeHeaders($extraHeaders); if ($method === 'GET' && !empty($requestBody)) { throw new \InvalidArgumentException('No body expected for "GET" request.'); } if (!isset($extraHeaders['Content-type']) && $method === 'POST' && is_array($requestBody)) { $extraHeaders['Content-type'] = 'Content-type: application/x-www-form-urlencoded'; } $extraHeaders['Host'] = 'Host: ' . $endpoint->getHost(); $extraHeaders['Connection'] = 'Connection: close'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $endpoint->getAbsoluteUri()); if ($method === 'POST' || $method === 'PUT') { if ($requestBody && is_array($requestBody)) { $requestBody = http_build_query($requestBody, '', '&'); } if ($method === 'PUT') { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); } else { curl_setopt($ch, CURLOPT_POST, true); } curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); } else { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); } if ($this->maxRedirects > 0) { // Handle open_basedir & safe mode if (!ini_get('safe_mode') && !ini_get('open_basedir')) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); } curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects); } curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $extraHeaders); curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); foreach ($this->parameters as $key => $value) { curl_setopt($ch, $key, $value); } if ($this->forceSSL3) { curl_setopt($ch, CURLOPT_SSLVERSION, 3); } $response = curl_exec($ch); $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (false === $response) { $errNo = curl_errno($ch); $errStr = curl_error($ch); curl_close($ch); if (empty($errStr)) { throw new TokenResponseException('Failed to request resource.', $responseCode); } throw new TokenResponseException('cURL Error # ' . $errNo . ': ' . $errStr, $responseCode); } curl_close($ch); return $response; }
/** * Any implementing HTTP providers should send a request to the provided endpoint with the parameters. * They should return, in string form, the response body and throw an exception on error. * * @param UriInterface $endpoint * @param mixed $requestBody * @param array $extraHeaders * @param string $method * * @return string * * @throws TokenResponseException */ public function retrieveResponse(UriInterface $endpoint, $requestBody, array $extraHeaders = array(), $method = 'POST') { $http = new \DokuHTTPClient(); $http->headers = array_merge($http->headers, $extraHeaders); $ok = $http->sendRequest($endpoint->getAbsoluteUri(), $requestBody, $method); if (!$ok) { throw new TokenResponseException($http->error); } return $http->resp_body; }
/** * {@inheritdoc} */ public function retrieveResponse(UriInterface $endpoint, $requestBody, array $extraHeaders = [], $method = 'POST') { if (!$this->client) { $this->client = new BaseHttpClient(); } $request = new Request(new Uri($endpoint->getAbsoluteUri())); $request->setMethod($method); if ($requestBody && is_array($requestBody)) { $request->setPosts($requestBody); } foreach ($extraHeaders as $name => $value) { $request->addHeader($name, $value); } $response = $this->client->send($request); return $response->getBody(); }
/** * Returns the cached current_uri object or creates and caches it if it is * not already created. In each case the query string is updated based on * the $query parameter. * * @param string $service_name The name of the service * @param string $query The query string of the current_uri * used in redirects * @return \OAuth\Common\Http\Uri\UriInterface */ protected function get_current_uri($service_name, $query) { if ($this->current_uri) { $this->current_uri->setQuery($query); return $this->current_uri; } $uri_factory = new \OAuth\Common\Http\Uri\UriFactory(); $current_uri = $uri_factory->createFromSuperGlobalArray($this->request->get_super_global(\phpbb\request\request_interface::SERVER)); $current_uri->setQuery($query); $this->current_uri = $current_uri; return $current_uri; }
/** * Returns the cached current_uri object or creates and caches it if it is * not already created. In each case the query string is updated based on * the $query parameter. * * @param string $service_name The name of the service * @param string $query The query string of the current_uri * used in redirects * @return \OAuth\Common\Http\Uri\UriInterface */ protected function get_current_uri($service_name, $query) { if ($this->current_uri) { $this->current_uri->setQuery($query); return $this->current_uri; } $uri_factory = new \OAuth\Common\Http\Uri\UriFactory(); $super_globals = $this->request->get_super_global(\phpbb\request\request_interface::SERVER); if (!empty($super_globals['HTTP_X_FORWARDED_PROTO']) && $super_globals['HTTP_X_FORWARDED_PROTO'] === 'https') { $super_globals['HTTPS'] = 'on'; $super_globals['SERVER_PORT'] = 443; } $current_uri = $uri_factory->createFromSuperGlobalArray($super_globals); $current_uri->setQuery($query); $this->current_uri = $current_uri; return $current_uri; }
/** * @param UriInterface $uri * @param array $params * @param string $method * * @return string */ public function getSignature(UriInterface $uri, array $params, $method = 'POST') { parse_str($uri->getQuery(), $queryStringData); foreach (array_merge($queryStringData, $params) as $key => $value) { $signatureData[rawurlencode($key)] = rawurlencode($value); } ksort($signatureData); // determine base uri $baseUri = $uri->getScheme() . '://' . $uri->getRawAuthority(); if ('/' === $uri->getPath()) { $baseUri .= $uri->hasExplicitTrailingHostSlash() ? '/' : ''; } else { $baseUri .= $uri->getPath(); } $baseString = strtoupper($method) . '&'; $baseString .= rawurlencode($baseUri) . '&'; $baseString .= rawurlencode($this->buildSignatureDataString($signatureData)); return base64_encode($this->hash($baseString)); }
/** * {@inheritdoc} * * In addition to the original method, allows array parameters for filters. */ public function getSignature(UriInterface $uri, array $params, $method = 'POST') { $queryStringData = !$uri->getQuery() ? [] : array_reduce(explode('&', $uri->getQuery()), function ($carry, $item) { list($key, $value) = explode('=', $item, 2); $carry[rawurldecode($key)] = rawurldecode($value); return $carry; }, []); foreach (array_merge($queryStringData, $params) as $key => $value) { $signatureData[rawurlencode($key)] = rawurlencode($value); } ksort($signatureData); // determine base uri $baseUri = $uri->getScheme() . '://' . $uri->getRawAuthority(); if ('/' == $uri->getPath()) { $baseUri .= $uri->hasExplicitTrailingHostSlash() ? '/' : ''; } else { $baseUri .= $uri->getPath(); } $baseString = strtoupper($method) . '&'; $baseString .= rawurlencode($baseUri) . '&'; $baseString .= rawurlencode($this->buildSignatureDataString($signatureData)); return base64_encode($this->hash($baseString)); }
/** * @return string * @throws TokenResponseException */ protected function streams() { $this->prepareRequest(); $context = stream_context_create(array('http' => array('method' => $this->method, 'header' => array_values($this->extraHeaders), 'content' => $this->requestBody, 'protocol_version' => '1.1', 'user_agent' => 'Yii2 EAuth Client', 'max_redirects' => $this->maxRedirects, 'timeout' => $this->timeout))); $level = error_reporting(0); $response = file_get_contents($this->endpoint->getAbsoluteUri(), false, $context); error_reporting($level); if (YII_DEBUG) { Yii::trace('EAuth http response: ' . PHP_EOL . var_export($response, true), __NAMESPACE__); } if (false === $response) { $lastError = error_get_last(); if (is_null($lastError)) { $errStr = 'Failed to request resource.'; } else { $errStr = $lastError['message']; } Yii::error('EAuth streams error: ' . $errStr, __NAMESPACE__); throw new TokenResponseException($errStr); } return $response; }
/** * Builds the full URI based on all the properties * * @return string */ public function getCurrentUri() { return $this->currentUri->getAbsoluteUri(); }