|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1);
|
|
4
|
|
|
|
|
5
|
|
|
namespace Aidphp\Http;
|
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
8
|
|
|
use Psr\Http\Message\UriInterface;
|
|
9
|
|
|
use Psr\Http\Message\StreamInterface;
|
|
10
|
|
|
use InvalidArgumentException;
|
|
11
|
|
|
|
|
12
|
|
|
class Request implements RequestInterface
|
|
13
|
|
|
{
|
|
14
|
|
|
use MessageTrait;
|
|
15
|
|
|
|
|
16
|
|
|
protected $method;
|
|
17
|
|
|
protected $requestTarget;
|
|
18
|
|
|
protected $uri;
|
|
19
|
|
|
|
|
20
|
55 |
|
public function __construct(string $method, $uri, array $headers = [], StreamInterface $body = null, string $version = '1.1')
|
|
21
|
|
|
{
|
|
22
|
55 |
|
$this->method = $this->filterMethod($method);
|
|
23
|
54 |
|
$this->uri = $uri instanceof UriInterface ? $uri : new Uri($uri);
|
|
24
|
54 |
|
$this->setHeaders($headers);
|
|
25
|
54 |
|
$this->stream = null !== $body ? $body : new Stream(fopen('php://temp', 'wb+'));
|
|
26
|
54 |
|
$this->protocol = $this->filterProtocolVersion($version);
|
|
27
|
|
|
|
|
28
|
54 |
|
if (! $this->hasHeader('Host'))
|
|
29
|
|
|
{
|
|
30
|
53 |
|
$this->updateHostFromUri();
|
|
31
|
|
|
}
|
|
32
|
54 |
|
}
|
|
33
|
|
|
|
|
34
|
2 |
|
public function getRequestTarget(): string
|
|
35
|
|
|
{
|
|
36
|
2 |
|
if (null !== $this->requestTarget)
|
|
37
|
|
|
{
|
|
38
|
1 |
|
return $this->requestTarget;
|
|
39
|
|
|
}
|
|
40
|
|
|
|
|
41
|
2 |
|
$target = $this->uri->getPath();
|
|
42
|
2 |
|
if (! $target)
|
|
43
|
|
|
{
|
|
44
|
1 |
|
$target = '/';
|
|
45
|
|
|
}
|
|
46
|
|
|
|
|
47
|
2 |
|
$query = $this->uri->getQuery();
|
|
48
|
2 |
|
if ($query)
|
|
49
|
|
|
{
|
|
50
|
1 |
|
$target .= '?' . $query;
|
|
51
|
|
|
}
|
|
52
|
|
|
|
|
53
|
2 |
|
return $target;
|
|
54
|
|
|
}
|
|
55
|
|
|
|
|
56
|
2 |
|
public function withRequestTarget($requestTarget): self
|
|
57
|
|
|
{
|
|
58
|
2 |
|
if (preg_match('#\s#', $requestTarget))
|
|
59
|
|
|
{
|
|
60
|
1 |
|
throw new InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
|
|
61
|
|
|
}
|
|
62
|
|
|
|
|
63
|
1 |
|
$new = clone $this;
|
|
64
|
1 |
|
$new->requestTarget = $requestTarget;
|
|
65
|
1 |
|
return $new;
|
|
66
|
|
|
}
|
|
67
|
|
|
|
|
68
|
17 |
|
public function getMethod(): string
|
|
69
|
|
|
{
|
|
70
|
17 |
|
return $this->method;
|
|
71
|
|
|
}
|
|
72
|
|
|
|
|
73
|
2 |
|
public function withMethod($method): self
|
|
74
|
|
|
{
|
|
75
|
2 |
|
$method = $this->filterMethod($method);
|
|
76
|
2 |
|
if ($this->method === $method)
|
|
77
|
|
|
{
|
|
78
|
1 |
|
return $this;
|
|
79
|
|
|
}
|
|
80
|
|
|
|
|
81
|
1 |
|
$new = clone $this;
|
|
82
|
1 |
|
$new->method = $method;
|
|
83
|
1 |
|
return $new;
|
|
84
|
|
|
}
|
|
85
|
|
|
|
|
86
|
27 |
|
public function getUri(): UriInterface
|
|
87
|
|
|
{
|
|
88
|
27 |
|
return $this->uri;
|
|
89
|
|
|
}
|
|
90
|
|
|
|
|
91
|
5 |
|
public function withUri(UriInterface $uri, $preserveHost = false): self
|
|
92
|
|
|
{
|
|
93
|
5 |
|
if ($this->uri === $uri)
|
|
94
|
|
|
{
|
|
95
|
1 |
|
return $this;
|
|
96
|
|
|
}
|
|
97
|
|
|
|
|
98
|
4 |
|
$new = clone $this;
|
|
99
|
4 |
|
$new->uri = $uri;
|
|
100
|
4 |
|
if (! $preserveHost || ! $this->hasHeader('Host'))
|
|
101
|
|
|
{
|
|
102
|
3 |
|
$new->updateHostFromUri();
|
|
103
|
|
|
}
|
|
104
|
4 |
|
return $new;
|
|
105
|
|
|
}
|
|
106
|
|
|
|
|
107
|
55 |
|
private function filterMethod(string $method): string
|
|
108
|
|
|
{
|
|
109
|
55 |
|
if (! preg_match('/^[!#$%&\'*+.^_`\|~0-9a-z-]+$/i', $method))
|
|
110
|
|
|
{
|
|
111
|
1 |
|
throw new InvalidArgumentException('Invalid HTTP method "' . $method . '" provided');
|
|
112
|
|
|
}
|
|
113
|
|
|
|
|
114
|
54 |
|
return $method;
|
|
115
|
|
|
}
|
|
116
|
|
|
|
|
117
|
53 |
|
private function updateHostFromUri(): void
|
|
118
|
|
|
{
|
|
119
|
53 |
|
$host = $this->uri->getHost();
|
|
120
|
53 |
|
if (! $host)
|
|
121
|
|
|
{
|
|
122
|
21 |
|
return;
|
|
123
|
|
|
}
|
|
124
|
|
|
|
|
125
|
32 |
|
$port = $this->uri->getPort();
|
|
126
|
32 |
|
if (null !== $port)
|
|
127
|
|
|
{
|
|
128
|
4 |
|
$host .= ':' . $port;
|
|
129
|
|
|
}
|
|
130
|
|
|
|
|
131
|
32 |
|
if (isset($this->headerNames['host']))
|
|
132
|
|
|
{
|
|
133
|
2 |
|
$header = $this->headerNames['host'];
|
|
134
|
|
|
}
|
|
135
|
|
|
else
|
|
136
|
|
|
{
|
|
137
|
32 |
|
$header = 'Host';
|
|
138
|
32 |
|
$this->headerNames['host'] = 'Host';
|
|
139
|
|
|
}
|
|
140
|
|
|
|
|
141
|
32 |
|
$this->headers = [$header => [$host]] + $this->headers;
|
|
142
|
|
|
}
|
|
143
|
|
|
} |