Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function handleRequest(RequestInterface $request, callable $next, callable $first)
 {
     // Check in storage
     if (array_key_exists($request->getRequestTarget(), $this->redirectStorage)) {
         $uri = $this->redirectStorage[$request->getRequestTarget()]['uri'];
         $statusCode = $this->redirectStorage[$request->getRequestTarget()]['status'];
         $redirectRequest = $this->buildRedirectRequest($request, $uri, $statusCode);
         return $first($redirectRequest);
     }
     return $next($request)->then(function (ResponseInterface $response) use($request, $first) {
         $statusCode = $response->getStatusCode();
         if (!array_key_exists($statusCode, $this->redirectCodes)) {
             return $response;
         }
         $uri = $this->createUri($response, $request);
         $redirectRequest = $this->buildRedirectRequest($request, $uri, $statusCode);
         $chainIdentifier = spl_object_hash((object) $first);
         if (!array_key_exists($chainIdentifier, $this->circularDetection)) {
             $this->circularDetection[$chainIdentifier] = [];
         }
         $this->circularDetection[$chainIdentifier][] = $request->getRequestTarget();
         if (in_array($redirectRequest->getRequestTarget(), $this->circularDetection[$chainIdentifier])) {
             throw new CircularRedirectionException('Circular redirection detected', $request, $response);
         }
         if ($this->redirectCodes[$statusCode]['permanent']) {
             $this->redirectStorage[$request->getRequestTarget()] = ['uri' => $uri, 'status' => $statusCode];
         }
         // Call redirect request in synchrone
         $redirectPromise = $first($redirectRequest);
         return $redirectPromise->wait();
     });
 }
Ejemplo n.º 2
0
 /**
  * Send a request to the server and return a Response object with the response.
  *
  * @param   RequestInterface $request The request object to send.
  *
  * @return  ResponseInterface
  *
  * @since   2.1
  */
 public function request(RequestInterface $request)
 {
     $uri = $request->getUri()->withPath(null)->withQuery(null)->withFragment(null);
     $uri = $uri . $request->getRequestTarget();
     $request = $request->withRequestTarget($uri);
     return $this->doRequest($request);
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function formatRequest(RequestInterface $request)
 {
     $message = sprintf("%s %s HTTP/%s\n", $request->getMethod(), $request->getRequestTarget(), $request->getProtocolVersion());
     foreach ($request->getHeaders() as $name => $values) {
         $message .= $name . ': ' . implode(', ', $values) . "\n";
     }
     return $this->addBody($request, $message);
 }
Ejemplo n.º 4
0
 /**
  * Produce the header of request as a string based on a PSR Request
  *
  * @param RequestInterface $request
  *
  * @return string
  */
 protected function transformRequestHeadersToString(RequestInterface $request)
 {
     $message = vsprintf('%s %s HTTP/%s', [strtoupper($request->getMethod()), $request->getRequestTarget(), $request->getProtocolVersion()]) . "\r\n";
     foreach ($request->getHeaders() as $name => $values) {
         $message .= $name . ': ' . implode(', ', $values) . "\r\n";
     }
     $message .= "\r\n";
     return $message;
 }
 function it_creates_an_http_exception(RequestInterface $request, ResponseInterface $response)
 {
     $request->getRequestTarget()->willReturn('/uri');
     $request->getMethod()->willReturn('GET');
     $response->getStatusCode()->willReturn(100);
     $response->getReasonPhrase()->willReturn('Continue');
     $e = $this->create($request, $response);
     $e->shouldHaveType('Http\\Client\\Exception\\HttpException');
     $e->getMessage()->shouldReturn('[url] /uri [http method] GET [status code] 100 [reason phrase] Continue');
 }
Ejemplo n.º 6
0
 protected function getFileName(RequestInterface $request)
 {
     $result = trim($request->getMethod() . ' ' . $request->getRequestTarget()) . ' HTTP/' . $request->getProtocolVersion();
     foreach ($request->getHeaders() as $name => $values) {
         if (array_key_exists(strtoupper($name), $this->ignored_headers)) {
             continue;
         }
         $result .= "\r\n{$name}: " . implode(', ', $values);
     }
     $request = $result . "\r\n\r\n" . $request->getBody();
     return md5((string) $request) . ".txt";
 }
Ejemplo n.º 7
0
 /**
  * Serialize a request message to a string.
  *
  * @param RequestInterface $request
  * @return string
  */
 public static function toString(RequestInterface $request)
 {
     $headers = self::serializeHeaders($request->getHeaders());
     $body = (string) $request->getBody();
     $format = '%s %s HTTP/%s%s%s';
     if (!empty($headers)) {
         $headers = "\r\n" . $headers;
     }
     if (!empty($body)) {
         $headers .= "\r\n\r\n";
     }
     return sprintf($format, $request->getMethod(), $request->getRequestTarget(), $request->getProtocolVersion(), $headers, $body);
 }
Ejemplo n.º 8
0
 public static function create(RequestInterface $request, ResponseInterface $response, HalResource $resource, $previous = null, $message = null)
 {
     if (!$message) {
         $code = $response->getStatusCode();
         if ($code >= 400 && $code < 500) {
             $message = 'Client error';
         } elseif ($code >= 500 && $code < 600) {
             $message = 'Server error';
         } else {
             $message = 'Unsuccessful response';
         }
     }
     $message = sprintf('%s [url] %s [http method] %s [status code] %s [reason phrase] %s.', $message, $request->getRequestTarget(), $request->getMethod(), $response->getStatusCode(), $response->getReasonPhrase());
     return new self($message, $request, $response, $resource, $previous);
 }
Ejemplo n.º 9
0
 /**
  * Serialize a request message to a string.
  *
  * @param RequestInterface $request
  * @return string
  */
 public static function toString(RequestInterface $request)
 {
     $httpMethod = $request->getMethod();
     if (empty($httpMethod)) {
         throw new UnexpectedValueException('Object can not be serialized because HTTP method is empty');
     }
     $headers = self::serializeHeaders($request->getHeaders());
     $body = (string) $request->getBody();
     $format = '%s %s HTTP/%s%s%s';
     if (!empty($headers)) {
         $headers = "\r\n" . $headers;
     }
     if (!empty($body)) {
         $headers .= "\r\n\r\n";
     }
     return sprintf($format, $httpMethod, $request->getRequestTarget(), $request->getProtocolVersion(), $headers, $body);
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public static function create(HttpRequestInterface $request, HttpResponseInterface $response = null, Exception $previous = null, array $handlerContext = null)
 {
     if ($request instanceof RequestInterface && $response instanceof ResponseInterface) {
         static $clientErrorCodes = [-32600, -32601, -32602, -32700];
         $errorCode = $response->getRpcErrorCode();
         if (in_array($errorCode, $clientErrorCodes)) {
             $label = 'Client RPC error response';
             $className = __NAMESPACE__ . '\\ClientException';
         } else {
             $label = 'Server RPC error response';
             $className = __NAMESPACE__ . '\\ServerException';
         }
         $message = $label . ' [uri] ' . $request->getRequestTarget() . ' [method] ' . $request->getRpcMethod() . ' [error code] ' . $errorCode . ' [error message] ' . $response->getRpcErrorMessage();
         return new $className($message, $request, $response, $previous);
     }
     return parent::create($request, $response, $previous);
 }
Ejemplo n.º 11
0
 /**
  * Normalize a request to string.
  *
  * @param RequestInterface $request
  *
  * @return string
  */
 public function normalizeRequestToString(RequestInterface $request)
 {
     return sprintf('%s %s %s', $request->getMethod(), $request->getRequestTarget(), $request->getProtocolVersion());
 }
Ejemplo n.º 12
0
 protected function runMatches(RequestInterface $request)
 {
     return $request->getRequestTarget() == $this->expected;
 }
Ejemplo n.º 13
0
 /**
  * @return string
  */
 public function getRequestTarget()
 {
     return $this->request->getRequestTarget();
 }
Ejemplo n.º 14
0
 /**
  * Factory method to create a new exception with a normalized error message
  *
  * @param RequestInterface  $request
  * @param ResponseInterface $response
  * @param \Exception|null   $previous
  *
  * @return HttpException
  */
 public static function create(RequestInterface $request, ResponseInterface $response, \Exception $previous = null)
 {
     $message = sprintf('[url] %s [http method] %s [status code] %s [reason phrase] %s', $request->getRequestTarget(), $request->getMethod(), $response->getStatusCode(), $response->getReasonPhrase());
     return new self($message, $request, $response, $previous);
 }
Ejemplo n.º 15
0
 /**
  * GetName
  *
  * @param Request $request request
  *
  * @return string
  *
  * @access protected
  */
 protected function getName(Request $request)
 {
     if (!($path = $request->getAttribute('jnjxp/viewd:script'))) {
         $path = parse_url($request->getRequestTarget(), PHP_URL_PATH);
         $path = trim($path, '/');
     }
     $name = $this->prefix . DIRECTORY_SEPARATOR . ($path ? $path : 'index');
     return $name;
 }
Ejemplo n.º 16
0
 private function send(RequestInterface $request, array $params = [])
 {
     $this->lastRequest = $request;
     $options = $this->prepareOptions($request->getMethod(), $request->getRequestTarget(), $params);
     try {
         $this->lastResponse = $response = $this->transport->send($request, $options);
     } catch (RequestException $e) {
         throw HttpException::wrap($e);
     }
     if ($this->logger) {
         $this->logWarnings($response);
     }
     return $response;
 }
Ejemplo n.º 17
0
 /**
  * {@inheritdoc}
  */
 public function send(RequestInterface $request, array $options = array())
 {
     static $ports = array('https' => 443, 'http' => 80, '' => 80);
     $options += Client::$defaultOptions;
     $uri = $request->getUri();
     $targetHost = $host = $uri->getHost();
     $targetPort = $port = $uri->getPort() ? $uri->getPort() : $ports[$uri->getScheme()];
     $requestTarget = $request->getRequestTarget();
     if ($options['proxy']) {
         list($host, $port) = explode(':', $options['proxy']);
         $requestTarget = (string) $uri;
     }
     if (!($stream = $this->getConnection($host, $port, $errno, $errstr, $options, $uri))) {
         throw new Exception(sprintf("Couldn't connect to %s:%s. %s - %s", $host, $port, $errno, $errstr));
     }
     $httpsThroughSocks = $options['proxy'] && $options['proxy_type'] != Client::PROXY_HTTP && $uri->getScheme() == 'https';
     $headers = \EasyRequest\get_headers($request);
     $headers[] = 'Connection: close';
     // handle proxy
     if ($options['proxy']) {
         try {
             switch ($options['proxy_type']) {
                 case Client::PROXY_HTTP:
                     if ($options['proxy_userpwd']) {
                         $headers[] = 'Proxy-Authorization: Basic ' . base64_encode($options['proxy_userpwd']);
                     }
                     break;
                 case Client::PROXY_SOCKS5:
                     $this->handleSocks5($stream, $options, $targetHost, $targetPort);
                     break;
                 case Client::PROXY_SOCKS4:
                 case Client::PROXY_SOCKS4A:
                     if ($options['proxy_userpwd']) {
                         // socks4 does not support authentication,
                         // if user give a wrong version, just handle it
                         $this->handleSocks5($stream, $options, $targetHost, $targetPort);
                     } else {
                         $this->handleSocks4($stream, $options, $targetHost, $targetPort);
                     }
                     break;
             }
         } catch (\Exception $e) {
             fclose($stream);
             throw $e;
         }
         if ($httpsThroughSocks) {
             $this->toggleCrypto($stream, true);
         }
     }
     // build request header
     $header = sprintf("%s %s HTTP/%s\r\n", $request->getMethod(), $requestTarget, $request->getProtocolVersion());
     $header .= implode("\r\n", $headers) . "\r\n";
     $header .= "\r\n";
     $this->sendRequest($header, $request->getBody(), $options, function ($message) use($stream) {
         fwrite($stream, $message);
     });
     $request->getBody()->close();
     // ignore some warning such as "SSL: Connection reset by peer",
     // this issue sometimes happen on some SOCKS servers while
     // browsing to HTTPS website.
     $httpsThroughSocks && ($level = error_reporting(~E_WARNING));
     $response = '';
     while (!feof($stream)) {
         $response .= fgets($stream, 1024);
     }
     fclose($stream);
     $httpsThroughSocks && error_reporting($level);
     list($header, $body) = explode("\r\n\r\n", $response, 2) + array('', '');
     $response = Response::parse($header, '');
     if ($response->getHeaderLine('Transfer-Encoding') == 'chunked') {
         $body = \EasyRequest\decode_chunked($body);
     }
     if ($response->getHeaderLine('Content-Encoding') == 'gzip') {
         $body = \EasyRequest\decode_gzip($body);
     }
     $response->getBody()->write($body);
     return $response;
 }
Ejemplo n.º 18
0
 /**
  * Generates the event name.
  *
  * @param RequestInterface $request
  *
  * @return string
  */
 private function getStopwatchEventName(RequestInterface $request)
 {
     return sprintf('%s %s', $request->getMethod(), $request->getRequestTarget());
 }