public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) { // Use only the path for routing. $requestTarget = parse_url($request->getRequestTarget(), PHP_URL_PATH); $route = $this->getStaticRoute($requestTarget); if ($route) { return $route($request, $response, $next); } $route = $this->getPrefixRoute($requestTarget); if ($route) { return $route($request, $response, $next); } // Try each of the routes. foreach ($this->patternRoutes as $route) { if ($route->matchesRequestTarget($requestTarget)) { $pathVariables = $route->getPathVariables(); if ($this->pathVariablesAttributeName) { $request = $request->withAttribute($this->pathVariablesAttributeName, $pathVariables); } else { foreach ($pathVariables as $name => $value) { $request = $request->withAttribute($name, $value); } } return $route($request, $response, $next); } } // If no route exists, set the status code of the response to 404 and // return the response without propagating. return $response->withStatus(404); }
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $tokenHeader = $request->getHeader('X-Auth-Token'); if (count($tokenHeader) > 0) { $tokenHeaderValue = $tokenHeader[0]; } else { $tokenHeaderValue = null; } $data = array('requestMethod' => $request->getMethod(), 'requestUri' => $request->getRequestTarget(), 'queryParams' => $request->getQueryParams(), 'formParams' => $request->getParsedBody(), 'rawBody' => (string) $request->getBody(), 'headers' => $request->getHeaders(), 'X-Auth-Token' => $tokenHeaderValue); return new JsonResponse($data); }
/** * Construct action request. * * @param \Psr\Http\Message\ServerRequestInterface $request Request to wrap. */ public function __construct(\Psr\Http\Message\ServerRequestInterface $request) { parent::__construct($request->getBody()); foreach ($request->getHeaders() as $name => $value) { $this->setHeader($name, $value); } $this->protocolVersion = $request->getProtocolVersion(); $this->method = $request->getMethod(); $this->requestTarget = $request->getRequestTarget(); $this->uri = $request->getUri(); $this->attributes = $request->getAttributes(); $this->cookies = $request->getCookieParams(); $this->data = $request->getParsedBody(); $this->query = $request->getQueryParams(); $this->server = $request->getServerParams(); $this->files = $request->getUploadedFiles(); if (isset($this->server['SCRIPT_NAME'])) { $this->attributes['basePath'] = dirname($this->server['SCRIPT_NAME']); $this->attributes['scriptName'] = basename($this->server['SCRIPT_NAME']); } else { $this->attributes['basePath'] = '/'; $this->attributes['scriptName'] = 'index.php'; } if (!isset($this->attributes['path'])) { $this->attributes['path'] = self::findPath($this); } if (!isset($this->attributes['rewrite'])) { $this->attributes['rewrite'] = false; } if (!isset($this->attributes['accepts'])) { $this->attributes['accepts'] = []; if (isset($this->server['HTTP_ACCEPT'])) { $contentTypes = explode(',', $this->server['HTTP_ACCEPT']); foreach ($contentTypes as $contentType) { $contentType = explode(';', $contentType); $this->attributes['accepts'][] = trim(strtolower($contentType[0])); } } } if (!isset($this->attributes['encodings'])) { $this->attributes['encodings'] = []; if (isset($this->server['HTTP_ACCEPT_ENCODING'])) { $acceptEncodings = explode(',', $this->server['HTTP_ACCEPT_ENCODING']); foreach ($acceptEncodings as $encoding) { $this->attributes['encodings'][] = trim(strtolower($encoding)); } } } }
function match($pattern, ServerRequestInterface $request, ServerRequestInterface &$modifiedRequest) { $modifiedRequest = $request; $path = $request->getRequestTarget(); if ($path == '.') { $path = ''; } if (!preg_match(self::SYNTAX, $pattern, $m)) { throw new ConfigException(sprintf("Invalid route pattern matching expression: '<kbd>%s</kbd>'", $pattern)); } array_push($m, '', ''); list($all, $methods, $pathPattern) = $m; if ($methods && !in_array($request->getMethod(), explode('|', rtrim($methods)))) { return false; } // The asterisk matches any path. if ($pathPattern == '*') { return true; } // The plus matches any non-empty path. if ($pathPattern == '+') { return !!strlen($path); } // @parameters never match the empty path (which is encoded as a single dot) $compiledPattern = preg_replace(['/(?<=[^\\*\\.])$/', '/\\*$/', '/\\.\\.\\.$/', '/\\.$/', '/@(\\w+)/', '[\\[\\]\\{\\}\\(\\)\\.\\?]'], ['(?<_next>$)', '(?<_next>.*)', '(?<_next>)', '(?<_next>$)', '(?<$1>(?=(?:$|[^\\.]))[^/]*)', '\\$0'], $pathPattern); if (!preg_match("#^{$compiledPattern}#", $path, $m2, PREG_OFFSET_CAPTURE)) { return false; } $newPath = substr($path, $m2['_next'][1] + 1); if ($path != $newPath) { $request = $request->withRequestTarget($newPath === false ? '' : $newPath); } foreach ($m2 as $k => $v) { if (is_string($k) && $k[0] != '_') { // exclude reserved _next key $request = $request->withAttribute('@' . $k, urldecode($v[0])); } } $modifiedRequest = $request; return true; }
/** * {@inheritdoc} */ public function getRequestTarget() { return $this->wrapped->getRequestTarget(); }
/** * {@inheritDoc} */ public function getRequestTarget() { return $this->psrRequest->getRequestTarget(); }
/** * @param ServerRequestInterface $request * @param ResponseInterface $response * @param string[] $methods * * @return ResponseInterface */ public function handleNotAllowed(ServerRequestInterface $request, ResponseInterface $response, array $methods) { if ($level = $this->shouldLog('not-allowed')) { $message = sprintf('%s on %s', $request->getMethod(), $request->getRequestTarget()); $this->logEvent('not-allowed', $message, $level); } return $this->handler->handleNotAllowed($request, $response, $methods); }
/** * Constructor. * * @param string $method Method that were not allowed. */ public function __construct(ServerRequestInterface $request) { parent::__construct("Bad request with '{$request->getMethod()}' method, at '{$request->getRequestTarget()}' location.", 400, "It seems that your request is malformed in some way." . " Try again or contact server administrator for more info."); }
/** * Generate the error log message. * * @param \Psr\Http\Message\ServerRequestInterface $request The current request. * @param \Exception $exception The exception to log a message for. * @return string Error message */ protected function getMessage($request, $exception) { $message = sprintf("[%s] %s", get_class($exception), $exception->getMessage()); $debug = Configure::read('debug'); if ($debug && method_exists($exception, 'getAttributes')) { $attributes = $exception->getAttributes(); if ($attributes) { $message .= "\nException Attributes: " . var_export($exception->getAttributes(), true); } } $message .= "\nRequest URL: " . $request->getRequestTarget(); $referer = $request->getHeaderLine('Referer'); if ($referer) { $message .= "\nReferer URL: " . $referer; } if ($this->config('trace')) { $message .= "\nStack Trace:\n" . $exception->getTraceAsString() . "\n\n"; } return $message; }
/** * Constructor. * * @param string $path Path that were not found. */ public function __construct(ServerRequestInterface $request) { parent::__construct("Route for '{$request->getRequestTarget()}' is not found.", 404, "The requested page is not found on this server." . " Please check the URL for typos or contact server administrator for more info."); }
/** * @param ServerRequestInterface $r * @param $title */ private function logRequest($r, $title, $forceShow = false) { /** @var ServerRequestInterface $current */ $current = $this->currentRequestMutator->get(); $showAll = !$this->currentRequestMutator->get() || $forceShow; $icon = $showAll ? '' : '<sup>*</sup>'; if ($showAll || $r->getHeaders() != $current->getHeaders()) { $out['Headers' . $icon] = map($r->getHeaders(), function ($v) { return implode('<br>', $v); }); } if ($showAll || $r->getAttributes() != $current->getAttributes()) { $out['Attributes' . $icon] = $r->getAttributes(); } if ($showAll || $r->getRequestTarget() != $current->getRequestTarget()) { $out['Request target' . $icon] = $r->getRequestTarget(); } if ($showAll || $r->getBody()->getSize() != self::$currentRequestSize) { $out['Size' . $icon] = $r->getBody()->getSize(); } $this->routingLogger->write("<div class='indent'>")->simpleTable($out, $title)->write('</div>'); }
/** * Constructor. * * @param string $method Method that were not allowed. */ public function __construct(ServerRequestInterface $request) { parent::__construct("Method '{$request->getMethod()}' is not allowed at the '{$request->getRequestTarget()}' location.", 405, "A request method is not supported for the requested page." . " Try again or contact server administrator for more info."); }
/** * {@inheritdoc} */ public function getRequestPlaceholders(ServerRequestInterface $request) { $placeholders = $this->getPlaceholders($request->getRequestTarget()); return $request->withAttributes($placeholders); }