예제 #1
0
 public function testAddsTimesOutAfterSending()
 {
     $p = new AsyncPlugin();
     $request = RequestFactory::getInstance()->create('PUT', 'http://www.example.com');
     $handle = CurlHandle::factory($request);
     $event = new Event(array('request' => $request, 'handle' => $handle->getHandle(), 'uploaded' => 10, 'upload_size' => 10, 'downloaded' => 0));
     $p->onCurlProgress($event);
 }
예제 #2
0
 /**
  * @covers Guzzle\Http\Plugin\AsyncPlugin::onCurlProgress
  */
 public function testAddsTimesOutAfterSending()
 {
     $p = new AsyncPlugin();
     $request = RequestFactory::getInstance()->create('PUT', 'http://www.example.com');
     $handle = CurlHandle::factory($request);
     $event = new Event(array('request' => $request, 'handle' => $handle, 'uploaded' => 10, 'upload_size' => 10, 'downloaded' => 0));
     $p->onCurlProgress($event);
     $this->assertEquals(1, $handle->getOptions()->get(CURLOPT_TIMEOUT_MS));
     $this->assertEquals(true, $handle->getOptions()->get(CURLOPT_NOBODY));
 }
예제 #3
0
 /**
  * Prepare a request to be sent from the Client by adding client specific behaviors and properties to the request.
  *
  * @param RequestInterface $request Request to prepare for the client
  * @param array            $options Options to apply to the request
  *
  * @return RequestInterface
  */
 protected function prepareRequest(RequestInterface $request, array $options = array())
 {
     $request->setClient($this)->setEventDispatcher(clone $this->getEventDispatcher());
     if ($curl = $this->config[self::CURL_OPTIONS]) {
         $request->getCurlOptions()->overwriteWith(CurlHandle::parseCurlConfig($curl));
     }
     if ($params = $this->config[self::REQUEST_PARAMS]) {
         Version::warn('request.params is deprecated. Use request.options to add default request options.');
         $request->getParams()->overwriteWith($params);
     }
     if ($this->userAgent && !$request->hasHeader('User-Agent')) {
         $request->setHeader('User-Agent', $this->userAgent);
     }
     if ($defaults = $this->config[self::REQUEST_OPTIONS]) {
         $this->requestFactory->applyOptions($request, $defaults, RequestFactoryInterface::OPTIONS_AS_DEFAULTS);
     }
     if ($options) {
         $this->requestFactory->applyOptions($request, $options);
     }
     $this->dispatch('client.create_request', array('client' => $this, 'request' => $request));
     return $request;
 }
예제 #4
0
 /**
  * Check if a cURL transfer resulted in what should be an exception
  *
  * @param RequestInterface $request Request to check
  * @param CurlHandle       $handle  Curl handle object
  * @param array            $curl    Array returned from curl_multi_info_read
  *
  * @return CurlException|bool
  */
 private function isCurlException(RequestInterface $request, CurlHandle $handle, array $curl)
 {
     if (CURLM_OK == $curl['result'] || CURLM_CALL_MULTI_PERFORM == $curl['result']) {
         return false;
     }
     $handle->setErrorNo($curl['result']);
     $e = new CurlException(sprintf('[curl] %s: %s [url] %s', $handle->getErrorNo(), $handle->getError(), $handle->getUrl()));
     $e->setCurlHandle($handle)->setRequest($request)->setCurlInfo($handle->getInfo())->setError($handle->getError(), $handle->getErrorNo());
     return $e;
 }
 /**
  * @dataProvider provideCurlConfig
  */
 public function testParseCurlConfigConvertsStringKeysToConstantKeys($options, $expected)
 {
     $actual = CurlHandle::parseCurlConfig($options);
     $this->assertEquals($expected, $actual);
 }
 /**
  * Returns a formatted message
  *
  * @param RequestInterface $request    Request that was sent
  * @param Response         $response   Response that was received
  * @param CurlHandle       $handle     Curl handle associated with the message
  * @param array            $customData Associative array of custom template data
  *
  * @return string
  */
 public function format(RequestInterface $request, Response $response = null, CurlHandle $handle = null, array $customData = array())
 {
     $cache = $customData;
     return preg_replace_callback('/{\\s*([A-Za-z_\\-\\.0-9]+)\\s*}/', function (array $matches) use($request, $response, $handle, &$cache) {
         if (array_key_exists($matches[1], $cache)) {
             return $cache[$matches[1]];
         }
         $result = '';
         switch ($matches[1]) {
             case 'request':
                 $result = (string) $request;
                 break;
             case 'response':
                 $result = (string) $response;
                 break;
             case 'req_body':
                 $result = $request instanceof EntityEnclosingRequestInterface ? (string) $request->getBody() : '';
                 break;
             case 'res_body':
                 $result = $response ? $response->getBody(true) : '';
                 break;
             case 'ts':
                 $result = gmdate('c');
                 break;
             case 'method':
                 $result = $request->getMethod();
                 break;
             case 'url':
                 $result = (string) $request->getUrl();
                 break;
             case 'resource':
                 $result = $request->getResource();
                 break;
             case 'protocol':
                 $result = 'HTTP';
                 break;
             case 'version':
                 $result = $request->getProtocolVersion();
                 break;
             case 'host':
                 $result = $request->getHost();
                 break;
             case 'hostname':
                 $result = gethostname();
                 break;
             case 'port':
                 $result = $request->getPort();
                 break;
             case 'code':
                 $result = $response ? $response->getStatusCode() : '';
                 break;
             case 'phrase':
                 $result = $response ? $response->getReasonPhrase() : '';
                 break;
             case 'connect_time':
                 if ($handle) {
                     $result = $handle->getInfo(CURLINFO_CONNECT_TIME);
                 } elseif ($response) {
                     $result = $response->getInfo('connect_time');
                 }
                 break;
             case 'total_time':
                 if ($handle) {
                     $result = $handle->getInfo(CURLINFO_TOTAL_TIME);
                 } elseif ($response) {
                     $result = $response->getInfo('total_time');
                 }
                 break;
             case 'curl_error':
                 $result = $handle ? $handle->getError() : '';
                 break;
             case 'curl_code':
                 $result = $handle ? $handle->getErrorNo() : '';
                 break;
             case 'curl_stderr':
                 $result = $handle ? $handle->getStderr() : '';
                 break;
             default:
                 if (strpos($matches[1], 'req_header_') === 0) {
                     $result = $request->getHeader(substr($matches[1], 11));
                 } elseif (strpos($matches[1], 'res_header_') === 0) {
                     $result = $response->getHeader(substr($matches[1], 11));
                 }
         }
         $cache[$matches[1]] = $result;
         return $result;
     }, $this->template);
 }
 public function prepare()
 {
     if (!$this->isPrepared()) {
         if (!$this->client) {
             throw new CommandException('A client must be associated with the command before it can be prepared.');
         }
         // If no response processing value was specified, then attempt to use the highest level of processing
         if (!isset($this[self::RESPONSE_PROCESSING])) {
             $this[self::RESPONSE_PROCESSING] = self::TYPE_MODEL;
         }
         // Notify subscribers of the client that the command is being prepared
         $this->client->dispatch('command.before_prepare', array('command' => $this));
         // Fail on missing required arguments, and change parameters via filters
         $this->validate();
         // Delegate to the subclass that implements the build method
         $this->build();
         // Add custom request headers set on the command
         if ($headers = $this[self::HEADERS_OPTION]) {
             foreach ($headers as $key => $value) {
                 $this->request->setHeader($key, $value);
             }
         }
         // Add any curl options to the request
         if ($options = $this[Client::CURL_OPTIONS]) {
             $this->request->getCurlOptions()->overwriteWith(CurlHandle::parseCurlConfig($options));
         }
         // Set a custom response body
         if ($responseBody = $this[self::RESPONSE_BODY]) {
             $this->request->setResponseBody($responseBody);
         }
         $this->client->dispatch('command.after_prepare', array('command' => $this));
     }
     return $this->request;
 }
예제 #8
0
 /**
  * Prepare a request to be sent from the Client by adding client specific behaviors and properties to the request.
  *
  * @param RequestInterface $request Request to prepare for the client
  *
  * @return RequestInterface
  */
 protected function prepareRequest(RequestInterface $request)
 {
     $request->setClient($this);
     // Add any curl options to the request
     if ($options = $this->config->get(self::CURL_OPTIONS)) {
         $request->getCurlOptions()->merge(CurlHandle::parseCurlConfig($options));
     }
     // Add request parameters to the request
     if ($options = $this->config->get(self::REQUEST_PARAMS)) {
         $request->getParams()->merge($options);
     }
     // Attach client observers to the request
     $request->setEventDispatcher(clone $this->getEventDispatcher());
     $this->dispatch('client.create_request', array('client' => $this, 'request' => $request));
     return $request;
 }
예제 #9
0
 /**
  * Prepare the command for executing and create a request object.
  *
  * @return RequestInterface Returns the generated request
  * @throws CommandException if a client object has not been set previously
  *      or in the prepare()
  */
 public function prepare()
 {
     if (!$this->isPrepared()) {
         if (!$this->client) {
             throw new CommandException('A Client object must be associated with the command before it can be prepared.');
         }
         // Fail on missing required arguments, and change parameters via filters
         $this->apiCommand->validate($this, $this->getInspector());
         $this->build();
         // Add custom request headers set on the command
         if ($headers = $this->get(self::HEADERS_OPTION)) {
             foreach ($headers as $key => $value) {
                 $this->request->setHeader($key, $value);
             }
         }
         // Add any curl options to the request
         $this->request->getCurlOptions()->merge(CurlHandle::parseCurlConfig($this->getAll()));
     }
     return $this->getRequest();
 }
 /**
  * Check if a cURL transfer resulted in what should be an exception
  *
  * @param RequestInterface $request Request to check
  * @param CurlHandle $handle Curl handle object
  * @param array $curl Curl message returned from curl_multi_info_read
  *
  * @return Exception|bool
  */
 private function isCurlException(RequestInterface $request, CurlHandle $handle, array $curl)
 {
     if (CURLE_OK == $curl['result']) {
         return false;
     }
     $handle->setErrorNo($curl['result']);
     $e = new CurlException(sprintf('[curl] %s: %s [url] %s [info] %s [debug] %s', $handle->getErrorNo(), $handle->getError(), $handle->getUrl(), var_export($handle->getInfo(), true), $handle->getStderr()));
     $e->setRequest($request)->setError($handle->getError(), $handle->getErrorNo());
     return $e;
 }
예제 #11
0
 public function prepare()
 {
     if (!$this->isPrepared()) {
         if (!$this->client) {
             throw new CommandException('A client must be associated with the command before it can be prepared.');
         }
         if (!isset($this[self::RESPONSE_PROCESSING])) {
             $this[self::RESPONSE_PROCESSING] = self::TYPE_MODEL;
         }
         $this->client->dispatch('command.before_prepare', array('command' => $this));
         $this->validate();
         $this->build();
         if ($headers = $this[self::HEADERS_OPTION]) {
             foreach ($headers as $key => $value) {
                 $this->request->setHeader($key, $value);
             }
         }
         if ($options = $this[Client::CURL_OPTIONS]) {
             $this->request->getCurlOptions()->overwriteWith(CurlHandle::parseCurlConfig($options));
         }
         if ($responseBody = $this[self::RESPONSE_BODY]) {
             $this->request->setResponseBody($responseBody);
         }
         $this->client->dispatch('command.after_prepare', array('command' => $this));
     }
     return $this->request;
 }
예제 #12
0
 /**
  * Prepare a request to be sent from the Client by adding client specific
  * behaviors and properties to the request.
  *
  * @param RequestInterface $request Request to prepare for the client
  *
  * @return RequestInterface
  */
 protected function prepareRequest(RequestInterface $request)
 {
     $request->setClient($this);
     // Add any curl options to the request
     $request->getCurlOptions()->merge(CurlHandle::parseCurlConfig($this->config));
     foreach ($this->config as $key => $value) {
         if (strpos($key, 'params.') === 0) {
             // Add request specific parameters to all requests (prefix with 'params.')
             $request->getParams()->set(substr($key, 7), $value);
         }
     }
     // Attach client observers to the request
     $request->setEventDispatcher(clone $this->getEventDispatcher());
     $this->dispatch('client.create_request', array('client' => $this, 'request' => $request));
     return $request;
 }
예제 #13
0
 public function testAddsCustomCurlOptions()
 {
     $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl());
     $request->getCurlOptions()->set(CURLOPT_TIMEOUT, 200);
     $handle = CurlHandle::factory($request);
     $this->assertEquals(200, $handle->getOptions()->get(CURLOPT_TIMEOUT));
 }
예제 #14
0
 /**
  * Prepare a request to be sent from the Client by adding client specific behaviors and properties to the request.
  *
  * @param RequestInterface $request Request to prepare for the client
  *
  * @return RequestInterface
  */
 protected function prepareRequest(RequestInterface $request)
 {
     $request->setClient($this);
     // Add any curl options to the request
     if ($options = $this->config->get(self::CURL_OPTIONS)) {
         $request->getCurlOptions()->merge(CurlHandle::parseCurlConfig($options));
     }
     // Add request parameters to the request
     if ($options = $this->config->get(self::REQUEST_PARAMS)) {
         $request->getParams()->merge($options);
     }
     // Attach client observers to the request
     $request->setEventDispatcher(clone $this->getEventDispatcher());
     // Set the User-Agent if one is specified on the client but not explicitly on the request
     if ($this->userAgent && !$request->hasHeader('User-Agent')) {
         $request->setHeader('User-Agent', $this->userAgent);
     }
     $this->dispatch('client.create_request', array('client' => $this, 'request' => $request));
     return $request;
 }
예제 #15
0
 /**
  * {@inheritdoc}
  */
 public function prepare()
 {
     if (!$this->isPrepared()) {
         if (!$this->client) {
             throw new CommandException('A client must be associated with the command before it can be prepared.');
         }
         // Notify subscribers of the client that the command is being prepared
         $this->client->dispatch('command.before_prepare', array('command' => $this));
         // Fail on missing required arguments, and change parameters via filters
         $this->validate();
         // Delegate to the subclass that implements the build method
         $this->build();
         // Add custom request headers set on the command
         if ($headers = $this->get(self::HEADERS_OPTION)) {
             foreach ($headers as $key => $value) {
                 $this->request->setHeader($key, $value);
             }
         }
         // Add any curl options to the request
         if ($options = $this->get(Client::CURL_OPTIONS)) {
             $this->request->getCurlOptions()->merge(CurlHandle::parseCurlConfig($options));
         }
         // Set a custom response body
         if ($responseBody = $this->get(self::RESPONSE_BODY)) {
             $this->request->setResponseBody($responseBody);
         }
     }
     return $this->request;
 }
 public function testAllowsWireTransferInfoToBeDisabled()
 {
     $request = RequestFactory::getInstance()->create('PUT', $this->getServer()->getUrl());
     $request->getCurlOptions()->set('disable_wire', true);
     $handle = CurlHandle::factory($request);
     $this->assertNull($handle->getOptions()->get(CURLOPT_STDERR));
     $this->assertNull($handle->getOptions()->get(CURLOPT_VERBOSE));
 }