Page logo can be a gif now
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / util / HTTPRequest.class.php
CommitLineData
86fc0430
TD
1<?php
2namespace wcf\util;
3536d2fe
AE
3use wcf\system\exception\HTTPNotFoundException;
4use wcf\system\exception\HTTPServerErrorException;
5use wcf\system\exception\HTTPUnauthorizedException;
86fc0430
TD
6use wcf\system\exception\SystemException;
7use wcf\system\io\RemoteFile;
8use wcf\system\Regex;
9use wcf\system\WCF;
10
11/**
8fe14bd6 12 * Sends HTTP/1.1 requests.
86fc0430
TD
13 * It supports POST, SSL, Basic Auth etc.
14 *
3536d2fe 15 * @author Tim Duesterhus
2b6cb5c2 16 * @copyright 2001-2015 WoltLab GmbH
86fc0430
TD
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package com.woltlab.wcf
19 * @subpackage util
20 * @category Community Framework
21 */
a195ffa6 22final class HTTPRequest {
86fc0430
TD
23 /**
24 * given options
a17de04e 25 * @var array
86fc0430
TD
26 */
27 private $options = array();
28
29 /**
30 * given post parameters
a17de04e 31 * @var array
86fc0430
TD
32 */
33 private $postParameters = array();
34
5f70a0de
TD
35 /**
36 * given files
06355ec3 37 * @var array
5f70a0de
TD
38 */
39 private $files = array();
40
86fc0430 41 /**
60f613e2 42 * indicates if request will be made via SSL
a17de04e 43 * @var boolean
86fc0430
TD
44 */
45 private $useSSL = false;
46
5819f701
TD
47 /**
48 * indicates if the connection to the proxy target will be made via SSL
49 * @var boolean
50 */
51 private $originUseSSL = false;
52
86fc0430
TD
53 /**
54 * target host
a17de04e 55 * @var string
86fc0430
TD
56 */
57 private $host;
58
5819f701
TD
59 /**
60 * target host if a proxy is used
61 * @var string
62 */
63 private $originHost;
64
86fc0430
TD
65 /**
66 * target port
a17de04e 67 * @var integer
86fc0430
TD
68 */
69 private $port;
a17de04e 70
5819f701
TD
71 /**
72 * target port if a proxy is used
73 * @var integer
74 */
75 private $originPort;
76
86fc0430
TD
77 /**
78 * target path
a17de04e 79 * @var string
86fc0430
TD
80 */
81 private $path;
82
83 /**
84 * target query string
a17de04e 85 * @var string
86fc0430
TD
86 */
87 private $query;
88
25cdc083
AE
89 /**
90 * request URL
91 * @var string
92 */
93 private $url = '';
94
86fc0430
TD
95 /**
96 * request headers
a17de04e 97 * @var array<string>
86fc0430
TD
98 */
99 private $headers = array();
100
8fe14bd6
TD
101 /**
102 * legacy headers
103 * @var array<string>
104 */
105 private $legacyHeaders = array();
106
5f70a0de
TD
107 /**
108 * request body
109 * @var string
110 */
111 private $body = '';
112
86fc0430
TD
113 /**
114 * reply headers
a17de04e 115 * @var array<string>
86fc0430
TD
116 */
117 private $replyHeaders = array();
118
119 /**
120 * reply body
a17de04e 121 * @var string
86fc0430
TD
122 */
123 private $replyBody = '';
124
125 /**
126 * reply status code
a17de04e 127 * @var integer
86fc0430
TD
128 */
129 private $statusCode = 0;
130
131 /**
a17de04e 132 * Constructs a new instance of HTTPRequest.
86fc0430
TD
133 *
134 * @param string $url URL to connect to
135 * @param array<string> $options
1b20f990 136 * @param mixed $postParameters Parameters to send via POST
5f70a0de 137 * @param array $files Files to attach to the request
86fc0430 138 */
1b20f990 139 public function __construct($url, array $options = array(), $postParameters = array(), array $files = array()) {
86fc0430
TD
140 $this->setURL($url);
141
142 $this->postParameters = $postParameters;
5f70a0de 143 $this->files = $files;
86fc0430
TD
144
145 $this->setOptions($options);
146
a195ffa6 147 // set default headers
8fe14bd6
TD
148 $this->addHeader('user-agent', "HTTP.PHP (HTTPRequest.class.php; WoltLab Community Framework/".WCF_VERSION."; ".WCF::getLanguage()->languageCode.")");
149 $this->addHeader('accept', '*/*');
150 $this->addHeader('accept-language', WCF::getLanguage()->getFixedLanguageCode());
151
4d28d5a2 152 if (isset($this->options['maxLength'])) {
5262964b 153 $this->addHeader('Range', 'bytes=0-'.($this->options['maxLength'] - 1));
4d28d5a2
SG
154 }
155
86fc0430 156 if ($this->options['method'] !== 'GET') {
5f70a0de 157 if (empty($this->files)) {
1b20f990
AE
158 if (is_array($postParameters)) {
159 $this->body = http_build_query($this->postParameters, '', '&');
160 }
161 else if (is_string($postParameters) && !empty($postParameters)) {
162 $this->body = $postParameters;
163 }
164
8fe14bd6 165 $this->addHeader('content-type', 'application/x-www-form-urlencoded');
5f70a0de
TD
166 }
167 else {
168 $boundary = StringUtil::getRandomID();
8fe14bd6 169 $this->addHeader('content-type', 'multipart/form-data; boundary='.$boundary);
5f70a0de
TD
170
171 // source of the iterators: http://stackoverflow.com/a/7623716/782822
172 if (!empty($this->postParameters)) {
173 $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->postParameters), \RecursiveIteratorIterator::SELF_FIRST);
174 foreach ($iterator as $k => $v) {
175 if (!$iterator->hasChildren()) {
176 $key = '';
177 for ($i = 0, $max = $iterator->getDepth(); $i <= $max; $i++) {
178 if ($i === 0) $key .= $iterator->getSubIterator($i)->key();
179 else $key .= '['.$iterator->getSubIterator($i)->key().']';
180 }
181
182 $this->body .= "--".$boundary."\r\n";
183 $this->body .= 'Content-Disposition: form-data; name="'.$key.'"'."\r\n\r\n";
184 $this->body .= $v."\r\n";
185 }
186 }
187 }
188
189 $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->files), \RecursiveIteratorIterator::SELF_FIRST);
190 foreach ($iterator as $k => $v) {
191 if (!$iterator->hasChildren()) {
192 $key = '';
193 for ($i = 0, $max = $iterator->getDepth(); $i <= $max; $i++) {
194 if ($i === 0) $key .= $iterator->getSubIterator($i)->key();
195 else $key .= '['.$iterator->getSubIterator($i)->key().']';
196 }
197
198 $this->body .= "--".$boundary."\r\n";
199 $this->body .= 'Content-Disposition: form-data; name="'.$k.'"; filename="'.basename($v).'"'."\r\n";
200 $this->body .= 'Content-Type: '.(FileUtil::getMimeType($v) ?: 'application/octet-stream.')."\r\n\r\n";
201 $this->body .= file_get_contents($v)."\r\n";
202 }
203 }
204
205 $this->body .= "--".$boundary."--";
206 }
8fe14bd6 207 $this->addHeader('content-length', strlen($this->body));
86fc0430
TD
208 }
209 if (isset($this->options['auth'])) {
8fe14bd6 210 $this->addHeader('authorization', "Basic ".base64_encode($options['auth']['username'].":".$options['auth']['password']));
86fc0430 211 }
8fe14bd6 212 $this->addHeader('connection', 'Close');
86fc0430
TD
213 }
214
215 /**
216 * Parses the given URL and applies PROXY_SERVER_HTTP.
217 *
a17de04e 218 * @param string $url
86fc0430
TD
219 */
220 private function setURL($url) {
adc039a5 221 $parsedUrl = $originUrl = parse_url($url);
86fc0430
TD
222 if (PROXY_SERVER_HTTP) {
223 $parsedUrl = parse_url(PROXY_SERVER_HTTP);
224 $this->path = $url;
225 }
226 else {
86fc0430
TD
227 $this->path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '/';
228 }
d5bd7602 229
86fc0430
TD
230 $this->useSSL = $parsedUrl['scheme'] === 'https';
231 $this->host = $parsedUrl['host'];
232 $this->port = isset($parsedUrl['port']) ? $parsedUrl['port'] : ($this->useSSL ? 443 : 80);
86fc0430 233 $this->query = isset($parsedUrl['query']) ? $parsedUrl['query'] : '';
d5bd7602 234
5819f701
TD
235 $this->originUseSSL = $originUrl['scheme'] === 'https';
236 $this->originHost = $originUrl['host'];
237 $this->originPort = isset($originUrl['port']) ? $originUrl['port'] : ($this->originUseSSL ? 443 : 80);
238
d5bd7602 239 // update the 'Host:' header if URL has changed
adc039a5 240 if ($this->url != $url) {
5819f701 241 $this->addHeader('host', $this->originHost.($this->originPort != ($this->originUseSSL ? 443 : 80) ? ':'.$this->originPort : ''));
d5bd7602
AE
242 }
243
244 $this->url = $url;
86fc0430
TD
245 }
246
247 /**
248 * Executes the HTTP request.
249 */
250 public function execute() {
251 // connect
7e6297c8
TD
252 $remoteFile = new RemoteFile(($this->useSSL ? 'ssl://' : '').$this->host, $this->port, $this->options['timeout'], array(
253 'ssl' => array(
254 'peer_name' => $this->originHost
255 )
256 ));
86fc0430 257
5819f701
TD
258 if ($this->originUseSSL && PROXY_SERVER_HTTP) {
259 if ($this->useSSL) throw new SystemException("Unable to proxy HTTPS when using TLS for proxy connection");
260
261 $request = "CONNECT ".$this->originHost.":".$this->originPort." HTTP/1.0\r\n";
262 if (isset($this->headers['user-agent'])) {
263 $request .= 'user-agent: '.reset($this->headers['user-agent'])."\r\n";
264 }
265 $request .= "Host: ".$this->originHost.":".$this->originPort."\r\n";
266 $request .= "\r\n";
267 $remoteFile->puts($request);
268 $this->replyHeaders = array();
269 while (!$remoteFile->eof()) {
270 $line = $remoteFile->gets();
271 if (rtrim($line) === '') {
272 $this->parseReplyHeaders();
273
274 break;
275 }
276 $this->replyHeaders[] = $line;
277 }
278 if ($this->statusCode != 200) throw new SystemException("Expected 200 Ok as reply to my CONNECT, got '".$this->statusCode."'");
279 $remoteFile->setTLS(true);
280 }
281
8fe14bd6 282 $request = $this->options['method']." ".$this->path.($this->query ? '?'.$this->query : '')." HTTP/1.1\r\n";
86fc0430 283
a195ffa6 284 // add headers
86fc0430
TD
285 foreach ($this->headers as $name => $values) {
286 foreach ($values as $value) {
287 $request .= $name.": ".$value."\r\n";
288 }
289 }
290 $request .= "\r\n";
8fe14bd6 291
a195ffa6 292 // add post parameters
5f70a0de 293 if ($this->options['method'] !== 'GET') $request .= $this->body."\r\n\r\n";
d5bd7602 294
86fc0430
TD
295 $remoteFile->puts($request);
296
297 $inHeader = true;
298 $this->replyHeaders = array();
299 $this->replyBody = '';
8fe14bd6 300 $chunkLength = 0;
5262964b 301 $bodyLength = 0;
c7fe2510 302
8d2262a1 303 $chunkedTransferRegex = new Regex('(^|,)[ \t]*chunked[ \t]*$', Regex::CASE_INSENSITIVE);
7ced4c6a
TD
304 // read http response, until one of is true
305 // a) EOF is reached
306 // b) bodyLength is at least maxLength
307 // c) bodyLength is at least Content-Length
308 while (!(
309 $remoteFile->eof() ||
310 (isset($this->options['maxLength']) && $bodyLength >= $this->options['maxLength']) ||
311 (isset($this->replyHeaders['content-length']) && $bodyLength >= end($this->replyHeaders['content-length']))
312 )) {
313
8fe14bd6 314 if ($chunkLength) {
5262964b 315 if (isset($this->options['maxLength'])) $chunkLength = min($chunkLength, $this->options['maxLength'] - $bodyLength);
8fe14bd6
TD
316 $line = $remoteFile->read($chunkLength);
317 }
8d2262a1 318 else if (!$inHeader && (!isset($this->replyHeaders['transfer-encoding']) || !$chunkedTransferRegex->match(end($this->replyHeaders['transfer-encoding'])))) {
4b3a6b71
TD
319 $length = 1024;
320 if (isset($this->options['maxLength'])) $length = min($length, $this->options['maxLength'] - $bodyLength);
7ced4c6a
TD
321 if (isset($this->replyHeaders['content-length'])) $length = min($length, end($this->replyHeaders['content-length']) - $bodyLength);
322
323 $line = $remoteFile->read($length);
324 }
8fe14bd6
TD
325 else {
326 $line = $remoteFile->gets();
327 }
328
86fc0430
TD
329 if ($inHeader) {
330 if (rtrim($line) === '') {
331 $inHeader = false;
8fe14bd6
TD
332 $this->parseReplyHeaders();
333
86fc0430
TD
334 continue;
335 }
336 $this->replyHeaders[] = $line;
337 }
338 else {
8fe14bd6 339 if (isset($this->replyHeaders['transfer-encoding']) && $chunkedTransferRegex->match(end($this->replyHeaders['transfer-encoding']))) {
8fe14bd6
TD
340 // last chunk finished
341 if ($chunkLength === 0) {
342 // read hex data and trash chunk-extension
343 list($hex) = explode(';', $line, 2);
344 $chunkLength = hexdec($hex);
345
346 // $chunkLength === 0 -> no more data
347 if ($chunkLength === 0) {
348 // clear remaining response
7ced4c6a 349 while (!$remoteFile->gets(1024));
8fe14bd6 350
8f3e6138
TD
351 // remove chunked from transfer-encoding
352 $this->replyHeaders['transfer-encoding'] = array_filter(array_map(function ($element) use ($chunkedTransferRegex) {
353 return $chunkedTransferRegex->replace($element, '');
354 }, $this->replyHeaders['transfer-encoding']), 'trim');
355 if (empty($this->replyHeaders['transfer-encoding'])) unset($this->replyHeaders['transfer-encoding']);
356
8fe14bd6
TD
357 // break out of main reading loop
358 break;
359 }
360 }
361 else {
362 $this->replyBody .= $line;
363 $chunkLength -= strlen($line);
8f3e6138
TD
364 $bodyLength += strlen($line);
365 if ($chunkLength === 0) $remoteFile->read(2); // CRLF
8fe14bd6
TD
366 }
367 }
368 else {
369 $this->replyBody .= $line;
8f3e6138 370 $bodyLength += strlen($line);
5262964b 371 }
86fc0430
TD
372 }
373 }
374
5262964b
TD
375 if (isset($this->options['maxLength'])) $this->replyBody = substr($this->replyBody, 0, $this->options['maxLength']);
376
8fe14bd6
TD
377 $remoteFile->close();
378
86fc0430
TD
379 $this->parseReply();
380 }
381
382 /**
8fe14bd6 383 * Parses the reply headers.
86fc0430 384 */
8fe14bd6 385 private function parseReplyHeaders() {
86fc0430 386 $headers = array();
8fe14bd6 387 $lastKey = '';
86fc0430
TD
388 foreach ($this->replyHeaders as $header) {
389 if (strpos($header, ':') === false) {
8fe14bd6 390 $headers[trim($header)] = array(trim($header));
86fc0430
TD
391 continue;
392 }
8fe14bd6
TD
393
394 // 4.2 Header fields can be
395 // extended over multiple lines by preceding each extra line with at
396 // least one SP or HT.
397 if (ltrim($header, "\t ") !== $header) {
398 $headers[$lastKey][] = array_pop($headers[$lastKey]).' '.trim($header);
399 }
400 else {
401 list($key, $value) = explode(':', $header, 2);
402
403 $lastKey = $key;
404 if (!isset($headers[$key])) $headers[$key] = array();
405 $headers[$key][] = trim($value);
406 }
86fc0430 407 }
8fe14bd6
TD
408 // 4.2 Field names are case-insensitive.
409 $this->replyHeaders = array_change_key_case($headers);
410 if (isset($this->replyHeaders['transfer-encoding'])) $this->replyHeaders['transfer-encoding'] = array(implode(',', $this->replyHeaders['transfer-encoding']));
411 $this->legacyHeaders = array_map('end', $headers);
86fc0430 412
a0eb8370 413 // get status code
86fc0430 414 $statusLine = reset($this->replyHeaders);
8fe14bd6
TD
415 $regex = new Regex('^HTTP/1.\d+ +(\d{3})');
416 if (!$regex->match($statusLine[0])) throw new SystemException("Unexpected status '".$statusLine."'");
86fc0430 417 $matches = $regex->getMatches();
aeaa135c 418 $this->statusCode = $matches[1];
8fe14bd6
TD
419 }
420
421 /**
422 * Parses the reply.
423 */
424 private function parseReply() {
425 // 4.4 Messages MUST NOT include both a Content-Length header field and a
426 // non-identity transfer-coding. If the message does include a non-
427 // identity transfer-coding, the Content-Length MUST be ignored.
5262964b 428 if (isset($this->replyHeaders['content-length']) && (!isset($this->replyHeaders['transfer-encoding']) || strtolower(end($this->replyHeaders['transfer-encoding'])) !== 'identity') && !isset($this->options['maxLength'])) {
8009cc11 429 if (strlen($this->replyBody) != end($this->replyHeaders['content-length'])) {
a0eb8370
DR
430 throw new SystemException('Body length does not match length given in header');
431 }
432 }
433
434 // validate status code
aeaa135c 435 switch ($this->statusCode) {
86fc0430
TD
436 case '301':
437 case '302':
438 case '303':
439 case '307':
440 // redirect
c7fe2510 441 if ($this->options['maxDepth'] <= 0) throw new SystemException("Received status code '".$this->statusCode."' from server, but recursion level is exhausted");
86fc0430
TD
442
443 $newRequest = clone $this;
444 $newRequest->options['maxDepth']--;
c7fe2510 445
8fe14bd6 446 // 10.3.4 The response to the request can be found under a different URI and SHOULD
c7fe2510 447 // be retrieved using a GET method on that resource.
c7fe2510 448 if ($this->statusCode == '303') {
86fc0430
TD
449 $newRequest->options['method'] = 'GET';
450 $newRequest->postParameters = array();
8fe14bd6
TD
451 $newRequest->addHeader('content-length', '');
452 $newRequest->addHeader('content-type', '');
86fc0430 453 }
c7fe2510 454
86fc0430 455 try {
8fe14bd6 456 $newRequest->setURL(end($this->replyHeaders['location']));
86fc0430
TD
457 }
458 catch (SystemException $e) {
8fe14bd6 459 throw new SystemException("Received 'Location: ".end($this->replyHeaders['location'])."' from server, which is invalid.", 0, $e);
86fc0430 460 }
86fc0430 461
283df336
TD
462 try {
463 $newRequest->execute();
464
465 // update data with data from the inner request
466 $this->url = $newRequest->url;
467 $this->statusCode = $newRequest->statusCode;
468 $this->replyHeaders = $newRequest->replyHeaders;
df37e22d 469 $this->legacyHeaders = $newRequest->legacyHeaders;
283df336
TD
470 $this->replyBody = $newRequest->replyBody;
471 }
472 catch (SystemException $e) {
473 // update data with data from the inner request
474 $this->url = $newRequest->url;
475 $this->statusCode = $newRequest->statusCode;
476 $this->replyHeaders = $newRequest->replyHeaders;
df37e22d 477 $this->legacyHeaders = $newRequest->legacyHeaders;
283df336
TD
478 $this->replyBody = $newRequest->replyBody;
479
480 throw $e;
481 }
482
86fc0430
TD
483 return;
484 break;
a17de04e 485
4d28d5a2
SG
486 case '206':
487 // check, if partial content was expected
5262964b 488 if (!isset($this->headers['range'])) {
4d28d5a2
SG
489 throw new HTTPServerErrorException("Received unexpected status code '206' from server");
490 }
5262964b 491 else if (!isset($this->replyHeaders['content-range'])) {
4d28d5a2
SG
492 throw new HTTPServerErrorException("Content-Range is missing in reply header");
493 }
494 break;
495
3536d2fe 496 case '401':
5cd59413 497 case '402':
3536d2fe
AE
498 case '403':
499 throw new HTTPUnauthorizedException("Received status code '".$this->statusCode."' from server");
500 break;
501
502 case '404':
503 throw new HTTPNotFoundException("Received status code '404' from server");
504 break;
8fe14bd6 505
86fc0430 506 default:
8fe14bd6
TD
507 // 6.1.1 However, applications MUST
508 // understand the class of any status code, as indicated by the first
509 // digit, and treat any unrecognized response as being equivalent to the
510 // x00 status code of that class, with the exception that an
511 // unrecognized response MUST NOT be cached.
512 switch (substr($this->statusCode, 0, 1)) {
513 case '2': // 200 and unknown 2XX
514 case '3': // 300 and unknown 3XX
515 // we are fine
516 break;
517 case '5': // 500 and unknown 5XX
518 throw new HTTPServerErrorException("Received status code '".$this->statusCode."' from server");
519 break;
520 default:
521 throw new SystemException("Received unhandled status code '".$this->statusCode."' from server");
522 break;
523 }
86fc0430
TD
524 break;
525 }
86fc0430
TD
526 }
527
528 /**
529 * Returns an array with the replied data.
8fe14bd6 530 * Note that the 'headers' element is deprecated and may be removed in the future.
86fc0430 531 *
a17de04e 532 * @return array
86fc0430
TD
533 */
534 public function getReply() {
a195ffa6
TD
535 return array(
536 'statusCode' => $this->statusCode,
8fe14bd6
TD
537 'headers' => $this->legacyHeaders,
538 'httpHeaders' => $this->replyHeaders,
25cdc083
AE
539 'body' => $this->replyBody,
540 'url' => $this->url
a195ffa6 541 );
86fc0430
TD
542 }
543
544 /**
545 * Sets options and applies default values when an option is omitted.
546 *
a17de04e 547 * @param array $options
86fc0430
TD
548 */
549 private function setOptions(array $options) {
550 if (!isset($options['timeout'])) {
c7fe2510 551 $options['timeout'] = 10;
86fc0430
TD
552 }
553
554 if (!isset($options['method'])) {
5f70a0de 555 $options['method'] = (!empty($this->postParameters) || !empty($this->files) ? 'POST' : 'GET');
86fc0430
TD
556 }
557
558 if (!isset($options['maxDepth'])) {
559 $options['maxDepth'] = 2;
560 }
561
562 if (isset($options['auth'])) {
563 if (!isset($options['auth']['username'])) {
c7fe2510 564 throw new SystemException('Username is missing in authentification data.');
86fc0430
TD
565 }
566 if (!isset($options['auth']['password'])) {
c7fe2510 567 throw new SystemException('Password is missing in authentification data.');
86fc0430
TD
568 }
569 }
570
571 $this->options = $options;
572 }
573
574 /**
575 * Adds a header to this request.
c7fe2510 576 * When an empty value is given existing headers of this name will be removed. When append
86fc0430
TD
577 * is set to false existing values will be overwritten.
578 *
a17de04e
MS
579 * @param string $name
580 * @param string $value
581 * @param boolean $append
86fc0430
TD
582 */
583 public function addHeader($name, $value, $append = false) {
8fe14bd6
TD
584 // 4.2 Field names are case-insensitive.
585 $name = strtolower($name);
586
86fc0430
TD
587 if ($value === '') {
588 unset($this->headers[$name]);
589 return;
590 }
591
592 if ($append && isset($this->headers[$name])) {
593 $this->headers[$name][] = $value;
594 }
a377993e
TD
595 else {
596 $this->headers[$name] = array($value);
597 }
86fc0430
TD
598 }
599
600 /**
601 * Resets reply data when cloning.
602 */
603 private function __clone() {
604 $this->replyHeaders = array();
605 $this->replyBody = '';
606 $this->statusCode = 0;
607 }
58e1d71f 608}