示例#1
0
 private function checkDecode(array $request, array $response, $stream)
 {
     // Automatically decode responses when instructed.
     if (!empty($request['client']['decode_content'])) {
         switch (Core::firstHeader($response, 'Content-Encoding', true)) {
             case 'gzip':
             case 'deflate':
                 $stream = new InflateStream(Stream::factory($stream));
                 break;
         }
     }
     return $stream;
 }
示例#2
0
 public function testReturnsFirstHeaderWhenMultiplePerLine()
 {
     $this->assertEquals('Bar', Core::firstHeader(['headers' => ['Foo' => ['Bar, Baz']]], 'Foo'));
 }
示例#3
0
 /**
  * Applies an array of request client options to a the options array.
  *
  * This method uses a large switch rather than double-dispatch to save on
  * high overhead of calling functions in PHP.
  */
 private function applyHandlerOptions(array $request, array &$options)
 {
     foreach ($request['client'] as $key => $value) {
         switch ($key) {
             // Violating PSR-4 to provide more room.
             case 'verify':
                 if ($value === false) {
                     unset($options[CURLOPT_CAINFO]);
                     $options[CURLOPT_SSL_VERIFYHOST] = 0;
                     $options[CURLOPT_SSL_VERIFYPEER] = false;
                     continue;
                 }
                 $options[CURLOPT_SSL_VERIFYHOST] = 2;
                 $options[CURLOPT_SSL_VERIFYPEER] = true;
                 if (is_string($value)) {
                     $options[CURLOPT_CAINFO] = $value;
                     if (!file_exists($value)) {
                         throw new \InvalidArgumentException("SSL CA bundle not found: {$value}");
                     }
                 }
                 break;
             case 'decode_content':
                 if ($value === false) {
                     continue;
                 }
                 $accept = Core::firstHeader($request, 'Accept-Encoding');
                 if ($accept) {
                     $options[CURLOPT_ENCODING] = $accept;
                 } else {
                     $options[CURLOPT_ENCODING] = '';
                     // Don't let curl send the header over the wire
                     $options[CURLOPT_HTTPHEADER][] = 'Accept-Encoding:';
                 }
                 break;
             case 'save_to':
                 if (is_string($value)) {
                     if (!is_dir(dirname($value))) {
                         throw new \RuntimeException(sprintf('Directory %s does not exist for save_to value of %s', dirname($value), $value));
                     }
                     $value = new LazyOpenStream($value, 'w+');
                 }
                 if ($value instanceof StreamInterface) {
                     $options[CURLOPT_WRITEFUNCTION] = function ($ch, $write) use($value) {
                         return $value->write($write);
                     };
                 } elseif (is_resource($value)) {
                     $options[CURLOPT_FILE] = $value;
                 } else {
                     throw new \InvalidArgumentException('save_to must be a ' . 'GuzzleHttp\\Stream\\StreamInterface or resource');
                 }
                 break;
             case 'timeout':
                 if (defined('CURLOPT_TIMEOUT_MS')) {
                     $options[CURLOPT_TIMEOUT_MS] = $value * 1000;
                 } else {
                     $options[CURLOPT_TIMEOUT] = $value;
                 }
                 break;
             case 'connect_timeout':
                 if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
                     $options[CURLOPT_CONNECTTIMEOUT_MS] = $value * 1000;
                 } else {
                     $options[CURLOPT_CONNECTTIMEOUT] = $value;
                 }
                 break;
             case 'proxy':
                 if (!is_array($value)) {
                     $options[CURLOPT_PROXY] = $value;
                 } elseif (isset($request['scheme'])) {
                     $scheme = $request['scheme'];
                     if (isset($value[$scheme])) {
                         $options[CURLOPT_PROXY] = $value[$scheme];
                     }
                 }
                 break;
             case 'cert':
                 if (is_array($value)) {
                     $options[CURLOPT_SSLCERTPASSWD] = $value[1];
                     $value = $value[0];
                 }
                 if (!file_exists($value)) {
                     throw new \InvalidArgumentException("SSL certificate not found: {$value}");
                 }
                 $options[CURLOPT_SSLCERT] = $value;
                 break;
             case 'ssl_key':
                 if (is_array($value)) {
                     $options[CURLOPT_SSLKEYPASSWD] = $value[1];
                     $value = $value[0];
                 }
                 if (!file_exists($value)) {
                     throw new \InvalidArgumentException("SSL private key not found: {$value}");
                 }
                 $options[CURLOPT_SSLKEY] = $value;
                 break;
             case 'progress':
                 if (!is_callable($value)) {
                     throw new \InvalidArgumentException('progress client option must be callable');
                 }
                 $options[CURLOPT_NOPROGRESS] = false;
                 $options[CURLOPT_PROGRESSFUNCTION] = function () use($value) {
                     $args = func_get_args();
                     // PHP 5.5 pushed the handle onto the start of the args
                     if (is_resource($args[0])) {
                         array_shift($args);
                     }
                     call_user_func_array($value, $args);
                 };
                 break;
             case 'debug':
                 if ($value) {
                     $options[CURLOPT_STDERR] = Core::getDebugResource($value);
                     $options[CURLOPT_VERBOSE] = true;
                 }
                 break;
         }
     }
 }
示例#4
0
 public function testSendsPostWithNoBodyOrDefaultContentType()
 {
     Server::flush();
     Server::enqueue([['status' => 200]]);
     $handler = new CurlMultiHandler();
     $response = $handler(['http_method' => 'POST', 'uri' => '/', 'headers' => ['host' => [Server::$host]]]);
     $response->wait();
     $received = Server::received()[0];
     $this->assertEquals('POST', $received['http_method']);
     $this->assertNull(Core::header($received, 'content-type'));
     $this->assertSame('0', Core::firstHeader($received, 'content-length'));
 }