@@ -48,361 +48,361 @@ |
||
48 | 48 | * @package OC\Http |
49 | 49 | */ |
50 | 50 | class Client implements IClient { |
51 | - /** @var GuzzleClient */ |
|
52 | - private $client; |
|
53 | - /** @var IConfig */ |
|
54 | - private $config; |
|
55 | - /** @var ILogger */ |
|
56 | - private $logger; |
|
57 | - /** @var ICertificateManager */ |
|
58 | - private $certificateManager; |
|
59 | - |
|
60 | - public function __construct( |
|
61 | - IConfig $config, |
|
62 | - ILogger $logger, |
|
63 | - ICertificateManager $certificateManager, |
|
64 | - GuzzleClient $client |
|
65 | - ) { |
|
66 | - $this->config = $config; |
|
67 | - $this->logger = $logger; |
|
68 | - $this->client = $client; |
|
69 | - $this->certificateManager = $certificateManager; |
|
70 | - } |
|
71 | - |
|
72 | - private function buildRequestOptions(array $options): array { |
|
73 | - $proxy = $this->getProxyUri(); |
|
74 | - |
|
75 | - $defaults = [ |
|
76 | - RequestOptions::VERIFY => $this->getCertBundle(), |
|
77 | - RequestOptions::TIMEOUT => 30, |
|
78 | - ]; |
|
79 | - |
|
80 | - // Only add RequestOptions::PROXY if Nextcloud is explicitly |
|
81 | - // configured to use a proxy. This is needed in order not to override |
|
82 | - // Guzzle default values. |
|
83 | - if ($proxy !== null) { |
|
84 | - $defaults[RequestOptions::PROXY] = $proxy; |
|
85 | - } |
|
86 | - |
|
87 | - $options = array_merge($defaults, $options); |
|
88 | - |
|
89 | - if (!isset($options[RequestOptions::HEADERS]['User-Agent'])) { |
|
90 | - $options[RequestOptions::HEADERS]['User-Agent'] = 'Nextcloud Server Crawler'; |
|
91 | - } |
|
92 | - |
|
93 | - if (!isset($options[RequestOptions::HEADERS]['Accept-Encoding'])) { |
|
94 | - $options[RequestOptions::HEADERS]['Accept-Encoding'] = 'gzip'; |
|
95 | - } |
|
96 | - |
|
97 | - return $options; |
|
98 | - } |
|
99 | - |
|
100 | - private function getCertBundle(): string { |
|
101 | - // If the instance is not yet setup we need to use the static path as |
|
102 | - // $this->certificateManager->getAbsoluteBundlePath() tries to instantiate |
|
103 | - // a view |
|
104 | - if ($this->config->getSystemValue('installed', false) === false) { |
|
105 | - return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt'; |
|
106 | - } |
|
107 | - |
|
108 | - return $this->certificateManager->getAbsoluteBundlePath(); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Returns a null or an associative array specifiying the proxy URI for |
|
113 | - * 'http' and 'https' schemes, in addition to a 'no' key value pair |
|
114 | - * providing a list of host names that should not be proxied to. |
|
115 | - * |
|
116 | - * @return array|null |
|
117 | - * |
|
118 | - * The return array looks like: |
|
119 | - * [ |
|
120 | - * 'http' => 'username:[email protected]', |
|
121 | - * 'https' => 'username:[email protected]', |
|
122 | - * 'no' => ['foo.com', 'bar.com'] |
|
123 | - * ] |
|
124 | - * |
|
125 | - */ |
|
126 | - private function getProxyUri(): ?array { |
|
127 | - $proxyHost = $this->config->getSystemValue('proxy', ''); |
|
128 | - |
|
129 | - if ($proxyHost === '' || $proxyHost === null) { |
|
130 | - return null; |
|
131 | - } |
|
132 | - |
|
133 | - $proxyUserPwd = $this->config->getSystemValue('proxyuserpwd', ''); |
|
134 | - if ($proxyUserPwd !== '' && $proxyUserPwd !== null) { |
|
135 | - $proxyHost = $proxyUserPwd . '@' . $proxyHost; |
|
136 | - } |
|
137 | - |
|
138 | - $proxy = [ |
|
139 | - 'http' => $proxyHost, |
|
140 | - 'https' => $proxyHost, |
|
141 | - ]; |
|
142 | - |
|
143 | - $proxyExclude = $this->config->getSystemValue('proxyexclude', []); |
|
144 | - if ($proxyExclude !== [] && $proxyExclude !== null) { |
|
145 | - $proxy['no'] = $proxyExclude; |
|
146 | - } |
|
147 | - |
|
148 | - return $proxy; |
|
149 | - } |
|
150 | - |
|
151 | - protected function preventLocalAddress(string $uri, array $options): void { |
|
152 | - if (($options['nextcloud']['allow_local_address'] ?? false) || |
|
153 | - $this->config->getSystemValueBool('allow_local_remote_servers', false)) { |
|
154 | - return; |
|
155 | - } |
|
156 | - |
|
157 | - $host = parse_url($uri, PHP_URL_HOST); |
|
158 | - if ($host === false || $host === null) { |
|
159 | - $this->logger->warning("Could not detect any host in $uri"); |
|
160 | - throw new LocalServerException('Could not detect any host'); |
|
161 | - } |
|
162 | - |
|
163 | - $host = strtolower($host); |
|
164 | - // Remove brackets from IPv6 addresses |
|
165 | - if (strpos($host, '[') === 0 && substr($host, -1) === ']') { |
|
166 | - $host = substr($host, 1, -1); |
|
167 | - } |
|
168 | - |
|
169 | - // Disallow localhost and local network |
|
170 | - if ($host === 'localhost' || substr($host, -6) === '.local' || substr($host, -10) === '.localhost') { |
|
171 | - $this->logger->warning("Host $host was not connected to because it violates local access rules"); |
|
172 | - throw new LocalServerException('Host violates local access rules'); |
|
173 | - } |
|
174 | - |
|
175 | - // Disallow hostname only |
|
176 | - if (substr_count($host, '.') === 0) { |
|
177 | - $this->logger->warning("Host $host was not connected to because it violates local access rules"); |
|
178 | - throw new LocalServerException('Host violates local access rules'); |
|
179 | - } |
|
180 | - |
|
181 | - if ((bool)filter_var($host, FILTER_VALIDATE_IP) && !filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
182 | - $this->logger->warning("Host $host was not connected to because it violates local access rules"); |
|
183 | - throw new LocalServerException('Host violates local access rules'); |
|
184 | - } |
|
185 | - |
|
186 | - // Also check for IPv6 IPv4 nesting, because that's not covered by filter_var |
|
187 | - if ((bool)filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && substr_count($host, '.') > 0) { |
|
188 | - $delimiter = strrpos($host, ':'); // Get last colon |
|
189 | - $ipv4Address = substr($host, $delimiter + 1); |
|
190 | - |
|
191 | - if (!filter_var($ipv4Address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
192 | - $this->logger->warning("Host $host was not connected to because it violates local access rules"); |
|
193 | - throw new LocalServerException('Host violates local access rules'); |
|
194 | - } |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Sends a GET request |
|
200 | - * |
|
201 | - * @param string $uri |
|
202 | - * @param array $options Array such as |
|
203 | - * 'query' => [ |
|
204 | - * 'field' => 'abc', |
|
205 | - * 'other_field' => '123', |
|
206 | - * 'file_name' => fopen('/path/to/file', 'r'), |
|
207 | - * ], |
|
208 | - * 'headers' => [ |
|
209 | - * 'foo' => 'bar', |
|
210 | - * ], |
|
211 | - * 'cookies' => [' |
|
212 | - * 'foo' => 'bar', |
|
213 | - * ], |
|
214 | - * 'allow_redirects' => [ |
|
215 | - * 'max' => 10, // allow at most 10 redirects. |
|
216 | - * 'strict' => true, // use "strict" RFC compliant redirects. |
|
217 | - * 'referer' => true, // add a Referer header |
|
218 | - * 'protocols' => ['https'] // only allow https URLs |
|
219 | - * ], |
|
220 | - * 'save_to' => '/path/to/file', // save to a file or a stream |
|
221 | - * 'verify' => true, // bool or string to CA file |
|
222 | - * 'debug' => true, |
|
223 | - * 'timeout' => 5, |
|
224 | - * @return IResponse |
|
225 | - * @throws \Exception If the request could not get completed |
|
226 | - */ |
|
227 | - public function get(string $uri, array $options = []): IResponse { |
|
228 | - $this->preventLocalAddress($uri, $options); |
|
229 | - $response = $this->client->request('get', $uri, $this->buildRequestOptions($options)); |
|
230 | - $isStream = isset($options['stream']) && $options['stream']; |
|
231 | - return new Response($response, $isStream); |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Sends a HEAD request |
|
236 | - * |
|
237 | - * @param string $uri |
|
238 | - * @param array $options Array such as |
|
239 | - * 'headers' => [ |
|
240 | - * 'foo' => 'bar', |
|
241 | - * ], |
|
242 | - * 'cookies' => [' |
|
243 | - * 'foo' => 'bar', |
|
244 | - * ], |
|
245 | - * 'allow_redirects' => [ |
|
246 | - * 'max' => 10, // allow at most 10 redirects. |
|
247 | - * 'strict' => true, // use "strict" RFC compliant redirects. |
|
248 | - * 'referer' => true, // add a Referer header |
|
249 | - * 'protocols' => ['https'] // only allow https URLs |
|
250 | - * ], |
|
251 | - * 'save_to' => '/path/to/file', // save to a file or a stream |
|
252 | - * 'verify' => true, // bool or string to CA file |
|
253 | - * 'debug' => true, |
|
254 | - * 'timeout' => 5, |
|
255 | - * @return IResponse |
|
256 | - * @throws \Exception If the request could not get completed |
|
257 | - */ |
|
258 | - public function head(string $uri, array $options = []): IResponse { |
|
259 | - $this->preventLocalAddress($uri, $options); |
|
260 | - $response = $this->client->request('head', $uri, $this->buildRequestOptions($options)); |
|
261 | - return new Response($response); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Sends a POST request |
|
266 | - * |
|
267 | - * @param string $uri |
|
268 | - * @param array $options Array such as |
|
269 | - * 'body' => [ |
|
270 | - * 'field' => 'abc', |
|
271 | - * 'other_field' => '123', |
|
272 | - * 'file_name' => fopen('/path/to/file', 'r'), |
|
273 | - * ], |
|
274 | - * 'headers' => [ |
|
275 | - * 'foo' => 'bar', |
|
276 | - * ], |
|
277 | - * 'cookies' => [' |
|
278 | - * 'foo' => 'bar', |
|
279 | - * ], |
|
280 | - * 'allow_redirects' => [ |
|
281 | - * 'max' => 10, // allow at most 10 redirects. |
|
282 | - * 'strict' => true, // use "strict" RFC compliant redirects. |
|
283 | - * 'referer' => true, // add a Referer header |
|
284 | - * 'protocols' => ['https'] // only allow https URLs |
|
285 | - * ], |
|
286 | - * 'save_to' => '/path/to/file', // save to a file or a stream |
|
287 | - * 'verify' => true, // bool or string to CA file |
|
288 | - * 'debug' => true, |
|
289 | - * 'timeout' => 5, |
|
290 | - * @return IResponse |
|
291 | - * @throws \Exception If the request could not get completed |
|
292 | - */ |
|
293 | - public function post(string $uri, array $options = []): IResponse { |
|
294 | - $this->preventLocalAddress($uri, $options); |
|
295 | - |
|
296 | - if (isset($options['body']) && is_array($options['body'])) { |
|
297 | - $options['form_params'] = $options['body']; |
|
298 | - unset($options['body']); |
|
299 | - } |
|
300 | - $response = $this->client->request('post', $uri, $this->buildRequestOptions($options)); |
|
301 | - return new Response($response); |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Sends a PUT request |
|
306 | - * |
|
307 | - * @param string $uri |
|
308 | - * @param array $options Array such as |
|
309 | - * 'body' => [ |
|
310 | - * 'field' => 'abc', |
|
311 | - * 'other_field' => '123', |
|
312 | - * 'file_name' => fopen('/path/to/file', 'r'), |
|
313 | - * ], |
|
314 | - * 'headers' => [ |
|
315 | - * 'foo' => 'bar', |
|
316 | - * ], |
|
317 | - * 'cookies' => [' |
|
318 | - * 'foo' => 'bar', |
|
319 | - * ], |
|
320 | - * 'allow_redirects' => [ |
|
321 | - * 'max' => 10, // allow at most 10 redirects. |
|
322 | - * 'strict' => true, // use "strict" RFC compliant redirects. |
|
323 | - * 'referer' => true, // add a Referer header |
|
324 | - * 'protocols' => ['https'] // only allow https URLs |
|
325 | - * ], |
|
326 | - * 'save_to' => '/path/to/file', // save to a file or a stream |
|
327 | - * 'verify' => true, // bool or string to CA file |
|
328 | - * 'debug' => true, |
|
329 | - * 'timeout' => 5, |
|
330 | - * @return IResponse |
|
331 | - * @throws \Exception If the request could not get completed |
|
332 | - */ |
|
333 | - public function put(string $uri, array $options = []): IResponse { |
|
334 | - $this->preventLocalAddress($uri, $options); |
|
335 | - $response = $this->client->request('put', $uri, $this->buildRequestOptions($options)); |
|
336 | - return new Response($response); |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Sends a DELETE request |
|
341 | - * |
|
342 | - * @param string $uri |
|
343 | - * @param array $options Array such as |
|
344 | - * 'body' => [ |
|
345 | - * 'field' => 'abc', |
|
346 | - * 'other_field' => '123', |
|
347 | - * 'file_name' => fopen('/path/to/file', 'r'), |
|
348 | - * ], |
|
349 | - * 'headers' => [ |
|
350 | - * 'foo' => 'bar', |
|
351 | - * ], |
|
352 | - * 'cookies' => [' |
|
353 | - * 'foo' => 'bar', |
|
354 | - * ], |
|
355 | - * 'allow_redirects' => [ |
|
356 | - * 'max' => 10, // allow at most 10 redirects. |
|
357 | - * 'strict' => true, // use "strict" RFC compliant redirects. |
|
358 | - * 'referer' => true, // add a Referer header |
|
359 | - * 'protocols' => ['https'] // only allow https URLs |
|
360 | - * ], |
|
361 | - * 'save_to' => '/path/to/file', // save to a file or a stream |
|
362 | - * 'verify' => true, // bool or string to CA file |
|
363 | - * 'debug' => true, |
|
364 | - * 'timeout' => 5, |
|
365 | - * @return IResponse |
|
366 | - * @throws \Exception If the request could not get completed |
|
367 | - */ |
|
368 | - public function delete(string $uri, array $options = []): IResponse { |
|
369 | - $this->preventLocalAddress($uri, $options); |
|
370 | - $response = $this->client->request('delete', $uri, $this->buildRequestOptions($options)); |
|
371 | - return new Response($response); |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * Sends a options request |
|
376 | - * |
|
377 | - * @param string $uri |
|
378 | - * @param array $options Array such as |
|
379 | - * 'body' => [ |
|
380 | - * 'field' => 'abc', |
|
381 | - * 'other_field' => '123', |
|
382 | - * 'file_name' => fopen('/path/to/file', 'r'), |
|
383 | - * ], |
|
384 | - * 'headers' => [ |
|
385 | - * 'foo' => 'bar', |
|
386 | - * ], |
|
387 | - * 'cookies' => [' |
|
388 | - * 'foo' => 'bar', |
|
389 | - * ], |
|
390 | - * 'allow_redirects' => [ |
|
391 | - * 'max' => 10, // allow at most 10 redirects. |
|
392 | - * 'strict' => true, // use "strict" RFC compliant redirects. |
|
393 | - * 'referer' => true, // add a Referer header |
|
394 | - * 'protocols' => ['https'] // only allow https URLs |
|
395 | - * ], |
|
396 | - * 'save_to' => '/path/to/file', // save to a file or a stream |
|
397 | - * 'verify' => true, // bool or string to CA file |
|
398 | - * 'debug' => true, |
|
399 | - * 'timeout' => 5, |
|
400 | - * @return IResponse |
|
401 | - * @throws \Exception If the request could not get completed |
|
402 | - */ |
|
403 | - public function options(string $uri, array $options = []): IResponse { |
|
404 | - $this->preventLocalAddress($uri, $options); |
|
405 | - $response = $this->client->request('options', $uri, $this->buildRequestOptions($options)); |
|
406 | - return new Response($response); |
|
407 | - } |
|
51 | + /** @var GuzzleClient */ |
|
52 | + private $client; |
|
53 | + /** @var IConfig */ |
|
54 | + private $config; |
|
55 | + /** @var ILogger */ |
|
56 | + private $logger; |
|
57 | + /** @var ICertificateManager */ |
|
58 | + private $certificateManager; |
|
59 | + |
|
60 | + public function __construct( |
|
61 | + IConfig $config, |
|
62 | + ILogger $logger, |
|
63 | + ICertificateManager $certificateManager, |
|
64 | + GuzzleClient $client |
|
65 | + ) { |
|
66 | + $this->config = $config; |
|
67 | + $this->logger = $logger; |
|
68 | + $this->client = $client; |
|
69 | + $this->certificateManager = $certificateManager; |
|
70 | + } |
|
71 | + |
|
72 | + private function buildRequestOptions(array $options): array { |
|
73 | + $proxy = $this->getProxyUri(); |
|
74 | + |
|
75 | + $defaults = [ |
|
76 | + RequestOptions::VERIFY => $this->getCertBundle(), |
|
77 | + RequestOptions::TIMEOUT => 30, |
|
78 | + ]; |
|
79 | + |
|
80 | + // Only add RequestOptions::PROXY if Nextcloud is explicitly |
|
81 | + // configured to use a proxy. This is needed in order not to override |
|
82 | + // Guzzle default values. |
|
83 | + if ($proxy !== null) { |
|
84 | + $defaults[RequestOptions::PROXY] = $proxy; |
|
85 | + } |
|
86 | + |
|
87 | + $options = array_merge($defaults, $options); |
|
88 | + |
|
89 | + if (!isset($options[RequestOptions::HEADERS]['User-Agent'])) { |
|
90 | + $options[RequestOptions::HEADERS]['User-Agent'] = 'Nextcloud Server Crawler'; |
|
91 | + } |
|
92 | + |
|
93 | + if (!isset($options[RequestOptions::HEADERS]['Accept-Encoding'])) { |
|
94 | + $options[RequestOptions::HEADERS]['Accept-Encoding'] = 'gzip'; |
|
95 | + } |
|
96 | + |
|
97 | + return $options; |
|
98 | + } |
|
99 | + |
|
100 | + private function getCertBundle(): string { |
|
101 | + // If the instance is not yet setup we need to use the static path as |
|
102 | + // $this->certificateManager->getAbsoluteBundlePath() tries to instantiate |
|
103 | + // a view |
|
104 | + if ($this->config->getSystemValue('installed', false) === false) { |
|
105 | + return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt'; |
|
106 | + } |
|
107 | + |
|
108 | + return $this->certificateManager->getAbsoluteBundlePath(); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Returns a null or an associative array specifiying the proxy URI for |
|
113 | + * 'http' and 'https' schemes, in addition to a 'no' key value pair |
|
114 | + * providing a list of host names that should not be proxied to. |
|
115 | + * |
|
116 | + * @return array|null |
|
117 | + * |
|
118 | + * The return array looks like: |
|
119 | + * [ |
|
120 | + * 'http' => 'username:[email protected]', |
|
121 | + * 'https' => 'username:[email protected]', |
|
122 | + * 'no' => ['foo.com', 'bar.com'] |
|
123 | + * ] |
|
124 | + * |
|
125 | + */ |
|
126 | + private function getProxyUri(): ?array { |
|
127 | + $proxyHost = $this->config->getSystemValue('proxy', ''); |
|
128 | + |
|
129 | + if ($proxyHost === '' || $proxyHost === null) { |
|
130 | + return null; |
|
131 | + } |
|
132 | + |
|
133 | + $proxyUserPwd = $this->config->getSystemValue('proxyuserpwd', ''); |
|
134 | + if ($proxyUserPwd !== '' && $proxyUserPwd !== null) { |
|
135 | + $proxyHost = $proxyUserPwd . '@' . $proxyHost; |
|
136 | + } |
|
137 | + |
|
138 | + $proxy = [ |
|
139 | + 'http' => $proxyHost, |
|
140 | + 'https' => $proxyHost, |
|
141 | + ]; |
|
142 | + |
|
143 | + $proxyExclude = $this->config->getSystemValue('proxyexclude', []); |
|
144 | + if ($proxyExclude !== [] && $proxyExclude !== null) { |
|
145 | + $proxy['no'] = $proxyExclude; |
|
146 | + } |
|
147 | + |
|
148 | + return $proxy; |
|
149 | + } |
|
150 | + |
|
151 | + protected function preventLocalAddress(string $uri, array $options): void { |
|
152 | + if (($options['nextcloud']['allow_local_address'] ?? false) || |
|
153 | + $this->config->getSystemValueBool('allow_local_remote_servers', false)) { |
|
154 | + return; |
|
155 | + } |
|
156 | + |
|
157 | + $host = parse_url($uri, PHP_URL_HOST); |
|
158 | + if ($host === false || $host === null) { |
|
159 | + $this->logger->warning("Could not detect any host in $uri"); |
|
160 | + throw new LocalServerException('Could not detect any host'); |
|
161 | + } |
|
162 | + |
|
163 | + $host = strtolower($host); |
|
164 | + // Remove brackets from IPv6 addresses |
|
165 | + if (strpos($host, '[') === 0 && substr($host, -1) === ']') { |
|
166 | + $host = substr($host, 1, -1); |
|
167 | + } |
|
168 | + |
|
169 | + // Disallow localhost and local network |
|
170 | + if ($host === 'localhost' || substr($host, -6) === '.local' || substr($host, -10) === '.localhost') { |
|
171 | + $this->logger->warning("Host $host was not connected to because it violates local access rules"); |
|
172 | + throw new LocalServerException('Host violates local access rules'); |
|
173 | + } |
|
174 | + |
|
175 | + // Disallow hostname only |
|
176 | + if (substr_count($host, '.') === 0) { |
|
177 | + $this->logger->warning("Host $host was not connected to because it violates local access rules"); |
|
178 | + throw new LocalServerException('Host violates local access rules'); |
|
179 | + } |
|
180 | + |
|
181 | + if ((bool)filter_var($host, FILTER_VALIDATE_IP) && !filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
182 | + $this->logger->warning("Host $host was not connected to because it violates local access rules"); |
|
183 | + throw new LocalServerException('Host violates local access rules'); |
|
184 | + } |
|
185 | + |
|
186 | + // Also check for IPv6 IPv4 nesting, because that's not covered by filter_var |
|
187 | + if ((bool)filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && substr_count($host, '.') > 0) { |
|
188 | + $delimiter = strrpos($host, ':'); // Get last colon |
|
189 | + $ipv4Address = substr($host, $delimiter + 1); |
|
190 | + |
|
191 | + if (!filter_var($ipv4Address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
192 | + $this->logger->warning("Host $host was not connected to because it violates local access rules"); |
|
193 | + throw new LocalServerException('Host violates local access rules'); |
|
194 | + } |
|
195 | + } |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Sends a GET request |
|
200 | + * |
|
201 | + * @param string $uri |
|
202 | + * @param array $options Array such as |
|
203 | + * 'query' => [ |
|
204 | + * 'field' => 'abc', |
|
205 | + * 'other_field' => '123', |
|
206 | + * 'file_name' => fopen('/path/to/file', 'r'), |
|
207 | + * ], |
|
208 | + * 'headers' => [ |
|
209 | + * 'foo' => 'bar', |
|
210 | + * ], |
|
211 | + * 'cookies' => [' |
|
212 | + * 'foo' => 'bar', |
|
213 | + * ], |
|
214 | + * 'allow_redirects' => [ |
|
215 | + * 'max' => 10, // allow at most 10 redirects. |
|
216 | + * 'strict' => true, // use "strict" RFC compliant redirects. |
|
217 | + * 'referer' => true, // add a Referer header |
|
218 | + * 'protocols' => ['https'] // only allow https URLs |
|
219 | + * ], |
|
220 | + * 'save_to' => '/path/to/file', // save to a file or a stream |
|
221 | + * 'verify' => true, // bool or string to CA file |
|
222 | + * 'debug' => true, |
|
223 | + * 'timeout' => 5, |
|
224 | + * @return IResponse |
|
225 | + * @throws \Exception If the request could not get completed |
|
226 | + */ |
|
227 | + public function get(string $uri, array $options = []): IResponse { |
|
228 | + $this->preventLocalAddress($uri, $options); |
|
229 | + $response = $this->client->request('get', $uri, $this->buildRequestOptions($options)); |
|
230 | + $isStream = isset($options['stream']) && $options['stream']; |
|
231 | + return new Response($response, $isStream); |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Sends a HEAD request |
|
236 | + * |
|
237 | + * @param string $uri |
|
238 | + * @param array $options Array such as |
|
239 | + * 'headers' => [ |
|
240 | + * 'foo' => 'bar', |
|
241 | + * ], |
|
242 | + * 'cookies' => [' |
|
243 | + * 'foo' => 'bar', |
|
244 | + * ], |
|
245 | + * 'allow_redirects' => [ |
|
246 | + * 'max' => 10, // allow at most 10 redirects. |
|
247 | + * 'strict' => true, // use "strict" RFC compliant redirects. |
|
248 | + * 'referer' => true, // add a Referer header |
|
249 | + * 'protocols' => ['https'] // only allow https URLs |
|
250 | + * ], |
|
251 | + * 'save_to' => '/path/to/file', // save to a file or a stream |
|
252 | + * 'verify' => true, // bool or string to CA file |
|
253 | + * 'debug' => true, |
|
254 | + * 'timeout' => 5, |
|
255 | + * @return IResponse |
|
256 | + * @throws \Exception If the request could not get completed |
|
257 | + */ |
|
258 | + public function head(string $uri, array $options = []): IResponse { |
|
259 | + $this->preventLocalAddress($uri, $options); |
|
260 | + $response = $this->client->request('head', $uri, $this->buildRequestOptions($options)); |
|
261 | + return new Response($response); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Sends a POST request |
|
266 | + * |
|
267 | + * @param string $uri |
|
268 | + * @param array $options Array such as |
|
269 | + * 'body' => [ |
|
270 | + * 'field' => 'abc', |
|
271 | + * 'other_field' => '123', |
|
272 | + * 'file_name' => fopen('/path/to/file', 'r'), |
|
273 | + * ], |
|
274 | + * 'headers' => [ |
|
275 | + * 'foo' => 'bar', |
|
276 | + * ], |
|
277 | + * 'cookies' => [' |
|
278 | + * 'foo' => 'bar', |
|
279 | + * ], |
|
280 | + * 'allow_redirects' => [ |
|
281 | + * 'max' => 10, // allow at most 10 redirects. |
|
282 | + * 'strict' => true, // use "strict" RFC compliant redirects. |
|
283 | + * 'referer' => true, // add a Referer header |
|
284 | + * 'protocols' => ['https'] // only allow https URLs |
|
285 | + * ], |
|
286 | + * 'save_to' => '/path/to/file', // save to a file or a stream |
|
287 | + * 'verify' => true, // bool or string to CA file |
|
288 | + * 'debug' => true, |
|
289 | + * 'timeout' => 5, |
|
290 | + * @return IResponse |
|
291 | + * @throws \Exception If the request could not get completed |
|
292 | + */ |
|
293 | + public function post(string $uri, array $options = []): IResponse { |
|
294 | + $this->preventLocalAddress($uri, $options); |
|
295 | + |
|
296 | + if (isset($options['body']) && is_array($options['body'])) { |
|
297 | + $options['form_params'] = $options['body']; |
|
298 | + unset($options['body']); |
|
299 | + } |
|
300 | + $response = $this->client->request('post', $uri, $this->buildRequestOptions($options)); |
|
301 | + return new Response($response); |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Sends a PUT request |
|
306 | + * |
|
307 | + * @param string $uri |
|
308 | + * @param array $options Array such as |
|
309 | + * 'body' => [ |
|
310 | + * 'field' => 'abc', |
|
311 | + * 'other_field' => '123', |
|
312 | + * 'file_name' => fopen('/path/to/file', 'r'), |
|
313 | + * ], |
|
314 | + * 'headers' => [ |
|
315 | + * 'foo' => 'bar', |
|
316 | + * ], |
|
317 | + * 'cookies' => [' |
|
318 | + * 'foo' => 'bar', |
|
319 | + * ], |
|
320 | + * 'allow_redirects' => [ |
|
321 | + * 'max' => 10, // allow at most 10 redirects. |
|
322 | + * 'strict' => true, // use "strict" RFC compliant redirects. |
|
323 | + * 'referer' => true, // add a Referer header |
|
324 | + * 'protocols' => ['https'] // only allow https URLs |
|
325 | + * ], |
|
326 | + * 'save_to' => '/path/to/file', // save to a file or a stream |
|
327 | + * 'verify' => true, // bool or string to CA file |
|
328 | + * 'debug' => true, |
|
329 | + * 'timeout' => 5, |
|
330 | + * @return IResponse |
|
331 | + * @throws \Exception If the request could not get completed |
|
332 | + */ |
|
333 | + public function put(string $uri, array $options = []): IResponse { |
|
334 | + $this->preventLocalAddress($uri, $options); |
|
335 | + $response = $this->client->request('put', $uri, $this->buildRequestOptions($options)); |
|
336 | + return new Response($response); |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Sends a DELETE request |
|
341 | + * |
|
342 | + * @param string $uri |
|
343 | + * @param array $options Array such as |
|
344 | + * 'body' => [ |
|
345 | + * 'field' => 'abc', |
|
346 | + * 'other_field' => '123', |
|
347 | + * 'file_name' => fopen('/path/to/file', 'r'), |
|
348 | + * ], |
|
349 | + * 'headers' => [ |
|
350 | + * 'foo' => 'bar', |
|
351 | + * ], |
|
352 | + * 'cookies' => [' |
|
353 | + * 'foo' => 'bar', |
|
354 | + * ], |
|
355 | + * 'allow_redirects' => [ |
|
356 | + * 'max' => 10, // allow at most 10 redirects. |
|
357 | + * 'strict' => true, // use "strict" RFC compliant redirects. |
|
358 | + * 'referer' => true, // add a Referer header |
|
359 | + * 'protocols' => ['https'] // only allow https URLs |
|
360 | + * ], |
|
361 | + * 'save_to' => '/path/to/file', // save to a file or a stream |
|
362 | + * 'verify' => true, // bool or string to CA file |
|
363 | + * 'debug' => true, |
|
364 | + * 'timeout' => 5, |
|
365 | + * @return IResponse |
|
366 | + * @throws \Exception If the request could not get completed |
|
367 | + */ |
|
368 | + public function delete(string $uri, array $options = []): IResponse { |
|
369 | + $this->preventLocalAddress($uri, $options); |
|
370 | + $response = $this->client->request('delete', $uri, $this->buildRequestOptions($options)); |
|
371 | + return new Response($response); |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * Sends a options request |
|
376 | + * |
|
377 | + * @param string $uri |
|
378 | + * @param array $options Array such as |
|
379 | + * 'body' => [ |
|
380 | + * 'field' => 'abc', |
|
381 | + * 'other_field' => '123', |
|
382 | + * 'file_name' => fopen('/path/to/file', 'r'), |
|
383 | + * ], |
|
384 | + * 'headers' => [ |
|
385 | + * 'foo' => 'bar', |
|
386 | + * ], |
|
387 | + * 'cookies' => [' |
|
388 | + * 'foo' => 'bar', |
|
389 | + * ], |
|
390 | + * 'allow_redirects' => [ |
|
391 | + * 'max' => 10, // allow at most 10 redirects. |
|
392 | + * 'strict' => true, // use "strict" RFC compliant redirects. |
|
393 | + * 'referer' => true, // add a Referer header |
|
394 | + * 'protocols' => ['https'] // only allow https URLs |
|
395 | + * ], |
|
396 | + * 'save_to' => '/path/to/file', // save to a file or a stream |
|
397 | + * 'verify' => true, // bool or string to CA file |
|
398 | + * 'debug' => true, |
|
399 | + * 'timeout' => 5, |
|
400 | + * @return IResponse |
|
401 | + * @throws \Exception If the request could not get completed |
|
402 | + */ |
|
403 | + public function options(string $uri, array $options = []): IResponse { |
|
404 | + $this->preventLocalAddress($uri, $options); |
|
405 | + $response = $this->client->request('options', $uri, $this->buildRequestOptions($options)); |
|
406 | + return new Response($response); |
|
407 | + } |
|
408 | 408 | } |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | // $this->certificateManager->getAbsoluteBundlePath() tries to instantiate |
103 | 103 | // a view |
104 | 104 | if ($this->config->getSystemValue('installed', false) === false) { |
105 | - return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt'; |
|
105 | + return \OC::$SERVERROOT.'/resources/config/ca-bundle.crt'; |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | return $this->certificateManager->getAbsoluteBundlePath(); |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | |
133 | 133 | $proxyUserPwd = $this->config->getSystemValue('proxyuserpwd', ''); |
134 | 134 | if ($proxyUserPwd !== '' && $proxyUserPwd !== null) { |
135 | - $proxyHost = $proxyUserPwd . '@' . $proxyHost; |
|
135 | + $proxyHost = $proxyUserPwd.'@'.$proxyHost; |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | $proxy = [ |
@@ -178,13 +178,13 @@ discard block |
||
178 | 178 | throw new LocalServerException('Host violates local access rules'); |
179 | 179 | } |
180 | 180 | |
181 | - if ((bool)filter_var($host, FILTER_VALIDATE_IP) && !filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
181 | + if ((bool) filter_var($host, FILTER_VALIDATE_IP) && !filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
182 | 182 | $this->logger->warning("Host $host was not connected to because it violates local access rules"); |
183 | 183 | throw new LocalServerException('Host violates local access rules'); |
184 | 184 | } |
185 | 185 | |
186 | 186 | // Also check for IPv6 IPv4 nesting, because that's not covered by filter_var |
187 | - if ((bool)filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && substr_count($host, '.') > 0) { |
|
187 | + if ((bool) filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && substr_count($host, '.') > 0) { |
|
188 | 188 | $delimiter = strrpos($host, ':'); // Get last colon |
189 | 189 | $ipv4Address = substr($host, $delimiter + 1); |
190 | 190 |