/** * Send a HTTP request * * @param peer.http.HttpRequest * @return peer.http.HttpResponse response object */ public function send(HttpRequest $request) { $this->lastRequest = $request; $this->cat && $this->cat->info('>>>', $request->getHeaderString()); $response = $this->response(); $this->cat && $this->cat->info('<<<', $response->getHeaderString()); return $response; }
/** * 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 delete_with_1byte_body() { $r = new HttpRequest(new URL('http://example.com/')); $r->setMethod(HttpConstants::DELETE); $r->setParameters(new RequestData('1')); $this->assertEquals("DELETE / HTTP/1.1\r\nConnection: close\r\nHost: example.com\r\nContent-Length: 1\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n", $r->getHeaderString()); }