Returns a header value by name.
public get ( string $key, mixed $default = null, boolean $first = true ) : string | array | ||
$key | string | The header name |
$default | mixed | The default value |
$first | boolean | Whether to return the first value or all header values |
return | string | array | The first header value if $first is true, an array of values otherwise |
public function it_will_create_event_from_request(Request $request, HeaderBag $headerBag) { $request->headers = $headerBag; $headerBag->get('X-GitHub-Event')->willReturn('push'); $headerBag->get('X-Hub-Signature')->willReturn('sha1=abc123'); $this->create($request)->shouldReturnAnInstanceOf('DevBoard\\Notify\\Event'); }
public function it_will_create_pull_request_event($signatureFactory, WebHookSignature $signature, Request $request, HeaderBag $headerBag) { $request->headers = $headerBag; $headerBag->get('X-GitHub-Event')->willReturn('pull_request'); $headerBag->get('X-Hub-Signature')->willReturn('sha1=abc123'); $signatureFactory->create('sha1=abc123')->willReturn($signature); $request->getContent()->willReturn('{}'); $this->create($request)->shouldReturnAnInstanceOf('DevBoard\\Github\\WebHook\\Data\\PullRequestEvent'); }
protected function prepareRequestUri() { $requestUri = ''; if ($this->headers->has('X_REWRITE_URL') && false !== stripos(PHP_OS, 'WIN')) { // check this first so IIS will catch $requestUri = $this->headers->get('X_REWRITE_URL'); } elseif ($this->server->get('IIS_WasUrlRewritten') == '1' && $this->server->get('UNENCODED_URL') != '') { // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem) $requestUri = $this->server->get('UNENCODED_URL'); } elseif ($this->server->has('REQUEST_URI')) { $requestUri = $this->server->get('REQUEST_URI'); // HTTP proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path $schemeAndHttpHost = $this->getScheme() . '://' . $this->getHttpHost(); if (strpos($requestUri, $schemeAndHttpHost) === 0) { $requestUri = substr($requestUri, strlen($schemeAndHttpHost)); } } elseif ($this->server->has('ORIG_PATH_INFO')) { // IIS 5.0, PHP as CGI $requestUri = $this->server->get('ORIG_PATH_INFO'); if ($this->server->get('QUERY_STRING')) { $requestUri .= '?' . $this->server->get('QUERY_STRING'); } } return $requestUri; }
function it_gets_an_item_from_the_header(Request $request, HeaderBag $headerBag) { $item = 'foo'; $return = 'bar'; $request->headers = $headerBag; $headerBag->get($item)->shouldBeCalled(1)->willReturn($return); $this->header($item)->shouldReturn($return); }
function it_invalidates_signature(Request $request, HeaderBag $headers) { $headers->get('X-Shopify-Hmac-Sha256')->willReturn(self::HMAC_HEADER_WRONG)->shouldbeCalled(); $request->headers = $headers; $request->getContent()->willReturn(self::DATA)->shouldbeCalled(); $this->setHttpRequest($request); $this->verify()->shouldBeBool(); $this->verify()->shouldReturn(false); }
/** * @covers Symfony\Component\HttpFoundation\HeaderBag::get */ public function testGet() { $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz')); $this->assertEquals('bar', $bag->get('foo'), '->get return current value'); $this->assertEquals('bar', $bag->get('FoO'), '->get key in case insensitive'); $this->assertEquals(array('bar'), $bag->get('foo', 'nope', false), '->get return the value as array'); // defaults $this->assertNull($bag->get('none'), '->get unknown values returns null'); $this->assertEquals('default', $bag->get('none', 'default'), '->get unknown values returns default'); $this->assertEquals(array('default'), $bag->get('none', 'default', false), '->get unknown values returns default as array'); $bag->set('foo', 'bor', false); $this->assertEquals('bar', $bag->get('foo'), '->get return first value'); $this->assertEquals(array('bar', 'bor'), $bag->get('foo', 'nope', false), '->get return all values as array'); }
public function let(AngularCsrfTokenManager $tokenManager, RouteMatcherInterface $routeMatcher, Request $secureValidRequest, Request $secureInvalidRequest, Request $unsecureRequest, HeaderBag $validHeaders, HeaderBag $invalidHeaders) { $tokenManager->isTokenValid(self::VALID_TOKEN)->willReturn(true); $tokenManager->isTokenValid(self::INVALID_TOKEN)->willReturn(false); $this->secureValidRequest = $secureValidRequest; $validHeaders->get(self::HEADER_NAME)->willReturn(self::VALID_TOKEN); $this->secureValidRequest->headers = $validHeaders; $this->secureInvalidRequest = $secureInvalidRequest; $invalidHeaders->get(self::HEADER_NAME)->willReturn(self::INVALID_TOKEN); $this->secureInvalidRequest->headers = $invalidHeaders; $this->unsecureRequest = $unsecureRequest; $routeMatcher->match($this->secureValidRequest, $this->routes)->willReturn(true); $routeMatcher->match($this->secureInvalidRequest, $this->routes)->willReturn(true); $routeMatcher->match($this->unsecureRequest, $this->routes)->willReturn(false); $this->beConstructedWith($tokenManager, $routeMatcher, $this->routes, self::HEADER_NAME); }
/** * Verifies that the provided header has the expected/mandatory fields. * * @param ParameterBag|HeaderBag $header object representation of the request header. * @param string $fieldName Name of the header field to be validated. * * @return void * @throws \Symfony\Component\HttpKernel\Exception\HttpException */ protected function validateField($header, $fieldName) { $passed = $header->has($fieldName); // return without exception so we can return a dummy user if (true === $passed) { // get rid of anything not a valid character $authInfo = filter_var($header->get($fieldName), FILTER_SANITIZE_STRING); // get rid of whitespaces $patterns = array("\r\n", "\n", "\r", "\\s", "\t"); $authInfo = str_replace($patterns, "", trim($authInfo)); // get rid of control characters if (empty($authInfo) || $authInfo !== preg_replace('#[[:cntrl:]]#i', '', $authInfo)) { throw new HttpException(Response::HTTP_NETWORK_AUTHENTICATION_REQUIRED, 'Mandatory header field (' . $fieldName . ') not provided or invalid.'); } } }
protected function prepareRequestUri() { $requestUri = ''; if ($this->headers->has('X_ORIGINAL_URL')) { // IIS with Microsoft Rewrite Module $requestUri = $this->headers->get('X_ORIGINAL_URL'); $this->headers->remove('X_ORIGINAL_URL'); $this->server->remove('HTTP_X_ORIGINAL_URL'); $this->server->remove('UNENCODED_URL'); $this->server->remove('IIS_WasUrlRewritten'); } elseif ($this->headers->has('X_REWRITE_URL')) { // IIS with ISAPI_Rewrite $requestUri = $this->headers->get('X_REWRITE_URL'); $this->headers->remove('X_REWRITE_URL'); } elseif ($this->server->get('IIS_WasUrlRewritten') == '1' && $this->server->get('UNENCODED_URL') != '') { // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem) $requestUri = $this->server->get('UNENCODED_URL'); $this->server->remove('UNENCODED_URL'); $this->server->remove('IIS_WasUrlRewritten'); } elseif ($this->server->has('REQUEST_URI')) { $requestUri = $this->server->get('REQUEST_URI'); // HTTP proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path $schemeAndHttpHost = $this->getSchemeAndHttpHost(); if (strpos($requestUri, $schemeAndHttpHost) === 0) { $requestUri = substr($requestUri, strlen($schemeAndHttpHost)); } } elseif ($this->server->has('ORIG_PATH_INFO')) { // IIS 5.0, PHP as CGI $requestUri = $this->server->get('ORIG_PATH_INFO'); if ('' != $this->server->get('QUERY_STRING')) { $requestUri .= '?' . $this->server->get('QUERY_STRING'); } $this->server->remove('ORIG_PATH_INFO'); } // normalize the request URI to ease creating sub-requests from this request $this->server->set('REQUEST_URI', $requestUri); return $requestUri; }
public function testCacheControlDirectiveParsing() { $bag = new HeaderBag(array('cache-control' => 'public, max-age=10')); $this->assertTrue($bag->hasCacheControlDirective('public')); $this->assertTrue($bag->getCacheControlDirective('public')); $this->assertTrue($bag->hasCacheControlDirective('max-age')); $this->assertEquals(10, $bag->getCacheControlDirective('max-age')); $bag->addCacheControlDirective('s-maxage', 100); $this->assertEquals('max-age=10, public, s-maxage=100', $bag->get('cache-control')); }
public function isRedirected($location) { return $this->isRedirect() && $location == $this->headers->get('Location'); }
/** * Retrieve and decode the tag list from the response * headers. * * If no tags header is found then throw a \RuntimeException. * If the JSON is invalid then throw a \RuntimeException * * @param HeaderBag $headers * * @throws \RuntimeException * * @return string[] */ private function getTagsFromHeaders(HeaderBag $headers) { $tagsRaw = $headers->get($this->options['header_tags']); $tags = $this->decodeTags($tagsRaw, true); return $tags; }
/** * @param string $name * * @return array|string */ public function getHeader($name) { return $this->headers->get($name); }
/** * Determine if the request is the result of an PJAX call. * * @return bool */ public function pjax() { return $this->headers->get('X-PJAX') == true; }
protected function getProgramByOrigin(HeaderBag $headers) { $program = null; if ($headers->has('Origin')) { $host = parse_url($headers->get('Origin'), PHP_URL_HOST); /** @var \Petrosoft\LoyaltyBundle\Models\FrontSiteSettings\Collection $siteCol */ $siteCol = $this->meetz->collection('Loyalty:Models\\FrontSiteSettings'); $siteCol->addFieldToFilter('hosts', array($host), 'in'); $siteSettings = $siteCol->getFirstItem(); if (!$siteSettings->isObjectNew()) { $program = $siteSettings->getData('loyalty_program_id'); if ($program instanceof \MongoId) { $program = $program->{'$id'}; } } } return $program; }
/** * @return \DateTime */ public function getLastModifiedDate() { return \DateTime::createFromFormat(DATE_RFC2822, $this->headers->get('Last-Modified')); }
/** * @param HeaderBag $headerBag * * @return WebHookSignature */ private function getSignature(HeaderBag $headerBag) { return $this->signatureFactory->create($headerBag->get('X-Hub-Signature')); }
/** * Validate the Accept header of the Request * @link http://jsonapi.org/format/#content-negotiation-servers * * - Accept MUST be `application/vnd.api+json` * - Accept MUST NOT contain any media type parameters * * @param HeaderBag $headerBag The headers of the Request * @throws \Symfony\Component\HttpKernel\Exception\HttpException If invalid request Accept */ private function validateRequestAccept(HeaderBag $headerBag) { if ($headerBag->has('Accept')) { // Validate MediaType $mediaType = $this->convertMediaType($headerBag->get('Accept')); if ($mediaType && 'application/vnd.api+json' === $mediaType['type']) { // Valid media type found if (!empty($mediaType['parameters'])) { // Media type parameters are not allowed throw new HttpException('406', 'Accept header MUST NOT have any media type parameters.'); } } } }
/** * Returns true if the request is a XMLHttpRequest. * * It works if your JavaScript library set an X-Requested-With HTTP header. * It is known to work with common JavaScript frameworks: * * @link http://en.wikipedia.org/wiki/List_of_Ajax_frameworks#JavaScript * * @return bool true if the request is an XMLHttpRequest, false otherwise * * @api */ public function isXmlHttpRequest() { return 'XMLHttpRequest' == $this->headers->get('X-Requested-With'); }