@@ -4,6 +4,5 @@ |
||
4 | 4 | |
5 | 5 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Client\ClientExceptionInterface; |
6 | 6 | |
7 | -interface GuzzleException extends ClientExceptionInterface |
|
8 | -{ |
|
7 | +interface GuzzleException extends ClientExceptionInterface { |
|
9 | 8 | } |
@@ -14,153 +14,153 @@ |
||
14 | 14 | */ |
15 | 15 | class RequestException extends TransferException implements RequestExceptionInterface |
16 | 16 | { |
17 | - /** |
|
18 | - * @var RequestInterface |
|
19 | - */ |
|
20 | - private $request; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var ResponseInterface|null |
|
24 | - */ |
|
25 | - private $response; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - private $handlerContext; |
|
31 | - |
|
32 | - public function __construct( |
|
33 | - string $message, |
|
34 | - RequestInterface $request, |
|
35 | - ResponseInterface $response = null, |
|
36 | - \Throwable $previous = null, |
|
37 | - array $handlerContext = [] |
|
38 | - ) { |
|
39 | - // Set the code of the exception if the response is set and not future. |
|
40 | - $code = $response ? $response->getStatusCode() : 0; |
|
41 | - parent::__construct($message, $code, $previous); |
|
42 | - $this->request = $request; |
|
43 | - $this->response = $response; |
|
44 | - $this->handlerContext = $handlerContext; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Wrap non-RequestExceptions with a RequestException |
|
49 | - */ |
|
50 | - public static function wrapException(RequestInterface $request, \Throwable $e): RequestException |
|
51 | - { |
|
52 | - return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Factory method to create a new exception with a normalized error message |
|
57 | - * |
|
58 | - * @param RequestInterface $request Request sent |
|
59 | - * @param ResponseInterface $response Response received |
|
60 | - * @param \Throwable|null $previous Previous exception |
|
61 | - * @param array $handlerContext Optional handler context |
|
62 | - * @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer |
|
63 | - */ |
|
64 | - public static function create( |
|
65 | - RequestInterface $request, |
|
66 | - ResponseInterface $response = null, |
|
67 | - \Throwable $previous = null, |
|
68 | - array $handlerContext = [], |
|
69 | - BodySummarizerInterface $bodySummarizer = null |
|
70 | - ): self { |
|
71 | - if (!$response) { |
|
72 | - return new self( |
|
73 | - 'Error completing request', |
|
74 | - $request, |
|
75 | - null, |
|
76 | - $previous, |
|
77 | - $handlerContext |
|
78 | - ); |
|
79 | - } |
|
80 | - |
|
81 | - $level = (int) \floor($response->getStatusCode() / 100); |
|
82 | - if ($level === 4) { |
|
83 | - $label = 'Client error'; |
|
84 | - $className = ClientException::class; |
|
85 | - } elseif ($level === 5) { |
|
86 | - $label = 'Server error'; |
|
87 | - $className = ServerException::class; |
|
88 | - } else { |
|
89 | - $label = 'Unsuccessful request'; |
|
90 | - $className = __CLASS__; |
|
91 | - } |
|
92 | - |
|
93 | - $uri = $request->getUri(); |
|
94 | - $uri = static::obfuscateUri($uri); |
|
95 | - |
|
96 | - // Client Error: `GET /` resulted in a `404 Not Found` response: |
|
97 | - // <html> ... (truncated) |
|
98 | - $message = \sprintf( |
|
99 | - '%s: `%s %s` resulted in a `%s %s` response', |
|
100 | - $label, |
|
101 | - $request->getMethod(), |
|
102 | - $uri->__toString(), |
|
103 | - $response->getStatusCode(), |
|
104 | - $response->getReasonPhrase() |
|
105 | - ); |
|
106 | - |
|
107 | - $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response); |
|
108 | - |
|
109 | - if ($summary !== null) { |
|
110 | - $message .= ":\n{$summary}\n"; |
|
111 | - } |
|
112 | - |
|
113 | - return new $className($message, $request, $response, $previous, $handlerContext); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Obfuscates URI if there is a username and a password present |
|
118 | - */ |
|
119 | - private static function obfuscateUri(UriInterface $uri): UriInterface |
|
120 | - { |
|
121 | - $userInfo = $uri->getUserInfo(); |
|
122 | - |
|
123 | - if (false !== ($pos = \strpos($userInfo, ':'))) { |
|
124 | - return $uri->withUserInfo(\substr($userInfo, 0, $pos), '***'); |
|
125 | - } |
|
126 | - |
|
127 | - return $uri; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Get the request that caused the exception |
|
132 | - */ |
|
133 | - public function getRequest(): RequestInterface |
|
134 | - { |
|
135 | - return $this->request; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Get the associated response |
|
140 | - */ |
|
141 | - public function getResponse(): ?ResponseInterface |
|
142 | - { |
|
143 | - return $this->response; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Check if a response was received |
|
148 | - */ |
|
149 | - public function hasResponse(): bool |
|
150 | - { |
|
151 | - return $this->response !== null; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Get contextual information about the error from the underlying handler. |
|
156 | - * |
|
157 | - * The contents of this array will vary depending on which handler you are |
|
158 | - * using. It may also be just an empty array. Relying on this data will |
|
159 | - * couple you to a specific handler, but can give more debug information |
|
160 | - * when needed. |
|
161 | - */ |
|
162 | - public function getHandlerContext(): array |
|
163 | - { |
|
164 | - return $this->handlerContext; |
|
165 | - } |
|
17 | + /** |
|
18 | + * @var RequestInterface |
|
19 | + */ |
|
20 | + private $request; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var ResponseInterface|null |
|
24 | + */ |
|
25 | + private $response; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + private $handlerContext; |
|
31 | + |
|
32 | + public function __construct( |
|
33 | + string $message, |
|
34 | + RequestInterface $request, |
|
35 | + ResponseInterface $response = null, |
|
36 | + \Throwable $previous = null, |
|
37 | + array $handlerContext = [] |
|
38 | + ) { |
|
39 | + // Set the code of the exception if the response is set and not future. |
|
40 | + $code = $response ? $response->getStatusCode() : 0; |
|
41 | + parent::__construct($message, $code, $previous); |
|
42 | + $this->request = $request; |
|
43 | + $this->response = $response; |
|
44 | + $this->handlerContext = $handlerContext; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Wrap non-RequestExceptions with a RequestException |
|
49 | + */ |
|
50 | + public static function wrapException(RequestInterface $request, \Throwable $e): RequestException |
|
51 | + { |
|
52 | + return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Factory method to create a new exception with a normalized error message |
|
57 | + * |
|
58 | + * @param RequestInterface $request Request sent |
|
59 | + * @param ResponseInterface $response Response received |
|
60 | + * @param \Throwable|null $previous Previous exception |
|
61 | + * @param array $handlerContext Optional handler context |
|
62 | + * @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer |
|
63 | + */ |
|
64 | + public static function create( |
|
65 | + RequestInterface $request, |
|
66 | + ResponseInterface $response = null, |
|
67 | + \Throwable $previous = null, |
|
68 | + array $handlerContext = [], |
|
69 | + BodySummarizerInterface $bodySummarizer = null |
|
70 | + ): self { |
|
71 | + if (!$response) { |
|
72 | + return new self( |
|
73 | + 'Error completing request', |
|
74 | + $request, |
|
75 | + null, |
|
76 | + $previous, |
|
77 | + $handlerContext |
|
78 | + ); |
|
79 | + } |
|
80 | + |
|
81 | + $level = (int) \floor($response->getStatusCode() / 100); |
|
82 | + if ($level === 4) { |
|
83 | + $label = 'Client error'; |
|
84 | + $className = ClientException::class; |
|
85 | + } elseif ($level === 5) { |
|
86 | + $label = 'Server error'; |
|
87 | + $className = ServerException::class; |
|
88 | + } else { |
|
89 | + $label = 'Unsuccessful request'; |
|
90 | + $className = __CLASS__; |
|
91 | + } |
|
92 | + |
|
93 | + $uri = $request->getUri(); |
|
94 | + $uri = static::obfuscateUri($uri); |
|
95 | + |
|
96 | + // Client Error: `GET /` resulted in a `404 Not Found` response: |
|
97 | + // <html> ... (truncated) |
|
98 | + $message = \sprintf( |
|
99 | + '%s: `%s %s` resulted in a `%s %s` response', |
|
100 | + $label, |
|
101 | + $request->getMethod(), |
|
102 | + $uri->__toString(), |
|
103 | + $response->getStatusCode(), |
|
104 | + $response->getReasonPhrase() |
|
105 | + ); |
|
106 | + |
|
107 | + $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response); |
|
108 | + |
|
109 | + if ($summary !== null) { |
|
110 | + $message .= ":\n{$summary}\n"; |
|
111 | + } |
|
112 | + |
|
113 | + return new $className($message, $request, $response, $previous, $handlerContext); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Obfuscates URI if there is a username and a password present |
|
118 | + */ |
|
119 | + private static function obfuscateUri(UriInterface $uri): UriInterface |
|
120 | + { |
|
121 | + $userInfo = $uri->getUserInfo(); |
|
122 | + |
|
123 | + if (false !== ($pos = \strpos($userInfo, ':'))) { |
|
124 | + return $uri->withUserInfo(\substr($userInfo, 0, $pos), '***'); |
|
125 | + } |
|
126 | + |
|
127 | + return $uri; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Get the request that caused the exception |
|
132 | + */ |
|
133 | + public function getRequest(): RequestInterface |
|
134 | + { |
|
135 | + return $this->request; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Get the associated response |
|
140 | + */ |
|
141 | + public function getResponse(): ?ResponseInterface |
|
142 | + { |
|
143 | + return $this->response; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Check if a response was received |
|
148 | + */ |
|
149 | + public function hasResponse(): bool |
|
150 | + { |
|
151 | + return $this->response !== null; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Get contextual information about the error from the underlying handler. |
|
156 | + * |
|
157 | + * The contents of this array will vary depending on which handler you are |
|
158 | + * using. It may also be just an empty array. Relying on this data will |
|
159 | + * couple you to a specific handler, but can give more debug information |
|
160 | + * when needed. |
|
161 | + */ |
|
162 | + public function getHandlerContext(): array |
|
163 | + { |
|
164 | + return $this->handlerContext; |
|
165 | + } |
|
166 | 166 | } |
@@ -78,7 +78,7 @@ |
||
78 | 78 | ); |
79 | 79 | } |
80 | 80 | |
81 | - $level = (int) \floor($response->getStatusCode() / 100); |
|
81 | + $level = (int)\floor($response->getStatusCode() / 100); |
|
82 | 82 | if ($level === 4) { |
83 | 83 | $label = 'Client error'; |
84 | 84 | $className = ClientException::class; |
@@ -12,8 +12,7 @@ |
||
12 | 12 | /** |
13 | 13 | * HTTP Request exception |
14 | 14 | */ |
15 | -class RequestException extends TransferException implements RequestExceptionInterface |
|
16 | -{ |
|
15 | +class RequestException extends TransferException implements RequestExceptionInterface { |
|
17 | 16 | /** |
18 | 17 | * @var RequestInterface |
19 | 18 | */ |
@@ -10,30 +10,30 @@ |
||
10 | 10 | */ |
11 | 11 | class BadResponseException extends RequestException |
12 | 12 | { |
13 | - public function __construct( |
|
14 | - string $message, |
|
15 | - RequestInterface $request, |
|
16 | - ResponseInterface $response, |
|
17 | - \Throwable $previous = null, |
|
18 | - array $handlerContext = [] |
|
19 | - ) { |
|
20 | - parent::__construct($message, $request, $response, $previous, $handlerContext); |
|
21 | - } |
|
13 | + public function __construct( |
|
14 | + string $message, |
|
15 | + RequestInterface $request, |
|
16 | + ResponseInterface $response, |
|
17 | + \Throwable $previous = null, |
|
18 | + array $handlerContext = [] |
|
19 | + ) { |
|
20 | + parent::__construct($message, $request, $response, $previous, $handlerContext); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * Current exception and the ones that extend it will always have a response. |
|
25 | - */ |
|
26 | - public function hasResponse(): bool |
|
27 | - { |
|
28 | - return true; |
|
29 | - } |
|
23 | + /** |
|
24 | + * Current exception and the ones that extend it will always have a response. |
|
25 | + */ |
|
26 | + public function hasResponse(): bool |
|
27 | + { |
|
28 | + return true; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * This function narrows the return type from the parent class and does not allow it to be nullable. |
|
33 | - */ |
|
34 | - public function getResponse(): ResponseInterface |
|
35 | - { |
|
36 | - /** @var ResponseInterface */ |
|
37 | - return parent::getResponse(); |
|
38 | - } |
|
31 | + /** |
|
32 | + * This function narrows the return type from the parent class and does not allow it to be nullable. |
|
33 | + */ |
|
34 | + public function getResponse(): ResponseInterface |
|
35 | + { |
|
36 | + /** @var ResponseInterface */ |
|
37 | + return parent::getResponse(); |
|
38 | + } |
|
39 | 39 | } |
@@ -8,8 +8,7 @@ |
||
8 | 8 | /** |
9 | 9 | * Exception when an HTTP error occurs (4xx or 5xx error) |
10 | 10 | */ |
11 | -class BadResponseException extends RequestException |
|
12 | -{ |
|
11 | +class BadResponseException extends RequestException { |
|
13 | 12 | public function __construct( |
14 | 13 | string $message, |
15 | 14 | RequestInterface $request, |
@@ -12,45 +12,45 @@ |
||
12 | 12 | */ |
13 | 13 | class ConnectException extends TransferException implements NetworkExceptionInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * @var RequestInterface |
|
17 | - */ |
|
18 | - private $request; |
|
15 | + /** |
|
16 | + * @var RequestInterface |
|
17 | + */ |
|
18 | + private $request; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - private $handlerContext; |
|
20 | + /** |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + private $handlerContext; |
|
24 | 24 | |
25 | - public function __construct( |
|
26 | - string $message, |
|
27 | - RequestInterface $request, |
|
28 | - \Throwable $previous = null, |
|
29 | - array $handlerContext = [] |
|
30 | - ) { |
|
31 | - parent::__construct($message, 0, $previous); |
|
32 | - $this->request = $request; |
|
33 | - $this->handlerContext = $handlerContext; |
|
34 | - } |
|
25 | + public function __construct( |
|
26 | + string $message, |
|
27 | + RequestInterface $request, |
|
28 | + \Throwable $previous = null, |
|
29 | + array $handlerContext = [] |
|
30 | + ) { |
|
31 | + parent::__construct($message, 0, $previous); |
|
32 | + $this->request = $request; |
|
33 | + $this->handlerContext = $handlerContext; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Get the request that caused the exception |
|
38 | - */ |
|
39 | - public function getRequest(): RequestInterface |
|
40 | - { |
|
41 | - return $this->request; |
|
42 | - } |
|
36 | + /** |
|
37 | + * Get the request that caused the exception |
|
38 | + */ |
|
39 | + public function getRequest(): RequestInterface |
|
40 | + { |
|
41 | + return $this->request; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Get contextual information about the error from the underlying handler. |
|
46 | - * |
|
47 | - * The contents of this array will vary depending on which handler you are |
|
48 | - * using. It may also be just an empty array. Relying on this data will |
|
49 | - * couple you to a specific handler, but can give more debug information |
|
50 | - * when needed. |
|
51 | - */ |
|
52 | - public function getHandlerContext(): array |
|
53 | - { |
|
54 | - return $this->handlerContext; |
|
55 | - } |
|
44 | + /** |
|
45 | + * Get contextual information about the error from the underlying handler. |
|
46 | + * |
|
47 | + * The contents of this array will vary depending on which handler you are |
|
48 | + * using. It may also be just an empty array. Relying on this data will |
|
49 | + * couple you to a specific handler, but can give more debug information |
|
50 | + * when needed. |
|
51 | + */ |
|
52 | + public function getHandlerContext(): array |
|
53 | + { |
|
54 | + return $this->handlerContext; |
|
55 | + } |
|
56 | 56 | } |
@@ -10,8 +10,7 @@ |
||
10 | 10 | * |
11 | 11 | * Note that no response is present for a ConnectException |
12 | 12 | */ |
13 | -class ConnectException extends TransferException implements NetworkExceptionInterface |
|
14 | -{ |
|
13 | +class ConnectException extends TransferException implements NetworkExceptionInterface { |
|
15 | 14 | /** |
16 | 15 | * @var RequestInterface |
17 | 16 | */ |
@@ -11,453 +11,453 @@ |
||
11 | 11 | |
12 | 12 | final class Utils |
13 | 13 | { |
14 | - /** |
|
15 | - * Remove the items given by the keys, case insensitively from the data. |
|
16 | - * |
|
17 | - * @param (string|int)[] $keys |
|
18 | - */ |
|
19 | - public static function caselessRemove(array $keys, array $data): array |
|
20 | - { |
|
21 | - $result = []; |
|
22 | - |
|
23 | - foreach ($keys as &$key) { |
|
24 | - $key = strtolower((string) $key); |
|
25 | - } |
|
26 | - |
|
27 | - foreach ($data as $k => $v) { |
|
28 | - if (!in_array(strtolower((string) $k), $keys)) { |
|
29 | - $result[$k] = $v; |
|
30 | - } |
|
31 | - } |
|
32 | - |
|
33 | - return $result; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Copy the contents of a stream into another stream until the given number |
|
38 | - * of bytes have been read. |
|
39 | - * |
|
40 | - * @param StreamInterface $source Stream to read from |
|
41 | - * @param StreamInterface $dest Stream to write to |
|
42 | - * @param int $maxLen Maximum number of bytes to read. Pass -1 |
|
43 | - * to read the entire stream. |
|
44 | - * |
|
45 | - * @throws \RuntimeException on error. |
|
46 | - */ |
|
47 | - public static function copyToStream(StreamInterface $source, StreamInterface $dest, int $maxLen = -1): void |
|
48 | - { |
|
49 | - $bufferSize = 8192; |
|
50 | - |
|
51 | - if ($maxLen === -1) { |
|
52 | - while (!$source->eof()) { |
|
53 | - if (!$dest->write($source->read($bufferSize))) { |
|
54 | - break; |
|
55 | - } |
|
56 | - } |
|
57 | - } else { |
|
58 | - $remaining = $maxLen; |
|
59 | - while ($remaining > 0 && !$source->eof()) { |
|
60 | - $buf = $source->read(min($bufferSize, $remaining)); |
|
61 | - $len = strlen($buf); |
|
62 | - if (!$len) { |
|
63 | - break; |
|
64 | - } |
|
65 | - $remaining -= $len; |
|
66 | - $dest->write($buf); |
|
67 | - } |
|
68 | - } |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Copy the contents of a stream into a string until the given number of |
|
73 | - * bytes have been read. |
|
74 | - * |
|
75 | - * @param StreamInterface $stream Stream to read |
|
76 | - * @param int $maxLen Maximum number of bytes to read. Pass -1 |
|
77 | - * to read the entire stream. |
|
78 | - * |
|
79 | - * @throws \RuntimeException on error. |
|
80 | - */ |
|
81 | - public static function copyToString(StreamInterface $stream, int $maxLen = -1): string |
|
82 | - { |
|
83 | - $buffer = ''; |
|
84 | - |
|
85 | - if ($maxLen === -1) { |
|
86 | - while (!$stream->eof()) { |
|
87 | - $buf = $stream->read(1048576); |
|
88 | - if ($buf === '') { |
|
89 | - break; |
|
90 | - } |
|
91 | - $buffer .= $buf; |
|
92 | - } |
|
93 | - |
|
94 | - return $buffer; |
|
95 | - } |
|
96 | - |
|
97 | - $len = 0; |
|
98 | - while (!$stream->eof() && $len < $maxLen) { |
|
99 | - $buf = $stream->read($maxLen - $len); |
|
100 | - if ($buf === '') { |
|
101 | - break; |
|
102 | - } |
|
103 | - $buffer .= $buf; |
|
104 | - $len = strlen($buffer); |
|
105 | - } |
|
106 | - |
|
107 | - return $buffer; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Calculate a hash of a stream. |
|
112 | - * |
|
113 | - * This method reads the entire stream to calculate a rolling hash, based |
|
114 | - * on PHP's `hash_init` functions. |
|
115 | - * |
|
116 | - * @param StreamInterface $stream Stream to calculate the hash for |
|
117 | - * @param string $algo Hash algorithm (e.g. md5, crc32, etc) |
|
118 | - * @param bool $rawOutput Whether or not to use raw output |
|
119 | - * |
|
120 | - * @throws \RuntimeException on error. |
|
121 | - */ |
|
122 | - public static function hash(StreamInterface $stream, string $algo, bool $rawOutput = false): string |
|
123 | - { |
|
124 | - $pos = $stream->tell(); |
|
125 | - |
|
126 | - if ($pos > 0) { |
|
127 | - $stream->rewind(); |
|
128 | - } |
|
129 | - |
|
130 | - $ctx = hash_init($algo); |
|
131 | - while (!$stream->eof()) { |
|
132 | - hash_update($ctx, $stream->read(1048576)); |
|
133 | - } |
|
134 | - |
|
135 | - $out = hash_final($ctx, $rawOutput); |
|
136 | - $stream->seek($pos); |
|
137 | - |
|
138 | - return $out; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Clone and modify a request with the given changes. |
|
143 | - * |
|
144 | - * This method is useful for reducing the number of clones needed to mutate |
|
145 | - * a message. |
|
146 | - * |
|
147 | - * The changes can be one of: |
|
148 | - * - method: (string) Changes the HTTP method. |
|
149 | - * - set_headers: (array) Sets the given headers. |
|
150 | - * - remove_headers: (array) Remove the given headers. |
|
151 | - * - body: (mixed) Sets the given body. |
|
152 | - * - uri: (UriInterface) Set the URI. |
|
153 | - * - query: (string) Set the query string value of the URI. |
|
154 | - * - version: (string) Set the protocol version. |
|
155 | - * |
|
156 | - * @param RequestInterface $request Request to clone and modify. |
|
157 | - * @param array $changes Changes to apply. |
|
158 | - */ |
|
159 | - public static function modifyRequest(RequestInterface $request, array $changes): RequestInterface |
|
160 | - { |
|
161 | - if (!$changes) { |
|
162 | - return $request; |
|
163 | - } |
|
164 | - |
|
165 | - $headers = $request->getHeaders(); |
|
166 | - |
|
167 | - if (!isset($changes['uri'])) { |
|
168 | - $uri = $request->getUri(); |
|
169 | - } else { |
|
170 | - // Remove the host header if one is on the URI |
|
171 | - if ($host = $changes['uri']->getHost()) { |
|
172 | - $changes['set_headers']['Host'] = $host; |
|
173 | - |
|
174 | - if ($port = $changes['uri']->getPort()) { |
|
175 | - $standardPorts = ['http' => 80, 'https' => 443]; |
|
176 | - $scheme = $changes['uri']->getScheme(); |
|
177 | - if (isset($standardPorts[$scheme]) && $port != $standardPorts[$scheme]) { |
|
178 | - $changes['set_headers']['Host'] .= ':'.$port; |
|
179 | - } |
|
180 | - } |
|
181 | - } |
|
182 | - $uri = $changes['uri']; |
|
183 | - } |
|
184 | - |
|
185 | - if (!empty($changes['remove_headers'])) { |
|
186 | - $headers = self::caselessRemove($changes['remove_headers'], $headers); |
|
187 | - } |
|
188 | - |
|
189 | - if (!empty($changes['set_headers'])) { |
|
190 | - $headers = self::caselessRemove(array_keys($changes['set_headers']), $headers); |
|
191 | - $headers = $changes['set_headers'] + $headers; |
|
192 | - } |
|
193 | - |
|
194 | - if (isset($changes['query'])) { |
|
195 | - $uri = $uri->withQuery($changes['query']); |
|
196 | - } |
|
197 | - |
|
198 | - if ($request instanceof ServerRequestInterface) { |
|
199 | - $new = (new ServerRequest( |
|
200 | - $changes['method'] ?? $request->getMethod(), |
|
201 | - $uri, |
|
202 | - $headers, |
|
203 | - $changes['body'] ?? $request->getBody(), |
|
204 | - $changes['version'] ?? $request->getProtocolVersion(), |
|
205 | - $request->getServerParams() |
|
206 | - )) |
|
207 | - ->withParsedBody($request->getParsedBody()) |
|
208 | - ->withQueryParams($request->getQueryParams()) |
|
209 | - ->withCookieParams($request->getCookieParams()) |
|
210 | - ->withUploadedFiles($request->getUploadedFiles()); |
|
211 | - |
|
212 | - foreach ($request->getAttributes() as $key => $value) { |
|
213 | - $new = $new->withAttribute($key, $value); |
|
214 | - } |
|
215 | - |
|
216 | - return $new; |
|
217 | - } |
|
218 | - |
|
219 | - return new Request( |
|
220 | - $changes['method'] ?? $request->getMethod(), |
|
221 | - $uri, |
|
222 | - $headers, |
|
223 | - $changes['body'] ?? $request->getBody(), |
|
224 | - $changes['version'] ?? $request->getProtocolVersion() |
|
225 | - ); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Read a line from the stream up to the maximum allowed buffer length. |
|
230 | - * |
|
231 | - * @param StreamInterface $stream Stream to read from |
|
232 | - * @param int|null $maxLength Maximum buffer length |
|
233 | - */ |
|
234 | - public static function readLine(StreamInterface $stream, int $maxLength = null): string |
|
235 | - { |
|
236 | - $buffer = ''; |
|
237 | - $size = 0; |
|
238 | - |
|
239 | - while (!$stream->eof()) { |
|
240 | - if ('' === ($byte = $stream->read(1))) { |
|
241 | - return $buffer; |
|
242 | - } |
|
243 | - $buffer .= $byte; |
|
244 | - // Break when a new line is found or the max length - 1 is reached |
|
245 | - if ($byte === "\n" || ++$size === $maxLength - 1) { |
|
246 | - break; |
|
247 | - } |
|
248 | - } |
|
249 | - |
|
250 | - return $buffer; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Create a new stream based on the input type. |
|
255 | - * |
|
256 | - * Options is an associative array that can contain the following keys: |
|
257 | - * - metadata: Array of custom metadata. |
|
258 | - * - size: Size of the stream. |
|
259 | - * |
|
260 | - * This method accepts the following `$resource` types: |
|
261 | - * - `OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface`: Returns the value as-is. |
|
262 | - * - `string`: Creates a stream object that uses the given string as the contents. |
|
263 | - * - `resource`: Creates a stream object that wraps the given PHP stream resource. |
|
264 | - * - `Iterator`: If the provided value implements `Iterator`, then a read-only |
|
265 | - * stream object will be created that wraps the given iterable. Each time the |
|
266 | - * stream is read from, data from the iterator will fill a buffer and will be |
|
267 | - * continuously called until the buffer is equal to the requested read size. |
|
268 | - * Subsequent read calls will first read from the buffer and then call `next` |
|
269 | - * on the underlying iterator until it is exhausted. |
|
270 | - * - `object` with `__toString()`: If the object has the `__toString()` method, |
|
271 | - * the object will be cast to a string and then a stream will be returned that |
|
272 | - * uses the string value. |
|
273 | - * - `NULL`: When `null` is passed, an empty stream object is returned. |
|
274 | - * - `callable` When a callable is passed, a read-only stream object will be |
|
275 | - * created that invokes the given callable. The callable is invoked with the |
|
276 | - * number of suggested bytes to read. The callable can return any number of |
|
277 | - * bytes, but MUST return `false` when there is no more data to return. The |
|
278 | - * stream object that wraps the callable will invoke the callable until the |
|
279 | - * number of requested bytes are available. Any additional bytes will be |
|
280 | - * buffered and used in subsequent reads. |
|
281 | - * |
|
282 | - * @param resource|string|int|float|bool|StreamInterface|callable|\Iterator|null $resource Entity body data |
|
283 | - * @param array{size?: int, metadata?: array} $options Additional options |
|
284 | - * |
|
285 | - * @throws \InvalidArgumentException if the $resource arg is not valid. |
|
286 | - */ |
|
287 | - public static function streamFor($resource = '', array $options = []): StreamInterface |
|
288 | - { |
|
289 | - if (is_scalar($resource)) { |
|
290 | - $stream = self::tryFopen('php://temp', 'r+'); |
|
291 | - if ($resource !== '') { |
|
292 | - fwrite($stream, (string) $resource); |
|
293 | - fseek($stream, 0); |
|
294 | - } |
|
295 | - |
|
296 | - return new Stream($stream, $options); |
|
297 | - } |
|
298 | - |
|
299 | - switch (gettype($resource)) { |
|
300 | - case 'resource': |
|
301 | - /* |
|
14 | + /** |
|
15 | + * Remove the items given by the keys, case insensitively from the data. |
|
16 | + * |
|
17 | + * @param (string|int)[] $keys |
|
18 | + */ |
|
19 | + public static function caselessRemove(array $keys, array $data): array |
|
20 | + { |
|
21 | + $result = []; |
|
22 | + |
|
23 | + foreach ($keys as &$key) { |
|
24 | + $key = strtolower((string) $key); |
|
25 | + } |
|
26 | + |
|
27 | + foreach ($data as $k => $v) { |
|
28 | + if (!in_array(strtolower((string) $k), $keys)) { |
|
29 | + $result[$k] = $v; |
|
30 | + } |
|
31 | + } |
|
32 | + |
|
33 | + return $result; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Copy the contents of a stream into another stream until the given number |
|
38 | + * of bytes have been read. |
|
39 | + * |
|
40 | + * @param StreamInterface $source Stream to read from |
|
41 | + * @param StreamInterface $dest Stream to write to |
|
42 | + * @param int $maxLen Maximum number of bytes to read. Pass -1 |
|
43 | + * to read the entire stream. |
|
44 | + * |
|
45 | + * @throws \RuntimeException on error. |
|
46 | + */ |
|
47 | + public static function copyToStream(StreamInterface $source, StreamInterface $dest, int $maxLen = -1): void |
|
48 | + { |
|
49 | + $bufferSize = 8192; |
|
50 | + |
|
51 | + if ($maxLen === -1) { |
|
52 | + while (!$source->eof()) { |
|
53 | + if (!$dest->write($source->read($bufferSize))) { |
|
54 | + break; |
|
55 | + } |
|
56 | + } |
|
57 | + } else { |
|
58 | + $remaining = $maxLen; |
|
59 | + while ($remaining > 0 && !$source->eof()) { |
|
60 | + $buf = $source->read(min($bufferSize, $remaining)); |
|
61 | + $len = strlen($buf); |
|
62 | + if (!$len) { |
|
63 | + break; |
|
64 | + } |
|
65 | + $remaining -= $len; |
|
66 | + $dest->write($buf); |
|
67 | + } |
|
68 | + } |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Copy the contents of a stream into a string until the given number of |
|
73 | + * bytes have been read. |
|
74 | + * |
|
75 | + * @param StreamInterface $stream Stream to read |
|
76 | + * @param int $maxLen Maximum number of bytes to read. Pass -1 |
|
77 | + * to read the entire stream. |
|
78 | + * |
|
79 | + * @throws \RuntimeException on error. |
|
80 | + */ |
|
81 | + public static function copyToString(StreamInterface $stream, int $maxLen = -1): string |
|
82 | + { |
|
83 | + $buffer = ''; |
|
84 | + |
|
85 | + if ($maxLen === -1) { |
|
86 | + while (!$stream->eof()) { |
|
87 | + $buf = $stream->read(1048576); |
|
88 | + if ($buf === '') { |
|
89 | + break; |
|
90 | + } |
|
91 | + $buffer .= $buf; |
|
92 | + } |
|
93 | + |
|
94 | + return $buffer; |
|
95 | + } |
|
96 | + |
|
97 | + $len = 0; |
|
98 | + while (!$stream->eof() && $len < $maxLen) { |
|
99 | + $buf = $stream->read($maxLen - $len); |
|
100 | + if ($buf === '') { |
|
101 | + break; |
|
102 | + } |
|
103 | + $buffer .= $buf; |
|
104 | + $len = strlen($buffer); |
|
105 | + } |
|
106 | + |
|
107 | + return $buffer; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Calculate a hash of a stream. |
|
112 | + * |
|
113 | + * This method reads the entire stream to calculate a rolling hash, based |
|
114 | + * on PHP's `hash_init` functions. |
|
115 | + * |
|
116 | + * @param StreamInterface $stream Stream to calculate the hash for |
|
117 | + * @param string $algo Hash algorithm (e.g. md5, crc32, etc) |
|
118 | + * @param bool $rawOutput Whether or not to use raw output |
|
119 | + * |
|
120 | + * @throws \RuntimeException on error. |
|
121 | + */ |
|
122 | + public static function hash(StreamInterface $stream, string $algo, bool $rawOutput = false): string |
|
123 | + { |
|
124 | + $pos = $stream->tell(); |
|
125 | + |
|
126 | + if ($pos > 0) { |
|
127 | + $stream->rewind(); |
|
128 | + } |
|
129 | + |
|
130 | + $ctx = hash_init($algo); |
|
131 | + while (!$stream->eof()) { |
|
132 | + hash_update($ctx, $stream->read(1048576)); |
|
133 | + } |
|
134 | + |
|
135 | + $out = hash_final($ctx, $rawOutput); |
|
136 | + $stream->seek($pos); |
|
137 | + |
|
138 | + return $out; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Clone and modify a request with the given changes. |
|
143 | + * |
|
144 | + * This method is useful for reducing the number of clones needed to mutate |
|
145 | + * a message. |
|
146 | + * |
|
147 | + * The changes can be one of: |
|
148 | + * - method: (string) Changes the HTTP method. |
|
149 | + * - set_headers: (array) Sets the given headers. |
|
150 | + * - remove_headers: (array) Remove the given headers. |
|
151 | + * - body: (mixed) Sets the given body. |
|
152 | + * - uri: (UriInterface) Set the URI. |
|
153 | + * - query: (string) Set the query string value of the URI. |
|
154 | + * - version: (string) Set the protocol version. |
|
155 | + * |
|
156 | + * @param RequestInterface $request Request to clone and modify. |
|
157 | + * @param array $changes Changes to apply. |
|
158 | + */ |
|
159 | + public static function modifyRequest(RequestInterface $request, array $changes): RequestInterface |
|
160 | + { |
|
161 | + if (!$changes) { |
|
162 | + return $request; |
|
163 | + } |
|
164 | + |
|
165 | + $headers = $request->getHeaders(); |
|
166 | + |
|
167 | + if (!isset($changes['uri'])) { |
|
168 | + $uri = $request->getUri(); |
|
169 | + } else { |
|
170 | + // Remove the host header if one is on the URI |
|
171 | + if ($host = $changes['uri']->getHost()) { |
|
172 | + $changes['set_headers']['Host'] = $host; |
|
173 | + |
|
174 | + if ($port = $changes['uri']->getPort()) { |
|
175 | + $standardPorts = ['http' => 80, 'https' => 443]; |
|
176 | + $scheme = $changes['uri']->getScheme(); |
|
177 | + if (isset($standardPorts[$scheme]) && $port != $standardPorts[$scheme]) { |
|
178 | + $changes['set_headers']['Host'] .= ':'.$port; |
|
179 | + } |
|
180 | + } |
|
181 | + } |
|
182 | + $uri = $changes['uri']; |
|
183 | + } |
|
184 | + |
|
185 | + if (!empty($changes['remove_headers'])) { |
|
186 | + $headers = self::caselessRemove($changes['remove_headers'], $headers); |
|
187 | + } |
|
188 | + |
|
189 | + if (!empty($changes['set_headers'])) { |
|
190 | + $headers = self::caselessRemove(array_keys($changes['set_headers']), $headers); |
|
191 | + $headers = $changes['set_headers'] + $headers; |
|
192 | + } |
|
193 | + |
|
194 | + if (isset($changes['query'])) { |
|
195 | + $uri = $uri->withQuery($changes['query']); |
|
196 | + } |
|
197 | + |
|
198 | + if ($request instanceof ServerRequestInterface) { |
|
199 | + $new = (new ServerRequest( |
|
200 | + $changes['method'] ?? $request->getMethod(), |
|
201 | + $uri, |
|
202 | + $headers, |
|
203 | + $changes['body'] ?? $request->getBody(), |
|
204 | + $changes['version'] ?? $request->getProtocolVersion(), |
|
205 | + $request->getServerParams() |
|
206 | + )) |
|
207 | + ->withParsedBody($request->getParsedBody()) |
|
208 | + ->withQueryParams($request->getQueryParams()) |
|
209 | + ->withCookieParams($request->getCookieParams()) |
|
210 | + ->withUploadedFiles($request->getUploadedFiles()); |
|
211 | + |
|
212 | + foreach ($request->getAttributes() as $key => $value) { |
|
213 | + $new = $new->withAttribute($key, $value); |
|
214 | + } |
|
215 | + |
|
216 | + return $new; |
|
217 | + } |
|
218 | + |
|
219 | + return new Request( |
|
220 | + $changes['method'] ?? $request->getMethod(), |
|
221 | + $uri, |
|
222 | + $headers, |
|
223 | + $changes['body'] ?? $request->getBody(), |
|
224 | + $changes['version'] ?? $request->getProtocolVersion() |
|
225 | + ); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Read a line from the stream up to the maximum allowed buffer length. |
|
230 | + * |
|
231 | + * @param StreamInterface $stream Stream to read from |
|
232 | + * @param int|null $maxLength Maximum buffer length |
|
233 | + */ |
|
234 | + public static function readLine(StreamInterface $stream, int $maxLength = null): string |
|
235 | + { |
|
236 | + $buffer = ''; |
|
237 | + $size = 0; |
|
238 | + |
|
239 | + while (!$stream->eof()) { |
|
240 | + if ('' === ($byte = $stream->read(1))) { |
|
241 | + return $buffer; |
|
242 | + } |
|
243 | + $buffer .= $byte; |
|
244 | + // Break when a new line is found or the max length - 1 is reached |
|
245 | + if ($byte === "\n" || ++$size === $maxLength - 1) { |
|
246 | + break; |
|
247 | + } |
|
248 | + } |
|
249 | + |
|
250 | + return $buffer; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Create a new stream based on the input type. |
|
255 | + * |
|
256 | + * Options is an associative array that can contain the following keys: |
|
257 | + * - metadata: Array of custom metadata. |
|
258 | + * - size: Size of the stream. |
|
259 | + * |
|
260 | + * This method accepts the following `$resource` types: |
|
261 | + * - `OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface`: Returns the value as-is. |
|
262 | + * - `string`: Creates a stream object that uses the given string as the contents. |
|
263 | + * - `resource`: Creates a stream object that wraps the given PHP stream resource. |
|
264 | + * - `Iterator`: If the provided value implements `Iterator`, then a read-only |
|
265 | + * stream object will be created that wraps the given iterable. Each time the |
|
266 | + * stream is read from, data from the iterator will fill a buffer and will be |
|
267 | + * continuously called until the buffer is equal to the requested read size. |
|
268 | + * Subsequent read calls will first read from the buffer and then call `next` |
|
269 | + * on the underlying iterator until it is exhausted. |
|
270 | + * - `object` with `__toString()`: If the object has the `__toString()` method, |
|
271 | + * the object will be cast to a string and then a stream will be returned that |
|
272 | + * uses the string value. |
|
273 | + * - `NULL`: When `null` is passed, an empty stream object is returned. |
|
274 | + * - `callable` When a callable is passed, a read-only stream object will be |
|
275 | + * created that invokes the given callable. The callable is invoked with the |
|
276 | + * number of suggested bytes to read. The callable can return any number of |
|
277 | + * bytes, but MUST return `false` when there is no more data to return. The |
|
278 | + * stream object that wraps the callable will invoke the callable until the |
|
279 | + * number of requested bytes are available. Any additional bytes will be |
|
280 | + * buffered and used in subsequent reads. |
|
281 | + * |
|
282 | + * @param resource|string|int|float|bool|StreamInterface|callable|\Iterator|null $resource Entity body data |
|
283 | + * @param array{size?: int, metadata?: array} $options Additional options |
|
284 | + * |
|
285 | + * @throws \InvalidArgumentException if the $resource arg is not valid. |
|
286 | + */ |
|
287 | + public static function streamFor($resource = '', array $options = []): StreamInterface |
|
288 | + { |
|
289 | + if (is_scalar($resource)) { |
|
290 | + $stream = self::tryFopen('php://temp', 'r+'); |
|
291 | + if ($resource !== '') { |
|
292 | + fwrite($stream, (string) $resource); |
|
293 | + fseek($stream, 0); |
|
294 | + } |
|
295 | + |
|
296 | + return new Stream($stream, $options); |
|
297 | + } |
|
298 | + |
|
299 | + switch (gettype($resource)) { |
|
300 | + case 'resource': |
|
301 | + /* |
|
302 | 302 | * The 'php://input' is a special stream with quirks and inconsistencies. |
303 | 303 | * We avoid using that stream by reading it into php://temp |
304 | 304 | */ |
305 | 305 | |
306 | - /** @var resource $resource */ |
|
307 | - if ((\stream_get_meta_data($resource)['uri'] ?? '') === 'php://input') { |
|
308 | - $stream = self::tryFopen('php://temp', 'w+'); |
|
309 | - stream_copy_to_stream($resource, $stream); |
|
310 | - fseek($stream, 0); |
|
311 | - $resource = $stream; |
|
312 | - } |
|
313 | - |
|
314 | - return new Stream($resource, $options); |
|
315 | - case 'object': |
|
316 | - /** @var object $resource */ |
|
317 | - if ($resource instanceof StreamInterface) { |
|
318 | - return $resource; |
|
319 | - } elseif ($resource instanceof \Iterator) { |
|
320 | - return new PumpStream(function () use ($resource) { |
|
321 | - if (!$resource->valid()) { |
|
322 | - return false; |
|
323 | - } |
|
324 | - $result = $resource->current(); |
|
325 | - $resource->next(); |
|
326 | - |
|
327 | - return $result; |
|
328 | - }, $options); |
|
329 | - } elseif (method_exists($resource, '__toString')) { |
|
330 | - return self::streamFor((string) $resource, $options); |
|
331 | - } |
|
332 | - break; |
|
333 | - case 'NULL': |
|
334 | - return new Stream(self::tryFopen('php://temp', 'r+'), $options); |
|
335 | - } |
|
336 | - |
|
337 | - if (is_callable($resource)) { |
|
338 | - return new PumpStream($resource, $options); |
|
339 | - } |
|
340 | - |
|
341 | - throw new \InvalidArgumentException('Invalid resource type: '.gettype($resource)); |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Safely opens a PHP stream resource using a filename. |
|
346 | - * |
|
347 | - * When fopen fails, PHP normally raises a warning. This function adds an |
|
348 | - * error handler that checks for errors and throws an exception instead. |
|
349 | - * |
|
350 | - * @param string $filename File to open |
|
351 | - * @param string $mode Mode used to open the file |
|
352 | - * |
|
353 | - * @return resource |
|
354 | - * |
|
355 | - * @throws \RuntimeException if the file cannot be opened |
|
356 | - */ |
|
357 | - public static function tryFopen(string $filename, string $mode) |
|
358 | - { |
|
359 | - $ex = null; |
|
360 | - set_error_handler(static function (int $errno, string $errstr) use ($filename, $mode, &$ex): bool { |
|
361 | - $ex = new \RuntimeException(sprintf( |
|
362 | - 'Unable to open "%s" using mode "%s": %s', |
|
363 | - $filename, |
|
364 | - $mode, |
|
365 | - $errstr |
|
366 | - )); |
|
367 | - |
|
368 | - return true; |
|
369 | - }); |
|
370 | - |
|
371 | - try { |
|
372 | - /** @var resource $handle */ |
|
373 | - $handle = fopen($filename, $mode); |
|
374 | - } catch (\Throwable $e) { |
|
375 | - $ex = new \RuntimeException(sprintf( |
|
376 | - 'Unable to open "%s" using mode "%s": %s', |
|
377 | - $filename, |
|
378 | - $mode, |
|
379 | - $e->getMessage() |
|
380 | - ), 0, $e); |
|
381 | - } |
|
382 | - |
|
383 | - restore_error_handler(); |
|
384 | - |
|
385 | - if ($ex) { |
|
386 | - /** @var $ex \RuntimeException */ |
|
387 | - throw $ex; |
|
388 | - } |
|
389 | - |
|
390 | - return $handle; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Safely gets the contents of a given stream. |
|
395 | - * |
|
396 | - * When stream_get_contents fails, PHP normally raises a warning. This |
|
397 | - * function adds an error handler that checks for errors and throws an |
|
398 | - * exception instead. |
|
399 | - * |
|
400 | - * @param resource $stream |
|
401 | - * |
|
402 | - * @throws \RuntimeException if the stream cannot be read |
|
403 | - */ |
|
404 | - public static function tryGetContents($stream): string |
|
405 | - { |
|
406 | - $ex = null; |
|
407 | - set_error_handler(static function (int $errno, string $errstr) use (&$ex): bool { |
|
408 | - $ex = new \RuntimeException(sprintf( |
|
409 | - 'Unable to read stream contents: %s', |
|
410 | - $errstr |
|
411 | - )); |
|
412 | - |
|
413 | - return true; |
|
414 | - }); |
|
415 | - |
|
416 | - try { |
|
417 | - /** @var string|false $contents */ |
|
418 | - $contents = stream_get_contents($stream); |
|
419 | - |
|
420 | - if ($contents === false) { |
|
421 | - $ex = new \RuntimeException('Unable to read stream contents'); |
|
422 | - } |
|
423 | - } catch (\Throwable $e) { |
|
424 | - $ex = new \RuntimeException(sprintf( |
|
425 | - 'Unable to read stream contents: %s', |
|
426 | - $e->getMessage() |
|
427 | - ), 0, $e); |
|
428 | - } |
|
429 | - |
|
430 | - restore_error_handler(); |
|
431 | - |
|
432 | - if ($ex) { |
|
433 | - /** @var $ex \RuntimeException */ |
|
434 | - throw $ex; |
|
435 | - } |
|
436 | - |
|
437 | - return $contents; |
|
438 | - } |
|
439 | - |
|
440 | - /** |
|
441 | - * Returns a UriInterface for the given value. |
|
442 | - * |
|
443 | - * This function accepts a string or UriInterface and returns a |
|
444 | - * UriInterface for the given value. If the value is already a |
|
445 | - * UriInterface, it is returned as-is. |
|
446 | - * |
|
447 | - * @param string|UriInterface $uri |
|
448 | - * |
|
449 | - * @throws \InvalidArgumentException |
|
450 | - */ |
|
451 | - public static function uriFor($uri): UriInterface |
|
452 | - { |
|
453 | - if ($uri instanceof UriInterface) { |
|
454 | - return $uri; |
|
455 | - } |
|
456 | - |
|
457 | - if (is_string($uri)) { |
|
458 | - return new Uri($uri); |
|
459 | - } |
|
460 | - |
|
461 | - throw new \InvalidArgumentException('URI must be a string or UriInterface'); |
|
462 | - } |
|
306 | + /** @var resource $resource */ |
|
307 | + if ((\stream_get_meta_data($resource)['uri'] ?? '') === 'php://input') { |
|
308 | + $stream = self::tryFopen('php://temp', 'w+'); |
|
309 | + stream_copy_to_stream($resource, $stream); |
|
310 | + fseek($stream, 0); |
|
311 | + $resource = $stream; |
|
312 | + } |
|
313 | + |
|
314 | + return new Stream($resource, $options); |
|
315 | + case 'object': |
|
316 | + /** @var object $resource */ |
|
317 | + if ($resource instanceof StreamInterface) { |
|
318 | + return $resource; |
|
319 | + } elseif ($resource instanceof \Iterator) { |
|
320 | + return new PumpStream(function () use ($resource) { |
|
321 | + if (!$resource->valid()) { |
|
322 | + return false; |
|
323 | + } |
|
324 | + $result = $resource->current(); |
|
325 | + $resource->next(); |
|
326 | + |
|
327 | + return $result; |
|
328 | + }, $options); |
|
329 | + } elseif (method_exists($resource, '__toString')) { |
|
330 | + return self::streamFor((string) $resource, $options); |
|
331 | + } |
|
332 | + break; |
|
333 | + case 'NULL': |
|
334 | + return new Stream(self::tryFopen('php://temp', 'r+'), $options); |
|
335 | + } |
|
336 | + |
|
337 | + if (is_callable($resource)) { |
|
338 | + return new PumpStream($resource, $options); |
|
339 | + } |
|
340 | + |
|
341 | + throw new \InvalidArgumentException('Invalid resource type: '.gettype($resource)); |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Safely opens a PHP stream resource using a filename. |
|
346 | + * |
|
347 | + * When fopen fails, PHP normally raises a warning. This function adds an |
|
348 | + * error handler that checks for errors and throws an exception instead. |
|
349 | + * |
|
350 | + * @param string $filename File to open |
|
351 | + * @param string $mode Mode used to open the file |
|
352 | + * |
|
353 | + * @return resource |
|
354 | + * |
|
355 | + * @throws \RuntimeException if the file cannot be opened |
|
356 | + */ |
|
357 | + public static function tryFopen(string $filename, string $mode) |
|
358 | + { |
|
359 | + $ex = null; |
|
360 | + set_error_handler(static function (int $errno, string $errstr) use ($filename, $mode, &$ex): bool { |
|
361 | + $ex = new \RuntimeException(sprintf( |
|
362 | + 'Unable to open "%s" using mode "%s": %s', |
|
363 | + $filename, |
|
364 | + $mode, |
|
365 | + $errstr |
|
366 | + )); |
|
367 | + |
|
368 | + return true; |
|
369 | + }); |
|
370 | + |
|
371 | + try { |
|
372 | + /** @var resource $handle */ |
|
373 | + $handle = fopen($filename, $mode); |
|
374 | + } catch (\Throwable $e) { |
|
375 | + $ex = new \RuntimeException(sprintf( |
|
376 | + 'Unable to open "%s" using mode "%s": %s', |
|
377 | + $filename, |
|
378 | + $mode, |
|
379 | + $e->getMessage() |
|
380 | + ), 0, $e); |
|
381 | + } |
|
382 | + |
|
383 | + restore_error_handler(); |
|
384 | + |
|
385 | + if ($ex) { |
|
386 | + /** @var $ex \RuntimeException */ |
|
387 | + throw $ex; |
|
388 | + } |
|
389 | + |
|
390 | + return $handle; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Safely gets the contents of a given stream. |
|
395 | + * |
|
396 | + * When stream_get_contents fails, PHP normally raises a warning. This |
|
397 | + * function adds an error handler that checks for errors and throws an |
|
398 | + * exception instead. |
|
399 | + * |
|
400 | + * @param resource $stream |
|
401 | + * |
|
402 | + * @throws \RuntimeException if the stream cannot be read |
|
403 | + */ |
|
404 | + public static function tryGetContents($stream): string |
|
405 | + { |
|
406 | + $ex = null; |
|
407 | + set_error_handler(static function (int $errno, string $errstr) use (&$ex): bool { |
|
408 | + $ex = new \RuntimeException(sprintf( |
|
409 | + 'Unable to read stream contents: %s', |
|
410 | + $errstr |
|
411 | + )); |
|
412 | + |
|
413 | + return true; |
|
414 | + }); |
|
415 | + |
|
416 | + try { |
|
417 | + /** @var string|false $contents */ |
|
418 | + $contents = stream_get_contents($stream); |
|
419 | + |
|
420 | + if ($contents === false) { |
|
421 | + $ex = new \RuntimeException('Unable to read stream contents'); |
|
422 | + } |
|
423 | + } catch (\Throwable $e) { |
|
424 | + $ex = new \RuntimeException(sprintf( |
|
425 | + 'Unable to read stream contents: %s', |
|
426 | + $e->getMessage() |
|
427 | + ), 0, $e); |
|
428 | + } |
|
429 | + |
|
430 | + restore_error_handler(); |
|
431 | + |
|
432 | + if ($ex) { |
|
433 | + /** @var $ex \RuntimeException */ |
|
434 | + throw $ex; |
|
435 | + } |
|
436 | + |
|
437 | + return $contents; |
|
438 | + } |
|
439 | + |
|
440 | + /** |
|
441 | + * Returns a UriInterface for the given value. |
|
442 | + * |
|
443 | + * This function accepts a string or UriInterface and returns a |
|
444 | + * UriInterface for the given value. If the value is already a |
|
445 | + * UriInterface, it is returned as-is. |
|
446 | + * |
|
447 | + * @param string|UriInterface $uri |
|
448 | + * |
|
449 | + * @throws \InvalidArgumentException |
|
450 | + */ |
|
451 | + public static function uriFor($uri): UriInterface |
|
452 | + { |
|
453 | + if ($uri instanceof UriInterface) { |
|
454 | + return $uri; |
|
455 | + } |
|
456 | + |
|
457 | + if (is_string($uri)) { |
|
458 | + return new Uri($uri); |
|
459 | + } |
|
460 | + |
|
461 | + throw new \InvalidArgumentException('URI must be a string or UriInterface'); |
|
462 | + } |
|
463 | 463 | } |
@@ -21,11 +21,11 @@ discard block |
||
21 | 21 | $result = []; |
22 | 22 | |
23 | 23 | foreach ($keys as &$key) { |
24 | - $key = strtolower((string) $key); |
|
24 | + $key = strtolower((string)$key); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | foreach ($data as $k => $v) { |
28 | - if (!in_array(strtolower((string) $k), $keys)) { |
|
28 | + if (!in_array(strtolower((string)$k), $keys)) { |
|
29 | 29 | $result[$k] = $v; |
30 | 30 | } |
31 | 31 | } |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | if (is_scalar($resource)) { |
290 | 290 | $stream = self::tryFopen('php://temp', 'r+'); |
291 | 291 | if ($resource !== '') { |
292 | - fwrite($stream, (string) $resource); |
|
292 | + fwrite($stream, (string)$resource); |
|
293 | 293 | fseek($stream, 0); |
294 | 294 | } |
295 | 295 | |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | if ($resource instanceof StreamInterface) { |
318 | 318 | return $resource; |
319 | 319 | } elseif ($resource instanceof \Iterator) { |
320 | - return new PumpStream(function () use ($resource) { |
|
320 | + return new PumpStream(function() use ($resource) { |
|
321 | 321 | if (!$resource->valid()) { |
322 | 322 | return false; |
323 | 323 | } |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | return $result; |
328 | 328 | }, $options); |
329 | 329 | } elseif (method_exists($resource, '__toString')) { |
330 | - return self::streamFor((string) $resource, $options); |
|
330 | + return self::streamFor((string)$resource, $options); |
|
331 | 331 | } |
332 | 332 | break; |
333 | 333 | case 'NULL': |
@@ -357,7 +357,7 @@ discard block |
||
357 | 357 | public static function tryFopen(string $filename, string $mode) |
358 | 358 | { |
359 | 359 | $ex = null; |
360 | - set_error_handler(static function (int $errno, string $errstr) use ($filename, $mode, &$ex): bool { |
|
360 | + set_error_handler(static function(int $errno, string $errstr) use ($filename, $mode, &$ex): bool { |
|
361 | 361 | $ex = new \RuntimeException(sprintf( |
362 | 362 | 'Unable to open "%s" using mode "%s": %s', |
363 | 363 | $filename, |
@@ -404,7 +404,7 @@ discard block |
||
404 | 404 | public static function tryGetContents($stream): string |
405 | 405 | { |
406 | 406 | $ex = null; |
407 | - set_error_handler(static function (int $errno, string $errstr) use (&$ex): bool { |
|
407 | + set_error_handler(static function(int $errno, string $errstr) use (&$ex): bool { |
|
408 | 408 | $ex = new \RuntimeException(sprintf( |
409 | 409 | 'Unable to read stream contents: %s', |
410 | 410 | $errstr |
@@ -9,8 +9,7 @@ |
||
9 | 9 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
10 | 10 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\UriInterface; |
11 | 11 | |
12 | -final class Utils |
|
13 | -{ |
|
12 | +final class Utils { |
|
14 | 13 | /** |
15 | 14 | * Remove the items given by the keys, case insensitively from the data. |
16 | 15 | * |
@@ -11,147 +11,147 @@ |
||
11 | 11 | */ |
12 | 12 | final class LimitStream implements StreamInterface |
13 | 13 | { |
14 | - use StreamDecoratorTrait; |
|
15 | - |
|
16 | - /** @var int Offset to start reading from */ |
|
17 | - private $offset; |
|
18 | - |
|
19 | - /** @var int Limit the number of bytes that can be read */ |
|
20 | - private $limit; |
|
21 | - |
|
22 | - /** @var StreamInterface */ |
|
23 | - private $stream; |
|
24 | - |
|
25 | - /** |
|
26 | - * @param StreamInterface $stream Stream to wrap |
|
27 | - * @param int $limit Total number of bytes to allow to be read |
|
28 | - * from the stream. Pass -1 for no limit. |
|
29 | - * @param int $offset Position to seek to before reading (only |
|
30 | - * works on seekable streams). |
|
31 | - */ |
|
32 | - public function __construct( |
|
33 | - StreamInterface $stream, |
|
34 | - int $limit = -1, |
|
35 | - int $offset = 0 |
|
36 | - ) { |
|
37 | - $this->stream = $stream; |
|
38 | - $this->setLimit($limit); |
|
39 | - $this->setOffset($offset); |
|
40 | - } |
|
41 | - |
|
42 | - public function eof(): bool |
|
43 | - { |
|
44 | - // Always return true if the underlying stream is EOF |
|
45 | - if ($this->stream->eof()) { |
|
46 | - return true; |
|
47 | - } |
|
48 | - |
|
49 | - // No limit and the underlying stream is not at EOF |
|
50 | - if ($this->limit === -1) { |
|
51 | - return false; |
|
52 | - } |
|
53 | - |
|
54 | - return $this->stream->tell() >= $this->offset + $this->limit; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Returns the size of the limited subset of data |
|
59 | - */ |
|
60 | - public function getSize(): ?int |
|
61 | - { |
|
62 | - if (null === ($length = $this->stream->getSize())) { |
|
63 | - return null; |
|
64 | - } elseif ($this->limit === -1) { |
|
65 | - return $length - $this->offset; |
|
66 | - } else { |
|
67 | - return min($this->limit, $length - $this->offset); |
|
68 | - } |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Allow for a bounded seek on the read limited stream |
|
73 | - */ |
|
74 | - public function seek($offset, $whence = SEEK_SET): void |
|
75 | - { |
|
76 | - if ($whence !== SEEK_SET || $offset < 0) { |
|
77 | - throw new \RuntimeException(sprintf( |
|
78 | - 'Cannot seek to offset %s with whence %s', |
|
79 | - $offset, |
|
80 | - $whence |
|
81 | - )); |
|
82 | - } |
|
83 | - |
|
84 | - $offset += $this->offset; |
|
85 | - |
|
86 | - if ($this->limit !== -1) { |
|
87 | - if ($offset > $this->offset + $this->limit) { |
|
88 | - $offset = $this->offset + $this->limit; |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - $this->stream->seek($offset); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Give a relative tell() |
|
97 | - */ |
|
98 | - public function tell(): int |
|
99 | - { |
|
100 | - return $this->stream->tell() - $this->offset; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Set the offset to start limiting from |
|
105 | - * |
|
106 | - * @param int $offset Offset to seek to and begin byte limiting from |
|
107 | - * |
|
108 | - * @throws \RuntimeException if the stream cannot be seeked. |
|
109 | - */ |
|
110 | - public function setOffset(int $offset): void |
|
111 | - { |
|
112 | - $current = $this->stream->tell(); |
|
113 | - |
|
114 | - if ($current !== $offset) { |
|
115 | - // If the stream cannot seek to the offset position, then read to it |
|
116 | - if ($this->stream->isSeekable()) { |
|
117 | - $this->stream->seek($offset); |
|
118 | - } elseif ($current > $offset) { |
|
119 | - throw new \RuntimeException("Could not seek to stream offset $offset"); |
|
120 | - } else { |
|
121 | - $this->stream->read($offset - $current); |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - $this->offset = $offset; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Set the limit of bytes that the decorator allows to be read from the |
|
130 | - * stream. |
|
131 | - * |
|
132 | - * @param int $limit Number of bytes to allow to be read from the stream. |
|
133 | - * Use -1 for no limit. |
|
134 | - */ |
|
135 | - public function setLimit(int $limit): void |
|
136 | - { |
|
137 | - $this->limit = $limit; |
|
138 | - } |
|
139 | - |
|
140 | - public function read($length): string |
|
141 | - { |
|
142 | - if ($this->limit === -1) { |
|
143 | - return $this->stream->read($length); |
|
144 | - } |
|
145 | - |
|
146 | - // Check if the current position is less than the total allowed |
|
147 | - // bytes + original offset |
|
148 | - $remaining = ($this->offset + $this->limit) - $this->stream->tell(); |
|
149 | - if ($remaining > 0) { |
|
150 | - // Only return the amount of requested data, ensuring that the byte |
|
151 | - // limit is not exceeded |
|
152 | - return $this->stream->read(min($remaining, $length)); |
|
153 | - } |
|
154 | - |
|
155 | - return ''; |
|
156 | - } |
|
14 | + use StreamDecoratorTrait; |
|
15 | + |
|
16 | + /** @var int Offset to start reading from */ |
|
17 | + private $offset; |
|
18 | + |
|
19 | + /** @var int Limit the number of bytes that can be read */ |
|
20 | + private $limit; |
|
21 | + |
|
22 | + /** @var StreamInterface */ |
|
23 | + private $stream; |
|
24 | + |
|
25 | + /** |
|
26 | + * @param StreamInterface $stream Stream to wrap |
|
27 | + * @param int $limit Total number of bytes to allow to be read |
|
28 | + * from the stream. Pass -1 for no limit. |
|
29 | + * @param int $offset Position to seek to before reading (only |
|
30 | + * works on seekable streams). |
|
31 | + */ |
|
32 | + public function __construct( |
|
33 | + StreamInterface $stream, |
|
34 | + int $limit = -1, |
|
35 | + int $offset = 0 |
|
36 | + ) { |
|
37 | + $this->stream = $stream; |
|
38 | + $this->setLimit($limit); |
|
39 | + $this->setOffset($offset); |
|
40 | + } |
|
41 | + |
|
42 | + public function eof(): bool |
|
43 | + { |
|
44 | + // Always return true if the underlying stream is EOF |
|
45 | + if ($this->stream->eof()) { |
|
46 | + return true; |
|
47 | + } |
|
48 | + |
|
49 | + // No limit and the underlying stream is not at EOF |
|
50 | + if ($this->limit === -1) { |
|
51 | + return false; |
|
52 | + } |
|
53 | + |
|
54 | + return $this->stream->tell() >= $this->offset + $this->limit; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Returns the size of the limited subset of data |
|
59 | + */ |
|
60 | + public function getSize(): ?int |
|
61 | + { |
|
62 | + if (null === ($length = $this->stream->getSize())) { |
|
63 | + return null; |
|
64 | + } elseif ($this->limit === -1) { |
|
65 | + return $length - $this->offset; |
|
66 | + } else { |
|
67 | + return min($this->limit, $length - $this->offset); |
|
68 | + } |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Allow for a bounded seek on the read limited stream |
|
73 | + */ |
|
74 | + public function seek($offset, $whence = SEEK_SET): void |
|
75 | + { |
|
76 | + if ($whence !== SEEK_SET || $offset < 0) { |
|
77 | + throw new \RuntimeException(sprintf( |
|
78 | + 'Cannot seek to offset %s with whence %s', |
|
79 | + $offset, |
|
80 | + $whence |
|
81 | + )); |
|
82 | + } |
|
83 | + |
|
84 | + $offset += $this->offset; |
|
85 | + |
|
86 | + if ($this->limit !== -1) { |
|
87 | + if ($offset > $this->offset + $this->limit) { |
|
88 | + $offset = $this->offset + $this->limit; |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + $this->stream->seek($offset); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Give a relative tell() |
|
97 | + */ |
|
98 | + public function tell(): int |
|
99 | + { |
|
100 | + return $this->stream->tell() - $this->offset; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Set the offset to start limiting from |
|
105 | + * |
|
106 | + * @param int $offset Offset to seek to and begin byte limiting from |
|
107 | + * |
|
108 | + * @throws \RuntimeException if the stream cannot be seeked. |
|
109 | + */ |
|
110 | + public function setOffset(int $offset): void |
|
111 | + { |
|
112 | + $current = $this->stream->tell(); |
|
113 | + |
|
114 | + if ($current !== $offset) { |
|
115 | + // If the stream cannot seek to the offset position, then read to it |
|
116 | + if ($this->stream->isSeekable()) { |
|
117 | + $this->stream->seek($offset); |
|
118 | + } elseif ($current > $offset) { |
|
119 | + throw new \RuntimeException("Could not seek to stream offset $offset"); |
|
120 | + } else { |
|
121 | + $this->stream->read($offset - $current); |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + $this->offset = $offset; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Set the limit of bytes that the decorator allows to be read from the |
|
130 | + * stream. |
|
131 | + * |
|
132 | + * @param int $limit Number of bytes to allow to be read from the stream. |
|
133 | + * Use -1 for no limit. |
|
134 | + */ |
|
135 | + public function setLimit(int $limit): void |
|
136 | + { |
|
137 | + $this->limit = $limit; |
|
138 | + } |
|
139 | + |
|
140 | + public function read($length): string |
|
141 | + { |
|
142 | + if ($this->limit === -1) { |
|
143 | + return $this->stream->read($length); |
|
144 | + } |
|
145 | + |
|
146 | + // Check if the current position is less than the total allowed |
|
147 | + // bytes + original offset |
|
148 | + $remaining = ($this->offset + $this->limit) - $this->stream->tell(); |
|
149 | + if ($remaining > 0) { |
|
150 | + // Only return the amount of requested data, ensuring that the byte |
|
151 | + // limit is not exceeded |
|
152 | + return $this->stream->read(min($remaining, $length)); |
|
153 | + } |
|
154 | + |
|
155 | + return ''; |
|
156 | + } |
|
157 | 157 | } |
@@ -9,8 +9,7 @@ |
||
9 | 9 | /** |
10 | 10 | * Decorator used to return only a subset of a stream. |
11 | 11 | */ |
12 | -final class LimitStream implements StreamInterface |
|
13 | -{ |
|
12 | +final class LimitStream implements StreamInterface { |
|
14 | 13 | use StreamDecoratorTrait; |
15 | 14 | |
16 | 15 | /** @var int Offset to start reading from */ |
@@ -12,150 +12,150 @@ |
||
12 | 12 | */ |
13 | 13 | class Response implements ResponseInterface |
14 | 14 | { |
15 | - use MessageTrait; |
|
16 | - |
|
17 | - /** Map of standard HTTP status code/reason phrases */ |
|
18 | - private const PHRASES = [ |
|
19 | - 100 => 'Continue', |
|
20 | - 101 => 'Switching Protocols', |
|
21 | - 102 => 'Processing', |
|
22 | - 200 => 'OK', |
|
23 | - 201 => 'Created', |
|
24 | - 202 => 'Accepted', |
|
25 | - 203 => 'Non-Authoritative Information', |
|
26 | - 204 => 'No Content', |
|
27 | - 205 => 'Reset Content', |
|
28 | - 206 => 'Partial Content', |
|
29 | - 207 => 'Multi-status', |
|
30 | - 208 => 'Already Reported', |
|
31 | - 300 => 'Multiple Choices', |
|
32 | - 301 => 'Moved Permanently', |
|
33 | - 302 => 'Found', |
|
34 | - 303 => 'See Other', |
|
35 | - 304 => 'Not Modified', |
|
36 | - 305 => 'Use Proxy', |
|
37 | - 306 => 'Switch Proxy', |
|
38 | - 307 => 'Temporary Redirect', |
|
39 | - 308 => 'Permanent Redirect', |
|
40 | - 400 => 'Bad Request', |
|
41 | - 401 => 'Unauthorized', |
|
42 | - 402 => 'Payment Required', |
|
43 | - 403 => 'Forbidden', |
|
44 | - 404 => 'Not Found', |
|
45 | - 405 => 'Method Not Allowed', |
|
46 | - 406 => 'Not Acceptable', |
|
47 | - 407 => 'Proxy Authentication Required', |
|
48 | - 408 => 'Request Time-out', |
|
49 | - 409 => 'Conflict', |
|
50 | - 410 => 'Gone', |
|
51 | - 411 => 'Length Required', |
|
52 | - 412 => 'Precondition Failed', |
|
53 | - 413 => 'Request Entity Too Large', |
|
54 | - 414 => 'Request-URI Too Large', |
|
55 | - 415 => 'Unsupported Media Type', |
|
56 | - 416 => 'Requested range not satisfiable', |
|
57 | - 417 => 'Expectation Failed', |
|
58 | - 418 => 'I\'m a teapot', |
|
59 | - 422 => 'Unprocessable Entity', |
|
60 | - 423 => 'Locked', |
|
61 | - 424 => 'Failed Dependency', |
|
62 | - 425 => 'Unordered Collection', |
|
63 | - 426 => 'Upgrade Required', |
|
64 | - 428 => 'Precondition Required', |
|
65 | - 429 => 'Too Many Requests', |
|
66 | - 431 => 'Request Header Fields Too Large', |
|
67 | - 451 => 'Unavailable For Legal Reasons', |
|
68 | - 500 => 'Internal Server Error', |
|
69 | - 501 => 'Not Implemented', |
|
70 | - 502 => 'Bad Gateway', |
|
71 | - 503 => 'Service Unavailable', |
|
72 | - 504 => 'Gateway Time-out', |
|
73 | - 505 => 'HTTP Version not supported', |
|
74 | - 506 => 'Variant Also Negotiates', |
|
75 | - 507 => 'Insufficient Storage', |
|
76 | - 508 => 'Loop Detected', |
|
77 | - 510 => 'Not Extended', |
|
78 | - 511 => 'Network Authentication Required', |
|
79 | - ]; |
|
80 | - |
|
81 | - /** @var string */ |
|
82 | - private $reasonPhrase; |
|
83 | - |
|
84 | - /** @var int */ |
|
85 | - private $statusCode; |
|
86 | - |
|
87 | - /** |
|
88 | - * @param int $status Status code |
|
89 | - * @param (string|string[])[] $headers Response headers |
|
90 | - * @param string|resource|StreamInterface|null $body Response body |
|
91 | - * @param string $version Protocol version |
|
92 | - * @param string|null $reason Reason phrase (when empty a default will be used based on the status code) |
|
93 | - */ |
|
94 | - public function __construct( |
|
95 | - int $status = 200, |
|
96 | - array $headers = [], |
|
97 | - $body = null, |
|
98 | - string $version = '1.1', |
|
99 | - string $reason = null |
|
100 | - ) { |
|
101 | - $this->assertStatusCodeRange($status); |
|
102 | - |
|
103 | - $this->statusCode = $status; |
|
104 | - |
|
105 | - if ($body !== '' && $body !== null) { |
|
106 | - $this->stream = Utils::streamFor($body); |
|
107 | - } |
|
108 | - |
|
109 | - $this->setHeaders($headers); |
|
110 | - if ($reason == '' && isset(self::PHRASES[$this->statusCode])) { |
|
111 | - $this->reasonPhrase = self::PHRASES[$this->statusCode]; |
|
112 | - } else { |
|
113 | - $this->reasonPhrase = (string) $reason; |
|
114 | - } |
|
115 | - |
|
116 | - $this->protocol = $version; |
|
117 | - } |
|
118 | - |
|
119 | - public function getStatusCode(): int |
|
120 | - { |
|
121 | - return $this->statusCode; |
|
122 | - } |
|
123 | - |
|
124 | - public function getReasonPhrase(): string |
|
125 | - { |
|
126 | - return $this->reasonPhrase; |
|
127 | - } |
|
128 | - |
|
129 | - public function withStatus($code, $reasonPhrase = ''): ResponseInterface |
|
130 | - { |
|
131 | - $this->assertStatusCodeIsInteger($code); |
|
132 | - $code = (int) $code; |
|
133 | - $this->assertStatusCodeRange($code); |
|
134 | - |
|
135 | - $new = clone $this; |
|
136 | - $new->statusCode = $code; |
|
137 | - if ($reasonPhrase == '' && isset(self::PHRASES[$new->statusCode])) { |
|
138 | - $reasonPhrase = self::PHRASES[$new->statusCode]; |
|
139 | - } |
|
140 | - $new->reasonPhrase = (string) $reasonPhrase; |
|
141 | - |
|
142 | - return $new; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @param mixed $statusCode |
|
147 | - */ |
|
148 | - private function assertStatusCodeIsInteger($statusCode): void |
|
149 | - { |
|
150 | - if (filter_var($statusCode, FILTER_VALIDATE_INT) === false) { |
|
151 | - throw new \InvalidArgumentException('Status code must be an integer value.'); |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - private function assertStatusCodeRange(int $statusCode): void |
|
156 | - { |
|
157 | - if ($statusCode < 100 || $statusCode >= 600) { |
|
158 | - throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 5xx.'); |
|
159 | - } |
|
160 | - } |
|
15 | + use MessageTrait; |
|
16 | + |
|
17 | + /** Map of standard HTTP status code/reason phrases */ |
|
18 | + private const PHRASES = [ |
|
19 | + 100 => 'Continue', |
|
20 | + 101 => 'Switching Protocols', |
|
21 | + 102 => 'Processing', |
|
22 | + 200 => 'OK', |
|
23 | + 201 => 'Created', |
|
24 | + 202 => 'Accepted', |
|
25 | + 203 => 'Non-Authoritative Information', |
|
26 | + 204 => 'No Content', |
|
27 | + 205 => 'Reset Content', |
|
28 | + 206 => 'Partial Content', |
|
29 | + 207 => 'Multi-status', |
|
30 | + 208 => 'Already Reported', |
|
31 | + 300 => 'Multiple Choices', |
|
32 | + 301 => 'Moved Permanently', |
|
33 | + 302 => 'Found', |
|
34 | + 303 => 'See Other', |
|
35 | + 304 => 'Not Modified', |
|
36 | + 305 => 'Use Proxy', |
|
37 | + 306 => 'Switch Proxy', |
|
38 | + 307 => 'Temporary Redirect', |
|
39 | + 308 => 'Permanent Redirect', |
|
40 | + 400 => 'Bad Request', |
|
41 | + 401 => 'Unauthorized', |
|
42 | + 402 => 'Payment Required', |
|
43 | + 403 => 'Forbidden', |
|
44 | + 404 => 'Not Found', |
|
45 | + 405 => 'Method Not Allowed', |
|
46 | + 406 => 'Not Acceptable', |
|
47 | + 407 => 'Proxy Authentication Required', |
|
48 | + 408 => 'Request Time-out', |
|
49 | + 409 => 'Conflict', |
|
50 | + 410 => 'Gone', |
|
51 | + 411 => 'Length Required', |
|
52 | + 412 => 'Precondition Failed', |
|
53 | + 413 => 'Request Entity Too Large', |
|
54 | + 414 => 'Request-URI Too Large', |
|
55 | + 415 => 'Unsupported Media Type', |
|
56 | + 416 => 'Requested range not satisfiable', |
|
57 | + 417 => 'Expectation Failed', |
|
58 | + 418 => 'I\'m a teapot', |
|
59 | + 422 => 'Unprocessable Entity', |
|
60 | + 423 => 'Locked', |
|
61 | + 424 => 'Failed Dependency', |
|
62 | + 425 => 'Unordered Collection', |
|
63 | + 426 => 'Upgrade Required', |
|
64 | + 428 => 'Precondition Required', |
|
65 | + 429 => 'Too Many Requests', |
|
66 | + 431 => 'Request Header Fields Too Large', |
|
67 | + 451 => 'Unavailable For Legal Reasons', |
|
68 | + 500 => 'Internal Server Error', |
|
69 | + 501 => 'Not Implemented', |
|
70 | + 502 => 'Bad Gateway', |
|
71 | + 503 => 'Service Unavailable', |
|
72 | + 504 => 'Gateway Time-out', |
|
73 | + 505 => 'HTTP Version not supported', |
|
74 | + 506 => 'Variant Also Negotiates', |
|
75 | + 507 => 'Insufficient Storage', |
|
76 | + 508 => 'Loop Detected', |
|
77 | + 510 => 'Not Extended', |
|
78 | + 511 => 'Network Authentication Required', |
|
79 | + ]; |
|
80 | + |
|
81 | + /** @var string */ |
|
82 | + private $reasonPhrase; |
|
83 | + |
|
84 | + /** @var int */ |
|
85 | + private $statusCode; |
|
86 | + |
|
87 | + /** |
|
88 | + * @param int $status Status code |
|
89 | + * @param (string|string[])[] $headers Response headers |
|
90 | + * @param string|resource|StreamInterface|null $body Response body |
|
91 | + * @param string $version Protocol version |
|
92 | + * @param string|null $reason Reason phrase (when empty a default will be used based on the status code) |
|
93 | + */ |
|
94 | + public function __construct( |
|
95 | + int $status = 200, |
|
96 | + array $headers = [], |
|
97 | + $body = null, |
|
98 | + string $version = '1.1', |
|
99 | + string $reason = null |
|
100 | + ) { |
|
101 | + $this->assertStatusCodeRange($status); |
|
102 | + |
|
103 | + $this->statusCode = $status; |
|
104 | + |
|
105 | + if ($body !== '' && $body !== null) { |
|
106 | + $this->stream = Utils::streamFor($body); |
|
107 | + } |
|
108 | + |
|
109 | + $this->setHeaders($headers); |
|
110 | + if ($reason == '' && isset(self::PHRASES[$this->statusCode])) { |
|
111 | + $this->reasonPhrase = self::PHRASES[$this->statusCode]; |
|
112 | + } else { |
|
113 | + $this->reasonPhrase = (string) $reason; |
|
114 | + } |
|
115 | + |
|
116 | + $this->protocol = $version; |
|
117 | + } |
|
118 | + |
|
119 | + public function getStatusCode(): int |
|
120 | + { |
|
121 | + return $this->statusCode; |
|
122 | + } |
|
123 | + |
|
124 | + public function getReasonPhrase(): string |
|
125 | + { |
|
126 | + return $this->reasonPhrase; |
|
127 | + } |
|
128 | + |
|
129 | + public function withStatus($code, $reasonPhrase = ''): ResponseInterface |
|
130 | + { |
|
131 | + $this->assertStatusCodeIsInteger($code); |
|
132 | + $code = (int) $code; |
|
133 | + $this->assertStatusCodeRange($code); |
|
134 | + |
|
135 | + $new = clone $this; |
|
136 | + $new->statusCode = $code; |
|
137 | + if ($reasonPhrase == '' && isset(self::PHRASES[$new->statusCode])) { |
|
138 | + $reasonPhrase = self::PHRASES[$new->statusCode]; |
|
139 | + } |
|
140 | + $new->reasonPhrase = (string) $reasonPhrase; |
|
141 | + |
|
142 | + return $new; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @param mixed $statusCode |
|
147 | + */ |
|
148 | + private function assertStatusCodeIsInteger($statusCode): void |
|
149 | + { |
|
150 | + if (filter_var($statusCode, FILTER_VALIDATE_INT) === false) { |
|
151 | + throw new \InvalidArgumentException('Status code must be an integer value.'); |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + private function assertStatusCodeRange(int $statusCode): void |
|
156 | + { |
|
157 | + if ($statusCode < 100 || $statusCode >= 600) { |
|
158 | + throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 5xx.'); |
|
159 | + } |
|
160 | + } |
|
161 | 161 | } |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | if ($reason == '' && isset(self::PHRASES[$this->statusCode])) { |
111 | 111 | $this->reasonPhrase = self::PHRASES[$this->statusCode]; |
112 | 112 | } else { |
113 | - $this->reasonPhrase = (string) $reason; |
|
113 | + $this->reasonPhrase = (string)$reason; |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | $this->protocol = $version; |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | public function withStatus($code, $reasonPhrase = ''): ResponseInterface |
130 | 130 | { |
131 | 131 | $this->assertStatusCodeIsInteger($code); |
132 | - $code = (int) $code; |
|
132 | + $code = (int)$code; |
|
133 | 133 | $this->assertStatusCodeRange($code); |
134 | 134 | |
135 | 135 | $new = clone $this; |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | if ($reasonPhrase == '' && isset(self::PHRASES[$new->statusCode])) { |
138 | 138 | $reasonPhrase = self::PHRASES[$new->statusCode]; |
139 | 139 | } |
140 | - $new->reasonPhrase = (string) $reasonPhrase; |
|
140 | + $new->reasonPhrase = (string)$reasonPhrase; |
|
141 | 141 | |
142 | 142 | return $new; |
143 | 143 | } |
@@ -10,8 +10,7 @@ |
||
10 | 10 | /** |
11 | 11 | * PSR-7 response implementation. |
12 | 12 | */ |
13 | -class Response implements ResponseInterface |
|
14 | -{ |
|
13 | +class Response implements ResponseInterface { |
|
15 | 14 | use MessageTrait; |
16 | 15 | |
17 | 16 | /** Map of standard HTTP status code/reason phrases */ |
@@ -13,144 +13,144 @@ |
||
13 | 13 | */ |
14 | 14 | trait StreamDecoratorTrait |
15 | 15 | { |
16 | - /** |
|
17 | - * @param StreamInterface $stream Stream to decorate |
|
18 | - */ |
|
19 | - public function __construct(StreamInterface $stream) |
|
20 | - { |
|
21 | - $this->stream = $stream; |
|
22 | - } |
|
23 | - |
|
24 | - /** |
|
25 | - * Magic method used to create a new stream if streams are not added in |
|
26 | - * the constructor of a decorator (e.g., LazyOpenStream). |
|
27 | - * |
|
28 | - * @return StreamInterface |
|
29 | - */ |
|
30 | - public function __get(string $name) |
|
31 | - { |
|
32 | - if ($name === 'stream') { |
|
33 | - $this->stream = $this->createStream(); |
|
34 | - |
|
35 | - return $this->stream; |
|
36 | - } |
|
37 | - |
|
38 | - throw new \UnexpectedValueException("$name not found on class"); |
|
39 | - } |
|
40 | - |
|
41 | - public function __toString(): string |
|
42 | - { |
|
43 | - try { |
|
44 | - if ($this->isSeekable()) { |
|
45 | - $this->seek(0); |
|
46 | - } |
|
47 | - |
|
48 | - return $this->getContents(); |
|
49 | - } catch (\Throwable $e) { |
|
50 | - if (\PHP_VERSION_ID >= 70400) { |
|
51 | - throw $e; |
|
52 | - } |
|
53 | - trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
54 | - |
|
55 | - return ''; |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - public function getContents(): string |
|
60 | - { |
|
61 | - return Utils::copyToString($this); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Allow decorators to implement custom methods |
|
66 | - * |
|
67 | - * @return mixed |
|
68 | - */ |
|
69 | - public function __call(string $method, array $args) |
|
70 | - { |
|
71 | - /** @var callable $callable */ |
|
72 | - $callable = [$this->stream, $method]; |
|
73 | - $result = ($callable)(...$args); |
|
74 | - |
|
75 | - // Always return the wrapped object if the result is a return $this |
|
76 | - return $result === $this->stream ? $this : $result; |
|
77 | - } |
|
78 | - |
|
79 | - public function close(): void |
|
80 | - { |
|
81 | - $this->stream->close(); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @return mixed |
|
86 | - */ |
|
87 | - public function getMetadata($key = null) |
|
88 | - { |
|
89 | - return $this->stream->getMetadata($key); |
|
90 | - } |
|
91 | - |
|
92 | - public function detach() |
|
93 | - { |
|
94 | - return $this->stream->detach(); |
|
95 | - } |
|
96 | - |
|
97 | - public function getSize(): ?int |
|
98 | - { |
|
99 | - return $this->stream->getSize(); |
|
100 | - } |
|
101 | - |
|
102 | - public function eof(): bool |
|
103 | - { |
|
104 | - return $this->stream->eof(); |
|
105 | - } |
|
106 | - |
|
107 | - public function tell(): int |
|
108 | - { |
|
109 | - return $this->stream->tell(); |
|
110 | - } |
|
111 | - |
|
112 | - public function isReadable(): bool |
|
113 | - { |
|
114 | - return $this->stream->isReadable(); |
|
115 | - } |
|
116 | - |
|
117 | - public function isWritable(): bool |
|
118 | - { |
|
119 | - return $this->stream->isWritable(); |
|
120 | - } |
|
121 | - |
|
122 | - public function isSeekable(): bool |
|
123 | - { |
|
124 | - return $this->stream->isSeekable(); |
|
125 | - } |
|
126 | - |
|
127 | - public function rewind(): void |
|
128 | - { |
|
129 | - $this->seek(0); |
|
130 | - } |
|
131 | - |
|
132 | - public function seek($offset, $whence = SEEK_SET): void |
|
133 | - { |
|
134 | - $this->stream->seek($offset, $whence); |
|
135 | - } |
|
136 | - |
|
137 | - public function read($length): string |
|
138 | - { |
|
139 | - return $this->stream->read($length); |
|
140 | - } |
|
141 | - |
|
142 | - public function write($string): int |
|
143 | - { |
|
144 | - return $this->stream->write($string); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Implement in subclasses to dynamically create streams when requested. |
|
149 | - * |
|
150 | - * @throws \BadMethodCallException |
|
151 | - */ |
|
152 | - protected function createStream(): StreamInterface |
|
153 | - { |
|
154 | - throw new \BadMethodCallException('Not implemented'); |
|
155 | - } |
|
16 | + /** |
|
17 | + * @param StreamInterface $stream Stream to decorate |
|
18 | + */ |
|
19 | + public function __construct(StreamInterface $stream) |
|
20 | + { |
|
21 | + $this->stream = $stream; |
|
22 | + } |
|
23 | + |
|
24 | + /** |
|
25 | + * Magic method used to create a new stream if streams are not added in |
|
26 | + * the constructor of a decorator (e.g., LazyOpenStream). |
|
27 | + * |
|
28 | + * @return StreamInterface |
|
29 | + */ |
|
30 | + public function __get(string $name) |
|
31 | + { |
|
32 | + if ($name === 'stream') { |
|
33 | + $this->stream = $this->createStream(); |
|
34 | + |
|
35 | + return $this->stream; |
|
36 | + } |
|
37 | + |
|
38 | + throw new \UnexpectedValueException("$name not found on class"); |
|
39 | + } |
|
40 | + |
|
41 | + public function __toString(): string |
|
42 | + { |
|
43 | + try { |
|
44 | + if ($this->isSeekable()) { |
|
45 | + $this->seek(0); |
|
46 | + } |
|
47 | + |
|
48 | + return $this->getContents(); |
|
49 | + } catch (\Throwable $e) { |
|
50 | + if (\PHP_VERSION_ID >= 70400) { |
|
51 | + throw $e; |
|
52 | + } |
|
53 | + trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
54 | + |
|
55 | + return ''; |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + public function getContents(): string |
|
60 | + { |
|
61 | + return Utils::copyToString($this); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Allow decorators to implement custom methods |
|
66 | + * |
|
67 | + * @return mixed |
|
68 | + */ |
|
69 | + public function __call(string $method, array $args) |
|
70 | + { |
|
71 | + /** @var callable $callable */ |
|
72 | + $callable = [$this->stream, $method]; |
|
73 | + $result = ($callable)(...$args); |
|
74 | + |
|
75 | + // Always return the wrapped object if the result is a return $this |
|
76 | + return $result === $this->stream ? $this : $result; |
|
77 | + } |
|
78 | + |
|
79 | + public function close(): void |
|
80 | + { |
|
81 | + $this->stream->close(); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @return mixed |
|
86 | + */ |
|
87 | + public function getMetadata($key = null) |
|
88 | + { |
|
89 | + return $this->stream->getMetadata($key); |
|
90 | + } |
|
91 | + |
|
92 | + public function detach() |
|
93 | + { |
|
94 | + return $this->stream->detach(); |
|
95 | + } |
|
96 | + |
|
97 | + public function getSize(): ?int |
|
98 | + { |
|
99 | + return $this->stream->getSize(); |
|
100 | + } |
|
101 | + |
|
102 | + public function eof(): bool |
|
103 | + { |
|
104 | + return $this->stream->eof(); |
|
105 | + } |
|
106 | + |
|
107 | + public function tell(): int |
|
108 | + { |
|
109 | + return $this->stream->tell(); |
|
110 | + } |
|
111 | + |
|
112 | + public function isReadable(): bool |
|
113 | + { |
|
114 | + return $this->stream->isReadable(); |
|
115 | + } |
|
116 | + |
|
117 | + public function isWritable(): bool |
|
118 | + { |
|
119 | + return $this->stream->isWritable(); |
|
120 | + } |
|
121 | + |
|
122 | + public function isSeekable(): bool |
|
123 | + { |
|
124 | + return $this->stream->isSeekable(); |
|
125 | + } |
|
126 | + |
|
127 | + public function rewind(): void |
|
128 | + { |
|
129 | + $this->seek(0); |
|
130 | + } |
|
131 | + |
|
132 | + public function seek($offset, $whence = SEEK_SET): void |
|
133 | + { |
|
134 | + $this->stream->seek($offset, $whence); |
|
135 | + } |
|
136 | + |
|
137 | + public function read($length): string |
|
138 | + { |
|
139 | + return $this->stream->read($length); |
|
140 | + } |
|
141 | + |
|
142 | + public function write($string): int |
|
143 | + { |
|
144 | + return $this->stream->write($string); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Implement in subclasses to dynamically create streams when requested. |
|
149 | + * |
|
150 | + * @throws \BadMethodCallException |
|
151 | + */ |
|
152 | + protected function createStream(): StreamInterface |
|
153 | + { |
|
154 | + throw new \BadMethodCallException('Not implemented'); |
|
155 | + } |
|
156 | 156 | } |
@@ -50,7 +50,7 @@ |
||
50 | 50 | if (\PHP_VERSION_ID >= 70400) { |
51 | 51 | throw $e; |
52 | 52 | } |
53 | - trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
53 | + trigger_error(sprintf('%s::__toString exception: %s', self::class, (string)$e), E_USER_ERROR); |
|
54 | 54 | |
55 | 55 | return ''; |
56 | 56 | } |
@@ -11,8 +11,7 @@ |
||
11 | 11 | * |
12 | 12 | * @property StreamInterface $stream |
13 | 13 | */ |
14 | -trait StreamDecoratorTrait |
|
15 | -{ |
|
14 | +trait StreamDecoratorTrait { |
|
16 | 15 | /** |
17 | 16 | * @param StreamInterface $stream Stream to decorate |
18 | 17 | */ |
@@ -12,254 +12,254 @@ |
||
12 | 12 | */ |
13 | 13 | trait MessageTrait |
14 | 14 | { |
15 | - /** @var string[][] Map of all registered headers, as original name => array of values */ |
|
16 | - private $headers = []; |
|
17 | - |
|
18 | - /** @var string[] Map of lowercase header name => original name at registration */ |
|
19 | - private $headerNames = []; |
|
20 | - |
|
21 | - /** @var string */ |
|
22 | - private $protocol = '1.1'; |
|
23 | - |
|
24 | - /** @var StreamInterface|null */ |
|
25 | - private $stream; |
|
26 | - |
|
27 | - public function getProtocolVersion(): string |
|
28 | - { |
|
29 | - return $this->protocol; |
|
30 | - } |
|
31 | - |
|
32 | - public function withProtocolVersion($version): MessageInterface |
|
33 | - { |
|
34 | - if ($this->protocol === $version) { |
|
35 | - return $this; |
|
36 | - } |
|
37 | - |
|
38 | - $new = clone $this; |
|
39 | - $new->protocol = $version; |
|
40 | - |
|
41 | - return $new; |
|
42 | - } |
|
43 | - |
|
44 | - public function getHeaders(): array |
|
45 | - { |
|
46 | - return $this->headers; |
|
47 | - } |
|
48 | - |
|
49 | - public function hasHeader($header): bool |
|
50 | - { |
|
51 | - return isset($this->headerNames[strtolower($header)]); |
|
52 | - } |
|
53 | - |
|
54 | - public function getHeader($header): array |
|
55 | - { |
|
56 | - $header = strtolower($header); |
|
57 | - |
|
58 | - if (!isset($this->headerNames[$header])) { |
|
59 | - return []; |
|
60 | - } |
|
61 | - |
|
62 | - $header = $this->headerNames[$header]; |
|
63 | - |
|
64 | - return $this->headers[$header]; |
|
65 | - } |
|
66 | - |
|
67 | - public function getHeaderLine($header): string |
|
68 | - { |
|
69 | - return implode(', ', $this->getHeader($header)); |
|
70 | - } |
|
71 | - |
|
72 | - public function withHeader($header, $value): MessageInterface |
|
73 | - { |
|
74 | - $this->assertHeader($header); |
|
75 | - $value = $this->normalizeHeaderValue($value); |
|
76 | - $normalized = strtolower($header); |
|
77 | - |
|
78 | - $new = clone $this; |
|
79 | - if (isset($new->headerNames[$normalized])) { |
|
80 | - unset($new->headers[$new->headerNames[$normalized]]); |
|
81 | - } |
|
82 | - $new->headerNames[$normalized] = $header; |
|
83 | - $new->headers[$header] = $value; |
|
84 | - |
|
85 | - return $new; |
|
86 | - } |
|
87 | - |
|
88 | - public function withAddedHeader($header, $value): MessageInterface |
|
89 | - { |
|
90 | - $this->assertHeader($header); |
|
91 | - $value = $this->normalizeHeaderValue($value); |
|
92 | - $normalized = strtolower($header); |
|
93 | - |
|
94 | - $new = clone $this; |
|
95 | - if (isset($new->headerNames[$normalized])) { |
|
96 | - $header = $this->headerNames[$normalized]; |
|
97 | - $new->headers[$header] = array_merge($this->headers[$header], $value); |
|
98 | - } else { |
|
99 | - $new->headerNames[$normalized] = $header; |
|
100 | - $new->headers[$header] = $value; |
|
101 | - } |
|
102 | - |
|
103 | - return $new; |
|
104 | - } |
|
105 | - |
|
106 | - public function withoutHeader($header): MessageInterface |
|
107 | - { |
|
108 | - $normalized = strtolower($header); |
|
109 | - |
|
110 | - if (!isset($this->headerNames[$normalized])) { |
|
111 | - return $this; |
|
112 | - } |
|
113 | - |
|
114 | - $header = $this->headerNames[$normalized]; |
|
115 | - |
|
116 | - $new = clone $this; |
|
117 | - unset($new->headers[$header], $new->headerNames[$normalized]); |
|
118 | - |
|
119 | - return $new; |
|
120 | - } |
|
121 | - |
|
122 | - public function getBody(): StreamInterface |
|
123 | - { |
|
124 | - if (!$this->stream) { |
|
125 | - $this->stream = Utils::streamFor(''); |
|
126 | - } |
|
127 | - |
|
128 | - return $this->stream; |
|
129 | - } |
|
130 | - |
|
131 | - public function withBody(StreamInterface $body): MessageInterface |
|
132 | - { |
|
133 | - if ($body === $this->stream) { |
|
134 | - return $this; |
|
135 | - } |
|
136 | - |
|
137 | - $new = clone $this; |
|
138 | - $new->stream = $body; |
|
139 | - |
|
140 | - return $new; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * @param (string|string[])[] $headers |
|
145 | - */ |
|
146 | - private function setHeaders(array $headers): void |
|
147 | - { |
|
148 | - $this->headerNames = $this->headers = []; |
|
149 | - foreach ($headers as $header => $value) { |
|
150 | - // Numeric array keys are converted to int by PHP. |
|
151 | - $header = (string) $header; |
|
152 | - |
|
153 | - $this->assertHeader($header); |
|
154 | - $value = $this->normalizeHeaderValue($value); |
|
155 | - $normalized = strtolower($header); |
|
156 | - if (isset($this->headerNames[$normalized])) { |
|
157 | - $header = $this->headerNames[$normalized]; |
|
158 | - $this->headers[$header] = array_merge($this->headers[$header], $value); |
|
159 | - } else { |
|
160 | - $this->headerNames[$normalized] = $header; |
|
161 | - $this->headers[$header] = $value; |
|
162 | - } |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @param mixed $value |
|
168 | - * |
|
169 | - * @return string[] |
|
170 | - */ |
|
171 | - private function normalizeHeaderValue($value): array |
|
172 | - { |
|
173 | - if (!is_array($value)) { |
|
174 | - return $this->trimAndValidateHeaderValues([$value]); |
|
175 | - } |
|
176 | - |
|
177 | - if (count($value) === 0) { |
|
178 | - throw new \InvalidArgumentException('Header value can not be an empty array.'); |
|
179 | - } |
|
180 | - |
|
181 | - return $this->trimAndValidateHeaderValues($value); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Trims whitespace from the header values. |
|
186 | - * |
|
187 | - * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. |
|
188 | - * |
|
189 | - * header-field = field-name ":" OWS field-value OWS |
|
190 | - * OWS = *( SP / HTAB ) |
|
191 | - * |
|
192 | - * @param mixed[] $values Header values |
|
193 | - * |
|
194 | - * @return string[] Trimmed header values |
|
195 | - * |
|
196 | - * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 |
|
197 | - */ |
|
198 | - private function trimAndValidateHeaderValues(array $values): array |
|
199 | - { |
|
200 | - return array_map(function ($value) { |
|
201 | - if (!is_scalar($value) && null !== $value) { |
|
202 | - throw new \InvalidArgumentException(sprintf( |
|
203 | - 'Header value must be scalar or null but %s provided.', |
|
204 | - is_object($value) ? get_class($value) : gettype($value) |
|
205 | - )); |
|
206 | - } |
|
207 | - |
|
208 | - $trimmed = trim((string) $value, " \t"); |
|
209 | - $this->assertValue($trimmed); |
|
210 | - |
|
211 | - return $trimmed; |
|
212 | - }, array_values($values)); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 |
|
217 | - * |
|
218 | - * @param mixed $header |
|
219 | - */ |
|
220 | - private function assertHeader($header): void |
|
221 | - { |
|
222 | - if (!is_string($header)) { |
|
223 | - throw new \InvalidArgumentException(sprintf( |
|
224 | - 'Header name must be a string but %s provided.', |
|
225 | - is_object($header) ? get_class($header) : gettype($header) |
|
226 | - )); |
|
227 | - } |
|
228 | - |
|
229 | - if (!preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $header)) { |
|
230 | - throw new \InvalidArgumentException( |
|
231 | - sprintf('"%s" is not valid header name.', $header) |
|
232 | - ); |
|
233 | - } |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 |
|
238 | - * |
|
239 | - * field-value = *( field-content / obs-fold ) |
|
240 | - * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
|
241 | - * field-vchar = VCHAR / obs-text |
|
242 | - * VCHAR = %x21-7E |
|
243 | - * obs-text = %x80-FF |
|
244 | - * obs-fold = CRLF 1*( SP / HTAB ) |
|
245 | - */ |
|
246 | - private function assertValue(string $value): void |
|
247 | - { |
|
248 | - // The regular expression intentionally does not support the obs-fold production, because as |
|
249 | - // per RFC 7230#3.2.4: |
|
250 | - // |
|
251 | - // A sender MUST NOT generate a message that includes |
|
252 | - // line folding (i.e., that has any field-value that contains a match to |
|
253 | - // the obs-fold rule) unless the message is intended for packaging |
|
254 | - // within the message/http media type. |
|
255 | - // |
|
256 | - // Clients must not send a request with line folding and a server sending folded headers is |
|
257 | - // likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting |
|
258 | - // folding is not likely to break any legitimate use case. |
|
259 | - if (!preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D', $value)) { |
|
260 | - throw new \InvalidArgumentException( |
|
261 | - sprintf('"%s" is not valid header value.', $value) |
|
262 | - ); |
|
263 | - } |
|
264 | - } |
|
15 | + /** @var string[][] Map of all registered headers, as original name => array of values */ |
|
16 | + private $headers = []; |
|
17 | + |
|
18 | + /** @var string[] Map of lowercase header name => original name at registration */ |
|
19 | + private $headerNames = []; |
|
20 | + |
|
21 | + /** @var string */ |
|
22 | + private $protocol = '1.1'; |
|
23 | + |
|
24 | + /** @var StreamInterface|null */ |
|
25 | + private $stream; |
|
26 | + |
|
27 | + public function getProtocolVersion(): string |
|
28 | + { |
|
29 | + return $this->protocol; |
|
30 | + } |
|
31 | + |
|
32 | + public function withProtocolVersion($version): MessageInterface |
|
33 | + { |
|
34 | + if ($this->protocol === $version) { |
|
35 | + return $this; |
|
36 | + } |
|
37 | + |
|
38 | + $new = clone $this; |
|
39 | + $new->protocol = $version; |
|
40 | + |
|
41 | + return $new; |
|
42 | + } |
|
43 | + |
|
44 | + public function getHeaders(): array |
|
45 | + { |
|
46 | + return $this->headers; |
|
47 | + } |
|
48 | + |
|
49 | + public function hasHeader($header): bool |
|
50 | + { |
|
51 | + return isset($this->headerNames[strtolower($header)]); |
|
52 | + } |
|
53 | + |
|
54 | + public function getHeader($header): array |
|
55 | + { |
|
56 | + $header = strtolower($header); |
|
57 | + |
|
58 | + if (!isset($this->headerNames[$header])) { |
|
59 | + return []; |
|
60 | + } |
|
61 | + |
|
62 | + $header = $this->headerNames[$header]; |
|
63 | + |
|
64 | + return $this->headers[$header]; |
|
65 | + } |
|
66 | + |
|
67 | + public function getHeaderLine($header): string |
|
68 | + { |
|
69 | + return implode(', ', $this->getHeader($header)); |
|
70 | + } |
|
71 | + |
|
72 | + public function withHeader($header, $value): MessageInterface |
|
73 | + { |
|
74 | + $this->assertHeader($header); |
|
75 | + $value = $this->normalizeHeaderValue($value); |
|
76 | + $normalized = strtolower($header); |
|
77 | + |
|
78 | + $new = clone $this; |
|
79 | + if (isset($new->headerNames[$normalized])) { |
|
80 | + unset($new->headers[$new->headerNames[$normalized]]); |
|
81 | + } |
|
82 | + $new->headerNames[$normalized] = $header; |
|
83 | + $new->headers[$header] = $value; |
|
84 | + |
|
85 | + return $new; |
|
86 | + } |
|
87 | + |
|
88 | + public function withAddedHeader($header, $value): MessageInterface |
|
89 | + { |
|
90 | + $this->assertHeader($header); |
|
91 | + $value = $this->normalizeHeaderValue($value); |
|
92 | + $normalized = strtolower($header); |
|
93 | + |
|
94 | + $new = clone $this; |
|
95 | + if (isset($new->headerNames[$normalized])) { |
|
96 | + $header = $this->headerNames[$normalized]; |
|
97 | + $new->headers[$header] = array_merge($this->headers[$header], $value); |
|
98 | + } else { |
|
99 | + $new->headerNames[$normalized] = $header; |
|
100 | + $new->headers[$header] = $value; |
|
101 | + } |
|
102 | + |
|
103 | + return $new; |
|
104 | + } |
|
105 | + |
|
106 | + public function withoutHeader($header): MessageInterface |
|
107 | + { |
|
108 | + $normalized = strtolower($header); |
|
109 | + |
|
110 | + if (!isset($this->headerNames[$normalized])) { |
|
111 | + return $this; |
|
112 | + } |
|
113 | + |
|
114 | + $header = $this->headerNames[$normalized]; |
|
115 | + |
|
116 | + $new = clone $this; |
|
117 | + unset($new->headers[$header], $new->headerNames[$normalized]); |
|
118 | + |
|
119 | + return $new; |
|
120 | + } |
|
121 | + |
|
122 | + public function getBody(): StreamInterface |
|
123 | + { |
|
124 | + if (!$this->stream) { |
|
125 | + $this->stream = Utils::streamFor(''); |
|
126 | + } |
|
127 | + |
|
128 | + return $this->stream; |
|
129 | + } |
|
130 | + |
|
131 | + public function withBody(StreamInterface $body): MessageInterface |
|
132 | + { |
|
133 | + if ($body === $this->stream) { |
|
134 | + return $this; |
|
135 | + } |
|
136 | + |
|
137 | + $new = clone $this; |
|
138 | + $new->stream = $body; |
|
139 | + |
|
140 | + return $new; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * @param (string|string[])[] $headers |
|
145 | + */ |
|
146 | + private function setHeaders(array $headers): void |
|
147 | + { |
|
148 | + $this->headerNames = $this->headers = []; |
|
149 | + foreach ($headers as $header => $value) { |
|
150 | + // Numeric array keys are converted to int by PHP. |
|
151 | + $header = (string) $header; |
|
152 | + |
|
153 | + $this->assertHeader($header); |
|
154 | + $value = $this->normalizeHeaderValue($value); |
|
155 | + $normalized = strtolower($header); |
|
156 | + if (isset($this->headerNames[$normalized])) { |
|
157 | + $header = $this->headerNames[$normalized]; |
|
158 | + $this->headers[$header] = array_merge($this->headers[$header], $value); |
|
159 | + } else { |
|
160 | + $this->headerNames[$normalized] = $header; |
|
161 | + $this->headers[$header] = $value; |
|
162 | + } |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @param mixed $value |
|
168 | + * |
|
169 | + * @return string[] |
|
170 | + */ |
|
171 | + private function normalizeHeaderValue($value): array |
|
172 | + { |
|
173 | + if (!is_array($value)) { |
|
174 | + return $this->trimAndValidateHeaderValues([$value]); |
|
175 | + } |
|
176 | + |
|
177 | + if (count($value) === 0) { |
|
178 | + throw new \InvalidArgumentException('Header value can not be an empty array.'); |
|
179 | + } |
|
180 | + |
|
181 | + return $this->trimAndValidateHeaderValues($value); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Trims whitespace from the header values. |
|
186 | + * |
|
187 | + * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. |
|
188 | + * |
|
189 | + * header-field = field-name ":" OWS field-value OWS |
|
190 | + * OWS = *( SP / HTAB ) |
|
191 | + * |
|
192 | + * @param mixed[] $values Header values |
|
193 | + * |
|
194 | + * @return string[] Trimmed header values |
|
195 | + * |
|
196 | + * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 |
|
197 | + */ |
|
198 | + private function trimAndValidateHeaderValues(array $values): array |
|
199 | + { |
|
200 | + return array_map(function ($value) { |
|
201 | + if (!is_scalar($value) && null !== $value) { |
|
202 | + throw new \InvalidArgumentException(sprintf( |
|
203 | + 'Header value must be scalar or null but %s provided.', |
|
204 | + is_object($value) ? get_class($value) : gettype($value) |
|
205 | + )); |
|
206 | + } |
|
207 | + |
|
208 | + $trimmed = trim((string) $value, " \t"); |
|
209 | + $this->assertValue($trimmed); |
|
210 | + |
|
211 | + return $trimmed; |
|
212 | + }, array_values($values)); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 |
|
217 | + * |
|
218 | + * @param mixed $header |
|
219 | + */ |
|
220 | + private function assertHeader($header): void |
|
221 | + { |
|
222 | + if (!is_string($header)) { |
|
223 | + throw new \InvalidArgumentException(sprintf( |
|
224 | + 'Header name must be a string but %s provided.', |
|
225 | + is_object($header) ? get_class($header) : gettype($header) |
|
226 | + )); |
|
227 | + } |
|
228 | + |
|
229 | + if (!preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $header)) { |
|
230 | + throw new \InvalidArgumentException( |
|
231 | + sprintf('"%s" is not valid header name.', $header) |
|
232 | + ); |
|
233 | + } |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 |
|
238 | + * |
|
239 | + * field-value = *( field-content / obs-fold ) |
|
240 | + * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
|
241 | + * field-vchar = VCHAR / obs-text |
|
242 | + * VCHAR = %x21-7E |
|
243 | + * obs-text = %x80-FF |
|
244 | + * obs-fold = CRLF 1*( SP / HTAB ) |
|
245 | + */ |
|
246 | + private function assertValue(string $value): void |
|
247 | + { |
|
248 | + // The regular expression intentionally does not support the obs-fold production, because as |
|
249 | + // per RFC 7230#3.2.4: |
|
250 | + // |
|
251 | + // A sender MUST NOT generate a message that includes |
|
252 | + // line folding (i.e., that has any field-value that contains a match to |
|
253 | + // the obs-fold rule) unless the message is intended for packaging |
|
254 | + // within the message/http media type. |
|
255 | + // |
|
256 | + // Clients must not send a request with line folding and a server sending folded headers is |
|
257 | + // likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting |
|
258 | + // folding is not likely to break any legitimate use case. |
|
259 | + if (!preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D', $value)) { |
|
260 | + throw new \InvalidArgumentException( |
|
261 | + sprintf('"%s" is not valid header value.', $value) |
|
262 | + ); |
|
263 | + } |
|
264 | + } |
|
265 | 265 | } |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | $this->headerNames = $this->headers = []; |
149 | 149 | foreach ($headers as $header => $value) { |
150 | 150 | // Numeric array keys are converted to int by PHP. |
151 | - $header = (string) $header; |
|
151 | + $header = (string)$header; |
|
152 | 152 | |
153 | 153 | $this->assertHeader($header); |
154 | 154 | $value = $this->normalizeHeaderValue($value); |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | */ |
198 | 198 | private function trimAndValidateHeaderValues(array $values): array |
199 | 199 | { |
200 | - return array_map(function ($value) { |
|
200 | + return array_map(function($value) { |
|
201 | 201 | if (!is_scalar($value) && null !== $value) { |
202 | 202 | throw new \InvalidArgumentException(sprintf( |
203 | 203 | 'Header value must be scalar or null but %s provided.', |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | )); |
206 | 206 | } |
207 | 207 | |
208 | - $trimmed = trim((string) $value, " \t"); |
|
208 | + $trimmed = trim((string)$value, " \t"); |
|
209 | 209 | $this->assertValue($trimmed); |
210 | 210 | |
211 | 211 | return $trimmed; |
@@ -10,8 +10,7 @@ |
||
10 | 10 | /** |
11 | 11 | * Trait implementing functionality common to requests and responses. |
12 | 12 | */ |
13 | -trait MessageTrait |
|
14 | -{ |
|
13 | +trait MessageTrait { |
|
15 | 14 | /** @var string[][] Map of all registered headers, as original name => array of values */ |
16 | 15 | private $headers = []; |
17 | 16 |