Debian Codegraph @

vendor/guzzlehttp/psr7/src/Request.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace GuzzleHttp\Psr7;
6
7 use InvalidArgumentException;
8 use Psr\Http\Message\RequestInterface;
9 use Psr\Http\Message\StreamInterface;
10 use Psr\Http\Message\UriInterface;
11
12 /**
13 * PSR-7 request implementation.
14 */
15 class Request implements RequestInterface
16 {
17 use MessageTrait;
18
19 /** @var string */
20 private $method;
21
22 /** @var string|null */
23 private $requestTarget;
24
25 /** @var UriInterface */
26 private $uri;
27
28 /**
29 * @param string $method HTTP method
30 * @param string|UriInterface $uri URI
31 * @param (string|string[])[] $headers Request headers
32 * @param string|resource|StreamInterface|null $body Request body
33 * @param string $version Protocol version
34 */
35 public function __construct(
36 string $method,
37 $uri,
38 array $headers = [],
39 $body = null,
40 string $version = '1.1'
41 ) {
42 $this->assertMethod($method);
43 $this->assertProtocolVersion($version);
44
45 if (!$uri instanceof UriInterface) {
46 $uri = new Uri($uri);
47 }
48
49 self::warnOnMethodCasingChange($method);
50 $this->method = strtoupper($method);
51 $this->uri = $uri;
52 $this->setHeaders($headers);
53 $this->protocol = $version;
54
55 if (!isset($this->headerNames['host'])) {
56 $this->updateHostFromUri();
57 }
58
59 if ($body !== '' && $body !== null) {
60 $this->stream = Utils::streamFor($body);
61 }
62 }
63
64 public function getRequestTarget(): string
65 {
66 if ($this->requestTarget !== null) {
67 return $this->requestTarget;
68 }
69
70 $target = $this->uri->getPath();
71 if ($target === '') {
72 $target = '/';
73 }
74 if ($this->uri->getQuery() != '') {
75 $target .= '?'.$this->uri->getQuery();
76 }
77
78 return $target;
79 }
80
81 public function withRequestTarget($requestTarget): RequestInterface
82 {
83 $hasWhitespace = preg_match('#\s#', $requestTarget);
84
85 if ($hasWhitespace === false) {
86 throw new \RuntimeException('Unable to validate request target: '.preg_last_error_msg());
87 }
88
89 if ($hasWhitespace === 1) {
90 throw new InvalidArgumentException(
91 'Invalid request target provided; cannot contain whitespace'
92 );
93 }
94
95 $new = clone $this;
96 $new->requestTarget = $requestTarget;
97
98 return $new;
99 }
100
101 public function getMethod(): string
102 {
103 return $this->method;
104 }
105
106 public function withMethod($method): RequestInterface
107 {
108 $this->assertMethod($method);
109 self::warnOnMethodCasingChange($method);
110 $new = clone $this;
111 $new->method = strtoupper($method);
112
113 return $new;
114 }
115
116 public function getUri(): UriInterface
117 {
118 return $this->uri;
119 }
120
121 public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
122 {
123 if (!\is_bool($preserveHost)) {
124 \trigger_deprecation(
125 'guzzlehttp/psr7',
126 '2.11',
127 'Passing %s to RequestInterface::withUri() is deprecated; guzzlehttp/psr7 3.0 requires bool for $preserveHost.',
128 \get_debug_type($preserveHost)
129 );
130 }
131
132 if ($uri === $this->uri) {
133 return $this;
134 }
135
136 $new = clone $this;
137 $new->uri = $uri;
138
139 if (!$preserveHost || !isset($this->headerNames['host'])) {
140 $new->updateHostFromUri();
141 }
142
143 return $new;
144 }
145
146 private function updateHostFromUri(): void
147 {
148 $host = $this->uri->getHost();
149
150 if ($host == '') {
151 return;
152 }
153
154 Uri::assertValidHost($host);
155
156 if (($port = $this->uri->getPort()) !== null) {
157 $host .= ':'.$port;
158 }
159
160 $this->assertValue($host);
161
162 if (isset($this->headerNames['host'])) {
163 $header = $this->headerNames['host'];
164 } else {
165 $header = 'Host';
166 $this->headerNames['host'] = 'Host';
167 }
168 // Ensure Host is the first header.
169 // See: https://datatracker.ietf.org/doc/html/rfc7230#section-5.4
170 $this->headers = [$header => [$host]] + $this->headers;
171 }
172
173 /**
174 * @param mixed $method
175 */
176 private function assertMethod($method): void
177 {
178 if (!is_string($method) || $method === '') {
179 throw new InvalidArgumentException('Method must be a non-empty string.');
180 }
181
182 $this->assertNoLineSeparators($method, 'Method');
183 }
184
185 private static function warnOnMethodCasingChange(string $method): void
186 {
187 if ($method !== strtoupper($method)) {
188 \trigger_deprecation(
189 'guzzlehttp/psr7',
190 '2.11',
191 'Passing a non-uppercase HTTP method is deprecated; guzzlehttp/psr7 3.0 preserves method casing and will no longer uppercase it. Normalize the method before constructing or modifying requests if uppercase is required.'
192 );
193 }
194 }
195 }