public function handle(Event $event, Response $response) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type')) { return $response; } if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects) { $response->setContent(sprintf('<html><head></head><body><h1>This Request redirects to<br /><a href="%s">%s</a>.</h1></body></html>', $response->headers->get('location'), $response->headers->get('location'))); $response->setStatusCode(200); $response->headers->delete('Location'); } $request = $event->getParameter('request'); if (!$response->headers->has('X-Debug-Token') || '3' === substr($response->getStatusCode(), 0, 1) || $response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html') || 'html' !== $request->getRequestFormat() || $request->isXmlHttpRequest()) { return $response; } $this->injectToolbar($request, $response); return $response; }
public function handle(EventInterface $event, Response $response) { if (HttpKernelInterface::MASTER_REQUEST !== $event->get('request_type')) { return $response; } if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects) { $response->setContent(sprintf('<html><head></head><body><h1>This Request redirects to<br /><a href="%1$s">%1$s</a>.</h1><h4>The redirect was intercepted by the web debug toolbar to help debugging.<br/>For more information, see the "intercept-redirects" option of the Profiler.</h4></body></html>', $response->headers->get('Location'))); $response->setStatusCode(200); $response->headers->remove('Location'); } $request = $event->get('request'); if (!$response->headers->has('X-Debug-Token') || '3' === substr($response->getStatusCode(), 0, 1) || $response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html') || 'html' !== $request->getRequestFormat() || $request->isXmlHttpRequest()) { return $response; } $this->injectToolbar($response); return $response; }
public function testIsRedirectRedirection() { foreach (array(301, 302, 303, 307) as $code) { $response = new Response('', $code); $this->assertTrue($response->isRedirection()); $this->assertTrue($response->isRedirect()); } $response = new Response('', 304); $this->assertTrue($response->isRedirection()); $this->assertFalse($response->isRedirect()); $response = new Response('', 200); $this->assertFalse($response->isRedirection()); $this->assertFalse($response->isRedirect()); $response = new Response('', 404); $this->assertFalse($response->isRedirection()); $this->assertFalse($response->isRedirect()); $response = new Response('', 301, array('Location' => '/good-uri')); $this->assertFalse($response->isRedirect('/bad-uri')); $this->assertTrue($response->isRedirect('/good-uri')); }
public function assertRedirectUrl(Response $returnResponse, $expectedUrl) { $this->assertTrue($returnResponse->isRedirect($expectedUrl), "Expected: {$expectedUrl}\nGot: " . $returnResponse->headers->get('Location')); }
public static function assertRedirect(Response $response, $location) { self::assertTrue($response->isRedirect(), 'Response is not a redirect, got status code: ' . $response->getStatusCode()); self::assertEquals('http://localhost' . $location, $response->headers->get('Location')); }
/** * {@inheritdoc} */ public function collect(Request $request, Response $response, \Exception $exception = null) { $responseHeaders = $response->headers->all(); $cookies = array(); foreach ($response->headers->getCookies() as $cookie) { $cookies[] = $this->getCookieHeader($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly()); } if (count($cookies) > 0) { $responseHeaders['Set-Cookie'] = $cookies; } // attributes are serialized and as they can be anything, they need to be converted to strings. $attributes = array(); foreach ($request->attributes->all() as $key => $value) { if ('_route' === $key && is_object($value)) { $attributes[$key] = $this->varToString($value->getPath()); } elseif ('_route_params' === $key) { // we need to keep route params as an array (see getRouteParams()) foreach ($value as $k => $v) { $value[$k] = $this->varToString($v); } $attributes[$key] = $value; } else { $attributes[$key] = $this->varToString($value); } } $content = null; try { $content = $request->getContent(); } catch (\LogicException $e) { // the user already got the request content as a resource $content = false; } $sessionMetadata = array(); $sessionAttributes = array(); $session = null; $flashes = array(); if ($request->hasSession()) { $session = $request->getSession(); if ($session->isStarted()) { $sessionMetadata['Created'] = date(DATE_RFC822, $session->getMetadataBag()->getCreated()); $sessionMetadata['Last used'] = date(DATE_RFC822, $session->getMetadataBag()->getLastUsed()); $sessionMetadata['Lifetime'] = $session->getMetadataBag()->getLifetime(); $sessionAttributes = $session->all(); $flashes = $session->getFlashBag()->peekAll(); } } $statusCode = $response->getStatusCode(); $this->data = array('method' => $request->getMethod(), 'format' => $request->getRequestFormat(), 'content' => $content, 'content_type' => $response->headers->get('Content-Type', 'text/html'), 'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : '', 'status_code' => $statusCode, 'request_query' => $request->query->all(), 'request_request' => $request->request->all(), 'request_headers' => $request->headers->all(), 'request_server' => $request->server->all(), 'request_cookies' => $request->cookies->all(), 'request_attributes' => $attributes, 'response_headers' => $responseHeaders, 'session_metadata' => $sessionMetadata, 'session_attributes' => $sessionAttributes, 'flashes' => $flashes, 'path_info' => $request->getPathInfo(), 'controller' => 'n/a', 'locale' => $request->getLocale()); if (isset($this->data['request_headers']['php-auth-pw'])) { $this->data['request_headers']['php-auth-pw'] = '******'; } if (isset($this->data['request_server']['PHP_AUTH_PW'])) { $this->data['request_server']['PHP_AUTH_PW'] = '******'; } if (isset($this->data['request_request']['_password'])) { $this->data['request_request']['_password'] = '******'; } if (isset($this->controllers[$request])) { $this->data['controller'] = $this->parseController($this->controllers[$request]); unset($this->controllers[$request]); } if (null !== $session) { if ($request->attributes->has('_redirected')) { $this->data['redirect'] = $session->remove('sf_redirect'); } if ($response->isRedirect()) { $session->set('sf_redirect', array('token' => $response->headers->get('x-debug-token'), 'route' => $request->attributes->get('_route', 'n/a'), 'method' => $request->getMethod(), 'controller' => $this->parseController($request->attributes->get('_controller')), 'status_code' => $statusCode, 'status_text' => Response::$statusTexts[(int) $statusCode])); } } }
protected function check(Response $response) { return $response->isRedirect(); }
/** * {@inheritdoc} */ public function collect(Request $request, Response $response, \Exception $exception = null) { // attributes are serialized and as they can be anything, they need to be converted to strings. $attributes = array(); $route = ''; foreach ($request->attributes->all() as $key => $value) { if ('_route' === $key) { $route = is_object($value) ? $value->getPath() : $value; $attributes[$key] = $route; } else { $attributes[$key] = $value; } } $content = null; try { $content = $request->getContent(); } catch (\LogicException $e) { // the user already got the request content as a resource $content = false; } $sessionMetadata = array(); $sessionAttributes = array(); $session = null; $flashes = array(); if ($request->hasSession()) { $session = $request->getSession(); if ($session->isStarted()) { $sessionMetadata['Created'] = date(DATE_RFC822, $session->getMetadataBag()->getCreated()); $sessionMetadata['Last used'] = date(DATE_RFC822, $session->getMetadataBag()->getLastUsed()); $sessionMetadata['Lifetime'] = $session->getMetadataBag()->getLifetime(); $sessionAttributes = $session->all(); $flashes = $session->getFlashBag()->peekAll(); } } $statusCode = $response->getStatusCode(); $this->data = array( 'method' => $request->getMethod(), 'format' => $request->getRequestFormat(), 'content' => $content, 'content_type' => $response->headers->get('Content-Type', 'text/html'), 'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : '', 'status_code' => $statusCode, 'request_query' => $request->query->all(), 'request_request' => $request->request->all(), 'request_headers' => $request->headers->all(), 'request_server' => $request->server->all(), 'request_cookies' => $request->cookies->all(), 'request_attributes' => $attributes, 'route' => $route, 'response_headers' => $response->headers->all(), 'session_metadata' => $sessionMetadata, 'session_attributes' => $sessionAttributes, 'flashes' => $flashes, 'path_info' => $request->getPathInfo(), 'controller' => 'n/a', 'locale' => $request->getLocale(), ); if (isset($this->data['request_headers']['php-auth-pw'])) { $this->data['request_headers']['php-auth-pw'] = '******'; } if (isset($this->data['request_server']['PHP_AUTH_PW'])) { $this->data['request_server']['PHP_AUTH_PW'] = '******'; } if (isset($this->data['request_request']['_password'])) { $this->data['request_request']['_password'] = '******'; } foreach ($this->data as $key => $value) { if (!is_array($value)) { continue; } if ('request_headers' === $key || 'response_headers' === $key) { $value = array_map(function ($v) { return isset($v[0]) && !isset($v[1]) ? $v[0] : $v; }, $value); } if ('request_server' !== $key && 'request_cookies' !== $key) { $this->data[$key] = array_map(array($this, 'cloneVar'), $value); } } if (isset($this->controllers[$request])) { $this->data['controller'] = $this->parseController($this->controllers[$request]); unset($this->controllers[$request]); } if (null !== $session && $session->isStarted()) { if ($request->attributes->has('_redirected')) { $this->data['redirect'] = $session->remove('sf_redirect'); } if ($response->isRedirect()) { $session->set('sf_redirect', array( 'token' => $response->headers->get('x-debug-token'), 'route' => $request->attributes->get('_route', 'n/a'), 'method' => $request->getMethod(), 'controller' => $this->parseController($request->attributes->get('_controller')), 'status_code' => $statusCode, 'status_text' => Response::$statusTexts[(int) $statusCode], )); } } }
/** * Transform an exception handler's response into a Response object. * * @param mixed $response * @param \Exception $exception * * @return \Symfony\Component\HttpFoundation\Response */ protected function makeResponse($response, Exception $exception) { if (!$response instanceof Response) { $response = new Response($response); } if (!$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) { if ($exception instanceof HttpExceptionInterface) { $response->setStatusCode($exception->getStatusCode()); $response->headers->add($exception->getHeaders()); } else { $response->setStatusCode(500); } } return $response; }