/**
  * Sets options on a cURL resource based on a request.
  */
 private static function setOptionsFromRequest($curl, RequestInterface $request)
 {
     $options = array(CURLOPT_HTTP_VERSION => $request->getProtocolVersion() == 1.0 ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $request->getMethod(), CURLOPT_URL => $request->getHost() . $request->getResource(), CURLOPT_HTTPHEADER => $request->getHeaders());
     switch ($request->getMethod()) {
         case RequestInterface::METHOD_HEAD:
             $options[CURLOPT_NOBODY] = true;
             break;
         case RequestInterface::METHOD_GET:
             $options[CURLOPT_HTTPGET] = true;
             break;
         case RequestInterface::METHOD_POST:
         case RequestInterface::METHOD_PUT:
         case RequestInterface::METHOD_DELETE:
         case RequestInterface::METHOD_PATCH:
         case RequestInterface::METHOD_OPTIONS:
             $options[CURLOPT_POSTFIELDS] = $fields = static::getPostFields($request);
             // remove the content-type header
             if (is_array($fields)) {
                 $options[CURLOPT_HTTPHEADER] = array_filter($options[CURLOPT_HTTPHEADER], function ($header) {
                     return 0 !== stripos($header, 'Content-Type: ');
                 });
             }
             break;
     }
     curl_setopt_array($curl, $options);
 }
Esempio n. 2
0
 /**
  * Processes Set-Cookie headers from a request/response pair.
  *
  * @param RequestInterface $request  A request object
  * @param MessageInterface $response A response object
  */
 public function processSetCookieHeaders(RequestInterface $request, MessageInterface $response)
 {
     foreach ($response->getHeader('Set-Cookie', false) as $header) {
         $cookie = new Cookie();
         $cookie->fromSetCookieHeader($header, parse_url($request->getHost(), PHP_URL_HOST));
         $this->addCookie($cookie);
     }
 }
 /**
  * @param string|null $message
  */
 public function __construct(RequestInterface $request, Response $response, $message = null)
 {
     $this->request = $request;
     $this->response = $response;
     if ($message === null) {
         $curlError = $response->getHeader('X-Curl-Error-Result');
         $message = sprintf('HTTP %s request to "%s%s" failed: %d - "%s".', $request->getMethod(), $request->getHost(), $request->getResource(), $curlError ?: $response->getStatusCode(), $curlError ? curl_strerror($curlError) : $response->getReasonPhrase());
     }
     parent::__construct($message);
 }
Esempio n. 4
0
 /**
  * Returns true if the current cookie matches the supplied request.
  *
  * @return boolean
  */
 public function matchesRequest(RequestInterface $request)
 {
     // domain
     if (!$this->matchesDomain(parse_url($request->getHost(), PHP_URL_HOST))) {
         return false;
     }
     // path
     if (!$this->matchesPath($request->getResource())) {
         return false;
     }
     // secure
     if ($this->hasAttribute(static::ATTR_SECURE) && !$request->isSecure()) {
         return false;
     }
     return true;
 }
Esempio n. 5
0
 public function postSend(RequestInterface $request, MessageInterface $response)
 {
     $seconds = microtime(true) - $this->startTime;
     call_user_func($this->logger, sprintf('%sSent "%s %s%s" in %dms', $this->prefix, $request->getMethod(), $request->getHost(), $request->getResource(), round($seconds * 1000)));
 }
Esempio n. 6
0
 /**
  * @param RequestInterface $request
  * @param Response         $response
  */
 private function logResponse(RequestInterface $request, Response $response)
 {
     $this->logger->debug(sprintf('Request to %s%s with content "%s" was send and response with content "%s" and status %d received', $request->getHost(), $request->getResource(), $request->getContent(), $response->getContent(), $response->getStatusCode()));
 }
 public function postSend(RequestInterface $request, MessageInterface $response)
 {
     $this->data[] = array('request' => array('method' => $request->getMethod(), 'url' => sprintf('%s%s', $request->getHost(), $request->getResource()), 'headers' => $request->getHeaders(), 'content' => $request->getContent()), 'response' => array('statusCode' => $response->getStatusCode(), 'reasonPhrase' => $response->getReasonPhrase(), 'headers' => $response->getHeaders(), 'content' => $response->getContent()));
 }