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