/** * Finalize response for delivery to client * * Apply final preparations to the resposne object * so that it is suitable for delivery to the client. * * @param \Slim\Interfaces\Http\RequestInterface $request * @return \Slim\Interfaces\Http\Response Self * @api */ public function finalize(\Slim\Interfaces\Http\RequestInterface $request) { $sendBody = true; if (in_array($this->status, array(204, 304)) === true) { $this->headers->remove('Content-Type'); $this->headers->remove('Content-Length'); $sendBody = false; } else { $size = @$this->getSize(); if ($size) { $this->headers->set('Content-Length', $size); } } // Serialize cookies into HTTP header $this->cookies->setHeaders($this->headers); // Remove body if HEAD request if ($request->isHead() === true) { $sendBody = false; } // Truncate body if it should not be sent with response if ($sendBody === false) { $this->body->close(); $this->body = new \Guzzle\Stream\Stream(fopen('php://temp', 'r+')); } return $this; }
/** * Delete HTTP cookie header * * This method will construct and set the HTTP `Set-Cookie` header to invalidate * a client-side HTTP cookie. If a cookie with the same name (and, optionally, domain) * is already set in the HTTP response, it will also be removed. Slim uses this method * instead of PHP's native `setcookie` method. This allows more control of the HTTP header * irrespective of PHP's native implementation's dependency on PHP versions. * * This method accepts the \Slim\Http\Headers object by reference as its * first argument; this method directly modifies this object instead of * returning a value. * * @param \Slim\Interfaces\Http\HeadersInterface $headers * @param string $name * @param array $value * @api */ public function deleteHeader(HeadersInterface &$headers, $name, $value = array()) { $crumbs = $headers->has('Set-Cookie') ? explode("\n", $headers->get('Set-Cookie')) : array(); $cookies = array(); foreach ($crumbs as $crumb) { if (isset($value['domain']) && $value['domain']) { $regex = sprintf('@%s=.*domain=%s@', urlencode($name), preg_quote($value['domain'])); } else { $regex = sprintf('@%s=@', urlencode($name)); } if (preg_match($regex, $crumb) === 0) { $cookies[] = $crumb; } } if (!empty($cookies)) { $headers->set('Set-Cookie', implode("\n", $cookies)); } else { $headers->remove('Set-Cookie'); } $this->setHeader($headers, $name, array_merge(array('value' => '', 'path' => null, 'domain' => null, 'expires' => time() - 100), $value)); }
/** * Remove header * * @param string $name * @api */ public function removeHeader($name) { $this->headers->remove($name); }