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