Sets the HTTP protocol version (1.0 or 1.1).
public setProtocolVersion ( string $version ) : |
||
$version | string | The HTTP protocol version |
Результат |
public function handleKernelException(GetResponseForExceptionEvent $event) { if ($this->container->get('kernel')->getEnvironment() !== 'dev') { $exception = FlattenException::create($event->getException()); // First, log the exception to the standard error logs. $this->container->get('logger')->error(' In File ' . $exception->getFile() . ', on line ' . $exception->getLine() . ': ' . $exception->getMessage()); // Determine what the HTTP status code should be. if ($event->getException() instanceof \Symfony\Component\HttpKernel\Exception\HttpException) { $httpStatusCode = $event->getException()->getStatusCode(); } else { $httpStatusCode = $exception->getCode(); if ($exception->getCode() < 100 || $exception->getCode() >= 600) { $httpStatusCode = 500; } } $parameters = ['status_code' => $httpStatusCode, 'status_text' => $exception->getMessage(), 'exception' => $exception]; if (in_array('application/json', $event->getRequest()->getAcceptableContentTypes())) { $errorContent = $this->container->get('templating')->render(':default:exception.json.twig', $parameters); } else { $errorContent = $this->container->get('templating')->render(':default:error.html.twig', $parameters); } $response = new Response($errorContent, $httpStatusCode); $response->setProtocolVersion('1.1'); $event->setResponse($response); } }
public function testGetSetProtocolVersion() { $response = new Response(); $this->assertEquals('1.0', $response->getProtocolVersion()); $response->setProtocolVersion('1.1'); $this->assertEquals('1.1', $response->getProtocolVersion()); }
/** * Locks a Request during the call to the backend. * * @param Request $request A Request instance * @param Response $entry A Response instance * * @return bool true if the cache entry can be returned even if it is staled, false otherwise */ protected function lock(Request $request, Response $entry) { // try to acquire a lock to call the backend $lock = $this->store->lock($request); // there is already another process calling the backend if (true !== $lock) { // check if we can serve the stale entry if (null === ($age = $entry->headers->getCacheControlDirective('stale-while-revalidate'))) { $age = $this->options['stale_while_revalidate']; } if (abs($entry->getTtl()) < $age) { $this->record($request, 'stale-while-revalidate'); // server the stale response while there is a revalidation return true; } // wait for the lock to be released $wait = 0; while ($this->store->isLocked($request) && $wait < 5000000) { usleep(50000); $wait += 50000; } if ($wait < 2000000) { // replace the current entry with the fresh one $new = $this->lookup($request); $entry->headers = $new->headers; $entry->setContent($new->getContent()); $entry->setStatusCode($new->getStatusCode()); $entry->setProtocolVersion($new->getProtocolVersion()); foreach ($new->headers->getCookies() as $cookie) { $entry->headers->setCookie($cookie); } } else { // backend is slow as hell, send a 503 response (to avoid the dog pile effect) $entry->setStatusCode(503); $entry->setContent('503 Service Unavailable'); $entry->headers->set('Retry-After', 10); } return true; } // we have the lock, call the backend return false; }
/** * Do a forward to specified uri using request dispatcher. * * This method is used by all internal method needing to do a forward. * * @param string $uri Context-relative URI to forward to * @param \Symfony\Component\HttpFoundation\Request $request Current page request * @param \Symfony\Component\HttpFoundation\Response $response Current page response */ protected function doForward($uri, \Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\HttpFoundation\Response $response) { // Create a new URI $uri = $request->getUriForPath($uri); // Consider using standard $_POST, $_FILES etc. $subRequest = Request::create($uri, $request->getMethod(), $request->getMethod() == 'POST' ? $request->request->all() : $request->query->all(), $request->cookies->all(), $request->files->all(), $request->server->all()); // If it was a POST then ensure it also has any query parameters if ($request->getMethod() == 'POST') { $subRequest->query->add($request->query->all()); } if ($request->getSession()) { $subRequest->setSession($request->getSession()); } // Obtain a new subrequest without Silex attributes $allowedKeys = array_filter(array_keys($request->attributes->all()), function ($key) { // Filter out silex "_" attributes return substr($key, 0, 1) != '_'; }); $subRequest->attributes->add(array_intersect_key($request->attributes->all(), array_flip($allowedKeys))); // Call for a sub-request (Note: Non-conventionally passes parent query/attributes) $subResponse = $this->actionKernel->getApplication()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false); // Update our current response to bring in the response $response->setContent($subResponse->getContent()); $response->setStatusCode($subResponse->getStatusCode()); $response->setCharset($subResponse->getCharset()); $response->setProtocolVersion($subResponse->getProtocolVersion()); // Determine whether all headers are 'added' or should replace (?) $response->headers->add($subResponse->headers->all()); }
/** * Execute a Request object via cURL * * @param Request $request the request to execute * * @return Response * * @throws CurlErrorException */ private function handleRaw(Request $request) { if ($this->logger) { $this->logger->startRequest($request); } $curl = $this->getCurlRequest(); $curl->setOptionArray(array(CURLOPT_URL => $request->getUri(), CURLOPT_HTTPHEADER => $this->buildHeadersArray($request->headers), CURLINFO_HEADER_OUT => true)); $curl->setMethod($request->getMethod()); if ("POST" === $request->getMethod()) { $this->setPostFields($curl, $request); } if ("PUT" === $request->getMethod() && count($request->files->all()) > 0) { $file = current($request->files->all()); $curl->setOptionArray(array(CURLOPT_INFILE => '@' . $file->getRealPath(), CURLOPT_INFILESIZE => $file->getSize())); } $content = new ContentCollector(); $headers = new HeaderCollector(); // These options must not be tampered with to ensure proper functionality $curl->setOptionArray(array(CURLOPT_HEADERFUNCTION => array($headers, "collect"), CURLOPT_WRITEFUNCTION => array($content, "collect"))); $curl->execute(); $response = new Response($content->retrieve(), $headers->getCode(), $headers->retrieve()); $response->setProtocolVersion($headers->getVersion()); $response->setStatusCode($headers->getCode(), $headers->getMessage()); if ($this->logger) { $this->logger->stopRequest($response); } return $response; }
$success = false; try { $imagine = new \Imagine\Gd\Imagine(); $prefix = __DIR__ . '/../../config/'; foreach (array('herman.original.jpg', 'robert.original.jpg') as $original) { $image = $imagine->open($prefix . $original); $image->draw()->ellipse(new \Imagine\Image\Point(mt_rand(0, 50), mt_rand(0, 50)), new \Imagine\Image\Box(mt_rand(50, 200), mt_rand(50, 200)), new \Imagine\Image\Color(array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255))), true); $image->save($prefix . str_replace('.original', '', $original)); } $success = true; } catch (\Exception $e) { } return new JsonResponse(array('success' => $success)); }); $app->get('/image/{image_id}', function ($image_id, Application $app, Request $request) use($datas) { if (!isset($datas[$image_id])) { throw new \Exception('Invalid image id'); } $response = new Response(); $response->setPrivate(); $response->setProtocolVersion('1.1'); $response->setEtag($datas[$image_id]['etag']); $response->setLastModified($datas[$image_id]['last_modified']); $response->headers->addCacheControlDirective('must-revalidate', true); if (!$response->isNotModified($request)) { $response->headers->set('content-type', 'image/jpeg'); $response->setContent(file_get_contents(__DIR__ . '/../../config/' . $image_id)); } return $response; })->assert('image_id', '(herman|robert)\\.jpg'); return $app;
/** * {@inheritdoc} */ public function createResponse(ResponseInterface $psrResponse) { $response = new Response($psrResponse->getBody()->__toString(), $psrResponse->getStatusCode(), $psrResponse->getHeaders()); $response->setProtocolVersion($psrResponse->getProtocolVersion()); foreach ($psrResponse->getHeader('Set-Cookie') as $cookie) { $response->headers->setCookie($this->createCookie($cookie)); } return $response; }
private function createResponse($statusCode) { $memoryUsage = round(memory_get_peak_usage() / 1048576, 4); $response = new Response(); $response->setProtocolVersion('1.1'); $response->setStatusCode($statusCode); $response->headers->set('X-Men', $this->randomXPerson()); $response->headers->set('X-Memory-Usage', $memoryUsage); return $response; }