/**
  * 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)
 {
     // Use proxy socket and Modify target if a proxy is to be used for this request,
     // a proxy wants "GET http://example.com/ HTTP/X.X"
     if ($this->proxy && !$this->proxy->isExcluded($url = $request->getUrl())) {
         $request->setTarget(sprintf('%s://%s%s%s', $url->getScheme(), $url->getHost(), $url->getPort() ? ':' . $url->getPort() : '', $url->getPath('/')));
         $s = $this->proxySocket;
     } else {
         $s = $this->socket;
     }
     // Socket still open from last request. This is the case when unread
     // data is left on the socket (by not reading the body, e.g.), so do
     // it the quick & dirty way: Close and reopen!
     $s->isConnected() && $s->close();
     $s->setTimeout($timeout);
     $s->connect($connecttimeout);
     $s->write($request->getRequestString());
     return new HttpResponse(new SocketInputStream($s));
 }
 /**
  * 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->isExcluded($request->getUrl())) {
         curl_setopt($curl, CURLOPT_PROXY, $this->proxy->host);
         curl_setopt($curl, CURLOPT_PROXYPORT, $this->proxy->port);
     }
     $response = curl_exec($curl);
     if (FALSE === $response) {
         $errno = curl_errno($curl);
         $error = curl_error($curl);
         curl_close($curl);
         throw new IOException(sprintf('%d: %s', $errno, $error));
     }
     // ensure handle is closed
     curl_close($curl);
     return new HttpResponse(new MemoryInputStream($response));
 }
Ejemplo n.º 3
0
 public function duplicateHeader()
 {
     $r = new HttpRequest(new 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());
 }