public function testDeleteHeader() { $this->object->setHeader('Sample', 'value'); $this->assertThat($this->object->getHeader('sample'), $this->equalTo('value')); $this->object->deleteHeader('Sample'); $this->assertThat($this->object->getHeader('sample'), $this->equalTo(null)); }
/** * {@inheritdoc} */ public function getLatestResponseHeaders() { if (null === $this->response) { return; } return ['reset' => (int) (string) $this->response->getHeader('RateLimit-Reset'), 'remaining' => (int) (string) $this->response->getHeader('RateLimit-Remaining'), 'limit' => (int) (string) $this->response->getHeader('RateLimit-Limit')]; }
/** * Returns whether a resource has been modified between two requests. * * @return bool */ protected function matchModified() { $lastModified = $this->response->getHeader("Last-Modified"); $modifiedSince = $this->request->getModifiedSince(); if ($modifiedSince && $lastModified) { return strtotime($modifiedSince) >= strtotime($lastModified); } return false; }
/** * redirect from response 3xx */ protected function redirect() { $redirect_url = $this->response->getHeader("location"); $curl = new static($redirect_url); if ($this->cookie_jar) { $curl->storeSession($this->cookie_jar); } $this->response = $curl->get(); $this->redirect_urls[] = $redirect_url; }
public function testConstruct() { $response = new Response(["test" => "value"], "php://memory", 201, "1.1"); $this->assertInstanceOf('Psr\\Http\\Message\\StreamInterface', $response->getBody()); $this->assertEquals(["test" => ["value"]], $response->getHeaders()); $this->assertEquals("1.1", $response->getProtocolVersion()); $this->assertEquals(["value"], $response->getHeader("test")); $this->assertEquals(201, $response->getStatusCode()); $this->assertEquals("Created", $response->getReasonPhrase()); }
public function __construct(Response $parent) { $status = $parent->getStatus(); $convertToJson = false; $ok = self::statusIsOK($status); if ($ok) { $contentType = $parent->getHeader('Content-Type'); if (!empty($contentType)) { $convertToJson = stristr($contentType, '/json') !== false; } } $bodyString = $parent->getBody(); parent::__construct($convertToJson ? json_decode($bodyString) : $bodyString, $parent->getHeaders(), $status); $this->ok = $ok; }
/** * {@inheritdoc} */ public function validateResponse(Request $request, Response $response) : bool { if (Response::SWITCHING_PROTOCOLS !== $response->getStatusCode()) { return false; } if ('upgrade' !== strtolower($response->getHeader('Connection'))) { return false; } if ('websocket' !== strtolower($response->getHeader('Upgrade'))) { return false; } $key = $request->getHeader('Sec-WebSocket-Key'); if (!$response->hasHeader('Sec-WebSocket-Accept')) { return false; } return $this->responseKey($key) === $response->getHeader('Sec-WebSocket-Accept'); }
function onReceive($serv, $client_id, $from_id, $data) { $response = new Response(); $response->body = json_encode(array(1, 2, 3)); $this->server->getSwoole()->send($client_id, $response->getHeader() . $response->body); sleep(1); $this->server->getSwoole()->close($client_id); return; // //检测request data完整性 // $ret = $this->checkData($client_id, $data); // switch($ret) // { // //错误的请求 // case self::ST_ERROR; // $this->server->close($client_id); // return; // //请求不完整,继续等待 // case self::ST_WAIT: // return; // default: // break; // } // //完整的请求 // //开始处理 // $request = $this->requests[$client_id]; // $info = $serv->connection_info($client_id); // $request->remote_ip = $info['remote_ip']; // $_SERVER['SWOOLE_CONNECTION_INFO'] = $info; // $this->parseRequest($request); // $request->fd = $client_id; // $this->currentRequest = $request; // if ($this->async) // { // $this->onAsyncRequest($request); // } // else // { // //处理请求,产生response对象 // $response = $this->onRequest($request); // //发送response // $this->response($request, $response); // } Console::output("onReceive"); }
/** * @param Response $response */ function __construct(Response $response) { $this->response = $response; if (($h = $response->getHeader("Content-Type", Header::class)) && $h->match("application/json", Header::MATCH_WORD) && ($failure = json_decode($response->getBody()))) { $message = $failure->message; if (isset($failure->errors)) { $this->errors = (array) $failure->errors; } } else { $message = trim($response->getBody()->toString()); } if (!strlen($message)) { $message = $response->getTransferInfo("error"); } if (!strlen($message)) { $message = $response->getResponseStatus(); } parent::__construct($message, $response->getResponseCode(), null); }
/** * {@inheritdoc} */ public function buildOutgoingResponse(Response $response, Request $request = null, float $timeout = 0, bool $allowPersistent = false) : \Generator { if ('upgrade' === strtolower($response->getHeader('Connection'))) { return $response; } if ($allowPersistent && null !== $request && 'keep-alive' === strtolower($request->getHeader('Connection'))) { $response = $response->withHeader('Connection', 'keep-alive')->withHeader('Keep-Alive', sprintf('timeout=%d, max=%d', $this->keepAliveTimeout, $this->keepAliveMax)); } else { $response = $response->withHeader('Connection', 'close'); } $response = $response->withoutHeader('Content-Encoding'); if ($this->compressionEnabled && null !== $request && $request->hasHeader('Accept-Encoding') && $response->hasHeader('Content-Type') && preg_match('/gzip|deflate/i', $request->getHeader('Accept-Encoding'), $matches)) { $encoding = strtolower($matches[0]); $contentType = $response->getHeader('Content-Type'); foreach ($this->compressTypes as $pattern) { if (preg_match($pattern, $contentType)) { $response = $response->withHeader('Content-Encoding', $encoding); break; } } } return yield from $this->buildOutgoingStream($response, $timeout); }
/** * Import handler for the endpoint's underlying data * * \seekat\Call will call this when the request will have finished. * * @param Response $response * @return API self * @throws UnexpectedValueException * @throws RequestException * @throws \Exception */ function import(Response $response) : API { $this->__log->info(__FUNCTION__ . ": " . $response->getInfo(), ["url" => (string) $this->__url]); if ($response->getResponseCode() >= 400) { $e = new RequestException($response); $this->__log->critical(__FUNCTION__ . ": " . $e->getMessage(), ["url" => (string) $this->__url]); throw $e; } if (!($type = $response->getHeader("Content-Type", Header::class))) { $e = new RequestException($response); $this->__log->error(__FUNCTION__ . ": Empty Content-Type -> " . $e->getMessage(), ["url" => (string) $this->__url]); throw $e; } try { $this->__type = new ContentType($type); $this->__data = $this->__type->parseBody($response->getBody()); if ($link = $response->getHeader("Link", Header::class)) { $this->__links = new Links($link); } } catch (\Exception $e) { $this->__log->error(__FUNCTION__ . ": " . $e->getMessage(), ["url" => (string) $this->__url]); throw $e; } return $this; }
/** * Updates the cookie jar from a Response object. * * @param Symfony\Components\BrowserKit\Response $response A Response object * @param string $url The base URL */ public function updateFromResponse(Response $response, $uri = null) { foreach ($response->getHeader('Set-Cookie', false) as $cookie) { $this->set(Cookie::fromString($cookie), $uri); } }
/** * Updates the cookie jar from a Response object. * * @param Response $response A Response object * @param string $uri The base URL */ public function updateFromResponse(Response $response, $uri = null) { $cookies = array(); foreach ($response->getHeader('Set-Cookie', false) as $cookie) { foreach (explode(',', $cookie) as $i => $part) { if (0 === $i || preg_match('/^(?P<token>\\s*[0-9A-Za-z!#\\$%\\&\'\\*\\+\\-\\.^_`\\|~]+)=/', $part)) { $cookies[] = ltrim($part); } else { $cookies[count($cookies) - 1] .= ',' . $part; } } } foreach ($cookies as $cookie) { $this->set(Cookie::fromString($cookie, $uri)); } }
/** * Updates the cookie jar from a Response object. * * @param Response $response A Response object * @param string $uri The base URL */ public function updateFromResponse(Response $response, $uri = null) { $this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri); }
/** * Get response header(s) after the request has been sent. * @param string $name Header name (optional), If name was given and and not found NULL is returned * @param string $default Default value in case header with $name was not found * @return array|string|NULL */ public function getResponseHeader($name = NULL, $default = NULL) { return $this->response->getHeader($name, $default); }
/** * Sets following request and sends it * @param Request $request * @param Response $response * @param int $maxRedirects * @return Response * @throws RequestException */ private function doFollow(Request $request, Response $response, $maxRedirects) { /// Change method to GET $request->setMethod(Request::GET); /// Find out location $location = $response->getHeader('Location'); if (strpos($location, '/') == 0 && $request->getUrl() != NULL) { $parsed = @parse_url($request->getUrl()); if ($parsed == FALSE) { throw new \InvalidArgumentException('Invalid URL, got: ' . $request->getUrl()); } $url = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : ''; if (isset($parsed['user']) && isset($parsed['pass'])) { $url .= $parsed['user'] . ':' . $parsed['pass'] . '@'; } $url .= isset($parsed['host']) ? $parsed['host'] : ''; $url .= isset($parsed['port']) ? ':' . $parsed['port'] : ''; $url .= $location; $location = $url; } $request->setUrl($location); $request->addHeaders(array('Referer' => $request->getUrl())); $this->redirectCount++; return $this->send($request, true, $maxRedirects); }
public function onCompletion() { $ch = $this->curlHandle; $this->checkForError($ch); $this->curlInfo = curl_getinfo($ch); $response = new Response(); $response->url = $this->url; $response->body = $this->receivedBody; $response->header = $this->receivedHeader; $response->received = new \DateTime(); $response->parseCurlInfo($this->curlInfo); $response->contentLanguage = $response->getHeader('Content-Language', '\\n'); $this->response = $response; $this->closeHandle(); }