public function encode(Http\Request $request, $body) { if (is_array($body)) { $body = http_build_query($body); $request->setHeader('Content-Type', 'application/x-www-form-urlencoded'); $request->setHeader('Content-Length', strlen($body)); } return $body; }
/** * @param Request $request * @param string $currentTime */ private function setupChainId(Request $request, $currentTime) { if (!$request->hasHeader(self::CHAIN_ID_HEADER)) { $requestId = date('Y-m-d') . '/' . $currentTime . '_' . uniqid(); $request->setHeader(self::CHAIN_ID_HEADER, $requestId); $this->chainId = $requestId; } else { $this->chainId = $request->getHeader(self::CHAIN_ID_HEADER); } }
/** * @param Http\Request $request * @return Http\Response * * @throws Http\BadResponseException */ protected function processRequest(Http\Request $request) { $headers = []; foreach ($request->getHeaders() as $name => $value) { $headers[] = "{$name}: {$value}"; } $responseHeaders = []; $options = [CURLOPT_FOLLOWLOCATION => FALSE, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $request->getMethod(), CURLOPT_NOBODY => $request->isMethod(Http\Request::HEAD), CURLOPT_URL => $request->getUrl(), CURLOPT_HTTPHEADER => $headers, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_POSTFIELDS => $request->getBody(), CURLOPT_HEADER => FALSE, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_SSL_VERIFYPEER => 1, CURLOPT_HEADERFUNCTION => function ($curl, $line) use(&$responseHeaders, &$last) { if (strncasecmp($line, 'HTTP/', 5) === 0) { /** @todo Set proxy response as Response::setPrevious($proxyResponse)? */ # The HTTP/x.y may occur multiple times with proxy (HTTP/1.1 200 Connection Established) $responseHeaders = []; } elseif (in_array(substr($line, 0, 1), [' ', "\t"], TRUE)) { $last .= ' ' . trim($line); # RFC2616, 2.2 } elseif ($line !== "\r\n") { list($name, $value) = explode(':', $line, 2); $key = trim($name); $responseHeaders[$key][] = trim($value); $last =& $responseHeaders[$key][count($responseHeaders[$key]) - 1]; } return strlen($line); }]; if (defined('CURLOPT_PROTOCOLS')) { # HHVM issue. Even cURL v7.26.0, constants are missing. $options[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; } $options = array_replace($options, $this->options); if (!$this->curl) { $this->curl = curl_init(); if ($this->curl === FALSE) { throw new Http\BadResponseException('Cannot init cURL handler.'); } } $result = curl_setopt_array($this->curl, $options); if ($result === FALSE) { throw new Http\BadResponseException('Setting cURL options failed: ' . curl_error($this->curl), curl_errno($this->curl)); } $this->beforeCurlExec && call_user_func($this->beforeCurlExec, $this->curl, $request->getUrl()); $body = curl_exec($this->curl); if ($body === FALSE) { throw new Http\BadResponseException(curl_error($this->curl), curl_errno($this->curl)); } $code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); if ($code === FALSE) { throw new Http\BadResponseException('HTTP status code is missing: ' . curl_error($this->curl), curl_errno($this->curl)); } return new Http\Response($code, $responseHeaders, $body, $request->getCoder()); }
/** * @return Http\Response * * @throws Http\BadResponseException */ protected function processRequest(Http\Request $request) { $headerStr = []; foreach ($request->getHeaders() as $name => $value) { foreach ((array) $value as $v) { $headerStr[] = "{$name}: {$v}"; } } $options = ['http' => ['method' => $request->getMethod(), 'header' => implode("\r\n", $headerStr) . "\r\n", 'follow_location' => 0, 'protocol_version' => 1.1, 'ignore_errors' => TRUE], 'ssl' => ['verify_peer' => TRUE, 'disable_compression' => TRUE, 'SNI_enabled' => TRUE, 'ciphers' => 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:' . 'ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:' . 'ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:' . 'ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:' . 'DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:' . 'DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK']]; if (($body = $request->getBody()) !== NULL) { $options['http']['content'] = $body; } $options = array_replace_recursive($options, $this->options); list($code, $headers, $body) = $this->fileGetContents($request->getUrl(), $options); return new Http\Response($code, $headers, $body, $request->getCoder()); }
/** * @param Request */ public function onRequest(Request $request) { $http = $request->getMethod() . ' ' . $request->getUrl() . ' HTTP/1.1'; $content = $this->getFormattedMessage($http, $request); $this->writer->write($request, $content); }
protected function setupRequest(Http\Request $request) { $request->addHeader('Expect', ''); }