-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Response.php
234 lines (201 loc) · 6.18 KB
/
Response.php
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
<?php declare(strict_types=1);
namespace Amp\Http\Client;
use Amp\ByteStream\Payload;
use Amp\ByteStream\ReadableStream;
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Future;
use Amp\Http\HttpMessage;
use Amp\Http\HttpResponse;
/**
* An HTTP response.
*
* @psalm-import-type HeaderParamValueType from HttpMessage
* @psalm-import-type HeaderParamArrayType from HttpMessage
* @psalm-type ProtocolVersion = '1.0'|'1.1'|'2'
*/
final class Response extends HttpResponse
{
use ForbidSerialization;
use ForbidCloning;
/** @var ProtocolVersion */
private string $protocolVersion;
private Request $request;
private Payload $body;
/** @var Future<Trailers> */
private Future $trailers;
private ?Response $previousResponse;
/**
* @param ProtocolVersion $protocolVersion
* @param HeaderParamArrayType $headers
* @param Future<Trailers>|null $trailers
*
* @throws \Amp\Http\InvalidHeaderException
*/
public function __construct(
string $protocolVersion,
int $status,
?string $reason,
array $headers,
ReadableStream|string|null $body,
Request $request,
?Future $trailers = null,
?Response $previousResponse = null
) {
parent::__construct($status, $reason);
$this->setProtocolVersion($protocolVersion);
$this->setHeaders($headers);
$this->setBody($body);
$this->request = $request;
/** @noinspection PhpUnhandledExceptionInspection */
$this->trailers = $trailers ?? Future::complete(new Trailers([]));
$this->previousResponse = $previousResponse;
}
/**
* Retrieve the HTTP protocol version used for the request.
*
* @return ProtocolVersion
*/
public function getProtocolVersion(): string
{
return $this->protocolVersion;
}
/**
* @param ProtocolVersion $protocolVersion
*/
public function setProtocolVersion(string $protocolVersion): void
{
if (!\in_array($protocolVersion, ["1.0", "1.1", "2"], true)) {
throw new \Error(
"Invalid HTTP protocol version: " . $protocolVersion
);
}
$this->protocolVersion = $protocolVersion;
}
public function setStatus(int $status, ?string $reason = null): void
{
parent::setStatus($status, $reason);
}
/**
* Retrieve the Request instance that resulted in this Response instance.
*/
public function getRequest(): Request
{
return $this->request;
}
public function setRequest(Request $request): void
{
$this->request = $request;
}
/**
* Retrieve the original Request instance associated with this Response instance.
*
* A given Response may be the result of one or more redirects. This method is a shortcut to
* access information from the original Request that led to this response.
*/
public function getOriginalRequest(): Request
{
if (empty($this->previousResponse)) {
return $this->request;
}
return $this->previousResponse->getOriginalRequest();
}
/**
* Retrieve the original Response instance associated with this Response instance.
*
* A given Response may be the result of one or more redirects. This method is a shortcut to
* access information from the original Response that led to this response.
*/
public function getOriginalResponse(): Response
{
if (empty($this->previousResponse)) {
return $this;
}
return $this->previousResponse->getOriginalResponse();
}
/**
* If this Response is the result of a redirect traverse up the redirect history.
*/
public function getPreviousResponse(): ?Response
{
return $this->previousResponse;
}
public function setPreviousResponse(?Response $previousResponse): void
{
$this->previousResponse = $previousResponse;
}
/**
* Assign a value for the specified header field by replacing any existing values for that field.
*
* @param non-empty-string $name Header name.
* @param HeaderParamValueType $value Header value.
*/
public function setHeader(string $name, array|string $value): void
{
if (($name[0] ?? ":") === ":") {
throw new \Error("Header name cannot be empty or start with a colon (:)");
}
parent::setHeader($name, $value);
}
/**
* Assign a value for the specified header field by adding an additional header line.
*
* @param non-empty-string $name Header name.
* @param HeaderParamValueType $value Header value.
*/
public function addHeader(string $name, array|string $value): void
{
if (($name[0] ?? ":") === ":") {
throw new \Error("Header name cannot be empty or start with a colon (:)");
}
parent::addHeader($name, $value);
}
public function setHeaders(array $headers): void
{
/** @noinspection PhpUnhandledExceptionInspection */
parent::setHeaders($headers);
}
public function replaceHeaders(array $headers): void
{
/** @noinspection PhpUnhandledExceptionInspection */
parent::replaceHeaders($headers);
}
/**
* Remove the specified header field from the message.
*
* @param string $name Header name.
*/
public function removeHeader(string $name): void
{
parent::removeHeader($name);
}
/**
* Retrieve the response body.
*/
public function getBody(): Payload
{
return $this->body;
}
public function setBody(ReadableStream|string|null $body): void
{
$this->body = match (true) {
$body instanceof Payload => $body,
$body instanceof ReadableStream, \is_string($body) => new Payload($body),
$body === null => new Payload(''),
};
}
/**
* @return Future<Trailers>
*/
public function getTrailers(): Future
{
return $this->trailers;
}
/**
* @param Future<Trailers> $future
*/
public function setTrailers(Future $future): void
{
$this->trailers = $future;
}
}