Debian Codegraph @

lib/3rdParty/composer/guzzlehttp/psr7/src/Request.php

raw symbols SCIP
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 if (!$uri instanceof UriInterface) {
44 $uri = new Uri($uri);
45 }
46
47 self::warnOnMethodCasingChange($method);
48 $this->method = strtoupper($method);
49 $this->uri = $uri;
50 $this->setHeaders($headers);
51 $this->protocol = $version;
52
53 if (!isset($this->headerNames['host'])) {
54 $this->updateHostFromUri();
55 }
56
57 if ($body !== '' && $body !== null) {
58 $this->stream = Utils::streamFor($body);
59 }
60 }
61
62 public function getRequestTarget(): string
63 {
64 if ($this->requestTarget !== null) {
65 return $this->requestTarget;
66 }
67
68 $target = $this->uri->getPath();
69 if ($target === '') {
70 $target = '/';
71 }
72 if ($this->uri->getQuery() != '') {
73 $target .= '?'.$this->uri->getQuery();
74 }
75
76 return $target;
77 }
78
79 public function withRequestTarget($requestTarget): RequestInterface
80 {
81 if (preg_match('#\s#', $requestTarget)) {
82 throw new InvalidArgumentException(
83 'Invalid request target provided; cannot contain whitespace'
84 );
85 }
86
87 $new = clone $this;
88 $new->requestTarget = $requestTarget;
89
90 return $new;
91 }
92
93 public function getMethod(): string
94 {
95 return $this->method;
96 }
97
98 public function withMethod($method): RequestInterface
99 {
100 $this->assertMethod($method);
101 self::warnOnMethodCasingChange($method);
102 $new = clone $this;
103 $new->method = strtoupper($method);
104
105 return $new;
106 }
107
108 public function getUri(): UriInterface
109 {
110 return $this->uri;
111 }
112
113 public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
114 {
115 if (!\is_bool($preserveHost)) {
116 \trigger_deprecation(
117 'guzzlehttp/psr7',
118 '2.11',
119 'Passing %s to RequestInterface::withUri() is deprecated; guzzlehttp/psr7 3.0 requires bool for $preserveHost.',
120 \get_debug_type($preserveHost)
121 );
122 }
123
124 if ($uri === $this->uri) {
125 return $this;
126 }
127
128 $new = clone $this;
129 $new->uri = $uri;
130
131 if (!$preserveHost || !isset($this->headerNames['host'])) {
132 $new->updateHostFromUri();
133 }
134
135 return $new;
136 }
137
138 private function updateHostFromUri(): void
139 {
140 $host = $this->uri->getHost();
141
142 if ($host == '') {
143 return;
144 }
145
146 Uri::assertValidHost($host);
147
148 if (($port = $this->uri->getPort()) !== null) {
149 $host .= ':'.$port;
150 }
151
152 $this->assertValue($host);
153
154 if (isset($this->headerNames['host'])) {
155 $header = $this->headerNames['host'];
156 } else {
157 $header = 'Host';
158 $this->headerNames['host'] = 'Host';
159 }
160 // Ensure Host is the first header.
161 // See: https://datatracker.ietf.org/doc/html/rfc7230#section-5.4
162 $this->headers = [$header => [$host]] + $this->headers;
163 }
164
165 /**
166 * @param mixed $method
167 */
168 private function assertMethod($method): void
169 {
170 if (!is_string($method) || $method === '') {
171 throw new InvalidArgumentException('Method must be a non-empty string.');
172 }
173 }
174
175 private static function warnOnMethodCasingChange(string $method): void
176 {
177 if ($method !== strtoupper($method)) {
178 \trigger_deprecation(
179 'guzzlehttp/psr7',
180 '2.11',
181 '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.'
182 );
183 }
184 }
185 }