本文整理汇总了PHP中Psr\Http\Message\ServerRequestInterface::hasHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP ServerRequestInterface::hasHeader方法的具体用法?PHP ServerRequestInterface::hasHeader怎么用?PHP ServerRequestInterface::hasHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ServerRequestInterface
的用法示例。
在下文中一共展示了ServerRequestInterface::hasHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getRealUri
public static function getRealUri(ServerRequestInterface $request)
{
$uri = $request->getUri();
if ($request->hasHeader(self::PROTO_HEADER_HEROKU)) {
$uri = $uri->withScheme($request->getHeader(self::PROTO_HEADER_HEROKU)[0]);
}
if ($request->hasHeader(self::PORT_HEADER_HEROKU)) {
$uri = $uri->withPort(intval($request->getHeader(self::PORT_HEADER_HEROKU)[0]));
}
return $uri;
}
示例2: back
/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return \Psr\Http\Message\ResponseInterface
* @throws \Wandu\Http\Exception\BadRequestException
*/
function back(ServerRequestInterface $request)
{
if ($request->hasHeader('referer')) {
return redirect($request->getHeader('referer'));
}
throw new BadRequestException();
}
示例3: __invoke
public function __invoke(Request $request)
{
/** Check for token on header */
if (isset($this->options['header'])) {
if ($request->hasHeader($this->options['header'])) {
$header = $request->getHeader($this->options['header'])[0];
if (preg_match($this->options['regex'], $header, $matches)) {
return $matches[1];
}
}
}
/** If nothing on header, try query parameters */
if (isset($this->options['parameter'])) {
if (!empty($request->getQueryParams()[$this->options['parameter']])) {
return $request->getQueryParams()[$this->options['parameter']];
}
}
/** If nothing on parameters, try cookies */
if (isset($this->options['cookie'])) {
$cookie_params = $request->getCookieParams();
if (!empty($cookie_params[$this->options["cookie"]])) {
return $cookie_params[$this->options["cookie"]];
}
}
/** If nothing until now, check argument as last try */
if (isset($this->options['argument'])) {
if ($route = $request->getAttribute('route')) {
$argument = $route->getArgument($this->options['argument']);
if (!empty($argument)) {
return $argument;
}
}
}
throw new TokenNotFoundException('Token not found');
}
示例4: validateAuthorization
/**
* {@inheritdoc}
*/
public function validateAuthorization(ServerRequestInterface $request)
{
if ($request->hasHeader('authorization') === false) {
throw OAuthServerException::accessDenied('Missing "Authorization" header');
}
$header = $request->getHeader('authorization');
$jwt = trim(preg_replace('/^(?:\\s+)?Bearer\\s/', '', $header[0]));
try {
// Attempt to parse and validate the JWT
$token = (new Parser())->parse($jwt);
if ($token->verify(new Sha256(), $this->publicKey->getKeyPath()) === false) {
throw OAuthServerException::accessDenied('Access token could not be verified');
}
// Ensure access token hasn't expired
$data = new ValidationData();
$data->setCurrentTime(time());
if ($token->validate($data) === false) {
throw OAuthServerException::accessDenied('Access token is invalid');
}
// Check if token has been revoked
if ($this->accessTokenRepository->isAccessTokenRevoked($token->getClaim('jti'))) {
throw OAuthServerException::accessDenied('Access token has been revoked');
}
// Return the request with additional attributes
return $request->withAttribute('oauth_access_token_id', $token->getClaim('jti'))->withAttribute('oauth_client_id', $token->getClaim('aud'))->withAttribute('oauth_user_id', $token->getClaim('sub'))->withAttribute('oauth_scopes', $token->getClaim('scopes'));
} catch (\InvalidArgumentException $exception) {
// JWT couldn't be parsed so return the request as is
throw OAuthServerException::accessDenied($exception->getMessage());
} catch (\RuntimeException $exception) {
//JWR couldn't be parsed so return the request as is
throw OAuthServerException::accessDenied('Error while decoding to JSON');
}
}
示例5: __invoke
/**
* Invoking the middleware
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
* @return ResponseInterface $response
* @throws InternalServerError
* @throws UnsupportedMediaType
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
// Make sure we have deserializers
if (!isset($this->container['contentTypes'])) {
throw new InternalServerError('No serializers seems to be configured');
}
// Make sure we have a content type header to work with
if ($request->hasHeader('Content-Type')) {
// Get the content of the header
$header = $request->getHeaderLine('Content-Type');
// Make sure the header value isn't empty
if (!empty($header)) {
// Get priorities
$supported = $this->container['contentTypes'];
// Remove included parts that aren't part of the RFC
$header = preg_replace('/(;[a-z]*=[a-z0-9\\-]*)/i', '', $header);
// Replace the original header value
$request = $request->withHeader('Content-Type', $header);
// Check if the content type is supported
if (!in_array($header, $supported)) {
// The content type isn't supported
throw new UnsupportedMediaType('Can not handle the supplied content type. Supported types are ' . implode(', ', $supported));
}
}
}
// Call next middleware
return $next($request, $response, $next);
}
示例6: __invoke
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
{
if ($request->getMethod() === 'POST' && $request->hasHeader(self::HEADER_NAME)) {
$fakeMethod = $request->getHeaderLine(self::HEADER_NAME);
$request = $request->withMethod(strtoupper($fakeMethod));
}
return $out ? $out($request, $response) : $response;
}
示例7: getSignatureFromRequest
/**
* @param \Psr\Http\Message\ServerRequestInterface $request
*
* @return string
* @throws \livetyping\hermitage\app\exceptions\UnauthorizedException
*/
protected function getSignatureFromRequest(Request $request) : string
{
if (!$request->hasHeader(self::HEADER_AUTHENTICATE_SIGNATURE)) {
throw new UnauthorizedException('Signature is required.');
}
$signature = current($request->getHeader(self::HEADER_AUTHENTICATE_SIGNATURE));
return $signature;
}
示例8: __invoke
/**
* Process an incoming request and/or response.
*
* Accepts a server-side request and a response instance, and does
* something with them.
*
* If the response is not complete and/or further processing would not
* interfere with the work done in the middleware, or if the middleware
* wants to delegate to another process, it can use the `$out` callable
* if present.
*
* If the middleware does not return a value, execution of the current
* request is considered complete, and the response instance provided will
* be considered the response to return.
*
* Alternately, the middleware may return a response instance.
*
* Often, middleware will `return $out();`, with the assumption that a
* later middleware will return a response.
*
* @param Request $request
* @param Response $response
* @param null|callable $out
* @return null|Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
{
if (!$request->hasHeader('Accept-Language')) {
return $out($request, $response);
}
$locale = $request->getHeaderLine('Accept-Language');
$this->translator->setLocale($this->normalizeLocale($locale));
return $out($request, $response);
}
示例9: enrichRequestWithParsedBody
public function enrichRequestWithParsedBody(ServerRequestInterface $request)
{
if ($request->hasHeader(HeaderName::CONTENT_TYPE) && $request->getHeaderLine(HeaderName::CONTENT_TYPE) === 'application/json') {
$parsedBody = $this->serializer->deserialize($request->getBody()->__toString(), 'array', 'json');
return $request->withParsedBody($parsedBody);
} else {
return $request->withParsedBody([]);
}
}
示例10: __invoke
public function __invoke(Request $request, Response $response, callable $out = null)
{
if (!$request->hasHeader('authorization')) {
return $response->withStatus(401);
}
if (!$this->isValid($request)) {
}
return $out($request, $response);
}
示例11: __invoke
public function __invoke(Request $request, Response $response, callable $next) : Response
{
$parent = $request->getOriginalRequest();
$parentUrl = str_replace('/thank-you', '', (string) $parent->getUri());
if (!$request->hasHeader('Referer') || !preg_match('#^' . $parentUrl . '#', $request->getHeaderLine('Referer'))) {
return $response->withStatus(302)->withHeader('Location', $parentUrl);
}
return new HtmlResponse($this->template->render('contact::thankyou', []));
}
示例12: getHeadersFromRequest
/**
* @param ServerRequestInterface $request
*
* @return array
*/
private function getHeadersFromRequest(ServerRequestInterface $request)
{
$headerNames = [];
foreach ($this->service->getOverrideHeaders() as $headerName) {
if ($request->hasHeader($headerName)) {
$headerNames[$headerName] = $request->getHeaderLine($headerName);
}
}
return $headerNames;
}
示例13: fetchToken
/**
* Fetch token from request.
*
* @param ServerRequestInterface $request
* @return string
*/
protected function fetchToken(ServerRequestInterface $request)
{
if ($request->hasHeader(self::HEADER)) {
return (string) $request->getHeaderLine(self::HEADER);
}
$data = $request->getParsedBody();
if (is_array($data) && isset($data[self::PARAMETER])) {
if (is_string($data[self::PARAMETER])) {
return (string) $data[self::PARAMETER];
}
}
return '';
}
示例14: checkOrigin
/**
* Check if the provided origin is allowed to
* access the api
*
* @return bool
*/
protected function checkOrigin()
{
// Check if we allow all "*"
if ($this->options['allowedOrigins'] === true) {
return true;
}
// Make sure the origin header is set and that the value (domain) is
// in the allowed origins list.
if ($this->request->hasHeader('origin') && in_array($this->request->getHeaderLine('origin'), $this->options['allowedOrigins'])) {
return true;
}
return false;
}
示例15: isAcceptableServerRequest
private function isAcceptableServerRequest(ServerRequestInterface $request) : bool
{
if (!$request->hasHeader("Accept-encoding")) {
return false;
}
$accept = $request->getHeaderLine("Accept-encoding");
if (strpos($accept, '*') !== false) {
return true;
}
if (strpos($accept, 'gzip') !== false) {
return true;
}
return false;
}