/** * 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 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 sign_merges_url_params_but_parameters_are_unique() { $request = new HttpRequest(new URL('http://example.com/foo')); $request->setParameter('param', 'value'); $request->getUrl()->setParam('param', 'bar'); $this->digest->sign($request); $this->assertStringContains('uri="/foo?param=bar", ', $request->getHeaderString()); }
/** * 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" for (and "CONNECT" for HTTPs). if ($this->proxy && !$this->proxy->excludes()->contains($url = $request->getUrl())) { $s = $this->connect($this->proxySocket, $timeout, $connecttimeout); $this->proxy($s, $request, $url); } else { $s = $this->connect($this->socket, $timeout, $connecttimeout); } $this->cat && $this->cat->info('>>>', $request->getHeaderString()); $s->write($request->getRequestString()); $response = new HttpResponse(new SocketInputStream($s)); $this->cat && $this->cat->info('<<<', $response->getHeaderString()); return $response; }
/** * Sign HTTP request * * @param peer.http.HttpRequest $request */ public function sign(HttpRequest $request) { $request->setHeader('Authorization', $this->getValueRepresentation()); }
/** * Perform any request * * @param string $method request method, e.g. HttpConstants::GET * @param var $parameters * @param [:string] $headers default array() * @return peer.http.HttpResponse response object * @throws io.IOException */ public function request($method, $parameters, $headers = []) { $r = new HttpRequest($this->url); $r->setMethod($method); $r->setParameters($parameters); $r->addHeaders($headers); return $this->send($r); }
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()); }
/** * Sign the given request; ie. add an Authorization: Digest header * and increase the internal nonce counter. * * @param peer.http.HttpRequest $request */ public function sign(HttpRequest $request) { $url = $request->target; $params = []; if (is_array($request->parameters)) { $params = array_merge($params, $request->parameters); } if ($request->getUrl()->hasParams()) { $params = array_merge($params, $request->getUrl()->getParams()); } if (sizeof($params)) { $url .= '?'; foreach ($params as $k => $v) { $url .= $k . '=' . $v . '&'; } $url = substr($url, 0, -1); } $request->setHeader('Authorization', $this->getValueRepresentation($request->method, $url)); // Increase internal counter $this->counter++; }