12use InvalidArgumentException;
140 $method = $environment[
'REQUEST_METHOD'];
151 in_array(
$request->getMediaType(), [
'application/x-www-form-urlencoded',
'multipart/form-data'])
185 $this->originalMethod =
$method;
197 $this->protocolVersion = str_replace(
'HTTP/',
'',
$serverParams[
'SERVER_PROTOCOL']);
200 if (!$this->headers->has(
'Host') && $this->uri->getHost() !==
'') {
201 $port = $this->uri->getPort() ?
":{$this->uri->getPort()}" :
'';
203 $this->headers->set(
'Host', $this->uri->getHost() . $port);
215 $backup = libxml_disable_entity_loader(
true);
216 $backup_errors = libxml_use_internal_errors(
true);
218 libxml_disable_entity_loader($backup);
219 libxml_clear_errors();
220 libxml_use_internal_errors($backup_errors);
228 $backup = libxml_disable_entity_loader(
true);
229 $backup_errors = libxml_use_internal_errors(
true);
231 libxml_disable_entity_loader($backup);
232 libxml_clear_errors();
233 libxml_use_internal_errors($backup_errors);
275 if ($this->method ===
null) {
277 $customMethod = $this->
getHeaderLine(
'X-Http-Method-Override');
281 } elseif ($this->originalMethod ===
'POST') {
283 if ($overrideMethod !==
null) {
284 $this->method = $overrideMethod;
326 $clone = clone $this;
327 $clone->originalMethod =
$method;
347 throw new InvalidArgumentException(sprintf(
348 'Unsupported HTTP method; must be a string, received %s',
354 if (preg_match(
"/^[!#$%&'*+.^_`|~0-9a-z-]+$/i",
$method) !== 1) {
467 return $this->
getHeaderLine(
'X-Requested-With') ===
'XMLHttpRequest';
492 if ($this->requestTarget) {
496 if ($this->uri ===
null) {
500 $basePath = $this->uri->getBasePath();
501 $path = $this->uri->getPath();
504 $query = $this->uri->getQuery();
508 $this->requestTarget =
$path;
534 throw new InvalidArgumentException(
535 'Invalid request target provided; must be a string and cannot contain whitespace'
538 $clone = clone $this;
590 $clone = clone $this;
593 if (!$preserveHost) {
594 if (
$uri->getHost() !==
'') {
595 $clone->headers->set(
'Host',
$uri->getHost());
598 if (
$uri->getHost() !==
'' && (!$this->hasHeader(
'Host') || $this->getHeaderLine(
'Host') ===
'')) {
599 $clone->headers->set(
'Host',
$uri->getHost());
631 $contentTypeParts = preg_split(
'/\s*[;,]\s*/',
$contentType);
633 return strtolower($contentTypeParts[0]);
649 $contentTypeParams = [];
651 $contentTypeParts = preg_split(
'/\s*[;,]\s*/',
$contentType);
652 $contentTypePartsLength = count($contentTypeParts);
653 for (
$i = 1;
$i < $contentTypePartsLength;
$i++) {
654 $paramParts = explode(
'=', $contentTypeParts[
$i]);
655 $contentTypeParams[strtolower($paramParts[0])] = $paramParts[1];
659 return $contentTypeParams;
672 if (isset($mediaTypeParams[
'charset'])) {
673 return $mediaTypeParams[
'charset'];
688 $result = $this->headers->get(
'Content-Length');
752 $clone = clone $this;
776 if (is_array($this->queryParams)) {
780 if ($this->uri ===
null) {
784 parse_str($this->uri->getQuery(), $this->queryParams);
813 $clone = clone $this;
814 $clone->queryParams =
$query;
853 $clone = clone $this;
910 return $this->attributes->all();
950 $clone = clone $this;
951 $clone->attributes->set(
$name, $value);
973 $clone = clone $this;
995 $clone = clone $this;
996 $clone->attributes->remove(
$name);
1023 if ($this->bodyParsed !==
false) {
1034 $parts = explode(
'+', $mediaType);
1035 if (count($parts) >= 2) {
1036 $mediaType =
'application/' . $parts[count($parts)-1];
1039 if (isset($this->bodyParsers[$mediaType]) ===
true) {
1041 $parsed = $this->bodyParsers[$mediaType](
$body);
1043 if (!is_null($parsed) && !is_object($parsed) && !is_array($parsed)) {
1044 throw new RuntimeException(
1045 'Request body media type parser return value must be an array, an object, or null'
1048 $this->bodyParsed = $parsed;
1086 throw new InvalidArgumentException(
'Parsed body value must be an array, an object, or null');
1089 $clone = clone $this;
1090 $clone->bodyParsed =
$data;
1104 $this->bodyParsed =
false;
1121 if ($callable instanceof Closure) {
1122 $callable = $callable->bindTo($this);
1124 $this->bodyParsers[(string)$mediaType] = $callable;
1146 if (is_array($postParams) && isset($postParams[
$key])) {
1148 } elseif (is_object($postParams) && property_exists($postParams,
$key)) {
1150 } elseif (isset($getParams[
$key])) {
1171 if (is_array($postParams) && isset($postParams[
$key])) {
1173 } elseif (is_object($postParams) && property_exists($postParams,
$key)) {
1194 if (isset($getParams[
$key])) {
1219 foreach ($only as
$key) {
foreach($paths as $path) $request
An exception for terminatinating execution or to throw for unit testing.
all()
Get all items in collection.
static parseHeader($header)
Parse HTTP request Cookie: header and extract into a PHP associative array.
Abstract message (base class for Request and Response)
getHeaderLine($name)
Retrieves a comma-separated string of the values for a single header.
getHeader($name)
Retrieves a message header value by the given case-insensitive name.
getBody()
Gets the body of the message.
Provides a PSR-7 implementation of a reusable raw request body.
registerMediaTypeParser($mediaType, callable $callable)
Register media type parser.
reparseBody()
Force Body to be parsed again.
isOptions()
Is this a OPTIONS request?
isGet()
Is this a GET request?
withUri(UriInterface $uri, $preserveHost=false)
Returns an instance with the provided URI.
getCookieParams()
Retrieve cookies.
withoutAttribute($name)
Return an instance that removes the specified derived request attribute.
withAttributes(array $attributes)
Create a new instance with the specified derived request attributes.
getMediaType()
Get request media type, if known.
getParam($key, $default=null)
Fetch request parameter value from body or query string (in that order).
getParsedBody()
Retrieve any parameters provided in the request body.
isMethod($method)
Does this request use a given method?
getParams(array $only=null)
Fetch associative array of body and query string parameters.
withQueryParams(array $query)
Return an instance with the specified query string arguments.
getRequestTarget()
Retrieves the message's request target.
isPost()
Is this a POST request?
getServerParams()
Retrieve server parameters.
getUploadedFiles()
Retrieve normalized file upload data.
getUri()
Retrieves the URI instance.
isPut()
Is this a PUT request?
isXhr()
Is this an XHR request?
withParsedBody($data)
Return an instance with the specified body parameters.
getMethod()
Retrieves the HTTP method of the request.
isPatch()
Is this a PATCH request?
getContentType()
Get request content type.
getCookieParam($key, $default=null)
Fetch cookie value from cookies sent by the client to the server.
withAttribute($name, $value)
Return an instance with the specified derived request attribute.
isDelete()
Is this a DELETE request?
__construct( $method, UriInterface $uri, HeadersInterface $headers, array $cookies, array $serverParams, StreamInterface $body, array $uploadedFiles=[])
Create new HTTP request.
getQueryParam($key, $default=null)
Fetch parameter value from query string.
isHead()
Is this a HEAD request?
withUploadedFiles(array $uploadedFiles)
Create a new instance with the specified uploaded files.
getParsedBodyParam($key, $default=null)
Fetch parameter value from request body.
getServerParam($key, $default=null)
Retrieve a server parameter.
getContentLength()
Get request content length, if known.
getAttributes()
Retrieve attributes derived from the request.
withCookieParams(array $cookies)
Return an instance with the specified cookies.
static createFromEnvironment(Environment $environment)
Create new HTTP request with data extracted from the application Environment object.
getQueryParams()
Retrieve query string arguments.
withRequestTarget($requestTarget)
Return an instance with the specific request-target.
withMethod($method)
Return an instance with the provided HTTP method.
filterMethod($method)
Validate the HTTP method.
getOriginalMethod()
Get the original HTTP method (ignore override).
getContentCharset()
Get request content character set, if known.
getAttribute($name, $default=null)
Retrieve a single derived request attribute.
getMediaTypeParams()
Get request media type params, if known.
__clone()
This method is applied to the cloned object after PHP performs an initial shallow-copy.
static createFromEnvironment(Environment $env)
Create a normalized tree of UploadedFile instances from the Environment.
static createFromEnvironment(Environment $env)
Create new Uri from environment.
Representation of an incoming, server-side HTTP request.
Value object representing a file uploaded through an HTTP request.
Value object representing a URI.
if( $path[strlen( $path) - 1]==='/') if(is_dir($path)) if(!file_exists( $path)) if(preg_match('#\.php$#D', mb_strtolower($path, 'UTF-8'))) $contentType
Slim Framework (https://slimframework.com)