/** * Sends the headers of the response to the client. * * @return void */ protected function sendHeaders() { if (!headers_sent()) { $status = $this->response->getMessage() ?: $this->response->getStatus(); header(sprintf("%s %s", $this->response->getProtocol(), $status)); foreach ($this->response->getHeaders() as $name => $value) { header("{$name}: {$value}", true); } foreach ($this->response->getCookies() as $cookie) { header("Set-Cookie: {$cookie}", false); } } }
/** * Responsible for sending all headers in a Response. Marked final because * all headers should be bundled in Response object. * * @param Response $response * @final */ protected function sendHeadersFor(Response $response) { header('HTTP/1.1 ' . ResponseCodes::getMessageForCode($response->code)); $format = $response->request->accepts->format(); header('Content-Type: ' . MimeTypes::preferredMimeTypeFor($format)); foreach ($response->headers as $header) { header($header); } foreach ($response->getCookies() as $cookie) { if ($cookie->value == '') { setcookie($cookie->name, '', time() - 10000, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httponly); } else { setcookie($cookie->name, $cookie->value, $cookie->expire, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httponly); } } flush(); // TODO: Determine other headers to send here. Content-Type, Caching, Etags, ... }
/** * Updates the cookie jar from a Response object. * * @param Symfony\Components\BrowserKit\Response $response A Response object */ public function updateFromResponse(Response $response) { foreach ($response->getCookies() as $name => $cookie) { $this->set(new Cookie($name, isset($cookie['value']) ? $cookie['value'] : '', isset($cookie['expire']) ? $cookie['expire'] : 0, isset($cookie['path']) ? $cookie['path'] : '/', isset($cookie['domain']) ? $cookie['domain'] : '', isset($cookie['secure']) ? $cookie['secure'] : false)); } }
/** * @param \Icicle\Http\Message\Response $response */ public function addFromResponse(Response $response) { foreach ($response->getCookies() as $cookie) { $this->insert($cookie); } }
/** * Test cookies * * Pre-conditions: * A response is instantiated and assigned a cookie * * Post-conditions: * The cookie is added to the Response cookies array */ public function testCookies() { $r1 = new Response(); $cookie = new Cookie('foo', 'bar'); $r1->addCookie($cookie); $this->assertContains($cookie, $r1->getCookies(), 'Response does not contain cookie!'); }
/** * @param Request $request * @param bool $redirectsEnabled * @param int $maxRedirects * @return Response * @throws RequestException */ public function send(Request $request, $redirectsEnabled = false, $maxRedirects = 20) { /// Is there curl_init function if (!function_exists('curl_init')) { throw new RequestException('curl_init doesn\'t exists. Is curl extension instaled and enabled?'); } /// Dont send request without url if (!$request->getUrl()) { return new Response(); } $url = $request->getUrl(); if (!empty($this->params)) { $url .= '?' . http_build_query($this->params); } @curl_setopt($this->handle, CURLOPT_URL, $url); if ($request->getJSON() !== null) { $request->addHeaders(array('Content-Type' => 'application/json')); } $this->setCookies($request->getCookies()); $this->setHeaders($request->getHeaders()); $this->setMethod($request->getMethod()); /// Set post fields to CURL handle if (count($request->getPostFields()) > 0 || $request->getJSON()) { $fields = $request->getJSON() ? json_encode($request->getJSON()) : $request->getPostFields(); $this->setPostFields($fields, $request->getMethod(), $request->getHeader('Content-Type')); $request->setJSON(null); } /// Execute $response = curl_exec($this->handle); /// Remove content type header to not be used in further requests $request->unsetHeader('Content-Type'); /// Handle CURL error if ($response == FALSE) { throw new RequestException("CURL error [" . curl_errno($this->handle) . "]: " . curl_error($this->handle), curl_errno($this->handle)); } /// Separate response header and body /// Http 100 workaround $parts = explode("\r\n\r\nHTTP/", $response); $parts = (count($parts) > 1 ? 'HTTP/' : '') . array_pop($parts); list($headers, $body) = explode("\r\n\r\n", $parts, 2); $response = new Response(curl_getinfo($this->handle, CURLINFO_HTTP_CODE), $headers, $body); /// If cookiesEnabled then call addCookies with response cookies if ($request->isCookiesEnabled()) { $request->addCookies($response->getCookies()); } /// Are redirects enabled? (Also check redirects count) if ($redirectsEnabled && ($response->getCode() == 301 || $response->getCode() == 302 || $response->getCode() == 303) && $this->redirectCount < $maxRedirects) { $response = $this->doFollow($request, $response, $maxRedirects); } else { if ($this->redirectCount == $maxRedirects) { throw new RequestException("Maximum of " . $maxRedirects . " redirects reached."); } $this->redirectCount = 0; } return $response; }
/** * Get response cookie(s) after the request has been sent. * If redirects were allowed and several responses were received, the data references the last received response. * @return array Array of Cookie objects */ public function getResponseCookies() { return $this->response->getCookies(); }
public function testGetCookies() { $this->assertThat($this->object->getCookies(), $this->equalTo($this->cookies)); }