1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thruster\Component\HttpMessage; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
use Psr\Http\Message\StreamInterface; |
8
|
|
|
use Psr\Http\Message\UriInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Request |
12
|
|
|
* |
13
|
|
|
* @package Thruster\Component\HttpMessage |
14
|
|
|
* @author Aurimas Niekis <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Request implements RequestInterface |
17
|
|
|
{ |
18
|
|
|
use MessageTrait { |
19
|
|
|
withHeader as protected withParentHeader; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $method; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $requestTarget; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var UriInterface |
34
|
|
|
*/ |
35
|
|
|
private $uri; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $method HTTP method for the request. |
39
|
|
|
* @param string|UriInterface $uri URI for the request. |
40
|
|
|
* @param array $headers Headers for the message. |
41
|
|
|
* @param string|resource|StreamInterface $body Message body. |
42
|
|
|
* @param string $protocolVersion HTTP protocol version. |
43
|
|
|
* |
44
|
|
|
* @throws InvalidArgumentException for an invalid URI |
45
|
|
|
*/ |
46
|
26 |
|
public function __construct( |
47
|
|
|
string $method, |
48
|
|
|
$uri, |
49
|
|
|
array $headers = [], |
50
|
|
|
$body = null, |
51
|
|
|
string $protocolVersion = '1.1' |
52
|
|
|
) { |
53
|
26 |
|
if (is_string($uri)) { |
54
|
23 |
|
$uri = new Uri($uri); |
55
|
3 |
|
} elseif (false === ($uri instanceof UriInterface)) { |
56
|
1 |
|
throw new \InvalidArgumentException( |
57
|
1 |
|
'URI must be a string or Psr\Http\Message\UriInterface' |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
25 |
|
$this->method = strtoupper($method); |
62
|
25 |
|
$this->uri = $uri; |
63
|
25 |
|
$this->setHeaders($headers); |
64
|
25 |
|
$this->protocol = $protocolVersion; |
65
|
|
|
|
66
|
25 |
|
$host = $uri->getHost(); |
67
|
25 |
|
if ($host && false === $this->hasHeader('Host')) { |
68
|
10 |
|
$this->updateHostFromUri($host); |
69
|
|
|
} |
70
|
|
|
|
71
|
25 |
|
if ($body) { |
72
|
2 |
|
$this->stream = stream_for($body); |
73
|
|
|
} |
74
|
25 |
|
} |
75
|
|
|
|
76
|
3 |
|
public function getRequestTarget() : string |
77
|
|
|
{ |
78
|
3 |
|
if ($this->requestTarget !== null) { |
79
|
1 |
|
return $this->requestTarget; |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
$target = $this->uri->getPath(); |
83
|
3 |
|
if (null == $target) { |
84
|
1 |
|
$target = '/'; |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
if ($this->uri->getQuery()) { |
88
|
1 |
|
$target .= '?' . $this->uri->getQuery(); |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
return $target; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
public function withRequestTarget($requestTarget) : self |
95
|
|
|
{ |
96
|
2 |
|
if (preg_match('#\s#', $requestTarget)) { |
97
|
1 |
|
throw new InvalidArgumentException( |
98
|
1 |
|
'Invalid request target provided; cannot contain whitespace' |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$new = clone $this; |
103
|
1 |
|
$new->requestTarget = $requestTarget; |
104
|
|
|
|
105
|
1 |
|
return $new; |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
public function getMethod() : string |
109
|
|
|
{ |
110
|
3 |
|
return $this->method; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
public function withMethod($method) : self |
114
|
|
|
{ |
115
|
1 |
|
$new = clone $this; |
116
|
1 |
|
$new->method = strtoupper($method); |
117
|
|
|
|
118
|
1 |
|
return $new; |
119
|
|
|
} |
120
|
|
|
|
121
|
5 |
|
public function getUri() : UriInterface |
122
|
|
|
{ |
123
|
5 |
|
return $this->uri; |
124
|
|
|
} |
125
|
|
|
|
126
|
5 |
|
public function withUri(UriInterface $uri, $preserveHost = false) : self |
127
|
|
|
{ |
128
|
5 |
|
if ($uri === $this->uri) { |
129
|
1 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
4 |
|
$new = clone $this; |
133
|
4 |
|
$new->uri = $uri; |
134
|
|
|
|
135
|
4 |
|
if (false === $preserveHost) { |
136
|
3 |
|
if ($host = $uri->getHost()) { |
137
|
3 |
|
$new->updateHostFromUri($host); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
4 |
|
return $new; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function withHeader($header, $value) : self |
145
|
|
|
{ |
146
|
|
|
/** @var Request $newInstance */ |
147
|
|
|
$newInstance = $this->withParentHeader($header, $value); |
148
|
|
|
return $newInstance; |
149
|
|
|
} |
150
|
|
|
|
151
|
11 |
|
private function updateHostFromUri(string $host) |
152
|
|
|
{ |
153
|
|
|
// Ensure Host is the first header. |
154
|
|
|
// See: http://tools.ietf.org/html/rfc7230#section-5.4 |
155
|
11 |
|
if ($port = $this->uri->getPort()) { |
156
|
2 |
|
$host .= ':' . $port; |
157
|
|
|
} |
158
|
|
|
|
159
|
11 |
|
$this->headerLines = ['Host' => [$host]] + $this->headerLines; |
160
|
11 |
|
$this->headers = ['host' => [$host]] + $this->headers; |
161
|
11 |
|
} |
162
|
|
|
} |
163
|
|
|
|