/** * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if ($staticFile = $this->getStaticFile($request->getUri())) { return $staticFile; } $outResponse = $next($request, $response); if (!$this->isHtmlAccepted($request)) { return $outResponse; } $debugBarHead = $this->debugBarRenderer->renderHead(); $debugBarBody = $this->debugBarRenderer->render(); if ($this->isHtmlResponse($outResponse)) { $body = $outResponse->getBody(); if (!$body->eof() && $body->isSeekable()) { $body->seek(0, SEEK_END); } $body->write($debugBarHead . $debugBarBody); return $outResponse; } $outResponseBody = Serializer::toString($outResponse); $template = '<html><head>%s</head><body><h1>DebugBar</h1><p>Response:</p><pre>%s</pre>%s</body></html>'; $escapedOutResponseBody = htmlspecialchars($outResponseBody); $result = sprintf($template, $debugBarHead, $escapedOutResponseBody, $debugBarBody); return new HtmlResponse($result); }
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if ($request->getMethod() === 'POST') { $parsedBody = $this->parseBody($request->getBody()); if (is_array($parsedBody) && isset($parsedBody[self::PARAM])) { $requestToSimulate = $parsedBody[self::PARAM]; $deserializedRequest = RequestSerializer::fromString($requestToSimulate); $request = new ServerRequest($request->getServerParams(), $request->getUploadedFiles(), $deserializedRequest->getUri(), $deserializedRequest->getMethod(), $deserializedRequest->getBody(), $deserializedRequest->getHeaders()); } } $requestAsString = RequestSerializer::toString($request); $responseResult = $next($request, $response); $responseAsString = ResponseSerializer::toString($responseResult); $html = sprintf($this->getHtmlTemplate(), self::PARAM, $requestAsString, $responseAsString); return new HtmlResponse($html); }
private function generateResponseLog(Request $request, Response $response) { if ($this->options['full']) { return ResponseSerializer::toString($response); } $reasonPhrase = $response->getReasonPhrase(); $msg = sprintf("%s %s", $response->getStatusCode(), $reasonPhrase ? $reasonPhrase : ''); if ($response->hasHeader('X-Request-Id')) { $msg .= ' RequestId: ' . $response->getHeader('X-Request-Id')[0]; } elseif ($request->hasHeader('X-Request-Id')) { $msg .= ' RequestId: ' . $request->getHeader('X-Request-Id')[0]; } if ($response->hasHeader('X-Response-Time')) { $msg .= ' ResponseTime: ' . $response->getHeader('X-Response-Time')[0]; } return $msg; }
/** * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $out * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null) { $outResponse = $out($request, $response); if (!$this->isHtmlAccepted($request)) { return $outResponse; } $debugBarHead = $this->debugBarRenderer->renderHead(); $debugBarBody = $this->debugBarRenderer->render(); if ($this->isHtmlResponse($outResponse)) { $outResponse->getBody()->write($debugBarHead . $debugBarBody); return $outResponse; } $outResponseBody = Serializer::toString($outResponse); $template = '<html><head>%s</head><body><h1>DebugBar</h1><p>Response:</p><pre>%s</pre>%s</body></html>'; $escapedOutResponseBody = htmlspecialchars($outResponseBody); $result = sprintf($template, $debugBarHead, $escapedOutResponseBody, $debugBarBody); return new HtmlResponse($result); }
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null) { $response = $out($request, $response); $contentType = $response->getHeaderLine('Content-Type'); $accept = $request->getHeaderLine('Accept'); if (!$this->acceptHtml($accept)) { return $out($request, $response); } if (self::HTML_CONTENT_TYPE === $contentType) { return $out($request, $response); } $noneHtmlBody = Serializer::toString($response); $escapedBody = htmlspecialchars($noneHtmlBody); $html = '<html><head><title>Middleware response debugger</title></head><body><h1>Debug response!</h1><hr><pre>%s</pre><hr></body></html>'; $htmlBody = sprintf($html, $escapedBody); $htmlResponse = new HtmlResponse($htmlBody); return $htmlResponse; }
public function unserialize($serialized) { $data = unserialize($serialized); $this->requestData = $data['requestData']; if (isset($data['request'])) { $this->request = \Zend\Diactoros\Request\Serializer::fromString($data['request']); } if (isset($data['response'])) { $this->response = \Zend\Diactoros\Response\Serializer::fromString($data['response']); } }
/** * @param string $fileName * @param RequestInterface $request * @param string $type * @return ResponseInterface */ private function executePhpFile($fileName, RequestInterface $request, $type) { $process = $this->getProcess($fileName, $request); $process->start(); $this->eventDispatcher->dispatch(new CgiExecuteEvent(sprintf('cgi.verify.%s.executing', $type), $request)); $process->wait(); if (!$process->isSuccessful()) { throw CodeExecutionException::fromProcess($process); } //if no status line, pre-pend 200 OK $output = $process->getOutput(); if (!preg_match('/^HTTP\\/([1-9]\\d*\\.\\d) ([1-5]\\d{2})(\\s+(.+))?\\r\\n/', $output)) { $output = "HTTP/1.0 200 OK\r\n" . $output; } return ResponseSerializer::fromString($output); }
public function format(ServerRequestInterface $request, ResponseInterface $response) { return Serializer::toString($response); }
/** * Create discover event * * @param string $message * * @return DiscoverEvent */ protected function createDiscoverEvent($message) { $response = ResponseSerializer::fromString($message); $event = new DiscoverEvent(); if ($response->hasHeader('CACHE-CONTROL')) { $value = $response->getHeaderLine('CACHE-CONTROL'); $event->setLifetime(intval(substr($value, strpos($value, '=') + 1))); } if ($response->hasHeader('DATE')) { $event->setDate(new \DateTime($response->getHeaderLine('DATE'))); } if ($response->hasHeader('LOCATION')) { $event->setDescriptionUrl(new Uri($response->getHeaderLine('LOCATION'))); } if ($response->hasHeader('SERVER')) { $event->setServerString($response->getHeaderLine('SERVER')); } if ($response->hasHeader('ST')) { $event->setSearchTargetString($response->getHeaderLine('ST')); } if ($response->hasHeader('USN')) { $event->setUniqueServiceNameString($response->getHeaderLine('USN')); } return $event; }
protected function getContentToLog(ServerRequestInterface $request, ResponseInterface $response, callable $out = null) { return Serializer::toString($response); }