Ejemplo n.º 1
0
 private function modifyRedirectRequest(RequestInterface $request, ResponseInterface $response)
 {
     $config = $request->getConfig();
     $protocols = $config->getPath('redirect/protocols') ?: ['http', 'https'];
     // Use a GET request if this is an entity enclosing request and we are
     // not forcing RFC compliance, but rather emulating what all browsers
     // would do.
     $statusCode = $response->getStatusCode();
     if ($statusCode == 303 || $statusCode <= 302 && $request->getBody() && !$config->getPath('redirect/strict')) {
         $request->setMethod('GET');
         $request->setBody(null);
     }
     $previousUrl = $request->getUrl();
     $this->setRedirectUrl($request, $response, $protocols);
     $this->rewindEntityBody($request);
     // Add the Referer header if it is told to do so and only
     // add the header if we are not redirecting from https to http.
     if ($config->getPath('redirect/referer') && ($request->getScheme() == 'https' || $request->getScheme() == $config['redirect_scheme'])) {
         $url = Url::fromString($previousUrl);
         $url->setUsername(null);
         $url->setPassword(null);
         $request->setHeader('Referer', (string) $url);
     } else {
         $request->removeHeader('Referer');
     }
 }
Ejemplo n.º 2
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];
 }
Ejemplo n.º 3
0
 private function addExpectHeader(RequestInterface $request, StreamInterface $body)
 {
     // Determine if the Expect header should be used
     if ($request->hasHeader('Expect')) {
         return;
     }
     $expect = $request->getConfig()['expect'];
     // Return if disabled or if you're not using HTTP/1.1
     if ($expect === false || $request->getProtocolVersion() !== '1.1') {
         return;
     }
     // The expect header is unconditionally enabled
     if ($expect === true) {
         $request->setHeader('Expect', '100-Continue');
         return;
     }
     // By default, send the expect header when the payload is > 1mb
     if ($expect === null) {
         $expect = 1048576;
     }
     // Always add if the body cannot be rewound, the size cannot be
     // determined, or the size is greater than the cutoff threshold
     $size = $body->getSize();
     if ($size === null || $size >= (int) $expect || !$body->isSeekable()) {
         $request->setHeader('Expect', '100-Continue');
     }
 }
Ejemplo n.º 4
0
 protected function applyOptions(RequestInterface $request, array $options = [])
 {
     $config = $request->getConfig();
     $emitter = $request->getEmitter();
     foreach ($options as $key => $value) {
         if (isset(self::$configMap[$key])) {
             $config[$key] = $value;
             continue;
         }
         switch ($key) {
             case 'allow_redirects':
                 if ($value === false) {
                     continue;
                 }
                 if ($value === true) {
                     $value = self::$defaultRedirect;
                 } elseif (!is_array($value)) {
                     throw new Iae('allow_redirects must be true, false, or array');
                 } else {
                     // Merge the default settings with the provided settings
                     $value += self::$defaultRedirect;
                 }
                 $config['redirect'] = $value;
                 $emitter->attach($this->redirectPlugin);
                 break;
             case 'decode_content':
                 if ($value === false) {
                     continue;
                 }
                 $config['decode_content'] = true;
                 if ($value !== true) {
                     $request->setHeader('Accept-Encoding', $value);
                 }
                 break;
             case 'headers':
                 if (!is_array($value)) {
                     throw new Iae('header value must be an array');
                 }
                 foreach ($value as $k => $v) {
                     $request->setHeader($k, $v);
                 }
                 break;
             case 'exceptions':
                 if ($value === true) {
                     $emitter->attach($this->errorPlugin);
                 }
                 break;
             case 'body':
                 if (is_array($value)) {
                     $this->addPostData($request, $value);
                 } elseif ($value !== null) {
                     $request->setBody(Stream::factory($value));
                 }
                 break;
             case 'auth':
                 if (!$value) {
                     continue;
                 }
                 if (is_array($value)) {
                     $type = isset($value[2]) ? strtolower($value[2]) : 'basic';
                 } else {
                     $type = strtolower($value);
                 }
                 $config['auth'] = $value;
                 if ($type == 'basic') {
                     $request->setHeader('Authorization', 'Basic ' . base64_encode("{$value['0']}:{$value['1']}"));
                 } elseif ($type == 'digest') {
                     // @todo: Do not rely on curl
                     $config->setPath('curl/' . CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
                     $config->setPath('curl/' . CURLOPT_USERPWD, "{$value['0']}:{$value['1']}");
                 }
                 break;
             case 'query':
                 if ($value instanceof Query) {
                     $original = $request->getQuery();
                     // Do not overwrite existing query string variables by
                     // overwriting the object with the query string data passed
                     // in the URL
                     $value->overwriteWith($original->toArray());
                     $request->setQuery($value);
                 } elseif (is_array($value)) {
                     // Do not overwrite existing query string variables
                     $query = $request->getQuery();
                     foreach ($value as $k => $v) {
                         if (!isset($query[$k])) {
                             $query[$k] = $v;
                         }
                     }
                 } else {
                     throw new Iae('query must be an array or Query object');
                 }
                 break;
             case 'cookies':
                 if ($value === true) {
                     static $cookie = null;
                     if (!$cookie) {
                         $cookie = new Cookie();
                     }
                     $emitter->attach($cookie);
                 } elseif (is_array($value)) {
                     $emitter->attach(new Cookie(CookieJar::fromArray($value, $request->getHost())));
                 } elseif ($value instanceof CookieJarInterface) {
                     $emitter->attach(new Cookie($value));
                 } elseif ($value !== false) {
                     throw new Iae('cookies must be an array, true, or CookieJarInterface');
                 }
                 break;
             case 'events':
                 if (!is_array($value)) {
                     throw new Iae('events must be an array');
                 }
                 $this->attachListeners($request, $this->prepareListeners($value, ['before', 'complete', 'error', 'progress', 'end']));
                 break;
             case 'subscribers':
                 if (!is_array($value)) {
                     throw new Iae('subscribers must be an array');
                 }
                 foreach ($value as $subscribers) {
                     $emitter->attach($subscribers);
                 }
                 break;
             case 'json':
                 $request->setBody(Stream::factory(json_encode($value)));
                 if (!$request->hasHeader('Content-Type')) {
                     $request->setHeader('Content-Type', 'application/json');
                 }
                 break;
             default:
                 // Check for custom handler functions.
                 if (isset($this->customOptions[$key])) {
                     $fn = $this->customOptions[$key];
                     $fn($request, $value);
                     continue;
                 }
                 throw new Iae("No method can handle the {$key} config key");
         }
     }
 }
Ejemplo n.º 5
0
 public function send(RequestInterface $request)
 {
     $isFuture = $request->getConfig()->get('future');
     $trans = new Transaction($this, $request, $isFuture);
     $fn = $this->fsm;
     try {
         $fn($trans);
         if ($isFuture) {
             // Turn the normal response into a future if needed.
             return $trans->response instanceof FutureInterface ? $trans->response : new FutureResponse(new FulfilledPromise($trans->response));
         }
         // Resolve deep futures if this is not a future
         // transaction. This accounts for things like retries
         // that do not have an immediate side-effect.
         while ($trans->response instanceof FutureInterface) {
             $trans->response = $trans->response->wait();
         }
         return $trans->response;
     } catch (\Exception $e) {
         if ($isFuture) {
             // Wrap the exception in a promise
             return new FutureResponse(new RejectedPromise($e));
         }
         throw RequestException::wrapException($trans->request, $e);
     }
 }