Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientRequest.php
More file actions
Latest commit
193 lines (170 loc) · 5.68 KB
/
Copy pathClientRequest.php
File metadata and controls
193 lines (170 loc) · 5.68 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
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespaceKoded\Http;
useKoded\Http\Interfaces\{HttpStatus, Request, Response};
usePsr\Http\Message\{RequestInterface, UriInterface};
usefunctionKoded\Stdlib\json_serialize;
class ClientRequest implements RequestInterface, \JsonSerializable
{
use HeaderTrait, MessageTrait, JsonSerializeTrait;
constE_INVALID_REQUEST_TARGET = 'The request target is invalid, it contains whitespaces';
constE_SAFE_METHODS_WITH_BODY = 'failed to open stream: you should not set the message body with safe HTTP methods';
protectedUriInterface$uri;
protectedstring$method = Request::GET;
protectedstring$requestTarget = '';
/**
* ClientRequest constructor.
*
* If body is provided, the content internally is encoded in JSON
* and stored in body Stream object.
*
* @param string $method
* @param UriInterface|string $uri
* @param mixed $body [optional] \Psr\Http\Message\StreamInterface|iterable|resource|callable|string|null
* @param array $headers [optional]
*/
publicfunction__construct(
string$method,
string|UriInterface$uri,
string|iterable$body = null,
array$headers = [])
{
$this->uri = $uriinstanceof UriInterface ? $uri : newUri($uri);
$this->stream = create_stream($this->prepareBody($body));
$this->setHost();
$this->setMethod($method, $this);
$this->setHeaders($headers);
}
publicfunctiongetMethod(): string
{
return\strtoupper($this->method);
}
publicfunctionwithMethod($method): ClientRequest
{
return$this->setMethod($method, clone$this);
}
publicfunctiongetUri(): UriInterface
{
return$this->uri;
}
publicfunctionwithUri(UriInterface$uri, $preserveHost = false): static
{
$instance = clone$this;
$instance->uri = $uri;
if (true === $preserveHost) {
return$instance->withHeader('Host', $this->uri->getHost() ?: $uri->getHost());
}
return$instance->withHeader('Host', $uri->getHost());
}
publicfunctiongetRequestTarget(): string
{
if ($this->requestTarget) {
return$this->requestTarget;
}
if (!$path = $this->uri->getPath()) {
return'/';
}
if ($query = $this->uri->getQuery()) {
$path .= '?' . $query;
}
return$path;
}
publicfunctionwithRequestTarget($requestTarget): static
{
if (\preg_match('/\s+/', $requestTarget)) {
thrownew \InvalidArgumentException(
self::E_INVALID_REQUEST_TARGET,
HttpStatus::BAD_REQUEST);
}
$instance = clone$this;
$instance->requestTarget = $requestTarget;
return$instance;
}
publicfunctiongetPath(): string
{
return\str_replace($_SERVER['SCRIPT_NAME'], '', $this->uri->getPath()) ?: '/';
}
publicfunctiongetBaseUri(): string
{
if (false === empty($host = $this->getUri()->getHost())) {
$port = $this->getUri()->getPort();
$port && $port = ":{$port}";
return$this->getUri()->getScheme() . "://{$host}{$port}";
}
return'';
}
publicfunctionisSecure(): bool
{
return'https' === $this->uri->getScheme();
}
publicfunctionisSafeMethod(): bool
{
return\in_array($this->method, Request::SAFE_METHODS);
}
protectedfunctionsetHost(): void
{
$this->headersMap['host'] = 'Host';
$this->headers = ['Host' => $this->uri->getHost() ?: $_SERVER['HTTP_HOST'] ?? ''] + $this->headers;
}
/**
* @param string $method The HTTP method
* @param RequestInterface $instance
*
* @return static
*/
protectedfunctionsetMethod(string$method, RequestInterface$instance): RequestInterface
{
$instance->method = \strtoupper($method);
return$instance;
}
/**
* Checks if body is non-empty if HTTP method is one of the *safe* methods.
* The consuming code may disallow this and return the response object.
*
* @return Response|null
*/
protectedfunctionassertSafeMethod(): ?Response
{
if ($this->isSafeMethod() && $this->getBody()->getSize() > 0) {
return$this->getPhpError(HttpStatus::BAD_REQUEST, self::E_SAFE_METHODS_WITH_BODY);
}
returnnull;
}
/**
* @param mixed $body
*
* @return mixed If $body is iterable returns JSON stringified body, or whatever it is
*/
protectedfunctionprepareBody(mixed$body): mixed
{
if (\is_iterable($body)) {
returnjson_serialize($body);
}
return$body;
}
/**
* @param int $status
* @param string|null $message
*
* @return Response JSON error message
* @link https://tools.ietf.org/html/rfc7807
*/
protectedfunctiongetPhpError(int$status, ?string$message = null): Response
{
returnnewServerResponse(json_serialize([
'title' => HttpStatus::CODE[$status],
'detail' => $message ?? \error_get_last()['message'] ?? HttpStatus::CODE[$status],
'instance' => (string)$this->getUri(),
'type' => 'https://httpstatuses.com/' . $status,
'status' => $status,
]), $status, ['Content-Type' => 'application/problem+json']);
}
}