@@ -11,132 +11,132 @@ |
||
11 | 11 | */ |
12 | 12 | final class StreamWrapper |
13 | 13 | { |
14 | - /** @var resource */ |
|
15 | - public $context; |
|
16 | - /** @var StreamInterface */ |
|
17 | - private $stream; |
|
18 | - /** @var string r, r+, or w */ |
|
19 | - private $mode; |
|
20 | - /** |
|
21 | - * Returns a resource representing the stream. |
|
22 | - * |
|
23 | - * @param StreamInterface $stream The stream to get a resource for |
|
24 | - * |
|
25 | - * @return resource |
|
26 | - * |
|
27 | - * @throws \InvalidArgumentException if stream is not readable or writable |
|
28 | - */ |
|
29 | - public static function getResource(StreamInterface $stream) |
|
30 | - { |
|
31 | - self::register(); |
|
32 | - if ($stream->isReadable()) { |
|
33 | - $mode = $stream->isWritable() ? 'r+' : 'r'; |
|
34 | - } elseif ($stream->isWritable()) { |
|
35 | - $mode = 'w'; |
|
36 | - } else { |
|
37 | - throw new \InvalidArgumentException('The stream must be readable, ' . 'writable, or both.'); |
|
38 | - } |
|
39 | - return \fopen('guzzle://stream', $mode, \false, self::createStreamContext($stream)); |
|
40 | - } |
|
41 | - /** |
|
42 | - * Creates a stream context that can be used to open a stream as a php stream resource. |
|
43 | - * |
|
44 | - * @return resource |
|
45 | - */ |
|
46 | - public static function createStreamContext(StreamInterface $stream) |
|
47 | - { |
|
48 | - return \stream_context_create(['guzzle' => ['stream' => $stream]]); |
|
49 | - } |
|
50 | - /** |
|
51 | - * Registers the stream wrapper if needed |
|
52 | - */ |
|
53 | - public static function register() : void |
|
54 | - { |
|
55 | - if (!\in_array('guzzle', \stream_get_wrappers())) { |
|
56 | - \stream_wrapper_register('guzzle', __CLASS__); |
|
57 | - } |
|
58 | - } |
|
59 | - public function stream_open(string $path, string $mode, int $options, string &$opened_path = null) : bool |
|
60 | - { |
|
61 | - $options = \stream_context_get_options($this->context); |
|
62 | - if (!isset($options['guzzle']['stream'])) { |
|
63 | - return \false; |
|
64 | - } |
|
65 | - $this->mode = $mode; |
|
66 | - $this->stream = $options['guzzle']['stream']; |
|
67 | - return \true; |
|
68 | - } |
|
69 | - public function stream_read(int $count) : string |
|
70 | - { |
|
71 | - return $this->stream->read($count); |
|
72 | - } |
|
73 | - public function stream_write(string $data) : int |
|
74 | - { |
|
75 | - return $this->stream->write($data); |
|
76 | - } |
|
77 | - public function stream_tell() : int |
|
78 | - { |
|
79 | - return $this->stream->tell(); |
|
80 | - } |
|
81 | - public function stream_eof() : bool |
|
82 | - { |
|
83 | - return $this->stream->eof(); |
|
84 | - } |
|
85 | - public function stream_seek(int $offset, int $whence) : bool |
|
86 | - { |
|
87 | - $this->stream->seek($offset, $whence); |
|
88 | - return \true; |
|
89 | - } |
|
90 | - /** |
|
91 | - * @return resource|false |
|
92 | - */ |
|
93 | - public function stream_cast(int $cast_as) |
|
94 | - { |
|
95 | - $stream = clone $this->stream; |
|
96 | - $resource = $stream->detach(); |
|
97 | - return $resource ?? \false; |
|
98 | - } |
|
99 | - /** |
|
100 | - * @return array{ |
|
101 | - * dev: int, |
|
102 | - * ino: int, |
|
103 | - * mode: int, |
|
104 | - * nlink: int, |
|
105 | - * uid: int, |
|
106 | - * gid: int, |
|
107 | - * rdev: int, |
|
108 | - * size: int, |
|
109 | - * atime: int, |
|
110 | - * mtime: int, |
|
111 | - * ctime: int, |
|
112 | - * blksize: int, |
|
113 | - * blocks: int |
|
114 | - * } |
|
115 | - */ |
|
116 | - public function stream_stat() : array |
|
117 | - { |
|
118 | - static $modeMap = ['r' => 33060, 'rb' => 33060, 'r+' => 33206, 'w' => 33188, 'wb' => 33188]; |
|
119 | - return ['dev' => 0, 'ino' => 0, 'mode' => $modeMap[$this->mode], 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => $this->stream->getSize() ?: 0, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0]; |
|
120 | - } |
|
121 | - /** |
|
122 | - * @return array{ |
|
123 | - * dev: int, |
|
124 | - * ino: int, |
|
125 | - * mode: int, |
|
126 | - * nlink: int, |
|
127 | - * uid: int, |
|
128 | - * gid: int, |
|
129 | - * rdev: int, |
|
130 | - * size: int, |
|
131 | - * atime: int, |
|
132 | - * mtime: int, |
|
133 | - * ctime: int, |
|
134 | - * blksize: int, |
|
135 | - * blocks: int |
|
136 | - * } |
|
137 | - */ |
|
138 | - public function url_stat(string $path, int $flags) : array |
|
139 | - { |
|
140 | - return ['dev' => 0, 'ino' => 0, 'mode' => 0, 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => 0, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0]; |
|
141 | - } |
|
14 | + /** @var resource */ |
|
15 | + public $context; |
|
16 | + /** @var StreamInterface */ |
|
17 | + private $stream; |
|
18 | + /** @var string r, r+, or w */ |
|
19 | + private $mode; |
|
20 | + /** |
|
21 | + * Returns a resource representing the stream. |
|
22 | + * |
|
23 | + * @param StreamInterface $stream The stream to get a resource for |
|
24 | + * |
|
25 | + * @return resource |
|
26 | + * |
|
27 | + * @throws \InvalidArgumentException if stream is not readable or writable |
|
28 | + */ |
|
29 | + public static function getResource(StreamInterface $stream) |
|
30 | + { |
|
31 | + self::register(); |
|
32 | + if ($stream->isReadable()) { |
|
33 | + $mode = $stream->isWritable() ? 'r+' : 'r'; |
|
34 | + } elseif ($stream->isWritable()) { |
|
35 | + $mode = 'w'; |
|
36 | + } else { |
|
37 | + throw new \InvalidArgumentException('The stream must be readable, ' . 'writable, or both.'); |
|
38 | + } |
|
39 | + return \fopen('guzzle://stream', $mode, \false, self::createStreamContext($stream)); |
|
40 | + } |
|
41 | + /** |
|
42 | + * Creates a stream context that can be used to open a stream as a php stream resource. |
|
43 | + * |
|
44 | + * @return resource |
|
45 | + */ |
|
46 | + public static function createStreamContext(StreamInterface $stream) |
|
47 | + { |
|
48 | + return \stream_context_create(['guzzle' => ['stream' => $stream]]); |
|
49 | + } |
|
50 | + /** |
|
51 | + * Registers the stream wrapper if needed |
|
52 | + */ |
|
53 | + public static function register() : void |
|
54 | + { |
|
55 | + if (!\in_array('guzzle', \stream_get_wrappers())) { |
|
56 | + \stream_wrapper_register('guzzle', __CLASS__); |
|
57 | + } |
|
58 | + } |
|
59 | + public function stream_open(string $path, string $mode, int $options, string &$opened_path = null) : bool |
|
60 | + { |
|
61 | + $options = \stream_context_get_options($this->context); |
|
62 | + if (!isset($options['guzzle']['stream'])) { |
|
63 | + return \false; |
|
64 | + } |
|
65 | + $this->mode = $mode; |
|
66 | + $this->stream = $options['guzzle']['stream']; |
|
67 | + return \true; |
|
68 | + } |
|
69 | + public function stream_read(int $count) : string |
|
70 | + { |
|
71 | + return $this->stream->read($count); |
|
72 | + } |
|
73 | + public function stream_write(string $data) : int |
|
74 | + { |
|
75 | + return $this->stream->write($data); |
|
76 | + } |
|
77 | + public function stream_tell() : int |
|
78 | + { |
|
79 | + return $this->stream->tell(); |
|
80 | + } |
|
81 | + public function stream_eof() : bool |
|
82 | + { |
|
83 | + return $this->stream->eof(); |
|
84 | + } |
|
85 | + public function stream_seek(int $offset, int $whence) : bool |
|
86 | + { |
|
87 | + $this->stream->seek($offset, $whence); |
|
88 | + return \true; |
|
89 | + } |
|
90 | + /** |
|
91 | + * @return resource|false |
|
92 | + */ |
|
93 | + public function stream_cast(int $cast_as) |
|
94 | + { |
|
95 | + $stream = clone $this->stream; |
|
96 | + $resource = $stream->detach(); |
|
97 | + return $resource ?? \false; |
|
98 | + } |
|
99 | + /** |
|
100 | + * @return array{ |
|
101 | + * dev: int, |
|
102 | + * ino: int, |
|
103 | + * mode: int, |
|
104 | + * nlink: int, |
|
105 | + * uid: int, |
|
106 | + * gid: int, |
|
107 | + * rdev: int, |
|
108 | + * size: int, |
|
109 | + * atime: int, |
|
110 | + * mtime: int, |
|
111 | + * ctime: int, |
|
112 | + * blksize: int, |
|
113 | + * blocks: int |
|
114 | + * } |
|
115 | + */ |
|
116 | + public function stream_stat() : array |
|
117 | + { |
|
118 | + static $modeMap = ['r' => 33060, 'rb' => 33060, 'r+' => 33206, 'w' => 33188, 'wb' => 33188]; |
|
119 | + return ['dev' => 0, 'ino' => 0, 'mode' => $modeMap[$this->mode], 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => $this->stream->getSize() ?: 0, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0]; |
|
120 | + } |
|
121 | + /** |
|
122 | + * @return array{ |
|
123 | + * dev: int, |
|
124 | + * ino: int, |
|
125 | + * mode: int, |
|
126 | + * nlink: int, |
|
127 | + * uid: int, |
|
128 | + * gid: int, |
|
129 | + * rdev: int, |
|
130 | + * size: int, |
|
131 | + * atime: int, |
|
132 | + * mtime: int, |
|
133 | + * ctime: int, |
|
134 | + * blksize: int, |
|
135 | + * blocks: int |
|
136 | + * } |
|
137 | + */ |
|
138 | + public function url_stat(string $path, int $flags) : array |
|
139 | + { |
|
140 | + return ['dev' => 0, 'ino' => 0, 'mode' => 0, 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => 0, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0]; |
|
141 | + } |
|
142 | 142 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
5 | 5 | |
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | } elseif ($stream->isWritable()) { |
35 | 35 | $mode = 'w'; |
36 | 36 | } else { |
37 | - throw new \InvalidArgumentException('The stream must be readable, ' . 'writable, or both.'); |
|
37 | + throw new \InvalidArgumentException('The stream must be readable, '.'writable, or both.'); |
|
38 | 38 | } |
39 | 39 | return \fopen('guzzle://stream', $mode, \false, self::createStreamContext($stream)); |
40 | 40 | } |
@@ -9,8 +9,7 @@ |
||
9 | 9 | * |
10 | 10 | * @see https://www.php.net/streamwrapper |
11 | 11 | */ |
12 | -final class StreamWrapper |
|
13 | -{ |
|
12 | +final class StreamWrapper { |
|
14 | 13 | /** @var resource */ |
15 | 14 | public $context; |
16 | 15 | /** @var StreamInterface */ |
@@ -8,182 +8,182 @@ |
||
8 | 8 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\ResponseInterface; |
9 | 9 | final class Message |
10 | 10 | { |
11 | - /** |
|
12 | - * Returns the string representation of an HTTP message. |
|
13 | - * |
|
14 | - * @param MessageInterface $message Message to convert to a string. |
|
15 | - */ |
|
16 | - public static function toString(MessageInterface $message) : string |
|
17 | - { |
|
18 | - if ($message instanceof RequestInterface) { |
|
19 | - $msg = \trim($message->getMethod() . ' ' . $message->getRequestTarget()) . ' HTTP/' . $message->getProtocolVersion(); |
|
20 | - if (!$message->hasHeader('host')) { |
|
21 | - $msg .= "\r\nHost: " . $message->getUri()->getHost(); |
|
22 | - } |
|
23 | - } elseif ($message instanceof ResponseInterface) { |
|
24 | - $msg = 'HTTP/' . $message->getProtocolVersion() . ' ' . $message->getStatusCode() . ' ' . $message->getReasonPhrase(); |
|
25 | - } else { |
|
26 | - throw new \InvalidArgumentException('Unknown message type'); |
|
27 | - } |
|
28 | - foreach ($message->getHeaders() as $name => $values) { |
|
29 | - if (\is_string($name) && \strtolower($name) === 'set-cookie') { |
|
30 | - foreach ($values as $value) { |
|
31 | - $msg .= "\r\n{$name}: " . $value; |
|
32 | - } |
|
33 | - } else { |
|
34 | - $msg .= "\r\n{$name}: " . \implode(', ', $values); |
|
35 | - } |
|
36 | - } |
|
37 | - return "{$msg}\r\n\r\n" . $message->getBody(); |
|
38 | - } |
|
39 | - /** |
|
40 | - * Get a short summary of the message body. |
|
41 | - * |
|
42 | - * Will return `null` if the response is not printable. |
|
43 | - * |
|
44 | - * @param MessageInterface $message The message to get the body summary |
|
45 | - * @param int $truncateAt The maximum allowed size of the summary |
|
46 | - */ |
|
47 | - public static function bodySummary(MessageInterface $message, int $truncateAt = 120) : ?string |
|
48 | - { |
|
49 | - $body = $message->getBody(); |
|
50 | - if (!$body->isSeekable() || !$body->isReadable()) { |
|
51 | - return null; |
|
52 | - } |
|
53 | - $size = $body->getSize(); |
|
54 | - if ($size === 0) { |
|
55 | - return null; |
|
56 | - } |
|
57 | - $body->rewind(); |
|
58 | - $summary = $body->read($truncateAt); |
|
59 | - $body->rewind(); |
|
60 | - if ($size > $truncateAt) { |
|
61 | - $summary .= ' (truncated...)'; |
|
62 | - } |
|
63 | - // Matches any printable character, including unicode characters: |
|
64 | - // letters, marks, numbers, punctuation, spacing, and separators. |
|
65 | - if (\preg_match('/[^\\pL\\pM\\pN\\pP\\pS\\pZ\\n\\r\\t]/u', $summary) !== 0) { |
|
66 | - return null; |
|
67 | - } |
|
68 | - return $summary; |
|
69 | - } |
|
70 | - /** |
|
71 | - * Attempts to rewind a message body and throws an exception on failure. |
|
72 | - * |
|
73 | - * The body of the message will only be rewound if a call to `tell()` |
|
74 | - * returns a value other than `0`. |
|
75 | - * |
|
76 | - * @param MessageInterface $message Message to rewind |
|
77 | - * |
|
78 | - * @throws \RuntimeException |
|
79 | - */ |
|
80 | - public static function rewindBody(MessageInterface $message) : void |
|
81 | - { |
|
82 | - $body = $message->getBody(); |
|
83 | - if ($body->tell()) { |
|
84 | - $body->rewind(); |
|
85 | - } |
|
86 | - } |
|
87 | - /** |
|
88 | - * Parses an HTTP message into an associative array. |
|
89 | - * |
|
90 | - * The array contains the "start-line" key containing the start line of |
|
91 | - * the message, "headers" key containing an associative array of header |
|
92 | - * array values, and a "body" key containing the body of the message. |
|
93 | - * |
|
94 | - * @param string $message HTTP request or response to parse. |
|
95 | - */ |
|
96 | - public static function parseMessage(string $message) : array |
|
97 | - { |
|
98 | - if (!$message) { |
|
99 | - throw new \InvalidArgumentException('Invalid message'); |
|
100 | - } |
|
101 | - $message = \ltrim($message, "\r\n"); |
|
102 | - $messageParts = \preg_split("/\r?\n\r?\n/", $message, 2); |
|
103 | - if ($messageParts === \false || \count($messageParts) !== 2) { |
|
104 | - throw new \InvalidArgumentException('Invalid message: Missing header delimiter'); |
|
105 | - } |
|
106 | - [$rawHeaders, $body] = $messageParts; |
|
107 | - $rawHeaders .= "\r\n"; |
|
108 | - // Put back the delimiter we split previously |
|
109 | - $headerParts = \preg_split("/\r?\n/", $rawHeaders, 2); |
|
110 | - if ($headerParts === \false || \count($headerParts) !== 2) { |
|
111 | - throw new \InvalidArgumentException('Invalid message: Missing status line'); |
|
112 | - } |
|
113 | - [$startLine, $rawHeaders] = $headerParts; |
|
114 | - if (\preg_match("/(?:^HTTP\\/|^[A-Z]+ \\S+ HTTP\\/)(\\d+(?:\\.\\d+)?)/i", $startLine, $matches) && $matches[1] === '1.0') { |
|
115 | - // Header folding is deprecated for HTTP/1.1, but allowed in HTTP/1.0 |
|
116 | - $rawHeaders = \preg_replace(Rfc7230::HEADER_FOLD_REGEX, ' ', $rawHeaders); |
|
117 | - } |
|
118 | - /** @var array[] $headerLines */ |
|
119 | - $count = \preg_match_all(Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, \PREG_SET_ORDER); |
|
120 | - // If these aren't the same, then one line didn't match and there's an invalid header. |
|
121 | - if ($count !== \substr_count($rawHeaders, "\n")) { |
|
122 | - // Folding is deprecated, see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 |
|
123 | - if (\preg_match(Rfc7230::HEADER_FOLD_REGEX, $rawHeaders)) { |
|
124 | - throw new \InvalidArgumentException('Invalid header syntax: Obsolete line folding'); |
|
125 | - } |
|
126 | - throw new \InvalidArgumentException('Invalid header syntax'); |
|
127 | - } |
|
128 | - $headers = []; |
|
129 | - foreach ($headerLines as $headerLine) { |
|
130 | - $headers[$headerLine[1]][] = $headerLine[2]; |
|
131 | - } |
|
132 | - return ['start-line' => $startLine, 'headers' => $headers, 'body' => $body]; |
|
133 | - } |
|
134 | - /** |
|
135 | - * Constructs a URI for an HTTP request message. |
|
136 | - * |
|
137 | - * @param string $path Path from the start-line |
|
138 | - * @param array $headers Array of headers (each value an array). |
|
139 | - */ |
|
140 | - public static function parseRequestUri(string $path, array $headers) : string |
|
141 | - { |
|
142 | - $hostKey = \array_filter(\array_keys($headers), function ($k) { |
|
143 | - // Numeric array keys are converted to int by PHP. |
|
144 | - $k = (string) $k; |
|
145 | - return \strtolower($k) === 'host'; |
|
146 | - }); |
|
147 | - // If no host is found, then a full URI cannot be constructed. |
|
148 | - if (!$hostKey) { |
|
149 | - return $path; |
|
150 | - } |
|
151 | - $host = $headers[\reset($hostKey)][0]; |
|
152 | - $scheme = \substr($host, -4) === ':443' ? 'https' : 'http'; |
|
153 | - return $scheme . '://' . $host . '/' . \ltrim($path, '/'); |
|
154 | - } |
|
155 | - /** |
|
156 | - * Parses a request message string into a request object. |
|
157 | - * |
|
158 | - * @param string $message Request message string. |
|
159 | - */ |
|
160 | - public static function parseRequest(string $message) : RequestInterface |
|
161 | - { |
|
162 | - $data = self::parseMessage($message); |
|
163 | - $matches = []; |
|
164 | - if (!\preg_match('/^[\\S]+\\s+([a-zA-Z]+:\\/\\/|\\/).*/', $data['start-line'], $matches)) { |
|
165 | - throw new \InvalidArgumentException('Invalid request string'); |
|
166 | - } |
|
167 | - $parts = \explode(' ', $data['start-line'], 3); |
|
168 | - $version = isset($parts[2]) ? \explode('/', $parts[2])[1] : '1.1'; |
|
169 | - $request = new Request($parts[0], $matches[1] === '/' ? self::parseRequestUri($parts[1], $data['headers']) : $parts[1], $data['headers'], $data['body'], $version); |
|
170 | - return $matches[1] === '/' ? $request : $request->withRequestTarget($parts[1]); |
|
171 | - } |
|
172 | - /** |
|
173 | - * Parses a response message string into a response object. |
|
174 | - * |
|
175 | - * @param string $message Response message string. |
|
176 | - */ |
|
177 | - public static function parseResponse(string $message) : ResponseInterface |
|
178 | - { |
|
179 | - $data = self::parseMessage($message); |
|
180 | - // According to https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.2 |
|
181 | - // the space between status-code and reason-phrase is required. But |
|
182 | - // browsers accept responses without space and reason as well. |
|
183 | - if (!\preg_match('/^HTTP\\/.* [0-9]{3}( .*|$)/', $data['start-line'])) { |
|
184 | - throw new \InvalidArgumentException('Invalid response string: ' . $data['start-line']); |
|
185 | - } |
|
186 | - $parts = \explode(' ', $data['start-line'], 3); |
|
187 | - return new Response((int) $parts[1], $data['headers'], $data['body'], \explode('/', $parts[0])[1], $parts[2] ?? null); |
|
188 | - } |
|
11 | + /** |
|
12 | + * Returns the string representation of an HTTP message. |
|
13 | + * |
|
14 | + * @param MessageInterface $message Message to convert to a string. |
|
15 | + */ |
|
16 | + public static function toString(MessageInterface $message) : string |
|
17 | + { |
|
18 | + if ($message instanceof RequestInterface) { |
|
19 | + $msg = \trim($message->getMethod() . ' ' . $message->getRequestTarget()) . ' HTTP/' . $message->getProtocolVersion(); |
|
20 | + if (!$message->hasHeader('host')) { |
|
21 | + $msg .= "\r\nHost: " . $message->getUri()->getHost(); |
|
22 | + } |
|
23 | + } elseif ($message instanceof ResponseInterface) { |
|
24 | + $msg = 'HTTP/' . $message->getProtocolVersion() . ' ' . $message->getStatusCode() . ' ' . $message->getReasonPhrase(); |
|
25 | + } else { |
|
26 | + throw new \InvalidArgumentException('Unknown message type'); |
|
27 | + } |
|
28 | + foreach ($message->getHeaders() as $name => $values) { |
|
29 | + if (\is_string($name) && \strtolower($name) === 'set-cookie') { |
|
30 | + foreach ($values as $value) { |
|
31 | + $msg .= "\r\n{$name}: " . $value; |
|
32 | + } |
|
33 | + } else { |
|
34 | + $msg .= "\r\n{$name}: " . \implode(', ', $values); |
|
35 | + } |
|
36 | + } |
|
37 | + return "{$msg}\r\n\r\n" . $message->getBody(); |
|
38 | + } |
|
39 | + /** |
|
40 | + * Get a short summary of the message body. |
|
41 | + * |
|
42 | + * Will return `null` if the response is not printable. |
|
43 | + * |
|
44 | + * @param MessageInterface $message The message to get the body summary |
|
45 | + * @param int $truncateAt The maximum allowed size of the summary |
|
46 | + */ |
|
47 | + public static function bodySummary(MessageInterface $message, int $truncateAt = 120) : ?string |
|
48 | + { |
|
49 | + $body = $message->getBody(); |
|
50 | + if (!$body->isSeekable() || !$body->isReadable()) { |
|
51 | + return null; |
|
52 | + } |
|
53 | + $size = $body->getSize(); |
|
54 | + if ($size === 0) { |
|
55 | + return null; |
|
56 | + } |
|
57 | + $body->rewind(); |
|
58 | + $summary = $body->read($truncateAt); |
|
59 | + $body->rewind(); |
|
60 | + if ($size > $truncateAt) { |
|
61 | + $summary .= ' (truncated...)'; |
|
62 | + } |
|
63 | + // Matches any printable character, including unicode characters: |
|
64 | + // letters, marks, numbers, punctuation, spacing, and separators. |
|
65 | + if (\preg_match('/[^\\pL\\pM\\pN\\pP\\pS\\pZ\\n\\r\\t]/u', $summary) !== 0) { |
|
66 | + return null; |
|
67 | + } |
|
68 | + return $summary; |
|
69 | + } |
|
70 | + /** |
|
71 | + * Attempts to rewind a message body and throws an exception on failure. |
|
72 | + * |
|
73 | + * The body of the message will only be rewound if a call to `tell()` |
|
74 | + * returns a value other than `0`. |
|
75 | + * |
|
76 | + * @param MessageInterface $message Message to rewind |
|
77 | + * |
|
78 | + * @throws \RuntimeException |
|
79 | + */ |
|
80 | + public static function rewindBody(MessageInterface $message) : void |
|
81 | + { |
|
82 | + $body = $message->getBody(); |
|
83 | + if ($body->tell()) { |
|
84 | + $body->rewind(); |
|
85 | + } |
|
86 | + } |
|
87 | + /** |
|
88 | + * Parses an HTTP message into an associative array. |
|
89 | + * |
|
90 | + * The array contains the "start-line" key containing the start line of |
|
91 | + * the message, "headers" key containing an associative array of header |
|
92 | + * array values, and a "body" key containing the body of the message. |
|
93 | + * |
|
94 | + * @param string $message HTTP request or response to parse. |
|
95 | + */ |
|
96 | + public static function parseMessage(string $message) : array |
|
97 | + { |
|
98 | + if (!$message) { |
|
99 | + throw new \InvalidArgumentException('Invalid message'); |
|
100 | + } |
|
101 | + $message = \ltrim($message, "\r\n"); |
|
102 | + $messageParts = \preg_split("/\r?\n\r?\n/", $message, 2); |
|
103 | + if ($messageParts === \false || \count($messageParts) !== 2) { |
|
104 | + throw new \InvalidArgumentException('Invalid message: Missing header delimiter'); |
|
105 | + } |
|
106 | + [$rawHeaders, $body] = $messageParts; |
|
107 | + $rawHeaders .= "\r\n"; |
|
108 | + // Put back the delimiter we split previously |
|
109 | + $headerParts = \preg_split("/\r?\n/", $rawHeaders, 2); |
|
110 | + if ($headerParts === \false || \count($headerParts) !== 2) { |
|
111 | + throw new \InvalidArgumentException('Invalid message: Missing status line'); |
|
112 | + } |
|
113 | + [$startLine, $rawHeaders] = $headerParts; |
|
114 | + if (\preg_match("/(?:^HTTP\\/|^[A-Z]+ \\S+ HTTP\\/)(\\d+(?:\\.\\d+)?)/i", $startLine, $matches) && $matches[1] === '1.0') { |
|
115 | + // Header folding is deprecated for HTTP/1.1, but allowed in HTTP/1.0 |
|
116 | + $rawHeaders = \preg_replace(Rfc7230::HEADER_FOLD_REGEX, ' ', $rawHeaders); |
|
117 | + } |
|
118 | + /** @var array[] $headerLines */ |
|
119 | + $count = \preg_match_all(Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, \PREG_SET_ORDER); |
|
120 | + // If these aren't the same, then one line didn't match and there's an invalid header. |
|
121 | + if ($count !== \substr_count($rawHeaders, "\n")) { |
|
122 | + // Folding is deprecated, see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 |
|
123 | + if (\preg_match(Rfc7230::HEADER_FOLD_REGEX, $rawHeaders)) { |
|
124 | + throw new \InvalidArgumentException('Invalid header syntax: Obsolete line folding'); |
|
125 | + } |
|
126 | + throw new \InvalidArgumentException('Invalid header syntax'); |
|
127 | + } |
|
128 | + $headers = []; |
|
129 | + foreach ($headerLines as $headerLine) { |
|
130 | + $headers[$headerLine[1]][] = $headerLine[2]; |
|
131 | + } |
|
132 | + return ['start-line' => $startLine, 'headers' => $headers, 'body' => $body]; |
|
133 | + } |
|
134 | + /** |
|
135 | + * Constructs a URI for an HTTP request message. |
|
136 | + * |
|
137 | + * @param string $path Path from the start-line |
|
138 | + * @param array $headers Array of headers (each value an array). |
|
139 | + */ |
|
140 | + public static function parseRequestUri(string $path, array $headers) : string |
|
141 | + { |
|
142 | + $hostKey = \array_filter(\array_keys($headers), function ($k) { |
|
143 | + // Numeric array keys are converted to int by PHP. |
|
144 | + $k = (string) $k; |
|
145 | + return \strtolower($k) === 'host'; |
|
146 | + }); |
|
147 | + // If no host is found, then a full URI cannot be constructed. |
|
148 | + if (!$hostKey) { |
|
149 | + return $path; |
|
150 | + } |
|
151 | + $host = $headers[\reset($hostKey)][0]; |
|
152 | + $scheme = \substr($host, -4) === ':443' ? 'https' : 'http'; |
|
153 | + return $scheme . '://' . $host . '/' . \ltrim($path, '/'); |
|
154 | + } |
|
155 | + /** |
|
156 | + * Parses a request message string into a request object. |
|
157 | + * |
|
158 | + * @param string $message Request message string. |
|
159 | + */ |
|
160 | + public static function parseRequest(string $message) : RequestInterface |
|
161 | + { |
|
162 | + $data = self::parseMessage($message); |
|
163 | + $matches = []; |
|
164 | + if (!\preg_match('/^[\\S]+\\s+([a-zA-Z]+:\\/\\/|\\/).*/', $data['start-line'], $matches)) { |
|
165 | + throw new \InvalidArgumentException('Invalid request string'); |
|
166 | + } |
|
167 | + $parts = \explode(' ', $data['start-line'], 3); |
|
168 | + $version = isset($parts[2]) ? \explode('/', $parts[2])[1] : '1.1'; |
|
169 | + $request = new Request($parts[0], $matches[1] === '/' ? self::parseRequestUri($parts[1], $data['headers']) : $parts[1], $data['headers'], $data['body'], $version); |
|
170 | + return $matches[1] === '/' ? $request : $request->withRequestTarget($parts[1]); |
|
171 | + } |
|
172 | + /** |
|
173 | + * Parses a response message string into a response object. |
|
174 | + * |
|
175 | + * @param string $message Response message string. |
|
176 | + */ |
|
177 | + public static function parseResponse(string $message) : ResponseInterface |
|
178 | + { |
|
179 | + $data = self::parseMessage($message); |
|
180 | + // According to https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.2 |
|
181 | + // the space between status-code and reason-phrase is required. But |
|
182 | + // browsers accept responses without space and reason as well. |
|
183 | + if (!\preg_match('/^HTTP\\/.* [0-9]{3}( .*|$)/', $data['start-line'])) { |
|
184 | + throw new \InvalidArgumentException('Invalid response string: ' . $data['start-line']); |
|
185 | + } |
|
186 | + $parts = \explode(' ', $data['start-line'], 3); |
|
187 | + return new Response((int) $parts[1], $data['headers'], $data['body'], \explode('/', $parts[0])[1], $parts[2] ?? null); |
|
188 | + } |
|
189 | 189 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
5 | 5 | |
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\MessageInterface; |
@@ -16,25 +16,25 @@ discard block |
||
16 | 16 | public static function toString(MessageInterface $message) : string |
17 | 17 | { |
18 | 18 | if ($message instanceof RequestInterface) { |
19 | - $msg = \trim($message->getMethod() . ' ' . $message->getRequestTarget()) . ' HTTP/' . $message->getProtocolVersion(); |
|
19 | + $msg = \trim($message->getMethod().' '.$message->getRequestTarget()).' HTTP/'.$message->getProtocolVersion(); |
|
20 | 20 | if (!$message->hasHeader('host')) { |
21 | - $msg .= "\r\nHost: " . $message->getUri()->getHost(); |
|
21 | + $msg .= "\r\nHost: ".$message->getUri()->getHost(); |
|
22 | 22 | } |
23 | 23 | } elseif ($message instanceof ResponseInterface) { |
24 | - $msg = 'HTTP/' . $message->getProtocolVersion() . ' ' . $message->getStatusCode() . ' ' . $message->getReasonPhrase(); |
|
24 | + $msg = 'HTTP/'.$message->getProtocolVersion().' '.$message->getStatusCode().' '.$message->getReasonPhrase(); |
|
25 | 25 | } else { |
26 | 26 | throw new \InvalidArgumentException('Unknown message type'); |
27 | 27 | } |
28 | 28 | foreach ($message->getHeaders() as $name => $values) { |
29 | 29 | if (\is_string($name) && \strtolower($name) === 'set-cookie') { |
30 | 30 | foreach ($values as $value) { |
31 | - $msg .= "\r\n{$name}: " . $value; |
|
31 | + $msg .= "\r\n{$name}: ".$value; |
|
32 | 32 | } |
33 | 33 | } else { |
34 | - $msg .= "\r\n{$name}: " . \implode(', ', $values); |
|
34 | + $msg .= "\r\n{$name}: ".\implode(', ', $values); |
|
35 | 35 | } |
36 | 36 | } |
37 | - return "{$msg}\r\n\r\n" . $message->getBody(); |
|
37 | + return "{$msg}\r\n\r\n".$message->getBody(); |
|
38 | 38 | } |
39 | 39 | /** |
40 | 40 | * Get a short summary of the message body. |
@@ -139,9 +139,9 @@ discard block |
||
139 | 139 | */ |
140 | 140 | public static function parseRequestUri(string $path, array $headers) : string |
141 | 141 | { |
142 | - $hostKey = \array_filter(\array_keys($headers), function ($k) { |
|
142 | + $hostKey = \array_filter(\array_keys($headers), function($k) { |
|
143 | 143 | // Numeric array keys are converted to int by PHP. |
144 | - $k = (string) $k; |
|
144 | + $k = (string)$k; |
|
145 | 145 | return \strtolower($k) === 'host'; |
146 | 146 | }); |
147 | 147 | // If no host is found, then a full URI cannot be constructed. |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | } |
151 | 151 | $host = $headers[\reset($hostKey)][0]; |
152 | 152 | $scheme = \substr($host, -4) === ':443' ? 'https' : 'http'; |
153 | - return $scheme . '://' . $host . '/' . \ltrim($path, '/'); |
|
153 | + return $scheme.'://'.$host.'/'.\ltrim($path, '/'); |
|
154 | 154 | } |
155 | 155 | /** |
156 | 156 | * Parses a request message string into a request object. |
@@ -181,9 +181,9 @@ discard block |
||
181 | 181 | // the space between status-code and reason-phrase is required. But |
182 | 182 | // browsers accept responses without space and reason as well. |
183 | 183 | if (!\preg_match('/^HTTP\\/.* [0-9]{3}( .*|$)/', $data['start-line'])) { |
184 | - throw new \InvalidArgumentException('Invalid response string: ' . $data['start-line']); |
|
184 | + throw new \InvalidArgumentException('Invalid response string: '.$data['start-line']); |
|
185 | 185 | } |
186 | 186 | $parts = \explode(' ', $data['start-line'], 3); |
187 | - return new Response((int) $parts[1], $data['headers'], $data['body'], \explode('/', $parts[0])[1], $parts[2] ?? null); |
|
187 | + return new Response((int)$parts[1], $data['headers'], $data['body'], \explode('/', $parts[0])[1], $parts[2] ?? null); |
|
188 | 188 | } |
189 | 189 | } |
@@ -6,8 +6,7 @@ |
||
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\MessageInterface; |
7 | 7 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface; |
8 | 8 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\ResponseInterface; |
9 | -final class Message |
|
10 | -{ |
|
9 | +final class Message { |
|
11 | 10 | /** |
12 | 11 | * Returns the string representation of an HTTP message. |
13 | 12 | * |
@@ -17,17 +17,17 @@ |
||
17 | 17 | */ |
18 | 18 | final class InflateStream implements StreamInterface |
19 | 19 | { |
20 | - use StreamDecoratorTrait; |
|
21 | - /** @var StreamInterface */ |
|
22 | - private $stream; |
|
23 | - public function __construct(StreamInterface $stream) |
|
24 | - { |
|
25 | - $resource = StreamWrapper::getResource($stream); |
|
26 | - // Specify window=15+32, so zlib will use header detection to both gzip (with header) and zlib data |
|
27 | - // See https://www.zlib.net/manual.html#Advanced definition of inflateInit2 |
|
28 | - // "Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection" |
|
29 | - // Default window size is 15. |
|
30 | - \stream_filter_append($resource, 'zlib.inflate', \STREAM_FILTER_READ, ['window' => 15 + 32]); |
|
31 | - $this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource)); |
|
32 | - } |
|
20 | + use StreamDecoratorTrait; |
|
21 | + /** @var StreamInterface */ |
|
22 | + private $stream; |
|
23 | + public function __construct(StreamInterface $stream) |
|
24 | + { |
|
25 | + $resource = StreamWrapper::getResource($stream); |
|
26 | + // Specify window=15+32, so zlib will use header detection to both gzip (with header) and zlib data |
|
27 | + // See https://www.zlib.net/manual.html#Advanced definition of inflateInit2 |
|
28 | + // "Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection" |
|
29 | + // Default window size is 15. |
|
30 | + \stream_filter_append($resource, 'zlib.inflate', \STREAM_FILTER_READ, ['window' => 15 + 32]); |
|
31 | + $this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource)); |
|
32 | + } |
|
33 | 33 | } |
@@ -15,8 +15,7 @@ |
||
15 | 15 | * @see https://datatracker.ietf.org/doc/html/rfc1952 |
16 | 16 | * @see https://www.php.net/manual/en/filters.compression.php |
17 | 17 | */ |
18 | -final class InflateStream implements StreamInterface |
|
19 | -{ |
|
18 | +final class InflateStream implements StreamInterface { |
|
20 | 19 | use StreamDecoratorTrait; |
21 | 20 | /** @var StreamInterface */ |
22 | 21 | private $stream; |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
5 | 5 | |
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
@@ -9,120 +9,120 @@ |
||
9 | 9 | */ |
10 | 10 | final class LimitStream implements StreamInterface |
11 | 11 | { |
12 | - use StreamDecoratorTrait; |
|
13 | - /** @var int Offset to start reading from */ |
|
14 | - private $offset; |
|
15 | - /** @var int Limit the number of bytes that can be read */ |
|
16 | - private $limit; |
|
17 | - /** @var StreamInterface */ |
|
18 | - private $stream; |
|
19 | - /** |
|
20 | - * @param StreamInterface $stream Stream to wrap |
|
21 | - * @param int $limit Total number of bytes to allow to be read |
|
22 | - * from the stream. Pass -1 for no limit. |
|
23 | - * @param int $offset Position to seek to before reading (only |
|
24 | - * works on seekable streams). |
|
25 | - */ |
|
26 | - public function __construct(StreamInterface $stream, int $limit = -1, int $offset = 0) |
|
27 | - { |
|
28 | - $this->stream = $stream; |
|
29 | - $this->setLimit($limit); |
|
30 | - $this->setOffset($offset); |
|
31 | - } |
|
32 | - public function eof() : bool |
|
33 | - { |
|
34 | - // Always return true if the underlying stream is EOF |
|
35 | - if ($this->stream->eof()) { |
|
36 | - return \true; |
|
37 | - } |
|
38 | - // No limit and the underlying stream is not at EOF |
|
39 | - if ($this->limit === -1) { |
|
40 | - return \false; |
|
41 | - } |
|
42 | - return $this->stream->tell() >= $this->offset + $this->limit; |
|
43 | - } |
|
44 | - /** |
|
45 | - * Returns the size of the limited subset of data |
|
46 | - */ |
|
47 | - public function getSize() : ?int |
|
48 | - { |
|
49 | - if (null === ($length = $this->stream->getSize())) { |
|
50 | - return null; |
|
51 | - } elseif ($this->limit === -1) { |
|
52 | - return $length - $this->offset; |
|
53 | - } else { |
|
54 | - return \min($this->limit, $length - $this->offset); |
|
55 | - } |
|
56 | - } |
|
57 | - /** |
|
58 | - * Allow for a bounded seek on the read limited stream |
|
59 | - */ |
|
60 | - public function seek($offset, $whence = \SEEK_SET) : void |
|
61 | - { |
|
62 | - if ($whence !== \SEEK_SET || $offset < 0) { |
|
63 | - throw new \RuntimeException(\sprintf('Cannot seek to offset %s with whence %s', $offset, $whence)); |
|
64 | - } |
|
65 | - $offset += $this->offset; |
|
66 | - if ($this->limit !== -1) { |
|
67 | - if ($offset > $this->offset + $this->limit) { |
|
68 | - $offset = $this->offset + $this->limit; |
|
69 | - } |
|
70 | - } |
|
71 | - $this->stream->seek($offset); |
|
72 | - } |
|
73 | - /** |
|
74 | - * Give a relative tell() |
|
75 | - */ |
|
76 | - public function tell() : int |
|
77 | - { |
|
78 | - return $this->stream->tell() - $this->offset; |
|
79 | - } |
|
80 | - /** |
|
81 | - * Set the offset to start limiting from |
|
82 | - * |
|
83 | - * @param int $offset Offset to seek to and begin byte limiting from |
|
84 | - * |
|
85 | - * @throws \RuntimeException if the stream cannot be seeked. |
|
86 | - */ |
|
87 | - public function setOffset(int $offset) : void |
|
88 | - { |
|
89 | - $current = $this->stream->tell(); |
|
90 | - if ($current !== $offset) { |
|
91 | - // If the stream cannot seek to the offset position, then read to it |
|
92 | - if ($this->stream->isSeekable()) { |
|
93 | - $this->stream->seek($offset); |
|
94 | - } elseif ($current > $offset) { |
|
95 | - throw new \RuntimeException("Could not seek to stream offset {$offset}"); |
|
96 | - } else { |
|
97 | - $this->stream->read($offset - $current); |
|
98 | - } |
|
99 | - } |
|
100 | - $this->offset = $offset; |
|
101 | - } |
|
102 | - /** |
|
103 | - * Set the limit of bytes that the decorator allows to be read from the |
|
104 | - * stream. |
|
105 | - * |
|
106 | - * @param int $limit Number of bytes to allow to be read from the stream. |
|
107 | - * Use -1 for no limit. |
|
108 | - */ |
|
109 | - public function setLimit(int $limit) : void |
|
110 | - { |
|
111 | - $this->limit = $limit; |
|
112 | - } |
|
113 | - public function read($length) : string |
|
114 | - { |
|
115 | - if ($this->limit === -1) { |
|
116 | - return $this->stream->read($length); |
|
117 | - } |
|
118 | - // Check if the current position is less than the total allowed |
|
119 | - // bytes + original offset |
|
120 | - $remaining = $this->offset + $this->limit - $this->stream->tell(); |
|
121 | - if ($remaining > 0) { |
|
122 | - // Only return the amount of requested data, ensuring that the byte |
|
123 | - // limit is not exceeded |
|
124 | - return $this->stream->read(\min($remaining, $length)); |
|
125 | - } |
|
126 | - return ''; |
|
127 | - } |
|
12 | + use StreamDecoratorTrait; |
|
13 | + /** @var int Offset to start reading from */ |
|
14 | + private $offset; |
|
15 | + /** @var int Limit the number of bytes that can be read */ |
|
16 | + private $limit; |
|
17 | + /** @var StreamInterface */ |
|
18 | + private $stream; |
|
19 | + /** |
|
20 | + * @param StreamInterface $stream Stream to wrap |
|
21 | + * @param int $limit Total number of bytes to allow to be read |
|
22 | + * from the stream. Pass -1 for no limit. |
|
23 | + * @param int $offset Position to seek to before reading (only |
|
24 | + * works on seekable streams). |
|
25 | + */ |
|
26 | + public function __construct(StreamInterface $stream, int $limit = -1, int $offset = 0) |
|
27 | + { |
|
28 | + $this->stream = $stream; |
|
29 | + $this->setLimit($limit); |
|
30 | + $this->setOffset($offset); |
|
31 | + } |
|
32 | + public function eof() : bool |
|
33 | + { |
|
34 | + // Always return true if the underlying stream is EOF |
|
35 | + if ($this->stream->eof()) { |
|
36 | + return \true; |
|
37 | + } |
|
38 | + // No limit and the underlying stream is not at EOF |
|
39 | + if ($this->limit === -1) { |
|
40 | + return \false; |
|
41 | + } |
|
42 | + return $this->stream->tell() >= $this->offset + $this->limit; |
|
43 | + } |
|
44 | + /** |
|
45 | + * Returns the size of the limited subset of data |
|
46 | + */ |
|
47 | + public function getSize() : ?int |
|
48 | + { |
|
49 | + if (null === ($length = $this->stream->getSize())) { |
|
50 | + return null; |
|
51 | + } elseif ($this->limit === -1) { |
|
52 | + return $length - $this->offset; |
|
53 | + } else { |
|
54 | + return \min($this->limit, $length - $this->offset); |
|
55 | + } |
|
56 | + } |
|
57 | + /** |
|
58 | + * Allow for a bounded seek on the read limited stream |
|
59 | + */ |
|
60 | + public function seek($offset, $whence = \SEEK_SET) : void |
|
61 | + { |
|
62 | + if ($whence !== \SEEK_SET || $offset < 0) { |
|
63 | + throw new \RuntimeException(\sprintf('Cannot seek to offset %s with whence %s', $offset, $whence)); |
|
64 | + } |
|
65 | + $offset += $this->offset; |
|
66 | + if ($this->limit !== -1) { |
|
67 | + if ($offset > $this->offset + $this->limit) { |
|
68 | + $offset = $this->offset + $this->limit; |
|
69 | + } |
|
70 | + } |
|
71 | + $this->stream->seek($offset); |
|
72 | + } |
|
73 | + /** |
|
74 | + * Give a relative tell() |
|
75 | + */ |
|
76 | + public function tell() : int |
|
77 | + { |
|
78 | + return $this->stream->tell() - $this->offset; |
|
79 | + } |
|
80 | + /** |
|
81 | + * Set the offset to start limiting from |
|
82 | + * |
|
83 | + * @param int $offset Offset to seek to and begin byte limiting from |
|
84 | + * |
|
85 | + * @throws \RuntimeException if the stream cannot be seeked. |
|
86 | + */ |
|
87 | + public function setOffset(int $offset) : void |
|
88 | + { |
|
89 | + $current = $this->stream->tell(); |
|
90 | + if ($current !== $offset) { |
|
91 | + // If the stream cannot seek to the offset position, then read to it |
|
92 | + if ($this->stream->isSeekable()) { |
|
93 | + $this->stream->seek($offset); |
|
94 | + } elseif ($current > $offset) { |
|
95 | + throw new \RuntimeException("Could not seek to stream offset {$offset}"); |
|
96 | + } else { |
|
97 | + $this->stream->read($offset - $current); |
|
98 | + } |
|
99 | + } |
|
100 | + $this->offset = $offset; |
|
101 | + } |
|
102 | + /** |
|
103 | + * Set the limit of bytes that the decorator allows to be read from the |
|
104 | + * stream. |
|
105 | + * |
|
106 | + * @param int $limit Number of bytes to allow to be read from the stream. |
|
107 | + * Use -1 for no limit. |
|
108 | + */ |
|
109 | + public function setLimit(int $limit) : void |
|
110 | + { |
|
111 | + $this->limit = $limit; |
|
112 | + } |
|
113 | + public function read($length) : string |
|
114 | + { |
|
115 | + if ($this->limit === -1) { |
|
116 | + return $this->stream->read($length); |
|
117 | + } |
|
118 | + // Check if the current position is less than the total allowed |
|
119 | + // bytes + original offset |
|
120 | + $remaining = $this->offset + $this->limit - $this->stream->tell(); |
|
121 | + if ($remaining > 0) { |
|
122 | + // Only return the amount of requested data, ensuring that the byte |
|
123 | + // limit is not exceeded |
|
124 | + return $this->stream->read(\min($remaining, $length)); |
|
125 | + } |
|
126 | + return ''; |
|
127 | + } |
|
128 | 128 | } |
@@ -7,8 +7,7 @@ |
||
7 | 7 | /** |
8 | 8 | * Decorator used to return only a subset of a stream. |
9 | 9 | */ |
10 | -final class LimitStream implements StreamInterface |
|
11 | -{ |
|
10 | +final class LimitStream implements StreamInterface { |
|
12 | 11 | use StreamDecoratorTrait; |
13 | 12 | /** @var int Offset to start reading from */ |
14 | 13 | private $offset; |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
5 | 5 | |
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
@@ -10,116 +10,116 @@ |
||
10 | 10 | */ |
11 | 11 | final class CachingStream implements StreamInterface |
12 | 12 | { |
13 | - use StreamDecoratorTrait; |
|
14 | - /** @var StreamInterface Stream being wrapped */ |
|
15 | - private $remoteStream; |
|
16 | - /** @var int Number of bytes to skip reading due to a write on the buffer */ |
|
17 | - private $skipReadBytes = 0; |
|
18 | - /** |
|
19 | - * @var StreamInterface |
|
20 | - */ |
|
21 | - private $stream; |
|
22 | - /** |
|
23 | - * We will treat the buffer object as the body of the stream |
|
24 | - * |
|
25 | - * @param StreamInterface $stream Stream to cache. The cursor is assumed to be at the beginning of the stream. |
|
26 | - * @param StreamInterface $target Optionally specify where data is cached |
|
27 | - */ |
|
28 | - public function __construct(StreamInterface $stream, StreamInterface $target = null) |
|
29 | - { |
|
30 | - $this->remoteStream = $stream; |
|
31 | - $this->stream = $target ?: new Stream(Utils::tryFopen('php://temp', 'r+')); |
|
32 | - } |
|
33 | - public function getSize() : ?int |
|
34 | - { |
|
35 | - $remoteSize = $this->remoteStream->getSize(); |
|
36 | - if (null === $remoteSize) { |
|
37 | - return null; |
|
38 | - } |
|
39 | - return \max($this->stream->getSize(), $remoteSize); |
|
40 | - } |
|
41 | - public function rewind() : void |
|
42 | - { |
|
43 | - $this->seek(0); |
|
44 | - } |
|
45 | - public function seek($offset, $whence = \SEEK_SET) : void |
|
46 | - { |
|
47 | - if ($whence === \SEEK_SET) { |
|
48 | - $byte = $offset; |
|
49 | - } elseif ($whence === \SEEK_CUR) { |
|
50 | - $byte = $offset + $this->tell(); |
|
51 | - } elseif ($whence === \SEEK_END) { |
|
52 | - $size = $this->remoteStream->getSize(); |
|
53 | - if ($size === null) { |
|
54 | - $size = $this->cacheEntireStream(); |
|
55 | - } |
|
56 | - $byte = $size + $offset; |
|
57 | - } else { |
|
58 | - throw new \InvalidArgumentException('Invalid whence'); |
|
59 | - } |
|
60 | - $diff = $byte - $this->stream->getSize(); |
|
61 | - if ($diff > 0) { |
|
62 | - // Read the remoteStream until we have read in at least the amount |
|
63 | - // of bytes requested, or we reach the end of the file. |
|
64 | - while ($diff > 0 && !$this->remoteStream->eof()) { |
|
65 | - $this->read($diff); |
|
66 | - $diff = $byte - $this->stream->getSize(); |
|
67 | - } |
|
68 | - } else { |
|
69 | - // We can just do a normal seek since we've already seen this byte. |
|
70 | - $this->stream->seek($byte); |
|
71 | - } |
|
72 | - } |
|
73 | - public function read($length) : string |
|
74 | - { |
|
75 | - // Perform a regular read on any previously read data from the buffer |
|
76 | - $data = $this->stream->read($length); |
|
77 | - $remaining = $length - \strlen($data); |
|
78 | - // More data was requested so read from the remote stream |
|
79 | - if ($remaining) { |
|
80 | - // If data was written to the buffer in a position that would have |
|
81 | - // been filled from the remote stream, then we must skip bytes on |
|
82 | - // the remote stream to emulate overwriting bytes from that |
|
83 | - // position. This mimics the behavior of other PHP stream wrappers. |
|
84 | - $remoteData = $this->remoteStream->read($remaining + $this->skipReadBytes); |
|
85 | - if ($this->skipReadBytes) { |
|
86 | - $len = \strlen($remoteData); |
|
87 | - $remoteData = \substr($remoteData, $this->skipReadBytes); |
|
88 | - $this->skipReadBytes = \max(0, $this->skipReadBytes - $len); |
|
89 | - } |
|
90 | - $data .= $remoteData; |
|
91 | - $this->stream->write($remoteData); |
|
92 | - } |
|
93 | - return $data; |
|
94 | - } |
|
95 | - public function write($string) : int |
|
96 | - { |
|
97 | - // When appending to the end of the currently read stream, you'll want |
|
98 | - // to skip bytes from being read from the remote stream to emulate |
|
99 | - // other stream wrappers. Basically replacing bytes of data of a fixed |
|
100 | - // length. |
|
101 | - $overflow = \strlen($string) + $this->tell() - $this->remoteStream->tell(); |
|
102 | - if ($overflow > 0) { |
|
103 | - $this->skipReadBytes += $overflow; |
|
104 | - } |
|
105 | - return $this->stream->write($string); |
|
106 | - } |
|
107 | - public function eof() : bool |
|
108 | - { |
|
109 | - return $this->stream->eof() && $this->remoteStream->eof(); |
|
110 | - } |
|
111 | - /** |
|
112 | - * Close both the remote stream and buffer stream |
|
113 | - */ |
|
114 | - public function close() : void |
|
115 | - { |
|
116 | - $this->remoteStream->close(); |
|
117 | - $this->stream->close(); |
|
118 | - } |
|
119 | - private function cacheEntireStream() : int |
|
120 | - { |
|
121 | - $target = new FnStream(['write' => 'strlen']); |
|
122 | - Utils::copyToStream($this, $target); |
|
123 | - return $this->tell(); |
|
124 | - } |
|
13 | + use StreamDecoratorTrait; |
|
14 | + /** @var StreamInterface Stream being wrapped */ |
|
15 | + private $remoteStream; |
|
16 | + /** @var int Number of bytes to skip reading due to a write on the buffer */ |
|
17 | + private $skipReadBytes = 0; |
|
18 | + /** |
|
19 | + * @var StreamInterface |
|
20 | + */ |
|
21 | + private $stream; |
|
22 | + /** |
|
23 | + * We will treat the buffer object as the body of the stream |
|
24 | + * |
|
25 | + * @param StreamInterface $stream Stream to cache. The cursor is assumed to be at the beginning of the stream. |
|
26 | + * @param StreamInterface $target Optionally specify where data is cached |
|
27 | + */ |
|
28 | + public function __construct(StreamInterface $stream, StreamInterface $target = null) |
|
29 | + { |
|
30 | + $this->remoteStream = $stream; |
|
31 | + $this->stream = $target ?: new Stream(Utils::tryFopen('php://temp', 'r+')); |
|
32 | + } |
|
33 | + public function getSize() : ?int |
|
34 | + { |
|
35 | + $remoteSize = $this->remoteStream->getSize(); |
|
36 | + if (null === $remoteSize) { |
|
37 | + return null; |
|
38 | + } |
|
39 | + return \max($this->stream->getSize(), $remoteSize); |
|
40 | + } |
|
41 | + public function rewind() : void |
|
42 | + { |
|
43 | + $this->seek(0); |
|
44 | + } |
|
45 | + public function seek($offset, $whence = \SEEK_SET) : void |
|
46 | + { |
|
47 | + if ($whence === \SEEK_SET) { |
|
48 | + $byte = $offset; |
|
49 | + } elseif ($whence === \SEEK_CUR) { |
|
50 | + $byte = $offset + $this->tell(); |
|
51 | + } elseif ($whence === \SEEK_END) { |
|
52 | + $size = $this->remoteStream->getSize(); |
|
53 | + if ($size === null) { |
|
54 | + $size = $this->cacheEntireStream(); |
|
55 | + } |
|
56 | + $byte = $size + $offset; |
|
57 | + } else { |
|
58 | + throw new \InvalidArgumentException('Invalid whence'); |
|
59 | + } |
|
60 | + $diff = $byte - $this->stream->getSize(); |
|
61 | + if ($diff > 0) { |
|
62 | + // Read the remoteStream until we have read in at least the amount |
|
63 | + // of bytes requested, or we reach the end of the file. |
|
64 | + while ($diff > 0 && !$this->remoteStream->eof()) { |
|
65 | + $this->read($diff); |
|
66 | + $diff = $byte - $this->stream->getSize(); |
|
67 | + } |
|
68 | + } else { |
|
69 | + // We can just do a normal seek since we've already seen this byte. |
|
70 | + $this->stream->seek($byte); |
|
71 | + } |
|
72 | + } |
|
73 | + public function read($length) : string |
|
74 | + { |
|
75 | + // Perform a regular read on any previously read data from the buffer |
|
76 | + $data = $this->stream->read($length); |
|
77 | + $remaining = $length - \strlen($data); |
|
78 | + // More data was requested so read from the remote stream |
|
79 | + if ($remaining) { |
|
80 | + // If data was written to the buffer in a position that would have |
|
81 | + // been filled from the remote stream, then we must skip bytes on |
|
82 | + // the remote stream to emulate overwriting bytes from that |
|
83 | + // position. This mimics the behavior of other PHP stream wrappers. |
|
84 | + $remoteData = $this->remoteStream->read($remaining + $this->skipReadBytes); |
|
85 | + if ($this->skipReadBytes) { |
|
86 | + $len = \strlen($remoteData); |
|
87 | + $remoteData = \substr($remoteData, $this->skipReadBytes); |
|
88 | + $this->skipReadBytes = \max(0, $this->skipReadBytes - $len); |
|
89 | + } |
|
90 | + $data .= $remoteData; |
|
91 | + $this->stream->write($remoteData); |
|
92 | + } |
|
93 | + return $data; |
|
94 | + } |
|
95 | + public function write($string) : int |
|
96 | + { |
|
97 | + // When appending to the end of the currently read stream, you'll want |
|
98 | + // to skip bytes from being read from the remote stream to emulate |
|
99 | + // other stream wrappers. Basically replacing bytes of data of a fixed |
|
100 | + // length. |
|
101 | + $overflow = \strlen($string) + $this->tell() - $this->remoteStream->tell(); |
|
102 | + if ($overflow > 0) { |
|
103 | + $this->skipReadBytes += $overflow; |
|
104 | + } |
|
105 | + return $this->stream->write($string); |
|
106 | + } |
|
107 | + public function eof() : bool |
|
108 | + { |
|
109 | + return $this->stream->eof() && $this->remoteStream->eof(); |
|
110 | + } |
|
111 | + /** |
|
112 | + * Close both the remote stream and buffer stream |
|
113 | + */ |
|
114 | + public function close() : void |
|
115 | + { |
|
116 | + $this->remoteStream->close(); |
|
117 | + $this->stream->close(); |
|
118 | + } |
|
119 | + private function cacheEntireStream() : int |
|
120 | + { |
|
121 | + $target = new FnStream(['write' => 'strlen']); |
|
122 | + Utils::copyToStream($this, $target); |
|
123 | + return $this->tell(); |
|
124 | + } |
|
125 | 125 | } |
@@ -8,8 +8,7 @@ |
||
8 | 8 | * Stream decorator that can cache previously read bytes from a sequentially |
9 | 9 | * read stream. |
10 | 10 | */ |
11 | -final class CachingStream implements StreamInterface |
|
12 | -{ |
|
11 | +final class CachingStream implements StreamInterface { |
|
13 | 12 | use StreamDecoratorTrait; |
14 | 13 | /** @var StreamInterface Stream being wrapped */ |
15 | 14 | private $remoteStream; |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
5 | 5 | |
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
@@ -14,559 +14,559 @@ |
||
14 | 14 | */ |
15 | 15 | class Uri implements UriInterface, \JsonSerializable |
16 | 16 | { |
17 | - /** |
|
18 | - * Absolute http and https URIs require a host per RFC 7230 Section 2.7 |
|
19 | - * but in generic URIs the host can be empty. So for http(s) URIs |
|
20 | - * we apply this default host when no host is given yet to form a |
|
21 | - * valid URI. |
|
22 | - */ |
|
23 | - private const HTTP_DEFAULT_HOST = 'localhost'; |
|
24 | - private const DEFAULT_PORTS = ['http' => 80, 'https' => 443, 'ftp' => 21, 'gopher' => 70, 'nntp' => 119, 'news' => 119, 'telnet' => 23, 'tn3270' => 23, 'imap' => 143, 'pop' => 110, 'ldap' => 389]; |
|
25 | - /** |
|
26 | - * Unreserved characters for use in a regex. |
|
27 | - * |
|
28 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-2.3 |
|
29 | - */ |
|
30 | - private const CHAR_UNRESERVED = 'a-zA-Z0-9_\\-\\.~'; |
|
31 | - /** |
|
32 | - * Sub-delims for use in a regex. |
|
33 | - * |
|
34 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-2.2 |
|
35 | - */ |
|
36 | - private const CHAR_SUB_DELIMS = '!\\$&\'\\(\\)\\*\\+,;='; |
|
37 | - private const QUERY_SEPARATORS_REPLACEMENT = ['=' => '%3D', '&' => '%26']; |
|
38 | - /** @var string Uri scheme. */ |
|
39 | - private $scheme = ''; |
|
40 | - /** @var string Uri user info. */ |
|
41 | - private $userInfo = ''; |
|
42 | - /** @var string Uri host. */ |
|
43 | - private $host = ''; |
|
44 | - /** @var int|null Uri port. */ |
|
45 | - private $port; |
|
46 | - /** @var string Uri path. */ |
|
47 | - private $path = ''; |
|
48 | - /** @var string Uri query string. */ |
|
49 | - private $query = ''; |
|
50 | - /** @var string Uri fragment. */ |
|
51 | - private $fragment = ''; |
|
52 | - /** @var string|null String representation */ |
|
53 | - private $composedComponents; |
|
54 | - public function __construct(string $uri = '') |
|
55 | - { |
|
56 | - if ($uri !== '') { |
|
57 | - $parts = self::parse($uri); |
|
58 | - if ($parts === \false) { |
|
59 | - throw new MalformedUriException("Unable to parse URI: {$uri}"); |
|
60 | - } |
|
61 | - $this->applyParts($parts); |
|
62 | - } |
|
63 | - } |
|
64 | - /** |
|
65 | - * UTF-8 aware \parse_url() replacement. |
|
66 | - * |
|
67 | - * The internal function produces broken output for non ASCII domain names |
|
68 | - * (IDN) when used with locales other than "C". |
|
69 | - * |
|
70 | - * On the other hand, cURL understands IDN correctly only when UTF-8 locale |
|
71 | - * is configured ("C.UTF-8", "en_US.UTF-8", etc.). |
|
72 | - * |
|
73 | - * @see https://bugs.php.net/bug.php?id=52923 |
|
74 | - * @see https://www.php.net/manual/en/function.parse-url.php#114817 |
|
75 | - * @see https://curl.haxx.se/libcurl/c/CURLOPT_URL.html#ENCODING |
|
76 | - * |
|
77 | - * @return array|false |
|
78 | - */ |
|
79 | - private static function parse(string $url) |
|
80 | - { |
|
81 | - // If IPv6 |
|
82 | - $prefix = ''; |
|
83 | - if (\preg_match('%^(.*://\\[[0-9:a-f]+\\])(.*?)$%', $url, $matches)) { |
|
84 | - /** @var array{0:string, 1:string, 2:string} $matches */ |
|
85 | - $prefix = $matches[1]; |
|
86 | - $url = $matches[2]; |
|
87 | - } |
|
88 | - /** @var string */ |
|
89 | - $encodedUrl = \preg_replace_callback('%[^:/@?&=#]+%usD', static function ($matches) { |
|
90 | - return \urlencode($matches[0]); |
|
91 | - }, $url); |
|
92 | - $result = \parse_url($prefix . $encodedUrl); |
|
93 | - if ($result === \false) { |
|
94 | - return \false; |
|
95 | - } |
|
96 | - return \array_map('urldecode', $result); |
|
97 | - } |
|
98 | - public function __toString() : string |
|
99 | - { |
|
100 | - if ($this->composedComponents === null) { |
|
101 | - $this->composedComponents = self::composeComponents($this->scheme, $this->getAuthority(), $this->path, $this->query, $this->fragment); |
|
102 | - } |
|
103 | - return $this->composedComponents; |
|
104 | - } |
|
105 | - /** |
|
106 | - * Composes a URI reference string from its various components. |
|
107 | - * |
|
108 | - * Usually this method does not need to be called manually but instead is used indirectly via |
|
109 | - * `Psr\Http\Message\UriInterface::__toString`. |
|
110 | - * |
|
111 | - * PSR-7 UriInterface treats an empty component the same as a missing component as |
|
112 | - * getQuery(), getFragment() etc. always return a string. This explains the slight |
|
113 | - * difference to RFC 3986 Section 5.3. |
|
114 | - * |
|
115 | - * Another adjustment is that the authority separator is added even when the authority is missing/empty |
|
116 | - * for the "file" scheme. This is because PHP stream functions like `file_get_contents` only work with |
|
117 | - * `file:///myfile` but not with `file:/myfile` although they are equivalent according to RFC 3986. But |
|
118 | - * `file:///` is the more common syntax for the file scheme anyway (Chrome for example redirects to |
|
119 | - * that format). |
|
120 | - * |
|
121 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-5.3 |
|
122 | - */ |
|
123 | - public static function composeComponents(?string $scheme, ?string $authority, string $path, ?string $query, ?string $fragment) : string |
|
124 | - { |
|
125 | - $uri = ''; |
|
126 | - // weak type checks to also accept null until we can add scalar type hints |
|
127 | - if ($scheme != '') { |
|
128 | - $uri .= $scheme . ':'; |
|
129 | - } |
|
130 | - if ($authority != '' || $scheme === 'file') { |
|
131 | - $uri .= '//' . $authority; |
|
132 | - } |
|
133 | - if ($authority != '' && $path != '' && $path[0] != '/') { |
|
134 | - $path = '/' . $path; |
|
135 | - } |
|
136 | - $uri .= $path; |
|
137 | - if ($query != '') { |
|
138 | - $uri .= '?' . $query; |
|
139 | - } |
|
140 | - if ($fragment != '') { |
|
141 | - $uri .= '#' . $fragment; |
|
142 | - } |
|
143 | - return $uri; |
|
144 | - } |
|
145 | - /** |
|
146 | - * Whether the URI has the default port of the current scheme. |
|
147 | - * |
|
148 | - * `Psr\Http\Message\UriInterface::getPort` may return null or the standard port. This method can be used |
|
149 | - * independently of the implementation. |
|
150 | - */ |
|
151 | - public static function isDefaultPort(UriInterface $uri) : bool |
|
152 | - { |
|
153 | - return $uri->getPort() === null || isset(self::DEFAULT_PORTS[$uri->getScheme()]) && $uri->getPort() === self::DEFAULT_PORTS[$uri->getScheme()]; |
|
154 | - } |
|
155 | - /** |
|
156 | - * Whether the URI is absolute, i.e. it has a scheme. |
|
157 | - * |
|
158 | - * An instance of UriInterface can either be an absolute URI or a relative reference. This method returns true |
|
159 | - * if it is the former. An absolute URI has a scheme. A relative reference is used to express a URI relative |
|
160 | - * to another URI, the base URI. Relative references can be divided into several forms: |
|
161 | - * - network-path references, e.g. '//example.com/path' |
|
162 | - * - absolute-path references, e.g. '/path' |
|
163 | - * - relative-path references, e.g. 'subpath' |
|
164 | - * |
|
165 | - * @see Uri::isNetworkPathReference |
|
166 | - * @see Uri::isAbsolutePathReference |
|
167 | - * @see Uri::isRelativePathReference |
|
168 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-4 |
|
169 | - */ |
|
170 | - public static function isAbsolute(UriInterface $uri) : bool |
|
171 | - { |
|
172 | - return $uri->getScheme() !== ''; |
|
173 | - } |
|
174 | - /** |
|
175 | - * Whether the URI is a network-path reference. |
|
176 | - * |
|
177 | - * A relative reference that begins with two slash characters is termed an network-path reference. |
|
178 | - * |
|
179 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.2 |
|
180 | - */ |
|
181 | - public static function isNetworkPathReference(UriInterface $uri) : bool |
|
182 | - { |
|
183 | - return $uri->getScheme() === '' && $uri->getAuthority() !== ''; |
|
184 | - } |
|
185 | - /** |
|
186 | - * Whether the URI is a absolute-path reference. |
|
187 | - * |
|
188 | - * A relative reference that begins with a single slash character is termed an absolute-path reference. |
|
189 | - * |
|
190 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.2 |
|
191 | - */ |
|
192 | - public static function isAbsolutePathReference(UriInterface $uri) : bool |
|
193 | - { |
|
194 | - return $uri->getScheme() === '' && $uri->getAuthority() === '' && isset($uri->getPath()[0]) && $uri->getPath()[0] === '/'; |
|
195 | - } |
|
196 | - /** |
|
197 | - * Whether the URI is a relative-path reference. |
|
198 | - * |
|
199 | - * A relative reference that does not begin with a slash character is termed a relative-path reference. |
|
200 | - * |
|
201 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.2 |
|
202 | - */ |
|
203 | - public static function isRelativePathReference(UriInterface $uri) : bool |
|
204 | - { |
|
205 | - return $uri->getScheme() === '' && $uri->getAuthority() === '' && (!isset($uri->getPath()[0]) || $uri->getPath()[0] !== '/'); |
|
206 | - } |
|
207 | - /** |
|
208 | - * Whether the URI is a same-document reference. |
|
209 | - * |
|
210 | - * A same-document reference refers to a URI that is, aside from its fragment |
|
211 | - * component, identical to the base URI. When no base URI is given, only an empty |
|
212 | - * URI reference (apart from its fragment) is considered a same-document reference. |
|
213 | - * |
|
214 | - * @param UriInterface $uri The URI to check |
|
215 | - * @param UriInterface|null $base An optional base URI to compare against |
|
216 | - * |
|
217 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.4 |
|
218 | - */ |
|
219 | - public static function isSameDocumentReference(UriInterface $uri, UriInterface $base = null) : bool |
|
220 | - { |
|
221 | - if ($base !== null) { |
|
222 | - $uri = UriResolver::resolve($base, $uri); |
|
223 | - return $uri->getScheme() === $base->getScheme() && $uri->getAuthority() === $base->getAuthority() && $uri->getPath() === $base->getPath() && $uri->getQuery() === $base->getQuery(); |
|
224 | - } |
|
225 | - return $uri->getScheme() === '' && $uri->getAuthority() === '' && $uri->getPath() === '' && $uri->getQuery() === ''; |
|
226 | - } |
|
227 | - /** |
|
228 | - * Creates a new URI with a specific query string value removed. |
|
229 | - * |
|
230 | - * Any existing query string values that exactly match the provided key are |
|
231 | - * removed. |
|
232 | - * |
|
233 | - * @param UriInterface $uri URI to use as a base. |
|
234 | - * @param string $key Query string key to remove. |
|
235 | - */ |
|
236 | - public static function withoutQueryValue(UriInterface $uri, string $key) : UriInterface |
|
237 | - { |
|
238 | - $result = self::getFilteredQueryString($uri, [$key]); |
|
239 | - return $uri->withQuery(\implode('&', $result)); |
|
240 | - } |
|
241 | - /** |
|
242 | - * Creates a new URI with a specific query string value. |
|
243 | - * |
|
244 | - * Any existing query string values that exactly match the provided key are |
|
245 | - * removed and replaced with the given key value pair. |
|
246 | - * |
|
247 | - * A value of null will set the query string key without a value, e.g. "key" |
|
248 | - * instead of "key=value". |
|
249 | - * |
|
250 | - * @param UriInterface $uri URI to use as a base. |
|
251 | - * @param string $key Key to set. |
|
252 | - * @param string|null $value Value to set |
|
253 | - */ |
|
254 | - public static function withQueryValue(UriInterface $uri, string $key, ?string $value) : UriInterface |
|
255 | - { |
|
256 | - $result = self::getFilteredQueryString($uri, [$key]); |
|
257 | - $result[] = self::generateQueryString($key, $value); |
|
258 | - return $uri->withQuery(\implode('&', $result)); |
|
259 | - } |
|
260 | - /** |
|
261 | - * Creates a new URI with multiple specific query string values. |
|
262 | - * |
|
263 | - * It has the same behavior as withQueryValue() but for an associative array of key => value. |
|
264 | - * |
|
265 | - * @param UriInterface $uri URI to use as a base. |
|
266 | - * @param (string|null)[] $keyValueArray Associative array of key and values |
|
267 | - */ |
|
268 | - public static function withQueryValues(UriInterface $uri, array $keyValueArray) : UriInterface |
|
269 | - { |
|
270 | - $result = self::getFilteredQueryString($uri, \array_keys($keyValueArray)); |
|
271 | - foreach ($keyValueArray as $key => $value) { |
|
272 | - $result[] = self::generateQueryString((string) $key, $value !== null ? (string) $value : null); |
|
273 | - } |
|
274 | - return $uri->withQuery(\implode('&', $result)); |
|
275 | - } |
|
276 | - /** |
|
277 | - * Creates a URI from a hash of `parse_url` components. |
|
278 | - * |
|
279 | - * @see https://www.php.net/manual/en/function.parse-url.php |
|
280 | - * |
|
281 | - * @throws MalformedUriException If the components do not form a valid URI. |
|
282 | - */ |
|
283 | - public static function fromParts(array $parts) : UriInterface |
|
284 | - { |
|
285 | - $uri = new self(); |
|
286 | - $uri->applyParts($parts); |
|
287 | - $uri->validateState(); |
|
288 | - return $uri; |
|
289 | - } |
|
290 | - public function getScheme() : string |
|
291 | - { |
|
292 | - return $this->scheme; |
|
293 | - } |
|
294 | - public function getAuthority() : string |
|
295 | - { |
|
296 | - $authority = $this->host; |
|
297 | - if ($this->userInfo !== '') { |
|
298 | - $authority = $this->userInfo . '@' . $authority; |
|
299 | - } |
|
300 | - if ($this->port !== null) { |
|
301 | - $authority .= ':' . $this->port; |
|
302 | - } |
|
303 | - return $authority; |
|
304 | - } |
|
305 | - public function getUserInfo() : string |
|
306 | - { |
|
307 | - return $this->userInfo; |
|
308 | - } |
|
309 | - public function getHost() : string |
|
310 | - { |
|
311 | - return $this->host; |
|
312 | - } |
|
313 | - public function getPort() : ?int |
|
314 | - { |
|
315 | - return $this->port; |
|
316 | - } |
|
317 | - public function getPath() : string |
|
318 | - { |
|
319 | - return $this->path; |
|
320 | - } |
|
321 | - public function getQuery() : string |
|
322 | - { |
|
323 | - return $this->query; |
|
324 | - } |
|
325 | - public function getFragment() : string |
|
326 | - { |
|
327 | - return $this->fragment; |
|
328 | - } |
|
329 | - public function withScheme($scheme) : UriInterface |
|
330 | - { |
|
331 | - $scheme = $this->filterScheme($scheme); |
|
332 | - if ($this->scheme === $scheme) { |
|
333 | - return $this; |
|
334 | - } |
|
335 | - $new = clone $this; |
|
336 | - $new->scheme = $scheme; |
|
337 | - $new->composedComponents = null; |
|
338 | - $new->removeDefaultPort(); |
|
339 | - $new->validateState(); |
|
340 | - return $new; |
|
341 | - } |
|
342 | - public function withUserInfo($user, $password = null) : UriInterface |
|
343 | - { |
|
344 | - $info = $this->filterUserInfoComponent($user); |
|
345 | - if ($password !== null) { |
|
346 | - $info .= ':' . $this->filterUserInfoComponent($password); |
|
347 | - } |
|
348 | - if ($this->userInfo === $info) { |
|
349 | - return $this; |
|
350 | - } |
|
351 | - $new = clone $this; |
|
352 | - $new->userInfo = $info; |
|
353 | - $new->composedComponents = null; |
|
354 | - $new->validateState(); |
|
355 | - return $new; |
|
356 | - } |
|
357 | - public function withHost($host) : UriInterface |
|
358 | - { |
|
359 | - $host = $this->filterHost($host); |
|
360 | - if ($this->host === $host) { |
|
361 | - return $this; |
|
362 | - } |
|
363 | - $new = clone $this; |
|
364 | - $new->host = $host; |
|
365 | - $new->composedComponents = null; |
|
366 | - $new->validateState(); |
|
367 | - return $new; |
|
368 | - } |
|
369 | - public function withPort($port) : UriInterface |
|
370 | - { |
|
371 | - $port = $this->filterPort($port); |
|
372 | - if ($this->port === $port) { |
|
373 | - return $this; |
|
374 | - } |
|
375 | - $new = clone $this; |
|
376 | - $new->port = $port; |
|
377 | - $new->composedComponents = null; |
|
378 | - $new->removeDefaultPort(); |
|
379 | - $new->validateState(); |
|
380 | - return $new; |
|
381 | - } |
|
382 | - public function withPath($path) : UriInterface |
|
383 | - { |
|
384 | - $path = $this->filterPath($path); |
|
385 | - if ($this->path === $path) { |
|
386 | - return $this; |
|
387 | - } |
|
388 | - $new = clone $this; |
|
389 | - $new->path = $path; |
|
390 | - $new->composedComponents = null; |
|
391 | - $new->validateState(); |
|
392 | - return $new; |
|
393 | - } |
|
394 | - public function withQuery($query) : UriInterface |
|
395 | - { |
|
396 | - $query = $this->filterQueryAndFragment($query); |
|
397 | - if ($this->query === $query) { |
|
398 | - return $this; |
|
399 | - } |
|
400 | - $new = clone $this; |
|
401 | - $new->query = $query; |
|
402 | - $new->composedComponents = null; |
|
403 | - return $new; |
|
404 | - } |
|
405 | - public function withFragment($fragment) : UriInterface |
|
406 | - { |
|
407 | - $fragment = $this->filterQueryAndFragment($fragment); |
|
408 | - if ($this->fragment === $fragment) { |
|
409 | - return $this; |
|
410 | - } |
|
411 | - $new = clone $this; |
|
412 | - $new->fragment = $fragment; |
|
413 | - $new->composedComponents = null; |
|
414 | - return $new; |
|
415 | - } |
|
416 | - public function jsonSerialize() : string |
|
417 | - { |
|
418 | - return $this->__toString(); |
|
419 | - } |
|
420 | - /** |
|
421 | - * Apply parse_url parts to a URI. |
|
422 | - * |
|
423 | - * @param array $parts Array of parse_url parts to apply. |
|
424 | - */ |
|
425 | - private function applyParts(array $parts) : void |
|
426 | - { |
|
427 | - $this->scheme = isset($parts['scheme']) ? $this->filterScheme($parts['scheme']) : ''; |
|
428 | - $this->userInfo = isset($parts['user']) ? $this->filterUserInfoComponent($parts['user']) : ''; |
|
429 | - $this->host = isset($parts['host']) ? $this->filterHost($parts['host']) : ''; |
|
430 | - $this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null; |
|
431 | - $this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : ''; |
|
432 | - $this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : ''; |
|
433 | - $this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : ''; |
|
434 | - if (isset($parts['pass'])) { |
|
435 | - $this->userInfo .= ':' . $this->filterUserInfoComponent($parts['pass']); |
|
436 | - } |
|
437 | - $this->removeDefaultPort(); |
|
438 | - } |
|
439 | - /** |
|
440 | - * @param mixed $scheme |
|
441 | - * |
|
442 | - * @throws \InvalidArgumentException If the scheme is invalid. |
|
443 | - */ |
|
444 | - private function filterScheme($scheme) : string |
|
445 | - { |
|
446 | - if (!\is_string($scheme)) { |
|
447 | - throw new \InvalidArgumentException('Scheme must be a string'); |
|
448 | - } |
|
449 | - return \strtr($scheme, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); |
|
450 | - } |
|
451 | - /** |
|
452 | - * @param mixed $component |
|
453 | - * |
|
454 | - * @throws \InvalidArgumentException If the user info is invalid. |
|
455 | - */ |
|
456 | - private function filterUserInfoComponent($component) : string |
|
457 | - { |
|
458 | - if (!\is_string($component)) { |
|
459 | - throw new \InvalidArgumentException('User info must be a string'); |
|
460 | - } |
|
461 | - return \preg_replace_callback('/(?:[^%' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . ']+|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $component); |
|
462 | - } |
|
463 | - /** |
|
464 | - * @param mixed $host |
|
465 | - * |
|
466 | - * @throws \InvalidArgumentException If the host is invalid. |
|
467 | - */ |
|
468 | - private function filterHost($host) : string |
|
469 | - { |
|
470 | - if (!\is_string($host)) { |
|
471 | - throw new \InvalidArgumentException('Host must be a string'); |
|
472 | - } |
|
473 | - return \strtr($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); |
|
474 | - } |
|
475 | - /** |
|
476 | - * @param mixed $port |
|
477 | - * |
|
478 | - * @throws \InvalidArgumentException If the port is invalid. |
|
479 | - */ |
|
480 | - private function filterPort($port) : ?int |
|
481 | - { |
|
482 | - if ($port === null) { |
|
483 | - return null; |
|
484 | - } |
|
485 | - $port = (int) $port; |
|
486 | - if (0 > $port || 0xffff < $port) { |
|
487 | - throw new \InvalidArgumentException(\sprintf('Invalid port: %d. Must be between 0 and 65535', $port)); |
|
488 | - } |
|
489 | - return $port; |
|
490 | - } |
|
491 | - /** |
|
492 | - * @param (string|int)[] $keys |
|
493 | - * |
|
494 | - * @return string[] |
|
495 | - */ |
|
496 | - private static function getFilteredQueryString(UriInterface $uri, array $keys) : array |
|
497 | - { |
|
498 | - $current = $uri->getQuery(); |
|
499 | - if ($current === '') { |
|
500 | - return []; |
|
501 | - } |
|
502 | - $decodedKeys = \array_map(function ($k) : string { |
|
503 | - return \rawurldecode((string) $k); |
|
504 | - }, $keys); |
|
505 | - return \array_filter(\explode('&', $current), function ($part) use($decodedKeys) { |
|
506 | - return !\in_array(\rawurldecode(\explode('=', $part)[0]), $decodedKeys, \true); |
|
507 | - }); |
|
508 | - } |
|
509 | - private static function generateQueryString(string $key, ?string $value) : string |
|
510 | - { |
|
511 | - // Query string separators ("=", "&") within the key or value need to be encoded |
|
512 | - // (while preventing double-encoding) before setting the query string. All other |
|
513 | - // chars that need percent-encoding will be encoded by withQuery(). |
|
514 | - $queryString = \strtr($key, self::QUERY_SEPARATORS_REPLACEMENT); |
|
515 | - if ($value !== null) { |
|
516 | - $queryString .= '=' . \strtr($value, self::QUERY_SEPARATORS_REPLACEMENT); |
|
517 | - } |
|
518 | - return $queryString; |
|
519 | - } |
|
520 | - private function removeDefaultPort() : void |
|
521 | - { |
|
522 | - if ($this->port !== null && self::isDefaultPort($this)) { |
|
523 | - $this->port = null; |
|
524 | - } |
|
525 | - } |
|
526 | - /** |
|
527 | - * Filters the path of a URI |
|
528 | - * |
|
529 | - * @param mixed $path |
|
530 | - * |
|
531 | - * @throws \InvalidArgumentException If the path is invalid. |
|
532 | - */ |
|
533 | - private function filterPath($path) : string |
|
534 | - { |
|
535 | - if (!\is_string($path)) { |
|
536 | - throw new \InvalidArgumentException('Path must be a string'); |
|
537 | - } |
|
538 | - return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\\/]++|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $path); |
|
539 | - } |
|
540 | - /** |
|
541 | - * Filters the query string or fragment of a URI. |
|
542 | - * |
|
543 | - * @param mixed $str |
|
544 | - * |
|
545 | - * @throws \InvalidArgumentException If the query or fragment is invalid. |
|
546 | - */ |
|
547 | - private function filterQueryAndFragment($str) : string |
|
548 | - { |
|
549 | - if (!\is_string($str)) { |
|
550 | - throw new \InvalidArgumentException('Query and fragment must be a string'); |
|
551 | - } |
|
552 | - return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\\/\\?]++|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $str); |
|
553 | - } |
|
554 | - private function rawurlencodeMatchZero(array $match) : string |
|
555 | - { |
|
556 | - return \rawurlencode($match[0]); |
|
557 | - } |
|
558 | - private function validateState() : void |
|
559 | - { |
|
560 | - if ($this->host === '' && ($this->scheme === 'http' || $this->scheme === 'https')) { |
|
561 | - $this->host = self::HTTP_DEFAULT_HOST; |
|
562 | - } |
|
563 | - if ($this->getAuthority() === '') { |
|
564 | - if (0 === \strpos($this->path, '//')) { |
|
565 | - throw new MalformedUriException('The path of a URI without an authority must not start with two slashes "//"'); |
|
566 | - } |
|
567 | - if ($this->scheme === '' && \false !== \strpos(\explode('/', $this->path, 2)[0], ':')) { |
|
568 | - throw new MalformedUriException('A relative URI must not have a path beginning with a segment containing a colon'); |
|
569 | - } |
|
570 | - } |
|
571 | - } |
|
17 | + /** |
|
18 | + * Absolute http and https URIs require a host per RFC 7230 Section 2.7 |
|
19 | + * but in generic URIs the host can be empty. So for http(s) URIs |
|
20 | + * we apply this default host when no host is given yet to form a |
|
21 | + * valid URI. |
|
22 | + */ |
|
23 | + private const HTTP_DEFAULT_HOST = 'localhost'; |
|
24 | + private const DEFAULT_PORTS = ['http' => 80, 'https' => 443, 'ftp' => 21, 'gopher' => 70, 'nntp' => 119, 'news' => 119, 'telnet' => 23, 'tn3270' => 23, 'imap' => 143, 'pop' => 110, 'ldap' => 389]; |
|
25 | + /** |
|
26 | + * Unreserved characters for use in a regex. |
|
27 | + * |
|
28 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-2.3 |
|
29 | + */ |
|
30 | + private const CHAR_UNRESERVED = 'a-zA-Z0-9_\\-\\.~'; |
|
31 | + /** |
|
32 | + * Sub-delims for use in a regex. |
|
33 | + * |
|
34 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-2.2 |
|
35 | + */ |
|
36 | + private const CHAR_SUB_DELIMS = '!\\$&\'\\(\\)\\*\\+,;='; |
|
37 | + private const QUERY_SEPARATORS_REPLACEMENT = ['=' => '%3D', '&' => '%26']; |
|
38 | + /** @var string Uri scheme. */ |
|
39 | + private $scheme = ''; |
|
40 | + /** @var string Uri user info. */ |
|
41 | + private $userInfo = ''; |
|
42 | + /** @var string Uri host. */ |
|
43 | + private $host = ''; |
|
44 | + /** @var int|null Uri port. */ |
|
45 | + private $port; |
|
46 | + /** @var string Uri path. */ |
|
47 | + private $path = ''; |
|
48 | + /** @var string Uri query string. */ |
|
49 | + private $query = ''; |
|
50 | + /** @var string Uri fragment. */ |
|
51 | + private $fragment = ''; |
|
52 | + /** @var string|null String representation */ |
|
53 | + private $composedComponents; |
|
54 | + public function __construct(string $uri = '') |
|
55 | + { |
|
56 | + if ($uri !== '') { |
|
57 | + $parts = self::parse($uri); |
|
58 | + if ($parts === \false) { |
|
59 | + throw new MalformedUriException("Unable to parse URI: {$uri}"); |
|
60 | + } |
|
61 | + $this->applyParts($parts); |
|
62 | + } |
|
63 | + } |
|
64 | + /** |
|
65 | + * UTF-8 aware \parse_url() replacement. |
|
66 | + * |
|
67 | + * The internal function produces broken output for non ASCII domain names |
|
68 | + * (IDN) when used with locales other than "C". |
|
69 | + * |
|
70 | + * On the other hand, cURL understands IDN correctly only when UTF-8 locale |
|
71 | + * is configured ("C.UTF-8", "en_US.UTF-8", etc.). |
|
72 | + * |
|
73 | + * @see https://bugs.php.net/bug.php?id=52923 |
|
74 | + * @see https://www.php.net/manual/en/function.parse-url.php#114817 |
|
75 | + * @see https://curl.haxx.se/libcurl/c/CURLOPT_URL.html#ENCODING |
|
76 | + * |
|
77 | + * @return array|false |
|
78 | + */ |
|
79 | + private static function parse(string $url) |
|
80 | + { |
|
81 | + // If IPv6 |
|
82 | + $prefix = ''; |
|
83 | + if (\preg_match('%^(.*://\\[[0-9:a-f]+\\])(.*?)$%', $url, $matches)) { |
|
84 | + /** @var array{0:string, 1:string, 2:string} $matches */ |
|
85 | + $prefix = $matches[1]; |
|
86 | + $url = $matches[2]; |
|
87 | + } |
|
88 | + /** @var string */ |
|
89 | + $encodedUrl = \preg_replace_callback('%[^:/@?&=#]+%usD', static function ($matches) { |
|
90 | + return \urlencode($matches[0]); |
|
91 | + }, $url); |
|
92 | + $result = \parse_url($prefix . $encodedUrl); |
|
93 | + if ($result === \false) { |
|
94 | + return \false; |
|
95 | + } |
|
96 | + return \array_map('urldecode', $result); |
|
97 | + } |
|
98 | + public function __toString() : string |
|
99 | + { |
|
100 | + if ($this->composedComponents === null) { |
|
101 | + $this->composedComponents = self::composeComponents($this->scheme, $this->getAuthority(), $this->path, $this->query, $this->fragment); |
|
102 | + } |
|
103 | + return $this->composedComponents; |
|
104 | + } |
|
105 | + /** |
|
106 | + * Composes a URI reference string from its various components. |
|
107 | + * |
|
108 | + * Usually this method does not need to be called manually but instead is used indirectly via |
|
109 | + * `Psr\Http\Message\UriInterface::__toString`. |
|
110 | + * |
|
111 | + * PSR-7 UriInterface treats an empty component the same as a missing component as |
|
112 | + * getQuery(), getFragment() etc. always return a string. This explains the slight |
|
113 | + * difference to RFC 3986 Section 5.3. |
|
114 | + * |
|
115 | + * Another adjustment is that the authority separator is added even when the authority is missing/empty |
|
116 | + * for the "file" scheme. This is because PHP stream functions like `file_get_contents` only work with |
|
117 | + * `file:///myfile` but not with `file:/myfile` although they are equivalent according to RFC 3986. But |
|
118 | + * `file:///` is the more common syntax for the file scheme anyway (Chrome for example redirects to |
|
119 | + * that format). |
|
120 | + * |
|
121 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-5.3 |
|
122 | + */ |
|
123 | + public static function composeComponents(?string $scheme, ?string $authority, string $path, ?string $query, ?string $fragment) : string |
|
124 | + { |
|
125 | + $uri = ''; |
|
126 | + // weak type checks to also accept null until we can add scalar type hints |
|
127 | + if ($scheme != '') { |
|
128 | + $uri .= $scheme . ':'; |
|
129 | + } |
|
130 | + if ($authority != '' || $scheme === 'file') { |
|
131 | + $uri .= '//' . $authority; |
|
132 | + } |
|
133 | + if ($authority != '' && $path != '' && $path[0] != '/') { |
|
134 | + $path = '/' . $path; |
|
135 | + } |
|
136 | + $uri .= $path; |
|
137 | + if ($query != '') { |
|
138 | + $uri .= '?' . $query; |
|
139 | + } |
|
140 | + if ($fragment != '') { |
|
141 | + $uri .= '#' . $fragment; |
|
142 | + } |
|
143 | + return $uri; |
|
144 | + } |
|
145 | + /** |
|
146 | + * Whether the URI has the default port of the current scheme. |
|
147 | + * |
|
148 | + * `Psr\Http\Message\UriInterface::getPort` may return null or the standard port. This method can be used |
|
149 | + * independently of the implementation. |
|
150 | + */ |
|
151 | + public static function isDefaultPort(UriInterface $uri) : bool |
|
152 | + { |
|
153 | + return $uri->getPort() === null || isset(self::DEFAULT_PORTS[$uri->getScheme()]) && $uri->getPort() === self::DEFAULT_PORTS[$uri->getScheme()]; |
|
154 | + } |
|
155 | + /** |
|
156 | + * Whether the URI is absolute, i.e. it has a scheme. |
|
157 | + * |
|
158 | + * An instance of UriInterface can either be an absolute URI or a relative reference. This method returns true |
|
159 | + * if it is the former. An absolute URI has a scheme. A relative reference is used to express a URI relative |
|
160 | + * to another URI, the base URI. Relative references can be divided into several forms: |
|
161 | + * - network-path references, e.g. '//example.com/path' |
|
162 | + * - absolute-path references, e.g. '/path' |
|
163 | + * - relative-path references, e.g. 'subpath' |
|
164 | + * |
|
165 | + * @see Uri::isNetworkPathReference |
|
166 | + * @see Uri::isAbsolutePathReference |
|
167 | + * @see Uri::isRelativePathReference |
|
168 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-4 |
|
169 | + */ |
|
170 | + public static function isAbsolute(UriInterface $uri) : bool |
|
171 | + { |
|
172 | + return $uri->getScheme() !== ''; |
|
173 | + } |
|
174 | + /** |
|
175 | + * Whether the URI is a network-path reference. |
|
176 | + * |
|
177 | + * A relative reference that begins with two slash characters is termed an network-path reference. |
|
178 | + * |
|
179 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.2 |
|
180 | + */ |
|
181 | + public static function isNetworkPathReference(UriInterface $uri) : bool |
|
182 | + { |
|
183 | + return $uri->getScheme() === '' && $uri->getAuthority() !== ''; |
|
184 | + } |
|
185 | + /** |
|
186 | + * Whether the URI is a absolute-path reference. |
|
187 | + * |
|
188 | + * A relative reference that begins with a single slash character is termed an absolute-path reference. |
|
189 | + * |
|
190 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.2 |
|
191 | + */ |
|
192 | + public static function isAbsolutePathReference(UriInterface $uri) : bool |
|
193 | + { |
|
194 | + return $uri->getScheme() === '' && $uri->getAuthority() === '' && isset($uri->getPath()[0]) && $uri->getPath()[0] === '/'; |
|
195 | + } |
|
196 | + /** |
|
197 | + * Whether the URI is a relative-path reference. |
|
198 | + * |
|
199 | + * A relative reference that does not begin with a slash character is termed a relative-path reference. |
|
200 | + * |
|
201 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.2 |
|
202 | + */ |
|
203 | + public static function isRelativePathReference(UriInterface $uri) : bool |
|
204 | + { |
|
205 | + return $uri->getScheme() === '' && $uri->getAuthority() === '' && (!isset($uri->getPath()[0]) || $uri->getPath()[0] !== '/'); |
|
206 | + } |
|
207 | + /** |
|
208 | + * Whether the URI is a same-document reference. |
|
209 | + * |
|
210 | + * A same-document reference refers to a URI that is, aside from its fragment |
|
211 | + * component, identical to the base URI. When no base URI is given, only an empty |
|
212 | + * URI reference (apart from its fragment) is considered a same-document reference. |
|
213 | + * |
|
214 | + * @param UriInterface $uri The URI to check |
|
215 | + * @param UriInterface|null $base An optional base URI to compare against |
|
216 | + * |
|
217 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-4.4 |
|
218 | + */ |
|
219 | + public static function isSameDocumentReference(UriInterface $uri, UriInterface $base = null) : bool |
|
220 | + { |
|
221 | + if ($base !== null) { |
|
222 | + $uri = UriResolver::resolve($base, $uri); |
|
223 | + return $uri->getScheme() === $base->getScheme() && $uri->getAuthority() === $base->getAuthority() && $uri->getPath() === $base->getPath() && $uri->getQuery() === $base->getQuery(); |
|
224 | + } |
|
225 | + return $uri->getScheme() === '' && $uri->getAuthority() === '' && $uri->getPath() === '' && $uri->getQuery() === ''; |
|
226 | + } |
|
227 | + /** |
|
228 | + * Creates a new URI with a specific query string value removed. |
|
229 | + * |
|
230 | + * Any existing query string values that exactly match the provided key are |
|
231 | + * removed. |
|
232 | + * |
|
233 | + * @param UriInterface $uri URI to use as a base. |
|
234 | + * @param string $key Query string key to remove. |
|
235 | + */ |
|
236 | + public static function withoutQueryValue(UriInterface $uri, string $key) : UriInterface |
|
237 | + { |
|
238 | + $result = self::getFilteredQueryString($uri, [$key]); |
|
239 | + return $uri->withQuery(\implode('&', $result)); |
|
240 | + } |
|
241 | + /** |
|
242 | + * Creates a new URI with a specific query string value. |
|
243 | + * |
|
244 | + * Any existing query string values that exactly match the provided key are |
|
245 | + * removed and replaced with the given key value pair. |
|
246 | + * |
|
247 | + * A value of null will set the query string key without a value, e.g. "key" |
|
248 | + * instead of "key=value". |
|
249 | + * |
|
250 | + * @param UriInterface $uri URI to use as a base. |
|
251 | + * @param string $key Key to set. |
|
252 | + * @param string|null $value Value to set |
|
253 | + */ |
|
254 | + public static function withQueryValue(UriInterface $uri, string $key, ?string $value) : UriInterface |
|
255 | + { |
|
256 | + $result = self::getFilteredQueryString($uri, [$key]); |
|
257 | + $result[] = self::generateQueryString($key, $value); |
|
258 | + return $uri->withQuery(\implode('&', $result)); |
|
259 | + } |
|
260 | + /** |
|
261 | + * Creates a new URI with multiple specific query string values. |
|
262 | + * |
|
263 | + * It has the same behavior as withQueryValue() but for an associative array of key => value. |
|
264 | + * |
|
265 | + * @param UriInterface $uri URI to use as a base. |
|
266 | + * @param (string|null)[] $keyValueArray Associative array of key and values |
|
267 | + */ |
|
268 | + public static function withQueryValues(UriInterface $uri, array $keyValueArray) : UriInterface |
|
269 | + { |
|
270 | + $result = self::getFilteredQueryString($uri, \array_keys($keyValueArray)); |
|
271 | + foreach ($keyValueArray as $key => $value) { |
|
272 | + $result[] = self::generateQueryString((string) $key, $value !== null ? (string) $value : null); |
|
273 | + } |
|
274 | + return $uri->withQuery(\implode('&', $result)); |
|
275 | + } |
|
276 | + /** |
|
277 | + * Creates a URI from a hash of `parse_url` components. |
|
278 | + * |
|
279 | + * @see https://www.php.net/manual/en/function.parse-url.php |
|
280 | + * |
|
281 | + * @throws MalformedUriException If the components do not form a valid URI. |
|
282 | + */ |
|
283 | + public static function fromParts(array $parts) : UriInterface |
|
284 | + { |
|
285 | + $uri = new self(); |
|
286 | + $uri->applyParts($parts); |
|
287 | + $uri->validateState(); |
|
288 | + return $uri; |
|
289 | + } |
|
290 | + public function getScheme() : string |
|
291 | + { |
|
292 | + return $this->scheme; |
|
293 | + } |
|
294 | + public function getAuthority() : string |
|
295 | + { |
|
296 | + $authority = $this->host; |
|
297 | + if ($this->userInfo !== '') { |
|
298 | + $authority = $this->userInfo . '@' . $authority; |
|
299 | + } |
|
300 | + if ($this->port !== null) { |
|
301 | + $authority .= ':' . $this->port; |
|
302 | + } |
|
303 | + return $authority; |
|
304 | + } |
|
305 | + public function getUserInfo() : string |
|
306 | + { |
|
307 | + return $this->userInfo; |
|
308 | + } |
|
309 | + public function getHost() : string |
|
310 | + { |
|
311 | + return $this->host; |
|
312 | + } |
|
313 | + public function getPort() : ?int |
|
314 | + { |
|
315 | + return $this->port; |
|
316 | + } |
|
317 | + public function getPath() : string |
|
318 | + { |
|
319 | + return $this->path; |
|
320 | + } |
|
321 | + public function getQuery() : string |
|
322 | + { |
|
323 | + return $this->query; |
|
324 | + } |
|
325 | + public function getFragment() : string |
|
326 | + { |
|
327 | + return $this->fragment; |
|
328 | + } |
|
329 | + public function withScheme($scheme) : UriInterface |
|
330 | + { |
|
331 | + $scheme = $this->filterScheme($scheme); |
|
332 | + if ($this->scheme === $scheme) { |
|
333 | + return $this; |
|
334 | + } |
|
335 | + $new = clone $this; |
|
336 | + $new->scheme = $scheme; |
|
337 | + $new->composedComponents = null; |
|
338 | + $new->removeDefaultPort(); |
|
339 | + $new->validateState(); |
|
340 | + return $new; |
|
341 | + } |
|
342 | + public function withUserInfo($user, $password = null) : UriInterface |
|
343 | + { |
|
344 | + $info = $this->filterUserInfoComponent($user); |
|
345 | + if ($password !== null) { |
|
346 | + $info .= ':' . $this->filterUserInfoComponent($password); |
|
347 | + } |
|
348 | + if ($this->userInfo === $info) { |
|
349 | + return $this; |
|
350 | + } |
|
351 | + $new = clone $this; |
|
352 | + $new->userInfo = $info; |
|
353 | + $new->composedComponents = null; |
|
354 | + $new->validateState(); |
|
355 | + return $new; |
|
356 | + } |
|
357 | + public function withHost($host) : UriInterface |
|
358 | + { |
|
359 | + $host = $this->filterHost($host); |
|
360 | + if ($this->host === $host) { |
|
361 | + return $this; |
|
362 | + } |
|
363 | + $new = clone $this; |
|
364 | + $new->host = $host; |
|
365 | + $new->composedComponents = null; |
|
366 | + $new->validateState(); |
|
367 | + return $new; |
|
368 | + } |
|
369 | + public function withPort($port) : UriInterface |
|
370 | + { |
|
371 | + $port = $this->filterPort($port); |
|
372 | + if ($this->port === $port) { |
|
373 | + return $this; |
|
374 | + } |
|
375 | + $new = clone $this; |
|
376 | + $new->port = $port; |
|
377 | + $new->composedComponents = null; |
|
378 | + $new->removeDefaultPort(); |
|
379 | + $new->validateState(); |
|
380 | + return $new; |
|
381 | + } |
|
382 | + public function withPath($path) : UriInterface |
|
383 | + { |
|
384 | + $path = $this->filterPath($path); |
|
385 | + if ($this->path === $path) { |
|
386 | + return $this; |
|
387 | + } |
|
388 | + $new = clone $this; |
|
389 | + $new->path = $path; |
|
390 | + $new->composedComponents = null; |
|
391 | + $new->validateState(); |
|
392 | + return $new; |
|
393 | + } |
|
394 | + public function withQuery($query) : UriInterface |
|
395 | + { |
|
396 | + $query = $this->filterQueryAndFragment($query); |
|
397 | + if ($this->query === $query) { |
|
398 | + return $this; |
|
399 | + } |
|
400 | + $new = clone $this; |
|
401 | + $new->query = $query; |
|
402 | + $new->composedComponents = null; |
|
403 | + return $new; |
|
404 | + } |
|
405 | + public function withFragment($fragment) : UriInterface |
|
406 | + { |
|
407 | + $fragment = $this->filterQueryAndFragment($fragment); |
|
408 | + if ($this->fragment === $fragment) { |
|
409 | + return $this; |
|
410 | + } |
|
411 | + $new = clone $this; |
|
412 | + $new->fragment = $fragment; |
|
413 | + $new->composedComponents = null; |
|
414 | + return $new; |
|
415 | + } |
|
416 | + public function jsonSerialize() : string |
|
417 | + { |
|
418 | + return $this->__toString(); |
|
419 | + } |
|
420 | + /** |
|
421 | + * Apply parse_url parts to a URI. |
|
422 | + * |
|
423 | + * @param array $parts Array of parse_url parts to apply. |
|
424 | + */ |
|
425 | + private function applyParts(array $parts) : void |
|
426 | + { |
|
427 | + $this->scheme = isset($parts['scheme']) ? $this->filterScheme($parts['scheme']) : ''; |
|
428 | + $this->userInfo = isset($parts['user']) ? $this->filterUserInfoComponent($parts['user']) : ''; |
|
429 | + $this->host = isset($parts['host']) ? $this->filterHost($parts['host']) : ''; |
|
430 | + $this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null; |
|
431 | + $this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : ''; |
|
432 | + $this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : ''; |
|
433 | + $this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : ''; |
|
434 | + if (isset($parts['pass'])) { |
|
435 | + $this->userInfo .= ':' . $this->filterUserInfoComponent($parts['pass']); |
|
436 | + } |
|
437 | + $this->removeDefaultPort(); |
|
438 | + } |
|
439 | + /** |
|
440 | + * @param mixed $scheme |
|
441 | + * |
|
442 | + * @throws \InvalidArgumentException If the scheme is invalid. |
|
443 | + */ |
|
444 | + private function filterScheme($scheme) : string |
|
445 | + { |
|
446 | + if (!\is_string($scheme)) { |
|
447 | + throw new \InvalidArgumentException('Scheme must be a string'); |
|
448 | + } |
|
449 | + return \strtr($scheme, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); |
|
450 | + } |
|
451 | + /** |
|
452 | + * @param mixed $component |
|
453 | + * |
|
454 | + * @throws \InvalidArgumentException If the user info is invalid. |
|
455 | + */ |
|
456 | + private function filterUserInfoComponent($component) : string |
|
457 | + { |
|
458 | + if (!\is_string($component)) { |
|
459 | + throw new \InvalidArgumentException('User info must be a string'); |
|
460 | + } |
|
461 | + return \preg_replace_callback('/(?:[^%' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . ']+|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $component); |
|
462 | + } |
|
463 | + /** |
|
464 | + * @param mixed $host |
|
465 | + * |
|
466 | + * @throws \InvalidArgumentException If the host is invalid. |
|
467 | + */ |
|
468 | + private function filterHost($host) : string |
|
469 | + { |
|
470 | + if (!\is_string($host)) { |
|
471 | + throw new \InvalidArgumentException('Host must be a string'); |
|
472 | + } |
|
473 | + return \strtr($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); |
|
474 | + } |
|
475 | + /** |
|
476 | + * @param mixed $port |
|
477 | + * |
|
478 | + * @throws \InvalidArgumentException If the port is invalid. |
|
479 | + */ |
|
480 | + private function filterPort($port) : ?int |
|
481 | + { |
|
482 | + if ($port === null) { |
|
483 | + return null; |
|
484 | + } |
|
485 | + $port = (int) $port; |
|
486 | + if (0 > $port || 0xffff < $port) { |
|
487 | + throw new \InvalidArgumentException(\sprintf('Invalid port: %d. Must be between 0 and 65535', $port)); |
|
488 | + } |
|
489 | + return $port; |
|
490 | + } |
|
491 | + /** |
|
492 | + * @param (string|int)[] $keys |
|
493 | + * |
|
494 | + * @return string[] |
|
495 | + */ |
|
496 | + private static function getFilteredQueryString(UriInterface $uri, array $keys) : array |
|
497 | + { |
|
498 | + $current = $uri->getQuery(); |
|
499 | + if ($current === '') { |
|
500 | + return []; |
|
501 | + } |
|
502 | + $decodedKeys = \array_map(function ($k) : string { |
|
503 | + return \rawurldecode((string) $k); |
|
504 | + }, $keys); |
|
505 | + return \array_filter(\explode('&', $current), function ($part) use($decodedKeys) { |
|
506 | + return !\in_array(\rawurldecode(\explode('=', $part)[0]), $decodedKeys, \true); |
|
507 | + }); |
|
508 | + } |
|
509 | + private static function generateQueryString(string $key, ?string $value) : string |
|
510 | + { |
|
511 | + // Query string separators ("=", "&") within the key or value need to be encoded |
|
512 | + // (while preventing double-encoding) before setting the query string. All other |
|
513 | + // chars that need percent-encoding will be encoded by withQuery(). |
|
514 | + $queryString = \strtr($key, self::QUERY_SEPARATORS_REPLACEMENT); |
|
515 | + if ($value !== null) { |
|
516 | + $queryString .= '=' . \strtr($value, self::QUERY_SEPARATORS_REPLACEMENT); |
|
517 | + } |
|
518 | + return $queryString; |
|
519 | + } |
|
520 | + private function removeDefaultPort() : void |
|
521 | + { |
|
522 | + if ($this->port !== null && self::isDefaultPort($this)) { |
|
523 | + $this->port = null; |
|
524 | + } |
|
525 | + } |
|
526 | + /** |
|
527 | + * Filters the path of a URI |
|
528 | + * |
|
529 | + * @param mixed $path |
|
530 | + * |
|
531 | + * @throws \InvalidArgumentException If the path is invalid. |
|
532 | + */ |
|
533 | + private function filterPath($path) : string |
|
534 | + { |
|
535 | + if (!\is_string($path)) { |
|
536 | + throw new \InvalidArgumentException('Path must be a string'); |
|
537 | + } |
|
538 | + return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\\/]++|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $path); |
|
539 | + } |
|
540 | + /** |
|
541 | + * Filters the query string or fragment of a URI. |
|
542 | + * |
|
543 | + * @param mixed $str |
|
544 | + * |
|
545 | + * @throws \InvalidArgumentException If the query or fragment is invalid. |
|
546 | + */ |
|
547 | + private function filterQueryAndFragment($str) : string |
|
548 | + { |
|
549 | + if (!\is_string($str)) { |
|
550 | + throw new \InvalidArgumentException('Query and fragment must be a string'); |
|
551 | + } |
|
552 | + return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\\/\\?]++|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $str); |
|
553 | + } |
|
554 | + private function rawurlencodeMatchZero(array $match) : string |
|
555 | + { |
|
556 | + return \rawurlencode($match[0]); |
|
557 | + } |
|
558 | + private function validateState() : void |
|
559 | + { |
|
560 | + if ($this->host === '' && ($this->scheme === 'http' || $this->scheme === 'https')) { |
|
561 | + $this->host = self::HTTP_DEFAULT_HOST; |
|
562 | + } |
|
563 | + if ($this->getAuthority() === '') { |
|
564 | + if (0 === \strpos($this->path, '//')) { |
|
565 | + throw new MalformedUriException('The path of a URI without an authority must not start with two slashes "//"'); |
|
566 | + } |
|
567 | + if ($this->scheme === '' && \false !== \strpos(\explode('/', $this->path, 2)[0], ':')) { |
|
568 | + throw new MalformedUriException('A relative URI must not have a path beginning with a segment containing a colon'); |
|
569 | + } |
|
570 | + } |
|
571 | + } |
|
572 | 572 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
5 | 5 | |
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Exception\MalformedUriException; |
@@ -86,10 +86,10 @@ discard block |
||
86 | 86 | $url = $matches[2]; |
87 | 87 | } |
88 | 88 | /** @var string */ |
89 | - $encodedUrl = \preg_replace_callback('%[^:/@?&=#]+%usD', static function ($matches) { |
|
89 | + $encodedUrl = \preg_replace_callback('%[^:/@?&=#]+%usD', static function($matches) { |
|
90 | 90 | return \urlencode($matches[0]); |
91 | 91 | }, $url); |
92 | - $result = \parse_url($prefix . $encodedUrl); |
|
92 | + $result = \parse_url($prefix.$encodedUrl); |
|
93 | 93 | if ($result === \false) { |
94 | 94 | return \false; |
95 | 95 | } |
@@ -125,20 +125,20 @@ discard block |
||
125 | 125 | $uri = ''; |
126 | 126 | // weak type checks to also accept null until we can add scalar type hints |
127 | 127 | if ($scheme != '') { |
128 | - $uri .= $scheme . ':'; |
|
128 | + $uri .= $scheme.':'; |
|
129 | 129 | } |
130 | 130 | if ($authority != '' || $scheme === 'file') { |
131 | - $uri .= '//' . $authority; |
|
131 | + $uri .= '//'.$authority; |
|
132 | 132 | } |
133 | 133 | if ($authority != '' && $path != '' && $path[0] != '/') { |
134 | - $path = '/' . $path; |
|
134 | + $path = '/'.$path; |
|
135 | 135 | } |
136 | 136 | $uri .= $path; |
137 | 137 | if ($query != '') { |
138 | - $uri .= '?' . $query; |
|
138 | + $uri .= '?'.$query; |
|
139 | 139 | } |
140 | 140 | if ($fragment != '') { |
141 | - $uri .= '#' . $fragment; |
|
141 | + $uri .= '#'.$fragment; |
|
142 | 142 | } |
143 | 143 | return $uri; |
144 | 144 | } |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | { |
270 | 270 | $result = self::getFilteredQueryString($uri, \array_keys($keyValueArray)); |
271 | 271 | foreach ($keyValueArray as $key => $value) { |
272 | - $result[] = self::generateQueryString((string) $key, $value !== null ? (string) $value : null); |
|
272 | + $result[] = self::generateQueryString((string)$key, $value !== null ? (string)$value : null); |
|
273 | 273 | } |
274 | 274 | return $uri->withQuery(\implode('&', $result)); |
275 | 275 | } |
@@ -295,10 +295,10 @@ discard block |
||
295 | 295 | { |
296 | 296 | $authority = $this->host; |
297 | 297 | if ($this->userInfo !== '') { |
298 | - $authority = $this->userInfo . '@' . $authority; |
|
298 | + $authority = $this->userInfo.'@'.$authority; |
|
299 | 299 | } |
300 | 300 | if ($this->port !== null) { |
301 | - $authority .= ':' . $this->port; |
|
301 | + $authority .= ':'.$this->port; |
|
302 | 302 | } |
303 | 303 | return $authority; |
304 | 304 | } |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | { |
344 | 344 | $info = $this->filterUserInfoComponent($user); |
345 | 345 | if ($password !== null) { |
346 | - $info .= ':' . $this->filterUserInfoComponent($password); |
|
346 | + $info .= ':'.$this->filterUserInfoComponent($password); |
|
347 | 347 | } |
348 | 348 | if ($this->userInfo === $info) { |
349 | 349 | return $this; |
@@ -432,7 +432,7 @@ discard block |
||
432 | 432 | $this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : ''; |
433 | 433 | $this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : ''; |
434 | 434 | if (isset($parts['pass'])) { |
435 | - $this->userInfo .= ':' . $this->filterUserInfoComponent($parts['pass']); |
|
435 | + $this->userInfo .= ':'.$this->filterUserInfoComponent($parts['pass']); |
|
436 | 436 | } |
437 | 437 | $this->removeDefaultPort(); |
438 | 438 | } |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | if (!\is_string($component)) { |
459 | 459 | throw new \InvalidArgumentException('User info must be a string'); |
460 | 460 | } |
461 | - return \preg_replace_callback('/(?:[^%' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . ']+|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $component); |
|
461 | + return \preg_replace_callback('/(?:[^%'.self::CHAR_UNRESERVED.self::CHAR_SUB_DELIMS.']+|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $component); |
|
462 | 462 | } |
463 | 463 | /** |
464 | 464 | * @param mixed $host |
@@ -482,7 +482,7 @@ discard block |
||
482 | 482 | if ($port === null) { |
483 | 483 | return null; |
484 | 484 | } |
485 | - $port = (int) $port; |
|
485 | + $port = (int)$port; |
|
486 | 486 | if (0 > $port || 0xffff < $port) { |
487 | 487 | throw new \InvalidArgumentException(\sprintf('Invalid port: %d. Must be between 0 and 65535', $port)); |
488 | 488 | } |
@@ -499,10 +499,10 @@ discard block |
||
499 | 499 | if ($current === '') { |
500 | 500 | return []; |
501 | 501 | } |
502 | - $decodedKeys = \array_map(function ($k) : string { |
|
503 | - return \rawurldecode((string) $k); |
|
502 | + $decodedKeys = \array_map(function($k) : string { |
|
503 | + return \rawurldecode((string)$k); |
|
504 | 504 | }, $keys); |
505 | - return \array_filter(\explode('&', $current), function ($part) use($decodedKeys) { |
|
505 | + return \array_filter(\explode('&', $current), function($part) use($decodedKeys) { |
|
506 | 506 | return !\in_array(\rawurldecode(\explode('=', $part)[0]), $decodedKeys, \true); |
507 | 507 | }); |
508 | 508 | } |
@@ -513,7 +513,7 @@ discard block |
||
513 | 513 | // chars that need percent-encoding will be encoded by withQuery(). |
514 | 514 | $queryString = \strtr($key, self::QUERY_SEPARATORS_REPLACEMENT); |
515 | 515 | if ($value !== null) { |
516 | - $queryString .= '=' . \strtr($value, self::QUERY_SEPARATORS_REPLACEMENT); |
|
516 | + $queryString .= '='.\strtr($value, self::QUERY_SEPARATORS_REPLACEMENT); |
|
517 | 517 | } |
518 | 518 | return $queryString; |
519 | 519 | } |
@@ -535,7 +535,7 @@ discard block |
||
535 | 535 | if (!\is_string($path)) { |
536 | 536 | throw new \InvalidArgumentException('Path must be a string'); |
537 | 537 | } |
538 | - return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\\/]++|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $path); |
|
538 | + return \preg_replace_callback('/(?:[^'.self::CHAR_UNRESERVED.self::CHAR_SUB_DELIMS.'%:@\\/]++|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $path); |
|
539 | 539 | } |
540 | 540 | /** |
541 | 541 | * Filters the query string or fragment of a URI. |
@@ -549,7 +549,7 @@ discard block |
||
549 | 549 | if (!\is_string($str)) { |
550 | 550 | throw new \InvalidArgumentException('Query and fragment must be a string'); |
551 | 551 | } |
552 | - return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\\/\\?]++|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $str); |
|
552 | + return \preg_replace_callback('/(?:[^'.self::CHAR_UNRESERVED.self::CHAR_SUB_DELIMS.'%:@\\/\\?]++|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $str); |
|
553 | 553 | } |
554 | 554 | private function rawurlencodeMatchZero(array $match) : string |
555 | 555 | { |
@@ -14,108 +14,108 @@ |
||
14 | 14 | */ |
15 | 15 | final class BufferStream implements StreamInterface |
16 | 16 | { |
17 | - /** @var int */ |
|
18 | - private $hwm; |
|
19 | - /** @var string */ |
|
20 | - private $buffer = ''; |
|
21 | - /** |
|
22 | - * @param int $hwm High water mark, representing the preferred maximum |
|
23 | - * buffer size. If the size of the buffer exceeds the high |
|
24 | - * water mark, then calls to write will continue to succeed |
|
25 | - * but will return 0 to inform writers to slow down |
|
26 | - * until the buffer has been drained by reading from it. |
|
27 | - */ |
|
28 | - public function __construct(int $hwm = 16384) |
|
29 | - { |
|
30 | - $this->hwm = $hwm; |
|
31 | - } |
|
32 | - public function __toString() : string |
|
33 | - { |
|
34 | - return $this->getContents(); |
|
35 | - } |
|
36 | - public function getContents() : string |
|
37 | - { |
|
38 | - $buffer = $this->buffer; |
|
39 | - $this->buffer = ''; |
|
40 | - return $buffer; |
|
41 | - } |
|
42 | - public function close() : void |
|
43 | - { |
|
44 | - $this->buffer = ''; |
|
45 | - } |
|
46 | - public function detach() |
|
47 | - { |
|
48 | - $this->close(); |
|
49 | - return null; |
|
50 | - } |
|
51 | - public function getSize() : ?int |
|
52 | - { |
|
53 | - return \strlen($this->buffer); |
|
54 | - } |
|
55 | - public function isReadable() : bool |
|
56 | - { |
|
57 | - return \true; |
|
58 | - } |
|
59 | - public function isWritable() : bool |
|
60 | - { |
|
61 | - return \true; |
|
62 | - } |
|
63 | - public function isSeekable() : bool |
|
64 | - { |
|
65 | - return \false; |
|
66 | - } |
|
67 | - public function rewind() : void |
|
68 | - { |
|
69 | - $this->seek(0); |
|
70 | - } |
|
71 | - public function seek($offset, $whence = \SEEK_SET) : void |
|
72 | - { |
|
73 | - throw new \RuntimeException('Cannot seek a BufferStream'); |
|
74 | - } |
|
75 | - public function eof() : bool |
|
76 | - { |
|
77 | - return \strlen($this->buffer) === 0; |
|
78 | - } |
|
79 | - public function tell() : int |
|
80 | - { |
|
81 | - throw new \RuntimeException('Cannot determine the position of a BufferStream'); |
|
82 | - } |
|
83 | - /** |
|
84 | - * Reads data from the buffer. |
|
85 | - */ |
|
86 | - public function read($length) : string |
|
87 | - { |
|
88 | - $currentLength = \strlen($this->buffer); |
|
89 | - if ($length >= $currentLength) { |
|
90 | - // No need to slice the buffer because we don't have enough data. |
|
91 | - $result = $this->buffer; |
|
92 | - $this->buffer = ''; |
|
93 | - } else { |
|
94 | - // Slice up the result to provide a subset of the buffer. |
|
95 | - $result = \substr($this->buffer, 0, $length); |
|
96 | - $this->buffer = \substr($this->buffer, $length); |
|
97 | - } |
|
98 | - return $result; |
|
99 | - } |
|
100 | - /** |
|
101 | - * Writes data to the buffer. |
|
102 | - */ |
|
103 | - public function write($string) : int |
|
104 | - { |
|
105 | - $this->buffer .= $string; |
|
106 | - if (\strlen($this->buffer) >= $this->hwm) { |
|
107 | - return 0; |
|
108 | - } |
|
109 | - return \strlen($string); |
|
110 | - } |
|
111 | - /** |
|
112 | - * @return mixed |
|
113 | - */ |
|
114 | - public function getMetadata($key = null) |
|
115 | - { |
|
116 | - if ($key === 'hwm') { |
|
117 | - return $this->hwm; |
|
118 | - } |
|
119 | - return $key ? null : []; |
|
120 | - } |
|
17 | + /** @var int */ |
|
18 | + private $hwm; |
|
19 | + /** @var string */ |
|
20 | + private $buffer = ''; |
|
21 | + /** |
|
22 | + * @param int $hwm High water mark, representing the preferred maximum |
|
23 | + * buffer size. If the size of the buffer exceeds the high |
|
24 | + * water mark, then calls to write will continue to succeed |
|
25 | + * but will return 0 to inform writers to slow down |
|
26 | + * until the buffer has been drained by reading from it. |
|
27 | + */ |
|
28 | + public function __construct(int $hwm = 16384) |
|
29 | + { |
|
30 | + $this->hwm = $hwm; |
|
31 | + } |
|
32 | + public function __toString() : string |
|
33 | + { |
|
34 | + return $this->getContents(); |
|
35 | + } |
|
36 | + public function getContents() : string |
|
37 | + { |
|
38 | + $buffer = $this->buffer; |
|
39 | + $this->buffer = ''; |
|
40 | + return $buffer; |
|
41 | + } |
|
42 | + public function close() : void |
|
43 | + { |
|
44 | + $this->buffer = ''; |
|
45 | + } |
|
46 | + public function detach() |
|
47 | + { |
|
48 | + $this->close(); |
|
49 | + return null; |
|
50 | + } |
|
51 | + public function getSize() : ?int |
|
52 | + { |
|
53 | + return \strlen($this->buffer); |
|
54 | + } |
|
55 | + public function isReadable() : bool |
|
56 | + { |
|
57 | + return \true; |
|
58 | + } |
|
59 | + public function isWritable() : bool |
|
60 | + { |
|
61 | + return \true; |
|
62 | + } |
|
63 | + public function isSeekable() : bool |
|
64 | + { |
|
65 | + return \false; |
|
66 | + } |
|
67 | + public function rewind() : void |
|
68 | + { |
|
69 | + $this->seek(0); |
|
70 | + } |
|
71 | + public function seek($offset, $whence = \SEEK_SET) : void |
|
72 | + { |
|
73 | + throw new \RuntimeException('Cannot seek a BufferStream'); |
|
74 | + } |
|
75 | + public function eof() : bool |
|
76 | + { |
|
77 | + return \strlen($this->buffer) === 0; |
|
78 | + } |
|
79 | + public function tell() : int |
|
80 | + { |
|
81 | + throw new \RuntimeException('Cannot determine the position of a BufferStream'); |
|
82 | + } |
|
83 | + /** |
|
84 | + * Reads data from the buffer. |
|
85 | + */ |
|
86 | + public function read($length) : string |
|
87 | + { |
|
88 | + $currentLength = \strlen($this->buffer); |
|
89 | + if ($length >= $currentLength) { |
|
90 | + // No need to slice the buffer because we don't have enough data. |
|
91 | + $result = $this->buffer; |
|
92 | + $this->buffer = ''; |
|
93 | + } else { |
|
94 | + // Slice up the result to provide a subset of the buffer. |
|
95 | + $result = \substr($this->buffer, 0, $length); |
|
96 | + $this->buffer = \substr($this->buffer, $length); |
|
97 | + } |
|
98 | + return $result; |
|
99 | + } |
|
100 | + /** |
|
101 | + * Writes data to the buffer. |
|
102 | + */ |
|
103 | + public function write($string) : int |
|
104 | + { |
|
105 | + $this->buffer .= $string; |
|
106 | + if (\strlen($this->buffer) >= $this->hwm) { |
|
107 | + return 0; |
|
108 | + } |
|
109 | + return \strlen($string); |
|
110 | + } |
|
111 | + /** |
|
112 | + * @return mixed |
|
113 | + */ |
|
114 | + public function getMetadata($key = null) |
|
115 | + { |
|
116 | + if ($key === 'hwm') { |
|
117 | + return $this->hwm; |
|
118 | + } |
|
119 | + return $key ? null : []; |
|
120 | + } |
|
121 | 121 | } |
@@ -12,8 +12,7 @@ |
||
12 | 12 | * what the configured high water mark of the stream is, or the maximum |
13 | 13 | * preferred size of the buffer. |
14 | 14 | */ |
15 | -final class BufferStream implements StreamInterface |
|
16 | -{ |
|
15 | +final class BufferStream implements StreamInterface { |
|
17 | 16 | /** @var int */ |
18 | 17 | private $hwm; |
19 | 18 | /** @var string */ |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
5 | 5 | |
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
@@ -9,144 +9,144 @@ |
||
9 | 9 | use RuntimeException; |
10 | 10 | class UploadedFile implements UploadedFileInterface |
11 | 11 | { |
12 | - private const ERRORS = [\UPLOAD_ERR_OK, \UPLOAD_ERR_INI_SIZE, \UPLOAD_ERR_FORM_SIZE, \UPLOAD_ERR_PARTIAL, \UPLOAD_ERR_NO_FILE, \UPLOAD_ERR_NO_TMP_DIR, \UPLOAD_ERR_CANT_WRITE, \UPLOAD_ERR_EXTENSION]; |
|
13 | - /** |
|
14 | - * @var string|null |
|
15 | - */ |
|
16 | - private $clientFilename; |
|
17 | - /** |
|
18 | - * @var string|null |
|
19 | - */ |
|
20 | - private $clientMediaType; |
|
21 | - /** |
|
22 | - * @var int |
|
23 | - */ |
|
24 | - private $error; |
|
25 | - /** |
|
26 | - * @var string|null |
|
27 | - */ |
|
28 | - private $file; |
|
29 | - /** |
|
30 | - * @var bool |
|
31 | - */ |
|
32 | - private $moved = \false; |
|
33 | - /** |
|
34 | - * @var int|null |
|
35 | - */ |
|
36 | - private $size; |
|
37 | - /** |
|
38 | - * @var StreamInterface|null |
|
39 | - */ |
|
40 | - private $stream; |
|
41 | - /** |
|
42 | - * @param StreamInterface|string|resource $streamOrFile |
|
43 | - */ |
|
44 | - public function __construct($streamOrFile, ?int $size, int $errorStatus, string $clientFilename = null, string $clientMediaType = null) |
|
45 | - { |
|
46 | - $this->setError($errorStatus); |
|
47 | - $this->size = $size; |
|
48 | - $this->clientFilename = $clientFilename; |
|
49 | - $this->clientMediaType = $clientMediaType; |
|
50 | - if ($this->isOk()) { |
|
51 | - $this->setStreamOrFile($streamOrFile); |
|
52 | - } |
|
53 | - } |
|
54 | - /** |
|
55 | - * Depending on the value set file or stream variable |
|
56 | - * |
|
57 | - * @param StreamInterface|string|resource $streamOrFile |
|
58 | - * |
|
59 | - * @throws InvalidArgumentException |
|
60 | - */ |
|
61 | - private function setStreamOrFile($streamOrFile) : void |
|
62 | - { |
|
63 | - if (\is_string($streamOrFile)) { |
|
64 | - $this->file = $streamOrFile; |
|
65 | - } elseif (\is_resource($streamOrFile)) { |
|
66 | - $this->stream = new Stream($streamOrFile); |
|
67 | - } elseif ($streamOrFile instanceof StreamInterface) { |
|
68 | - $this->stream = $streamOrFile; |
|
69 | - } else { |
|
70 | - throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile'); |
|
71 | - } |
|
72 | - } |
|
73 | - /** |
|
74 | - * @throws InvalidArgumentException |
|
75 | - */ |
|
76 | - private function setError(int $error) : void |
|
77 | - { |
|
78 | - if (\false === \in_array($error, UploadedFile::ERRORS, \true)) { |
|
79 | - throw new InvalidArgumentException('Invalid error status for UploadedFile'); |
|
80 | - } |
|
81 | - $this->error = $error; |
|
82 | - } |
|
83 | - private static function isStringNotEmpty($param) : bool |
|
84 | - { |
|
85 | - return \is_string($param) && \false === empty($param); |
|
86 | - } |
|
87 | - /** |
|
88 | - * Return true if there is no upload error |
|
89 | - */ |
|
90 | - private function isOk() : bool |
|
91 | - { |
|
92 | - return $this->error === \UPLOAD_ERR_OK; |
|
93 | - } |
|
94 | - public function isMoved() : bool |
|
95 | - { |
|
96 | - return $this->moved; |
|
97 | - } |
|
98 | - /** |
|
99 | - * @throws RuntimeException if is moved or not ok |
|
100 | - */ |
|
101 | - private function validateActive() : void |
|
102 | - { |
|
103 | - if (\false === $this->isOk()) { |
|
104 | - throw new RuntimeException('Cannot retrieve stream due to upload error'); |
|
105 | - } |
|
106 | - if ($this->isMoved()) { |
|
107 | - throw new RuntimeException('Cannot retrieve stream after it has already been moved'); |
|
108 | - } |
|
109 | - } |
|
110 | - public function getStream() : StreamInterface |
|
111 | - { |
|
112 | - $this->validateActive(); |
|
113 | - if ($this->stream instanceof StreamInterface) { |
|
114 | - return $this->stream; |
|
115 | - } |
|
116 | - /** @var string $file */ |
|
117 | - $file = $this->file; |
|
118 | - return new LazyOpenStream($file, 'r+'); |
|
119 | - } |
|
120 | - public function moveTo($targetPath) : void |
|
121 | - { |
|
122 | - $this->validateActive(); |
|
123 | - if (\false === self::isStringNotEmpty($targetPath)) { |
|
124 | - throw new InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string'); |
|
125 | - } |
|
126 | - if ($this->file) { |
|
127 | - $this->moved = \PHP_SAPI === 'cli' ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath); |
|
128 | - } else { |
|
129 | - Utils::copyToStream($this->getStream(), new LazyOpenStream($targetPath, 'w')); |
|
130 | - $this->moved = \true; |
|
131 | - } |
|
132 | - if (\false === $this->moved) { |
|
133 | - throw new RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath)); |
|
134 | - } |
|
135 | - } |
|
136 | - public function getSize() : ?int |
|
137 | - { |
|
138 | - return $this->size; |
|
139 | - } |
|
140 | - public function getError() : int |
|
141 | - { |
|
142 | - return $this->error; |
|
143 | - } |
|
144 | - public function getClientFilename() : ?string |
|
145 | - { |
|
146 | - return $this->clientFilename; |
|
147 | - } |
|
148 | - public function getClientMediaType() : ?string |
|
149 | - { |
|
150 | - return $this->clientMediaType; |
|
151 | - } |
|
12 | + private const ERRORS = [\UPLOAD_ERR_OK, \UPLOAD_ERR_INI_SIZE, \UPLOAD_ERR_FORM_SIZE, \UPLOAD_ERR_PARTIAL, \UPLOAD_ERR_NO_FILE, \UPLOAD_ERR_NO_TMP_DIR, \UPLOAD_ERR_CANT_WRITE, \UPLOAD_ERR_EXTENSION]; |
|
13 | + /** |
|
14 | + * @var string|null |
|
15 | + */ |
|
16 | + private $clientFilename; |
|
17 | + /** |
|
18 | + * @var string|null |
|
19 | + */ |
|
20 | + private $clientMediaType; |
|
21 | + /** |
|
22 | + * @var int |
|
23 | + */ |
|
24 | + private $error; |
|
25 | + /** |
|
26 | + * @var string|null |
|
27 | + */ |
|
28 | + private $file; |
|
29 | + /** |
|
30 | + * @var bool |
|
31 | + */ |
|
32 | + private $moved = \false; |
|
33 | + /** |
|
34 | + * @var int|null |
|
35 | + */ |
|
36 | + private $size; |
|
37 | + /** |
|
38 | + * @var StreamInterface|null |
|
39 | + */ |
|
40 | + private $stream; |
|
41 | + /** |
|
42 | + * @param StreamInterface|string|resource $streamOrFile |
|
43 | + */ |
|
44 | + public function __construct($streamOrFile, ?int $size, int $errorStatus, string $clientFilename = null, string $clientMediaType = null) |
|
45 | + { |
|
46 | + $this->setError($errorStatus); |
|
47 | + $this->size = $size; |
|
48 | + $this->clientFilename = $clientFilename; |
|
49 | + $this->clientMediaType = $clientMediaType; |
|
50 | + if ($this->isOk()) { |
|
51 | + $this->setStreamOrFile($streamOrFile); |
|
52 | + } |
|
53 | + } |
|
54 | + /** |
|
55 | + * Depending on the value set file or stream variable |
|
56 | + * |
|
57 | + * @param StreamInterface|string|resource $streamOrFile |
|
58 | + * |
|
59 | + * @throws InvalidArgumentException |
|
60 | + */ |
|
61 | + private function setStreamOrFile($streamOrFile) : void |
|
62 | + { |
|
63 | + if (\is_string($streamOrFile)) { |
|
64 | + $this->file = $streamOrFile; |
|
65 | + } elseif (\is_resource($streamOrFile)) { |
|
66 | + $this->stream = new Stream($streamOrFile); |
|
67 | + } elseif ($streamOrFile instanceof StreamInterface) { |
|
68 | + $this->stream = $streamOrFile; |
|
69 | + } else { |
|
70 | + throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile'); |
|
71 | + } |
|
72 | + } |
|
73 | + /** |
|
74 | + * @throws InvalidArgumentException |
|
75 | + */ |
|
76 | + private function setError(int $error) : void |
|
77 | + { |
|
78 | + if (\false === \in_array($error, UploadedFile::ERRORS, \true)) { |
|
79 | + throw new InvalidArgumentException('Invalid error status for UploadedFile'); |
|
80 | + } |
|
81 | + $this->error = $error; |
|
82 | + } |
|
83 | + private static function isStringNotEmpty($param) : bool |
|
84 | + { |
|
85 | + return \is_string($param) && \false === empty($param); |
|
86 | + } |
|
87 | + /** |
|
88 | + * Return true if there is no upload error |
|
89 | + */ |
|
90 | + private function isOk() : bool |
|
91 | + { |
|
92 | + return $this->error === \UPLOAD_ERR_OK; |
|
93 | + } |
|
94 | + public function isMoved() : bool |
|
95 | + { |
|
96 | + return $this->moved; |
|
97 | + } |
|
98 | + /** |
|
99 | + * @throws RuntimeException if is moved or not ok |
|
100 | + */ |
|
101 | + private function validateActive() : void |
|
102 | + { |
|
103 | + if (\false === $this->isOk()) { |
|
104 | + throw new RuntimeException('Cannot retrieve stream due to upload error'); |
|
105 | + } |
|
106 | + if ($this->isMoved()) { |
|
107 | + throw new RuntimeException('Cannot retrieve stream after it has already been moved'); |
|
108 | + } |
|
109 | + } |
|
110 | + public function getStream() : StreamInterface |
|
111 | + { |
|
112 | + $this->validateActive(); |
|
113 | + if ($this->stream instanceof StreamInterface) { |
|
114 | + return $this->stream; |
|
115 | + } |
|
116 | + /** @var string $file */ |
|
117 | + $file = $this->file; |
|
118 | + return new LazyOpenStream($file, 'r+'); |
|
119 | + } |
|
120 | + public function moveTo($targetPath) : void |
|
121 | + { |
|
122 | + $this->validateActive(); |
|
123 | + if (\false === self::isStringNotEmpty($targetPath)) { |
|
124 | + throw new InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string'); |
|
125 | + } |
|
126 | + if ($this->file) { |
|
127 | + $this->moved = \PHP_SAPI === 'cli' ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath); |
|
128 | + } else { |
|
129 | + Utils::copyToStream($this->getStream(), new LazyOpenStream($targetPath, 'w')); |
|
130 | + $this->moved = \true; |
|
131 | + } |
|
132 | + if (\false === $this->moved) { |
|
133 | + throw new RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath)); |
|
134 | + } |
|
135 | + } |
|
136 | + public function getSize() : ?int |
|
137 | + { |
|
138 | + return $this->size; |
|
139 | + } |
|
140 | + public function getError() : int |
|
141 | + { |
|
142 | + return $this->error; |
|
143 | + } |
|
144 | + public function getClientFilename() : ?string |
|
145 | + { |
|
146 | + return $this->clientFilename; |
|
147 | + } |
|
148 | + public function getClientMediaType() : ?string |
|
149 | + { |
|
150 | + return $this->clientMediaType; |
|
151 | + } |
|
152 | 152 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
5 | 5 | |
6 | 6 | use InvalidArgumentException; |
@@ -7,8 +7,7 @@ |
||
7 | 7 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
8 | 8 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\UploadedFileInterface; |
9 | 9 | use RuntimeException; |
10 | -class UploadedFile implements UploadedFileInterface |
|
11 | -{ |
|
10 | +class UploadedFile implements UploadedFileInterface { |
|
12 | 11 | private const ERRORS = [\UPLOAD_ERR_OK, \UPLOAD_ERR_INI_SIZE, \UPLOAD_ERR_FORM_SIZE, \UPLOAD_ERR_PARTIAL, \UPLOAD_ERR_NO_FILE, \UPLOAD_ERR_NO_TMP_DIR, \UPLOAD_ERR_CANT_WRITE, \UPLOAD_ERR_EXTENSION]; |
13 | 12 | /** |
14 | 13 | * @var string|null |
@@ -10,203 +10,203 @@ |
||
10 | 10 | */ |
11 | 11 | trait MessageTrait |
12 | 12 | { |
13 | - /** @var string[][] Map of all registered headers, as original name => array of values */ |
|
14 | - private $headers = []; |
|
15 | - /** @var string[] Map of lowercase header name => original name at registration */ |
|
16 | - private $headerNames = []; |
|
17 | - /** @var string */ |
|
18 | - private $protocol = '1.1'; |
|
19 | - /** @var StreamInterface|null */ |
|
20 | - private $stream; |
|
21 | - public function getProtocolVersion() : string |
|
22 | - { |
|
23 | - return $this->protocol; |
|
24 | - } |
|
25 | - public function withProtocolVersion($version) : MessageInterface |
|
26 | - { |
|
27 | - if ($this->protocol === $version) { |
|
28 | - return $this; |
|
29 | - } |
|
30 | - $new = clone $this; |
|
31 | - $new->protocol = $version; |
|
32 | - return $new; |
|
33 | - } |
|
34 | - public function getHeaders() : array |
|
35 | - { |
|
36 | - return $this->headers; |
|
37 | - } |
|
38 | - public function hasHeader($header) : bool |
|
39 | - { |
|
40 | - return isset($this->headerNames[\strtolower($header)]); |
|
41 | - } |
|
42 | - public function getHeader($header) : array |
|
43 | - { |
|
44 | - $header = \strtolower($header); |
|
45 | - if (!isset($this->headerNames[$header])) { |
|
46 | - return []; |
|
47 | - } |
|
48 | - $header = $this->headerNames[$header]; |
|
49 | - return $this->headers[$header]; |
|
50 | - } |
|
51 | - public function getHeaderLine($header) : string |
|
52 | - { |
|
53 | - return \implode(', ', $this->getHeader($header)); |
|
54 | - } |
|
55 | - public function withHeader($header, $value) : MessageInterface |
|
56 | - { |
|
57 | - $this->assertHeader($header); |
|
58 | - $value = $this->normalizeHeaderValue($value); |
|
59 | - $normalized = \strtolower($header); |
|
60 | - $new = clone $this; |
|
61 | - if (isset($new->headerNames[$normalized])) { |
|
62 | - unset($new->headers[$new->headerNames[$normalized]]); |
|
63 | - } |
|
64 | - $new->headerNames[$normalized] = $header; |
|
65 | - $new->headers[$header] = $value; |
|
66 | - return $new; |
|
67 | - } |
|
68 | - public function withAddedHeader($header, $value) : MessageInterface |
|
69 | - { |
|
70 | - $this->assertHeader($header); |
|
71 | - $value = $this->normalizeHeaderValue($value); |
|
72 | - $normalized = \strtolower($header); |
|
73 | - $new = clone $this; |
|
74 | - if (isset($new->headerNames[$normalized])) { |
|
75 | - $header = $this->headerNames[$normalized]; |
|
76 | - $new->headers[$header] = \array_merge($this->headers[$header], $value); |
|
77 | - } else { |
|
78 | - $new->headerNames[$normalized] = $header; |
|
79 | - $new->headers[$header] = $value; |
|
80 | - } |
|
81 | - return $new; |
|
82 | - } |
|
83 | - public function withoutHeader($header) : MessageInterface |
|
84 | - { |
|
85 | - $normalized = \strtolower($header); |
|
86 | - if (!isset($this->headerNames[$normalized])) { |
|
87 | - return $this; |
|
88 | - } |
|
89 | - $header = $this->headerNames[$normalized]; |
|
90 | - $new = clone $this; |
|
91 | - unset($new->headers[$header], $new->headerNames[$normalized]); |
|
92 | - return $new; |
|
93 | - } |
|
94 | - public function getBody() : StreamInterface |
|
95 | - { |
|
96 | - if (!$this->stream) { |
|
97 | - $this->stream = Utils::streamFor(''); |
|
98 | - } |
|
99 | - return $this->stream; |
|
100 | - } |
|
101 | - public function withBody(StreamInterface $body) : MessageInterface |
|
102 | - { |
|
103 | - if ($body === $this->stream) { |
|
104 | - return $this; |
|
105 | - } |
|
106 | - $new = clone $this; |
|
107 | - $new->stream = $body; |
|
108 | - return $new; |
|
109 | - } |
|
110 | - /** |
|
111 | - * @param (string|string[])[] $headers |
|
112 | - */ |
|
113 | - private function setHeaders(array $headers) : void |
|
114 | - { |
|
115 | - $this->headerNames = $this->headers = []; |
|
116 | - foreach ($headers as $header => $value) { |
|
117 | - // Numeric array keys are converted to int by PHP. |
|
118 | - $header = (string) $header; |
|
119 | - $this->assertHeader($header); |
|
120 | - $value = $this->normalizeHeaderValue($value); |
|
121 | - $normalized = \strtolower($header); |
|
122 | - if (isset($this->headerNames[$normalized])) { |
|
123 | - $header = $this->headerNames[$normalized]; |
|
124 | - $this->headers[$header] = \array_merge($this->headers[$header], $value); |
|
125 | - } else { |
|
126 | - $this->headerNames[$normalized] = $header; |
|
127 | - $this->headers[$header] = $value; |
|
128 | - } |
|
129 | - } |
|
130 | - } |
|
131 | - /** |
|
132 | - * @param mixed $value |
|
133 | - * |
|
134 | - * @return string[] |
|
135 | - */ |
|
136 | - private function normalizeHeaderValue($value) : array |
|
137 | - { |
|
138 | - if (!\is_array($value)) { |
|
139 | - return $this->trimAndValidateHeaderValues([$value]); |
|
140 | - } |
|
141 | - if (\count($value) === 0) { |
|
142 | - throw new \InvalidArgumentException('Header value can not be an empty array.'); |
|
143 | - } |
|
144 | - return $this->trimAndValidateHeaderValues($value); |
|
145 | - } |
|
146 | - /** |
|
147 | - * Trims whitespace from the header values. |
|
148 | - * |
|
149 | - * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. |
|
150 | - * |
|
151 | - * header-field = field-name ":" OWS field-value OWS |
|
152 | - * OWS = *( SP / HTAB ) |
|
153 | - * |
|
154 | - * @param mixed[] $values Header values |
|
155 | - * |
|
156 | - * @return string[] Trimmed header values |
|
157 | - * |
|
158 | - * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 |
|
159 | - */ |
|
160 | - private function trimAndValidateHeaderValues(array $values) : array |
|
161 | - { |
|
162 | - return \array_map(function ($value) { |
|
163 | - if (!\is_scalar($value) && null !== $value) { |
|
164 | - throw new \InvalidArgumentException(\sprintf('Header value must be scalar or null but %s provided.', \is_object($value) ? \get_class($value) : \gettype($value))); |
|
165 | - } |
|
166 | - $trimmed = \trim((string) $value, " \t"); |
|
167 | - $this->assertValue($trimmed); |
|
168 | - return $trimmed; |
|
169 | - }, \array_values($values)); |
|
170 | - } |
|
171 | - /** |
|
172 | - * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 |
|
173 | - * |
|
174 | - * @param mixed $header |
|
175 | - */ |
|
176 | - private function assertHeader($header) : void |
|
177 | - { |
|
178 | - if (!\is_string($header)) { |
|
179 | - throw new \InvalidArgumentException(\sprintf('Header name must be a string but %s provided.', \is_object($header) ? \get_class($header) : \gettype($header))); |
|
180 | - } |
|
181 | - if (!\preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $header)) { |
|
182 | - throw new \InvalidArgumentException(\sprintf('"%s" is not valid header name.', $header)); |
|
183 | - } |
|
184 | - } |
|
185 | - /** |
|
186 | - * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 |
|
187 | - * |
|
188 | - * field-value = *( field-content / obs-fold ) |
|
189 | - * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
|
190 | - * field-vchar = VCHAR / obs-text |
|
191 | - * VCHAR = %x21-7E |
|
192 | - * obs-text = %x80-FF |
|
193 | - * obs-fold = CRLF 1*( SP / HTAB ) |
|
194 | - */ |
|
195 | - private function assertValue(string $value) : void |
|
196 | - { |
|
197 | - // The regular expression intentionally does not support the obs-fold production, because as |
|
198 | - // per RFC 7230#3.2.4: |
|
199 | - // |
|
200 | - // A sender MUST NOT generate a message that includes |
|
201 | - // line folding (i.e., that has any field-value that contains a match to |
|
202 | - // the obs-fold rule) unless the message is intended for packaging |
|
203 | - // within the message/http media type. |
|
204 | - // |
|
205 | - // Clients must not send a request with line folding and a server sending folded headers is |
|
206 | - // likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting |
|
207 | - // folding is not likely to break any legitimate use case. |
|
208 | - if (!\preg_match('/^[\\x20\\x09\\x21-\\x7E\\x80-\\xFF]*$/D', $value)) { |
|
209 | - throw new \InvalidArgumentException(\sprintf('"%s" is not valid header value.', $value)); |
|
210 | - } |
|
211 | - } |
|
13 | + /** @var string[][] Map of all registered headers, as original name => array of values */ |
|
14 | + private $headers = []; |
|
15 | + /** @var string[] Map of lowercase header name => original name at registration */ |
|
16 | + private $headerNames = []; |
|
17 | + /** @var string */ |
|
18 | + private $protocol = '1.1'; |
|
19 | + /** @var StreamInterface|null */ |
|
20 | + private $stream; |
|
21 | + public function getProtocolVersion() : string |
|
22 | + { |
|
23 | + return $this->protocol; |
|
24 | + } |
|
25 | + public function withProtocolVersion($version) : MessageInterface |
|
26 | + { |
|
27 | + if ($this->protocol === $version) { |
|
28 | + return $this; |
|
29 | + } |
|
30 | + $new = clone $this; |
|
31 | + $new->protocol = $version; |
|
32 | + return $new; |
|
33 | + } |
|
34 | + public function getHeaders() : array |
|
35 | + { |
|
36 | + return $this->headers; |
|
37 | + } |
|
38 | + public function hasHeader($header) : bool |
|
39 | + { |
|
40 | + return isset($this->headerNames[\strtolower($header)]); |
|
41 | + } |
|
42 | + public function getHeader($header) : array |
|
43 | + { |
|
44 | + $header = \strtolower($header); |
|
45 | + if (!isset($this->headerNames[$header])) { |
|
46 | + return []; |
|
47 | + } |
|
48 | + $header = $this->headerNames[$header]; |
|
49 | + return $this->headers[$header]; |
|
50 | + } |
|
51 | + public function getHeaderLine($header) : string |
|
52 | + { |
|
53 | + return \implode(', ', $this->getHeader($header)); |
|
54 | + } |
|
55 | + public function withHeader($header, $value) : MessageInterface |
|
56 | + { |
|
57 | + $this->assertHeader($header); |
|
58 | + $value = $this->normalizeHeaderValue($value); |
|
59 | + $normalized = \strtolower($header); |
|
60 | + $new = clone $this; |
|
61 | + if (isset($new->headerNames[$normalized])) { |
|
62 | + unset($new->headers[$new->headerNames[$normalized]]); |
|
63 | + } |
|
64 | + $new->headerNames[$normalized] = $header; |
|
65 | + $new->headers[$header] = $value; |
|
66 | + return $new; |
|
67 | + } |
|
68 | + public function withAddedHeader($header, $value) : MessageInterface |
|
69 | + { |
|
70 | + $this->assertHeader($header); |
|
71 | + $value = $this->normalizeHeaderValue($value); |
|
72 | + $normalized = \strtolower($header); |
|
73 | + $new = clone $this; |
|
74 | + if (isset($new->headerNames[$normalized])) { |
|
75 | + $header = $this->headerNames[$normalized]; |
|
76 | + $new->headers[$header] = \array_merge($this->headers[$header], $value); |
|
77 | + } else { |
|
78 | + $new->headerNames[$normalized] = $header; |
|
79 | + $new->headers[$header] = $value; |
|
80 | + } |
|
81 | + return $new; |
|
82 | + } |
|
83 | + public function withoutHeader($header) : MessageInterface |
|
84 | + { |
|
85 | + $normalized = \strtolower($header); |
|
86 | + if (!isset($this->headerNames[$normalized])) { |
|
87 | + return $this; |
|
88 | + } |
|
89 | + $header = $this->headerNames[$normalized]; |
|
90 | + $new = clone $this; |
|
91 | + unset($new->headers[$header], $new->headerNames[$normalized]); |
|
92 | + return $new; |
|
93 | + } |
|
94 | + public function getBody() : StreamInterface |
|
95 | + { |
|
96 | + if (!$this->stream) { |
|
97 | + $this->stream = Utils::streamFor(''); |
|
98 | + } |
|
99 | + return $this->stream; |
|
100 | + } |
|
101 | + public function withBody(StreamInterface $body) : MessageInterface |
|
102 | + { |
|
103 | + if ($body === $this->stream) { |
|
104 | + return $this; |
|
105 | + } |
|
106 | + $new = clone $this; |
|
107 | + $new->stream = $body; |
|
108 | + return $new; |
|
109 | + } |
|
110 | + /** |
|
111 | + * @param (string|string[])[] $headers |
|
112 | + */ |
|
113 | + private function setHeaders(array $headers) : void |
|
114 | + { |
|
115 | + $this->headerNames = $this->headers = []; |
|
116 | + foreach ($headers as $header => $value) { |
|
117 | + // Numeric array keys are converted to int by PHP. |
|
118 | + $header = (string) $header; |
|
119 | + $this->assertHeader($header); |
|
120 | + $value = $this->normalizeHeaderValue($value); |
|
121 | + $normalized = \strtolower($header); |
|
122 | + if (isset($this->headerNames[$normalized])) { |
|
123 | + $header = $this->headerNames[$normalized]; |
|
124 | + $this->headers[$header] = \array_merge($this->headers[$header], $value); |
|
125 | + } else { |
|
126 | + $this->headerNames[$normalized] = $header; |
|
127 | + $this->headers[$header] = $value; |
|
128 | + } |
|
129 | + } |
|
130 | + } |
|
131 | + /** |
|
132 | + * @param mixed $value |
|
133 | + * |
|
134 | + * @return string[] |
|
135 | + */ |
|
136 | + private function normalizeHeaderValue($value) : array |
|
137 | + { |
|
138 | + if (!\is_array($value)) { |
|
139 | + return $this->trimAndValidateHeaderValues([$value]); |
|
140 | + } |
|
141 | + if (\count($value) === 0) { |
|
142 | + throw new \InvalidArgumentException('Header value can not be an empty array.'); |
|
143 | + } |
|
144 | + return $this->trimAndValidateHeaderValues($value); |
|
145 | + } |
|
146 | + /** |
|
147 | + * Trims whitespace from the header values. |
|
148 | + * |
|
149 | + * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. |
|
150 | + * |
|
151 | + * header-field = field-name ":" OWS field-value OWS |
|
152 | + * OWS = *( SP / HTAB ) |
|
153 | + * |
|
154 | + * @param mixed[] $values Header values |
|
155 | + * |
|
156 | + * @return string[] Trimmed header values |
|
157 | + * |
|
158 | + * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 |
|
159 | + */ |
|
160 | + private function trimAndValidateHeaderValues(array $values) : array |
|
161 | + { |
|
162 | + return \array_map(function ($value) { |
|
163 | + if (!\is_scalar($value) && null !== $value) { |
|
164 | + throw new \InvalidArgumentException(\sprintf('Header value must be scalar or null but %s provided.', \is_object($value) ? \get_class($value) : \gettype($value))); |
|
165 | + } |
|
166 | + $trimmed = \trim((string) $value, " \t"); |
|
167 | + $this->assertValue($trimmed); |
|
168 | + return $trimmed; |
|
169 | + }, \array_values($values)); |
|
170 | + } |
|
171 | + /** |
|
172 | + * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 |
|
173 | + * |
|
174 | + * @param mixed $header |
|
175 | + */ |
|
176 | + private function assertHeader($header) : void |
|
177 | + { |
|
178 | + if (!\is_string($header)) { |
|
179 | + throw new \InvalidArgumentException(\sprintf('Header name must be a string but %s provided.', \is_object($header) ? \get_class($header) : \gettype($header))); |
|
180 | + } |
|
181 | + if (!\preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $header)) { |
|
182 | + throw new \InvalidArgumentException(\sprintf('"%s" is not valid header name.', $header)); |
|
183 | + } |
|
184 | + } |
|
185 | + /** |
|
186 | + * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 |
|
187 | + * |
|
188 | + * field-value = *( field-content / obs-fold ) |
|
189 | + * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
|
190 | + * field-vchar = VCHAR / obs-text |
|
191 | + * VCHAR = %x21-7E |
|
192 | + * obs-text = %x80-FF |
|
193 | + * obs-fold = CRLF 1*( SP / HTAB ) |
|
194 | + */ |
|
195 | + private function assertValue(string $value) : void |
|
196 | + { |
|
197 | + // The regular expression intentionally does not support the obs-fold production, because as |
|
198 | + // per RFC 7230#3.2.4: |
|
199 | + // |
|
200 | + // A sender MUST NOT generate a message that includes |
|
201 | + // line folding (i.e., that has any field-value that contains a match to |
|
202 | + // the obs-fold rule) unless the message is intended for packaging |
|
203 | + // within the message/http media type. |
|
204 | + // |
|
205 | + // Clients must not send a request with line folding and a server sending folded headers is |
|
206 | + // likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting |
|
207 | + // folding is not likely to break any legitimate use case. |
|
208 | + if (!\preg_match('/^[\\x20\\x09\\x21-\\x7E\\x80-\\xFF]*$/D', $value)) { |
|
209 | + throw new \InvalidArgumentException(\sprintf('"%s" is not valid header value.', $value)); |
|
210 | + } |
|
211 | + } |
|
212 | 212 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
5 | 5 | |
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\MessageInterface; |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | $this->headerNames = $this->headers = []; |
116 | 116 | foreach ($headers as $header => $value) { |
117 | 117 | // Numeric array keys are converted to int by PHP. |
118 | - $header = (string) $header; |
|
118 | + $header = (string)$header; |
|
119 | 119 | $this->assertHeader($header); |
120 | 120 | $value = $this->normalizeHeaderValue($value); |
121 | 121 | $normalized = \strtolower($header); |
@@ -159,11 +159,11 @@ discard block |
||
159 | 159 | */ |
160 | 160 | private function trimAndValidateHeaderValues(array $values) : array |
161 | 161 | { |
162 | - return \array_map(function ($value) { |
|
162 | + return \array_map(function($value) { |
|
163 | 163 | if (!\is_scalar($value) && null !== $value) { |
164 | 164 | throw new \InvalidArgumentException(\sprintf('Header value must be scalar or null but %s provided.', \is_object($value) ? \get_class($value) : \gettype($value))); |
165 | 165 | } |
166 | - $trimmed = \trim((string) $value, " \t"); |
|
166 | + $trimmed = \trim((string)$value, " \t"); |
|
167 | 167 | $this->assertValue($trimmed); |
168 | 168 | return $trimmed; |
169 | 169 | }, \array_values($values)); |
@@ -8,8 +8,7 @@ |
||
8 | 8 | /** |
9 | 9 | * Trait implementing functionality common to requests and responses. |
10 | 10 | */ |
11 | -trait MessageTrait |
|
12 | -{ |
|
11 | +trait MessageTrait { |
|
13 | 12 | /** @var string[][] Map of all registered headers, as original name => array of values */ |
14 | 13 | private $headers = []; |
15 | 14 | /** @var string[] Map of lowercase header name => original name at registration */ |