ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Request.php
Go to the documentation of this file.
1<?php
2namespace GuzzleHttp\Psr7;
3
4use InvalidArgumentException;
8
12class Request implements RequestInterface
13{
14 use MessageTrait;
15
17 private $method;
18
21
23 private $uri;
24
32 public function __construct(
33 $method,
34 $uri,
35 array $headers = [],
36 $body = null,
37 $version = '1.1'
38 ) {
39 if (!($uri instanceof UriInterface)) {
40 $uri = new Uri($uri);
41 }
42
43 $this->method = strtoupper($method);
44 $this->uri = $uri;
45 $this->setHeaders($headers);
46 $this->protocol = $version;
47
48 if (!$this->hasHeader('Host')) {
49 $this->updateHostFromUri();
50 }
51
52 if ($body !== '' && $body !== null) {
53 $this->stream = stream_for($body);
54 }
55 }
56
57 public function getRequestTarget()
58 {
59 if ($this->requestTarget !== null) {
61 }
62
63 $target = $this->uri->getPath();
64 if ($target == '') {
65 $target = '/';
66 }
67 if ($this->uri->getQuery() != '') {
68 $target .= '?' . $this->uri->getQuery();
69 }
70
71 return $target;
72 }
73
75 {
76 if (preg_match('#\s#', $requestTarget)) {
77 throw new InvalidArgumentException(
78 'Invalid request target provided; cannot contain whitespace'
79 );
80 }
81
82 $new = clone $this;
83 $new->requestTarget = $requestTarget;
84 return $new;
85 }
86
87 public function getMethod()
88 {
89 return $this->method;
90 }
91
92 public function withMethod($method)
93 {
94 $new = clone $this;
95 $new->method = strtoupper($method);
96 return $new;
97 }
98
99 public function getUri()
100 {
101 return $this->uri;
102 }
103
104 public function withUri(UriInterface $uri, $preserveHost = false)
105 {
106 if ($uri === $this->uri) {
107 return $this;
108 }
109
110 $new = clone $this;
111 $new->uri = $uri;
112
113 if (!$preserveHost) {
114 $new->updateHostFromUri();
115 }
116
117 return $new;
118 }
119
120 private function updateHostFromUri()
121 {
122 $host = $this->uri->getHost();
123
124 if ($host == '') {
125 return;
126 }
127
128 if (($port = $this->uri->getPort()) !== null) {
129 $host .= ':' . $port;
130 }
131
132 if (isset($this->headerNames['host'])) {
133 $header = $this->headerNames['host'];
134 } else {
135 $header = 'Host';
136 $this->headerNames['host'] = 'Host';
137 }
138 // Ensure Host is the first header.
139 // See: http://tools.ietf.org/html/rfc7230#section-5.4
140 $this->headers = [$header => [$host]] + $this->headers;
141 }
142}
$version
Definition: build.php:27
An exception for terminatinating execution or to throw for unit testing.
PSR-7 request implementation.
Definition: Request.php:13
getRequestTarget()
Retrieves the message's request target.
Definition: Request.php:57
withUri(UriInterface $uri, $preserveHost=false)
Returns an instance with the provided URI.
Definition: Request.php:104
getMethod()
Retrieves the HTTP method of the request.
Definition: Request.php:87
withMethod($method)
Return an instance with the provided HTTP method.
Definition: Request.php:92
getUri()
Retrieves the URI instance.
Definition: Request.php:99
__construct( $method, $uri, array $headers=[], $body=null, $version='1.1')
Definition: Request.php:32
withRequestTarget($requestTarget)
Return an instance with the specific request-target.
Definition: Request.php:74
PSR-7 URI implementation.
Definition: Uri.php:14
hasHeader($name)
Checks if a header exists by the given case-insensitive name.
Representation of an outgoing, client-side request.
Describes a data stream.
Value object representing a URI.
$target
Definition: test.php:19
stream_for($resource='', array $options=[])
Create a new stream based on the input type.
Definition: functions.php:78
trait MessageTrait
Trait implementing functionality common to requests and responses.
setHeaders(array $headers)