-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
247 lines (213 loc) · 5.15 KB
/
Request.php
File metadata and controls
247 lines (213 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
namespace HackPHP\Http\Request;
use HackPHP\Http\Message;
use HackPHP\Http\URI\UriFactory;
use Psr\Http\Message\UriInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\RequestInterface;
class Request extends Message implements RequestInterface
{
/**
* Allowed HTTP Methods.
*
* @var array
*/
protected array $allowedMethods = [
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"CONNECT",
"OPTIONS",
"TRACE",
"PATCH"
];
/**
* @var string
*/
protected string $requestTarget = '';
/**
* @var string
*/
protected string $method = 'GET';
/**
* @var UriInterface
*/
protected ?UriInterface $uri;
/**
* Create new Request.
*
* @param string $method
* @param UriInterface|string|null $uri
* @param array $headers
* @param StreamInterface|resource|string|null $body
*/
public function __construct(
string $method = 'GET',
$uri = null,
array $headers = [],
$body = null
) {
$this->method = $this->filterMethod($method);
$this->setUri($uri);
$this->setAddedHeaders($headers);
$this->setBody($body);
if ($this->protocolVersion === '1.1') {
$this->headers['host'] = [$this->getHostHeader()];
}
}
/**
* @inheritDoc
*/
public function getRequestTarget()
{
if ($this->requestTarget !== null && $this->requestTarget !== '') {
return $this->requestTarget;
}
if ($this->uri === null) {
return '/';
}
$requestTarget = $this->uri->getPath();
$query = $this->uri->getQuery();
if ($query !== '') {
$requestTarget .= '?' . $query;
}
if ($requestTarget !== '') {
return $requestTarget;
}
}
/**
* @inheritDoc
*/
public function withRequestTarget($requestTarget)
{
$clone = clone $this;
$clone->requestTarget = $requestTarget;
return $clone;
}
/**
* @inheritDoc
*/
public function getMethod()
{
return $this->method;
}
/**
* @inheritDoc
*/
public function withMethod($method)
{
$clone = clone $this;
$clone->method = $this->filterMethod($method);
return $clone;
}
/**
* @inheritDoc
*/
public function getUri(): UriInterface
{
if ($this->uri === null) {
$this->uri = (new UriFactory)->createUri();
}
return $this->uri;
}
/**
* @inheritDoc
*/
public function withUri(UriInterface $uri, $preserveHost = false)
{
$clone = clone $this;
$this->uri = $uri;
if ($preserveHost && $this->hasHeader('Host')) {
return $clone;
}
return $clone->withHeader('Host', $clone->getHostHeader());
}
/**
* Filter HTTP request method.
*
* @param string $method
* @return string
*
* @todo Replace regix with in_array
*/
protected function filterMethod(string $method): string
{
if (!in_array($method, $this->allowedMethods)) {
throw new \InvalidArgumentException(sprintf(
'HTTP Method %s is invalid',
$method
));
}
return $method;
}
/**
* Return the "Host" header value
*
* @return string
*/
protected function getHostHeader(): string
{
$host = $this->uri->getHost();
if ($host === '') {
return '';
}
$port = $this->uri->getPort();
if ($port !== null) {
$host .= ':' . $port;
}
return $host;
}
/**
* Return the string representation of the request.
*
* @return string
*/
public function __toString(): string
{
$request = sprintf(
"%s %s HTTP/%s\r\n",
$this->getMethod(),
$this->getRequestTarget(),
$this->getProtocolVersion()
);
foreach (array_keys($this->headers) as $header) {
if (strtolower($header) === 'cookie') {
$cookie = implode('; ', $this->getHeader('Cookie'));
$request .= sprintf(
"%s: %s\r\n",
$header,
$cookie
);
} else {
$request .= sprintf(
"%s: %s\r\n",
$header,
$this->getHeaderLine($header)
);
}
}
return sprintf(
"%s\r\n%s",
$request,
$this->getBody()
);
}
/**
* Set the request uri.
*
* @param UriInterface|string|null $uri
* @return void
*/
protected function setUri($uri)
{
if ($uri instanceof UriInterface) {
$this->uri = $uri;
} elseif (is_string($uri)) {
$this->uri = (new UriFactory)->createUri($uri);
} else {
$this->uri = (new UriFactory)->createUri();
}
}
}