Merge "Align search result CSS with Wikimedia UI color palette"
[lhc/web/wiklou.git] / includes / http / MWHttpRequest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\Logger\LoggerFactory;
22 use Psr\Log\LoggerInterface;
23 use Psr\Log\LoggerAwareInterface;
24 use Psr\Log\NullLogger;
25
26 /**
27 * This wrapper class will call out to curl (if available) or fallback
28 * to regular PHP if necessary for handling internal HTTP requests.
29 *
30 * Renamed from HttpRequest to MWHttpRequest to avoid conflict with
31 * PHP's HTTP extension.
32 */
33 class MWHttpRequest implements LoggerAwareInterface {
34 const SUPPORTS_FILE_POSTS = false;
35
36 protected $content;
37 protected $timeout = 'default';
38 protected $headersOnly = null;
39 protected $postData = null;
40 protected $proxy = null;
41 protected $noProxy = false;
42 protected $sslVerifyHost = true;
43 protected $sslVerifyCert = true;
44 protected $caInfo = null;
45 protected $method = "GET";
46 protected $reqHeaders = [];
47 protected $url;
48 protected $parsedUrl;
49 /** @var callable */
50 protected $callback;
51 protected $maxRedirects = 5;
52 protected $followRedirects = false;
53 protected $connectTimeout;
54
55 /**
56 * @var CookieJar
57 */
58 protected $cookieJar;
59
60 protected $headerList = [];
61 protected $respVersion = "0.9";
62 protected $respStatus = "200 Ok";
63 protected $respHeaders = [];
64
65 /** @var StatusValue */
66 protected $status;
67
68 /**
69 * @var Profiler
70 */
71 protected $profiler;
72
73 /**
74 * @var string
75 */
76 protected $profileName;
77
78 /**
79 * @var LoggerInterface;
80 */
81 protected $logger;
82
83 /**
84 * @param string $url Url to use. If protocol-relative, will be expanded to an http:// URL
85 * @param array $options (optional) extra params to pass (see Http::request())
86 * @param string $caller The method making this request, for profiling
87 * @param Profiler $profiler An instance of the profiler for profiling, or null
88 */
89 protected function __construct(
90 $url, $options = [], $caller = __METHOD__, $profiler = null
91 ) {
92 global $wgHTTPTimeout, $wgHTTPConnectTimeout;
93
94 $this->url = wfExpandUrl( $url, PROTO_HTTP );
95 $this->parsedUrl = wfParseUrl( $this->url );
96
97 if ( isset( $options['logger'] ) ) {
98 $this->logger = $options['logger'];
99 } else {
100 $this->logger = new NullLogger();
101 }
102
103 if ( !$this->parsedUrl || !Http::isValidURI( $this->url ) ) {
104 $this->status = StatusValue::newFatal( 'http-invalid-url', $url );
105 } else {
106 $this->status = StatusValue::newGood( 100 ); // continue
107 }
108
109 if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
110 $this->timeout = $options['timeout'];
111 } else {
112 $this->timeout = $wgHTTPTimeout;
113 }
114 if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) {
115 $this->connectTimeout = $options['connectTimeout'];
116 } else {
117 $this->connectTimeout = $wgHTTPConnectTimeout;
118 }
119 if ( isset( $options['userAgent'] ) ) {
120 $this->setUserAgent( $options['userAgent'] );
121 }
122
123 $members = [ "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
124 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" ];
125
126 foreach ( $members as $o ) {
127 if ( isset( $options[$o] ) ) {
128 // ensure that MWHttpRequest::method is always
129 // uppercased. Bug 36137
130 if ( $o == 'method' ) {
131 $options[$o] = strtoupper( $options[$o] );
132 }
133 $this->$o = $options[$o];
134 }
135 }
136
137 if ( $this->noProxy ) {
138 $this->proxy = ''; // noProxy takes precedence
139 }
140
141 // Profile based on what's calling us
142 $this->profiler = $profiler;
143 $this->profileName = $caller;
144 }
145
146 /**
147 * @param LoggerInterface $logger
148 */
149 public function setLogger( LoggerInterface $logger ) {
150 $this->logger = $logger;
151 }
152
153 /**
154 * Simple function to test if we can make any sort of requests at all, using
155 * cURL or fopen()
156 * @return bool
157 */
158 public static function canMakeRequests() {
159 return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
160 }
161
162 /**
163 * Generate a new request object
164 * @param string $url Url to use
165 * @param array $options (optional) extra params to pass (see Http::request())
166 * @param string $caller The method making this request, for profiling
167 * @throws DomainException
168 * @return CurlHttpRequest|PhpHttpRequest
169 * @see MWHttpRequest::__construct
170 */
171 public static function factory( $url, $options = null, $caller = __METHOD__ ) {
172 if ( !Http::$httpEngine ) {
173 Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
174 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
175 throw new DomainException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
176 ' Http::$httpEngine is set to "curl"' );
177 }
178
179 if ( !is_array( $options ) ) {
180 $options = [];
181 }
182
183 if ( !isset( $options['logger'] ) ) {
184 $options['logger'] = LoggerFactory::getInstance( 'http' );
185 }
186
187 switch ( Http::$httpEngine ) {
188 case 'curl':
189 return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
190 case 'php':
191 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
192 throw new DomainException( __METHOD__ . ': allow_url_fopen ' .
193 'needs to be enabled for pure PHP http requests to ' .
194 'work. If possible, curl should be used instead. See ' .
195 'http://php.net/curl.'
196 );
197 }
198 return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() );
199 default:
200 throw new DomainException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
201 }
202 }
203
204 /**
205 * Get the body, or content, of the response to the request
206 *
207 * @return string
208 */
209 public function getContent() {
210 return $this->content;
211 }
212
213 /**
214 * Set the parameters of the request
215 *
216 * @param array $args
217 * @todo overload the args param
218 */
219 public function setData( $args ) {
220 $this->postData = $args;
221 }
222
223 /**
224 * Take care of setting up the proxy (do nothing if "noProxy" is set)
225 *
226 * @return void
227 */
228 protected function proxySetup() {
229 // If there is an explicit proxy set and proxies are not disabled, then use it
230 if ( $this->proxy && !$this->noProxy ) {
231 return;
232 }
233
234 // Otherwise, fallback to $wgHTTPProxy if this is not a machine
235 // local URL and proxies are not disabled
236 if ( self::isLocalURL( $this->url ) || $this->noProxy ) {
237 $this->proxy = '';
238 } else {
239 $this->proxy = Http::getProxy();
240 }
241 }
242
243 /**
244 * Check if the URL can be served by localhost
245 *
246 * @param string $url Full url to check
247 * @return bool
248 */
249 private static function isLocalURL( $url ) {
250 global $wgCommandLineMode, $wgLocalVirtualHosts;
251
252 if ( $wgCommandLineMode ) {
253 return false;
254 }
255
256 // Extract host part
257 $matches = [];
258 if ( preg_match( '!^https?://([\w.-]+)[/:].*$!', $url, $matches ) ) {
259 $host = $matches[1];
260 // Split up dotwise
261 $domainParts = explode( '.', $host );
262 // Check if this domain or any superdomain is listed as a local virtual host
263 $domainParts = array_reverse( $domainParts );
264
265 $domain = '';
266 $countParts = count( $domainParts );
267 for ( $i = 0; $i < $countParts; $i++ ) {
268 $domainPart = $domainParts[$i];
269 if ( $i == 0 ) {
270 $domain = $domainPart;
271 } else {
272 $domain = $domainPart . '.' . $domain;
273 }
274
275 if ( in_array( $domain, $wgLocalVirtualHosts ) ) {
276 return true;
277 }
278 }
279 }
280
281 return false;
282 }
283
284 /**
285 * Set the user agent
286 * @param string $UA
287 */
288 public function setUserAgent( $UA ) {
289 $this->setHeader( 'User-Agent', $UA );
290 }
291
292 /**
293 * Set an arbitrary header
294 * @param string $name
295 * @param string $value
296 */
297 public function setHeader( $name, $value ) {
298 // I feel like I should normalize the case here...
299 $this->reqHeaders[$name] = $value;
300 }
301
302 /**
303 * Get an array of the headers
304 * @return array
305 */
306 protected function getHeaderList() {
307 $list = [];
308
309 if ( $this->cookieJar ) {
310 $this->reqHeaders['Cookie'] =
311 $this->cookieJar->serializeToHttpRequest(
312 $this->parsedUrl['path'],
313 $this->parsedUrl['host']
314 );
315 }
316
317 foreach ( $this->reqHeaders as $name => $value ) {
318 $list[] = "$name: $value";
319 }
320
321 return $list;
322 }
323
324 /**
325 * Set a read callback to accept data read from the HTTP request.
326 * By default, data is appended to an internal buffer which can be
327 * retrieved through $req->getContent().
328 *
329 * To handle data as it comes in -- especially for large files that
330 * would not fit in memory -- you can instead set your own callback,
331 * in the form function($resource, $buffer) where the first parameter
332 * is the low-level resource being read (implementation specific),
333 * and the second parameter is the data buffer.
334 *
335 * You MUST return the number of bytes handled in the buffer; if fewer
336 * bytes are reported handled than were passed to you, the HTTP fetch
337 * will be aborted.
338 *
339 * @param callable|null $callback
340 * @throws InvalidArgumentException
341 */
342 public function setCallback( $callback ) {
343 if ( is_null( $callback ) ) {
344 $callback = [ $this, 'read' ];
345 } elseif ( !is_callable( $callback ) ) {
346 throw new InvalidArgumentException( __METHOD__ . ': invalid callback' );
347 }
348 $this->callback = $callback;
349 }
350
351 /**
352 * A generic callback to read the body of the response from a remote
353 * server.
354 *
355 * @param resource $fh
356 * @param string $content
357 * @return int
358 * @internal
359 */
360 public function read( $fh, $content ) {
361 $this->content .= $content;
362 return strlen( $content );
363 }
364
365 /**
366 * Take care of whatever is necessary to perform the URI request.
367 *
368 * @return StatusValue
369 * @note currently returns Status for B/C
370 */
371 public function execute() {
372 throw new LogicException( 'children must override this' );
373 }
374
375 protected function prepare() {
376 $this->content = "";
377
378 if ( strtoupper( $this->method ) == "HEAD" ) {
379 $this->headersOnly = true;
380 }
381
382 $this->proxySetup(); // set up any proxy as needed
383
384 if ( !$this->callback ) {
385 $this->setCallback( null );
386 }
387
388 if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
389 $this->setUserAgent( Http::userAgent() );
390 }
391 }
392
393 /**
394 * Parses the headers, including the HTTP status code and any
395 * Set-Cookie headers. This function expects the headers to be
396 * found in an array in the member variable headerList.
397 */
398 protected function parseHeader() {
399 $lastname = "";
400
401 foreach ( $this->headerList as $header ) {
402 if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
403 $this->respVersion = $match[1];
404 $this->respStatus = $match[2];
405 } elseif ( preg_match( "#^[ \t]#", $header ) ) {
406 $last = count( $this->respHeaders[$lastname] ) - 1;
407 $this->respHeaders[$lastname][$last] .= "\r\n$header";
408 } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
409 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
410 $lastname = strtolower( $match[1] );
411 }
412 }
413
414 $this->parseCookies();
415 }
416
417 /**
418 * Sets HTTPRequest status member to a fatal value with the error
419 * message if the returned integer value of the status code was
420 * not successful (< 300) or a redirect (>=300 and < 400). (see
421 * RFC2616, section 10,
422 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a
423 * list of status codes.)
424 */
425 protected function setStatus() {
426 if ( !$this->respHeaders ) {
427 $this->parseHeader();
428 }
429
430 if ( (int)$this->respStatus > 399 ) {
431 list( $code, $message ) = explode( " ", $this->respStatus, 2 );
432 $this->status->fatal( "http-bad-status", $code, $message );
433 }
434 }
435
436 /**
437 * Get the integer value of the HTTP status code (e.g. 200 for "200 Ok")
438 * (see RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
439 * for a list of status codes.)
440 *
441 * @return int
442 */
443 public function getStatus() {
444 if ( !$this->respHeaders ) {
445 $this->parseHeader();
446 }
447
448 return (int)$this->respStatus;
449 }
450
451 /**
452 * Returns true if the last status code was a redirect.
453 *
454 * @return bool
455 */
456 public function isRedirect() {
457 if ( !$this->respHeaders ) {
458 $this->parseHeader();
459 }
460
461 $status = (int)$this->respStatus;
462
463 if ( $status >= 300 && $status <= 303 ) {
464 return true;
465 }
466
467 return false;
468 }
469
470 /**
471 * Returns an associative array of response headers after the
472 * request has been executed. Because some headers
473 * (e.g. Set-Cookie) can appear more than once the, each value of
474 * the associative array is an array of the values given.
475 *
476 * @return array
477 */
478 public function getResponseHeaders() {
479 if ( !$this->respHeaders ) {
480 $this->parseHeader();
481 }
482
483 return $this->respHeaders;
484 }
485
486 /**
487 * Returns the value of the given response header.
488 *
489 * @param string $header
490 * @return string|null
491 */
492 public function getResponseHeader( $header ) {
493 if ( !$this->respHeaders ) {
494 $this->parseHeader();
495 }
496
497 if ( isset( $this->respHeaders[strtolower( $header )] ) ) {
498 $v = $this->respHeaders[strtolower( $header )];
499 return $v[count( $v ) - 1];
500 }
501
502 return null;
503 }
504
505 /**
506 * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
507 *
508 * To read response cookies from the jar, getCookieJar must be called first.
509 *
510 * @param CookieJar $jar
511 */
512 public function setCookieJar( $jar ) {
513 $this->cookieJar = $jar;
514 }
515
516 /**
517 * Returns the cookie jar in use.
518 *
519 * @return CookieJar
520 */
521 public function getCookieJar() {
522 if ( !$this->respHeaders ) {
523 $this->parseHeader();
524 }
525
526 return $this->cookieJar;
527 }
528
529 /**
530 * Sets a cookie. Used before a request to set up any individual
531 * cookies. Used internally after a request to parse the
532 * Set-Cookie headers.
533 * @see Cookie::set
534 * @param string $name
535 * @param string $value
536 * @param array $attr
537 */
538 public function setCookie( $name, $value, $attr = [] ) {
539 if ( !$this->cookieJar ) {
540 $this->cookieJar = new CookieJar;
541 }
542
543 if ( $this->parsedUrl && !isset( $attr['domain'] ) ) {
544 $attr['domain'] = $this->parsedUrl['host'];
545 }
546
547 $this->cookieJar->setCookie( $name, $value, $attr );
548 }
549
550 /**
551 * Parse the cookies in the response headers and store them in the cookie jar.
552 */
553 protected function parseCookies() {
554 if ( !$this->cookieJar ) {
555 $this->cookieJar = new CookieJar;
556 }
557
558 if ( isset( $this->respHeaders['set-cookie'] ) ) {
559 $url = parse_url( $this->getFinalUrl() );
560 foreach ( $this->respHeaders['set-cookie'] as $cookie ) {
561 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
562 }
563 }
564 }
565
566 /**
567 * Returns the final URL after all redirections.
568 *
569 * Relative values of the "Location" header are incorrect as
570 * stated in RFC, however they do happen and modern browsers
571 * support them. This function loops backwards through all
572 * locations in order to build the proper absolute URI - Marooned
573 * at wikia-inc.com
574 *
575 * Note that the multiple Location: headers are an artifact of
576 * CURL -- they shouldn't actually get returned this way. Rewrite
577 * this when bug 29232 is taken care of (high-level redirect
578 * handling rewrite).
579 *
580 * @return string
581 */
582 public function getFinalUrl() {
583 $headers = $this->getResponseHeaders();
584
585 // return full url (fix for incorrect but handled relative location)
586 if ( isset( $headers['location'] ) ) {
587 $locations = $headers['location'];
588 $domain = '';
589 $foundRelativeURI = false;
590 $countLocations = count( $locations );
591
592 for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
593 $url = parse_url( $locations[$i] );
594
595 if ( isset( $url['host'] ) ) {
596 $domain = $url['scheme'] . '://' . $url['host'];
597 break; // found correct URI (with host)
598 } else {
599 $foundRelativeURI = true;
600 }
601 }
602
603 if ( $foundRelativeURI ) {
604 if ( $domain ) {
605 return $domain . $locations[$countLocations - 1];
606 } else {
607 $url = parse_url( $this->url );
608 if ( isset( $url['host'] ) ) {
609 return $url['scheme'] . '://' . $url['host'] .
610 $locations[$countLocations - 1];
611 }
612 }
613 } else {
614 return $locations[$countLocations - 1];
615 }
616 }
617
618 return $this->url;
619 }
620
621 /**
622 * Returns true if the backend can follow redirects. Overridden by the
623 * child classes.
624 * @return bool
625 */
626 public function canFollowRedirects() {
627 return true;
628 }
629 }