/** * Sends a request * * @param peer.http.HttpRequest $request * @param int $timeout default 60 * @param float $connecttimeout default 2.0 * @return peer.http.HttpResponse response object */ public function send(HttpRequest $request, $timeout = 60, $connecttimeout = 2.0) { $curl = curl_copy_handle($this->handle); curl_setopt($curl, CURLOPT_URL, $request->url->getCanonicalURL()); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestString()); curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); if ($this->proxy && !$this->proxy->excludes()->contains($request->getUrl())) { curl_setopt($curl, CURLOPT_PROXY, $this->proxy->host()); curl_setopt($curl, CURLOPT_PROXYPORT, $this->proxy->port()); $read = function ($transfer) { if (preg_match('#^HTTP/[0-9]\\.[0-9] [0-9]{3} .+\\r\\n\\r\\n#', $transfer, $matches)) { // Strip "HTTP/x.x 200 Connection established" which is followed by // the real HTTP message: headers and body return substr($transfer, strlen($matches[0])); } else { return $transfer; } }; } else { $read = function ($transfer) { return $transfer; }; } $return = curl_exec($curl); if (false === $return) { $errno = curl_errno($curl); $error = curl_error($curl); curl_close($curl); throw new \io\IOException(sprintf('%d: %s', $errno, $error)); } // ensure handle is closed curl_close($curl); $this->cat && $this->cat->info('>>>', $request->getHeaderString()); $response = new HttpResponse(new MemoryInputStream($read($return)), false); $this->cat && $this->cat->info('<<<', $response->getHeaderString()); return $response; }
public function get_url_with_assoc_array_containing_assoc_array() { $r = new HttpRequest(new URL('http://example.com/')); $r->setMethod(HttpConstants::GET); $r->setParameters(['test' => ['10 EUR' => 'test 123']]); $this->assertEquals("GET /?test[10+EUR]=test+123 HTTP/1.1\r\nConnection: close\r\nHost: example.com\r\n\r\n", $r->getRequestString()); }
public function with_duplicate_header() { $r = new HttpRequest(new \peer\URL('http://example.com/')); $r->setHeader('X-Binford', 6100); $r->setHeader('X-Binford', 61000); $this->assertEquals("GET / HTTP/1.1\r\nConnection: close\r\nHost: example.com\r\nX-Binford: 61000\r\n\r\n", $r->getRequestString()); }