protected function getDefaultOptions(RequestInterface $request, RequestMediator $mediator)
 {
     $url = $request->getUrl();
     // Strip fragment from URL. See:
     // https://github.com/guzzle/guzzle/issues/453
     if (($pos = strpos($url, '#')) !== false) {
         $url = substr($url, 0, $pos);
     }
     $config = $request->getConfig();
     $options = array(CURLOPT_URL => $url, CURLOPT_CONNECTTIMEOUT => $config['connect_timeout'] ?: 150, CURLOPT_RETURNTRANSFER => false, CURLOPT_HEADER => false, CURLOPT_WRITEFUNCTION => array($mediator, 'writeResponseBody'), CURLOPT_HEADERFUNCTION => array($mediator, 'receiveResponseHeader'), CURLOPT_READFUNCTION => array($mediator, 'readRequestBody'), CURLOPT_HTTP_VERSION => $request->getProtocolVersion() === '1.0' ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1, CURLOPT_SSL_VERIFYPEER => 1, CURLOPT_SSL_VERIFYHOST => 2, '_headers' => $request->getHeaders());
     if (defined('CURLOPT_PROTOCOLS')) {
         // Allow only HTTP and HTTPS protocols
         $options[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
     }
     // Add CURLOPT_ENCODING if Accept-Encoding header is provided
     if ($request->hasHeader('Accept-Encoding')) {
         $options[CURLOPT_ENCODING] = $request->getHeader('Accept-Encoding');
         // Let cURL set the Accept-Encoding header. Without this change
         // curl could add a duplicate value.
         $this->removeHeader('Accept-Encoding', $options);
     }
     // cURL sometimes adds a content-type by default. Prevent this.
     if (!$request->hasHeader('Content-Type')) {
         $options[CURLOPT_HTTPHEADER][] = 'Content-Type:';
     }
     return $options;
 }
 /**
  * @param RequestInterface|ResponseInterface $message
  * @return string
  */
 protected function formatHeaders($message)
 {
     $headers = [];
     foreach ($message->getHeaders() as $header => $value) {
         $headers[] = sprintf('%s: %s', $header, implode("\n  : ", $value));
     }
     return implode("\n", $headers);
 }
Exemplo n.º 3
0
 /**
  * Creates a Ring request from a request object.
  *
  * This function does not hook up the "then" and "progress" events that
  * would be required for actually sending a Guzzle request through a
  * RingPHP handler.
  *
  * @param RequestInterface $request Request to convert.
  *
  * @return array Converted Guzzle Ring request.
  */
 public static function createRingRequest(RequestInterface $request)
 {
     $options = $request->getConfig()->toArray();
     $url = $request->getUrl();
     // No need to calculate the query string twice (in URL and query).
     $qs = ($pos = strpos($url, '?')) ? substr($url, $pos + 1) : null;
     return ['scheme' => $request->getScheme(), 'http_method' => $request->getMethod(), 'url' => $url, 'uri' => $request->getPath(), 'headers' => $request->getHeaders(), 'body' => $request->getBody(), 'version' => $request->getProtocolVersion(), 'client' => $options, 'query_string' => $qs, 'future' => isset($options['future']) ? $options['future'] : false];
 }
Exemplo n.º 4
0
 protected function getDefaultOptions(RequestInterface $request, RequestMediator $mediator)
 {
     $url = $request->getUrl();
     // Strip fragment from URL. See:
     // https://github.com/guzzle/guzzle/issues/453
     if (($pos = strpos($url, '#')) !== false) {
         $url = substr($url, 0, $pos);
     }
     $config = $request->getConfig();
     $options = [CURLOPT_URL => $url, CURLOPT_CONNECTTIMEOUT => $config['connect_timeout'] ?: 150, CURLOPT_RETURNTRANSFER => false, CURLOPT_HEADER => false, CURLOPT_WRITEFUNCTION => [$mediator, 'writeResponseBody'], CURLOPT_HEADERFUNCTION => [$mediator, 'receiveResponseHeader'], CURLOPT_READFUNCTION => [$mediator, 'readRequestBody'], CURLOPT_HTTP_VERSION => $request->getProtocolVersion() === '1.0' ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1, CURLOPT_SSL_VERIFYPEER => 1, CURLOPT_SSL_VERIFYHOST => 2, '_headers' => $request->getHeaders()];
     if (defined('CURLOPT_PROTOCOLS')) {
         // Allow only HTTP and HTTPS protocols
         $options[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
     }
     // cURL sometimes adds a content-type by default. Prevent this.
     if (!$request->hasHeader('Content-Type')) {
         $options[CURLOPT_HTTPHEADER][] = 'Content-Type:';
     }
     return $options;
 }
Exemplo n.º 5
0
 private function getRequestHeaderAsString(RequestInterface $request)
 {
     $message = vsprintf('%s %s HTTP/%s', [strtoupper($request->getMethod()), $request->getUrl(), $request->getProtocolVersion()]) . "\r\n";
     foreach ($request->getHeaders() as $name => $values) {
         $message .= $name . ': ' . implode(', ', $values) . "\r\n";
     }
     $message .= "\r\n";
     return $message;
 }
Exemplo n.º 6
0
 /**
  * @param RequestInterface $request
  */
 protected function extractHeadersArgument(RequestInterface $request)
 {
     $url = Url::fromString($request->getUrl());
     foreach ($request->getHeaders() as $name => $header) {
         if ('host' === strtolower($name) && $header[0] === $url->getHost()) {
             continue;
         }
         if ('user-agent' === strtolower($name)) {
             $this->addOption('A', escapeshellarg($header[0]));
             continue;
         }
         foreach ((array) $header as $headerValue) {
             $this->addOption('H', escapeshellarg("{$name}: {$headerValue}"));
         }
     }
 }
Exemplo n.º 7
0
 private function getDefaultOptions(RequestInterface $request)
 {
     $headers = '';
     foreach ($request->getHeaders() as $name => $values) {
         $headers .= $name . ': ' . implode(', ', $values) . "\r\n";
     }
     return ['http' => ['method' => $request->getMethod(), 'header' => trim($headers), 'protocol_version' => $request->getProtocolVersion(), 'ignore_errors' => true, 'follow_location' => 0, 'content' => (string) $request->getBody()]];
 }
Exemplo n.º 8
0
 /**
  * Handle an error. We handle errors by throwing an exception.
  *
  * @param string $error An error code representing the error
  *                      (use_underscore_separators).
  * @param string|null $message The error message.
  * @param \GuzzleHttp\Message\RequestInterface|null $request Optional. The
  *                                                  Guzzle request object.
  * @param \GuzzleHttp\Message\ResponseInterface|null $response Optional. The
  *                                                   Guzzle response object.
  *
  * @return void
  * @throws \Box\View\BoxViewException
  */
 protected static function error($error, $message = null, $request = null, $response = null)
 {
     if (!empty($request)) {
         $message .= "\n";
         $message .= 'Method: ' . $request->getMethod() . "\n";
         $message .= 'URL: ' . $request->getUrl() . "\n";
         $message .= 'Query: ' . json_encode($request->getQuery()->toArray()) . "\n";
         $message .= 'Headers: ' . json_encode($request->getHeaders()) . "\n";
         $message .= 'Request Body: ' . $request->getBody() . "\n";
     }
     if (!empty($response)) {
         $message .= "\n";
         $message .= 'Response Body: ' . $response->getBody() . "\n";
     }
     $exception = new BoxViewException($message);
     $exception->errorCode = $error;
     throw $exception;
 }
 /**
  * @param \GuzzleHttp\Message\RequestInterface $request
  * @return \SlevomatZboziApi\Request\ZboziApiRequest
  */
 private function getZboziApiRequest(\GuzzleHttp\Message\RequestInterface $request)
 {
     return new ZboziApiRequest($request->getMethod(), $request->getUrl(), $request->getHeaders(), $request->getBody() === null ? null : json_decode((string) $request->getBody()));
 }
Exemplo n.º 10
0
 private function createCanonicalizedAmzHeaders(RequestInterface $request)
 {
     $headers = [];
     foreach ($request->getHeaders() as $name => $header) {
         $name = strtolower($name);
         if (strpos($name, 'x-amz-') === 0) {
             $value = implode(',', $header);
             if (strlen($value) > 0) {
                 $headers[$name] = $name . ':' . $value;
             }
         }
     }
     if (!$headers) {
         return '';
     }
     ksort($headers);
     return implode("\n", $headers) . "\n";
 }
Exemplo n.º 11
0
 private function moveHeadersToQuery(RequestInterface $request)
 {
     $query = $request->getQuery();
     foreach ($request->getHeaders() as $name => $header) {
         $name = strtolower($name);
         if (substr($name, 0, 5) == 'x-amz') {
             $query[$name] = $header;
         }
         if ($name !== 'host') {
             $request->removeHeader($name);
         }
     }
 }
Exemplo n.º 12
0
 /**
  * Collect & sanitize data about a Guzzle request.
  *
  * @param RequestInterface $request Guzzle request.
  * @return array
  */
 private function collectRequest(RequestInterface $request)
 {
     $query = $request->getQuery();
     return ['headers' => $request->getHeaders(), 'method' => $request->getMethod(), 'scheme' => $request->getScheme(), 'host' => $request->getHost(), 'path' => $request->getPath(), 'query' => (string) $query, 'queryParams' => $query->toArray(), 'body' => (string) $request->getBody()];
 }