Fix Http::request() so that it continues to return false on non-200 requests.
[lhc/web/wiklou.git] / includes / HttpFunctions.php
1 <?php
2 /**
3 * @defgroup HTTP HTTP
4 */
5
6 /**
7 * Various HTTP related functions
8 * @ingroup HTTP
9 */
10 class Http {
11 static $httpEngine = false;
12
13 /**
14 * Perform an HTTP request
15 * @param $method string HTTP method. Usually GET/POST
16 * @param $url string Full URL to act on
17 * @param $options options to pass to HttpRequest object
18 * Possible keys for the array:
19 * timeout Timeout length in seconds
20 * postData An array of key-value pairs or a url-encoded form data
21 * proxy The proxy to use. Will use $wgHTTPProxy (if set) otherwise.
22 * noProxy Override $wgHTTPProxy (if set) and don't use any proxy at all.
23 * sslVerifyHost (curl only) Verify the SSL certificate
24 * caInfo (curl only) Provide CA information
25 * maxRedirects Maximum number of redirects to follow (defaults to 5)
26 * followRedirects Whether to follow redirects (defaults to true)
27 * @returns mixed (bool)false on failure or a string on success
28 */
29 public static function request( $method, $url, $options = array() ) {
30 wfDebug( "HTTP: $method: $url" );
31 $options['method'] = strtoupper( $method );
32 if ( !isset( $options['timeout'] ) ) {
33 $options['timeout'] = 'default';
34 }
35 $req = HttpRequest::factory( $url, $options );
36 $status = $req->execute();
37 if ( $status->isOK() ) {
38 return $req->getContent();
39 } else {
40 return false;
41 }
42 }
43
44 /**
45 * Simple wrapper for Http::request( 'GET' )
46 * @see Http::request()
47 */
48 public static function get( $url, $timeout = 'default', $options = array() ) {
49 $options['timeout'] = $timeout;
50 return Http::request( 'GET', $url, $options );
51 }
52
53 /**
54 * Simple wrapper for Http::request( 'POST' )
55 * @see Http::request()
56 */
57 public static function post( $url, $options = array() ) {
58 return Http::request( 'POST', $url, $options );
59 }
60
61 /**
62 * Check if the URL can be served by localhost
63 * @param $url string Full url to check
64 * @return bool
65 */
66 public static function isLocalURL( $url ) {
67 global $wgCommandLineMode, $wgConf;
68 if ( $wgCommandLineMode ) {
69 return false;
70 }
71
72 // Extract host part
73 $matches = array();
74 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
75 $host = $matches[1];
76 // Split up dotwise
77 $domainParts = explode( '.', $host );
78 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
79 $domainParts = array_reverse( $domainParts );
80 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
81 $domainPart = $domainParts[$i];
82 if ( $i == 0 ) {
83 $domain = $domainPart;
84 } else {
85 $domain = $domainPart . '.' . $domain;
86 }
87 if ( $wgConf->isLocalVHost( $domain ) ) {
88 return true;
89 }
90 }
91 }
92 return false;
93 }
94
95 /**
96 * A standard user-agent we can use for external requests.
97 * @returns string
98 */
99 public static function userAgent() {
100 global $wgVersion;
101 return "MediaWiki/$wgVersion";
102 }
103
104 /**
105 * Checks that the given URI is a valid one
106 * @param $uri Mixed: URI to check for validity
107 * @returns bool
108 */
109 public static function isValidURI( $uri ) {
110 return preg_match(
111 '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
112 $uri,
113 $matches
114 );
115 }
116 }
117
118 /**
119 * This wrapper class will call out to curl (if available) or fallback
120 * to regular PHP if necessary for handling internal HTTP requests.
121 */
122 class HttpRequest {
123 protected $content;
124 protected $timeout = 'default';
125 protected $headersOnly = null;
126 protected $postData = null;
127 protected $proxy = null;
128 protected $noProxy = false;
129 protected $sslVerifyHost = true;
130 protected $caInfo = null;
131 protected $method = "GET";
132 protected $reqHeaders = array();
133 protected $url;
134 protected $parsedUrl;
135 protected $callback;
136 protected $maxRedirects = 5;
137 protected $followRedirects = true;
138
139 protected $cookieJar;
140
141 protected $headerList = array();
142 protected $respVersion = "0.9";
143 protected $respStatus = "200 Ok";
144 protected $respHeaders = array();
145
146 public $status;
147
148 /**
149 * @param $url string url to use
150 * @param $options array (optional) extra params to pass (see Http::request())
151 */
152 function __construct( $url, $options = array() ) {
153 global $wgHTTPTimeout;
154
155 $this->url = $url;
156 $this->parsedUrl = parse_url( $url );
157
158 if ( !Http::isValidURI( $this->url ) ) {
159 $this->status = Status::newFromFatal('http-invalid-url');
160 } else {
161 $this->status = Status::newGood( 100 ); // continue
162 }
163
164 if ( isset($options['timeout']) && $options['timeout'] != 'default' ) {
165 $this->timeout = $options['timeout'];
166 } else {
167 $this->timeout = $wgHTTPTimeout;
168 }
169
170 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
171 "method", "followRedirects", "maxRedirects" );
172 foreach ( $members as $o ) {
173 if ( isset($options[$o]) ) {
174 $this->$o = $options[$o];
175 }
176 }
177 }
178
179 /**
180 * Generate a new request object
181 * @see HttpRequest::__construct
182 */
183 public static function factory( $url, $options = null ) {
184 if ( !Http::$httpEngine ) {
185 Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
186 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
187 throw new MWException( __METHOD__.': curl (http://php.net/curl) is not installed, but'.
188 ' Http::$httpEngine is set to "curl"' );
189 }
190
191 switch( Http::$httpEngine ) {
192 case 'curl':
193 return new CurlHttpRequest( $url, $options );
194 case 'php':
195 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
196 throw new MWException( __METHOD__.': allow_url_fopen needs to be enabled for pure PHP'.
197 ' http requests to work. If possible, curl should be used instead. See http://php.net/curl.' );
198 }
199 return new PhpHttpRequest( $url, $options );
200 default:
201 throw new MWException( __METHOD__.': The setting of Http::$httpEngine is not valid.' );
202 }
203 }
204
205 /**
206 * Get the body, or content, of the response to the request
207 * @return string
208 */
209 public function getContent() {
210 return $this->content;
211 }
212
213 /**
214 * Take care of setting up the proxy
215 * (override in subclass)
216 * @return string
217 */
218 public function proxySetup() {
219 global $wgHTTPProxy;
220
221 if ( $this->proxy ) {
222 return;
223 }
224 if ( Http::isLocalURL( $this->url ) ) {
225 $this->proxy = 'http://localhost:80/';
226 } elseif ( $wgHTTPProxy ) {
227 $this->proxy = $wgHTTPProxy ;
228 }
229 }
230
231 /**
232 * Set the refererer header
233 */
234 public function setReferer( $url ) {
235 $this->setHeader('Referer', $url);
236 }
237
238 /**
239 * Set the user agent
240 */
241 public function setUserAgent( $UA ) {
242 $this->setHeader('User-Agent', $UA);
243 }
244
245 /**
246 * Set an arbitrary header
247 */
248 public function setHeader($name, $value) {
249 // I feel like I should normalize the case here...
250 $this->reqHeaders[$name] = $value;
251 }
252
253 /**
254 * Get an array of the headers
255 */
256 public function getHeaderList() {
257 $list = array();
258
259 if( $this->cookieJar ) {
260 $this->reqHeaders['Cookie'] =
261 $this->cookieJar->serializeToHttpRequest($this->parsedUrl['path'],
262 $this->parsedUrl['host']);
263 }
264 foreach($this->reqHeaders as $name => $value) {
265 $list[] = "$name: $value";
266 }
267 return $list;
268 }
269
270 /**
271 * Set the callback
272 * @param $callback callback
273 */
274 public function setCallback( $callback ) {
275 $this->callback = $callback;
276 }
277
278 /**
279 * A generic callback to read the body of the response from a remote
280 * server.
281 * @param $fh handle
282 * @param $content string
283 */
284 public function read( $fh, $content ) {
285 $this->content .= $content;
286 return strlen( $content );
287 }
288
289 /**
290 * Take care of whatever is necessary to perform the URI request.
291 * @return Status
292 */
293 public function execute() {
294 global $wgTitle;
295
296 if( strtoupper($this->method) == "HEAD" ) {
297 $this->headersOnly = true;
298 }
299
300 if ( is_array( $this->postData ) ) {
301 $this->postData = wfArrayToCGI( $this->postData );
302 }
303
304 if ( is_object( $wgTitle ) && !isset($this->reqHeaders['Referer']) ) {
305 $this->setReferer( $wgTitle->getFullURL() );
306 }
307
308 if ( !$this->noProxy ) {
309 $this->proxySetup();
310 }
311
312 if ( !$this->callback ) {
313 $this->setCallback( array( $this, 'read' ) );
314 }
315
316 if ( !isset($this->reqHeaders['User-Agent']) ) {
317 $this->setUserAgent(Http::userAgent());
318 }
319 }
320
321 protected function parseHeader() {
322 $lastname = "";
323 foreach( $this->headerList as $header ) {
324 if( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
325 $this->respVersion = $match[1];
326 $this->respStatus = $match[2];
327 } elseif( preg_match( "#^[ \t]#", $header ) ) {
328 $last = count($this->respHeaders[$lastname]) - 1;
329 $this->respHeaders[$lastname][$last] .= "\r\n$header";
330 } elseif( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
331 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
332 $lastname = strtolower( $match[1] );
333 }
334 }
335
336 if((int)$this->respStatus !== 200) {
337 $this->status->fatal('Not Ok');
338 }
339 $this->parseCookies();
340 }
341
342 /**
343 * Returns an associative array of response headers after the
344 * request has been executed. Because some headers
345 * (e.g. Set-Cookie) can appear more than once the, each value of
346 * the associative array is an array of the values given.
347 * @return array
348 */
349 public function getResponseHeaders() {
350 if( !$this->respHeaders ) {
351 $this->parseHeader();
352 }
353 return $this->respHeaders;
354 }
355
356 /**
357 * Tells the HttpRequest object to use this pre-loaded CookieJar.
358 * @param $jar CookieJar
359 */
360 public function setCookieJar( $jar ) {
361 $this->cookieJar = $jar;
362 }
363
364 /**
365 * Returns the cookie jar in use.
366 * @returns CookieJar
367 */
368 public function getCookieJar() {
369 if( !$this->respHeaders ) {
370 $this->parseHeader();
371 }
372 return $this->cookieJar;
373 }
374
375 /**
376 * Sets a cookie. Used before a request to set up any individual
377 * cookies. Used internally after a request to parse the
378 * Set-Cookie headers.
379 * @see Cookie::set
380 */
381 public function setCookie( $name, $value = null, $attr = null) {
382 if( !$this->cookieJar ) {
383 $this->cookieJar = new CookieJar;
384 }
385 $this->cookieJar->setCookie($name, $value, $attr);
386 }
387
388 /**
389 * Parse the cookies in the response headers and store them in the cookie jar.
390 */
391 protected function parseCookies() {
392 if( !$this->cookieJar ) {
393 $this->cookieJar = new CookieJar;
394 }
395 if( isset( $this->respHeaders['set-cookie'] ) ) {
396 $url = parse_url( $this->getFinalUrl() );
397 foreach( $this->respHeaders['set-cookie'] as $cookie ) {
398 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
399 }
400 }
401 }
402
403 /**
404 * Returns the final URL after all redirections.
405 * @returns string
406 */
407 public function getFinalUrl() {
408 $finalUrl = $this->url;
409 if ( isset( $this->respHeaders['location'] ) ) {
410 $redir = $this->respHeaders['location'];
411 $finalUrl = $redir[count($redir) - 1];
412 }
413
414 return $finalUrl;
415 }
416 }
417
418
419 class Cookie {
420 protected $name;
421 protected $value;
422 protected $expires;
423 protected $path;
424 protected $domain;
425 protected $isSessionKey = true;
426 // TO IMPLEMENT protected $secure
427 // TO IMPLEMENT? protected $maxAge (add onto expires)
428 // TO IMPLEMENT? protected $version
429 // TO IMPLEMENT? protected $comment
430
431 function __construct( $name, $value, $attr ) {
432 $this->name = $name;
433 $this->set( $value, $attr );
434 }
435
436 /**
437 * Sets a cookie. Used before a request to set up any individual
438 * cookies. Used internally after a request to parse the
439 * Set-Cookie headers.
440 * @param $name string the name of the cookie
441 * @param $value string the value of the cookie
442 * @param $attr array possible key/values:
443 * expires A date string
444 * path The path this cookie is used on
445 * domain Domain this cookie is used on
446 */
447 public function set( $value, $attr ) {
448 $this->value = $value;
449 if( isset( $attr['expires'] ) ) {
450 $this->isSessionKey = false;
451 $this->expires = strtotime( $attr['expires'] );
452 }
453 if( isset( $attr['path'] ) ) {
454 $this->path = $attr['path'];
455 } else {
456 $this->path = "/";
457 }
458 if( isset( $attr['domain'] ) ) {
459 if( self::validateCookieDomain( $attr['domain'] ) ) {
460 $this->domain = $attr['domain'];
461 }
462 } else {
463 throw new MWException("You must specify a domain.");
464 }
465 }
466
467 /**
468 * Return the true if the cookie is valid is valid. Otherwise,
469 * false. The uses a method similar to IE cookie security
470 * described here:
471 * http://kuza55.blogspot.com/2008/02/understanding-cookie-security.html
472 * A better method might be to use a blacklist like
473 * http://publicsuffix.org/
474 *
475 * @param $domain string the domain to validate
476 * @param $originDomain string (optional) the domain the cookie originates from
477 * @return bool
478 */
479 public static function validateCookieDomain( $domain, $originDomain = null) {
480 // Don't allow a trailing dot
481 if( substr( $domain, -1 ) == "." ) return false;
482
483 $dc = explode(".", $domain);
484
485 // Don't allow cookies for "localhost", "ls" or other dot-less hosts
486 if( count($dc) < 2 ) return false;
487
488 // Only allow full, valid IP addresses
489 if( preg_match( '/^[0-9.]+$/', $domain ) ) {
490 if( count( $dc ) != 4 ) return false;
491
492 if( ip2long( $domain ) === false ) return false;
493
494 if( $originDomain == null || $originDomain == $domain ) return true;
495
496 }
497
498 // Don't allow cookies for "co.uk" or "gov.uk", etc, but allow "supermarket.uk"
499 if( strrpos( $domain, "." ) - strlen( $domain ) == -3 ) {
500 if( (count($dc) == 2 && strlen( $dc[0] ) <= 2 )
501 || (count($dc) == 3 && strlen( $dc[0] ) == "" && strlen( $dc[1] ) <= 2 ) ) {
502 return false;
503 }
504 if( (count($dc) == 2 || (count($dc) == 3 && $dc[0] == "") )
505 && preg_match( '/(com|net|org|gov|edu)\...$/', $domain) ) {
506 return false;
507 }
508 }
509
510 if( $originDomain != null ) {
511 if( substr( $domain, 0, 1 ) != "." && $domain != $originDomain ) {
512 return false;
513 }
514 if( substr( $domain, 0, 1 ) == "."
515 && substr_compare( $originDomain, $domain, -strlen( $domain ),
516 strlen( $domain ), TRUE ) != 0 ) {
517 return false;
518 }
519 }
520
521 return true;
522 }
523
524 /**
525 * Serialize the cookie jar into a format useful for HTTP Request headers.
526 * @param $path string the path that will be used. Required.
527 * @param $domain string the domain that will be used. Required.
528 * @return string
529 */
530 public function serializeToHttpRequest( $path, $domain ) {
531 $ret = "";
532
533 if( $this->canServeDomain( $domain )
534 && $this->canServePath( $path )
535 && $this->isUnExpired() ) {
536 $ret = $this->name ."=". $this->value;
537 }
538
539 return $ret;
540 }
541
542 protected function canServeDomain( $domain ) {
543 if( $domain == $this->domain
544 || ( substr( $this->domain, 0, 1) == "."
545 && substr_compare( $domain, $this->domain, -strlen( $this->domain ),
546 strlen( $this->domain ), TRUE ) == 0 ) ) {
547 return true;
548 }
549 return false;
550 }
551
552 protected function canServePath( $path ) {
553 if( $this->path && substr_compare( $this->path, $path, 0, strlen( $this->path ) ) == 0 ) {
554 return true;
555 }
556 return false;
557 }
558
559 protected function isUnExpired() {
560 if( $this->isSessionKey || $this->expires > time() ) {
561 return true;
562 }
563 return false;
564 }
565
566 }
567
568 class CookieJar {
569 private $cookie = array();
570
571 /**
572 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
573 * @see Cookie::set()
574 */
575 public function setCookie ($name, $value, $attr) {
576 /* cookies: case insensitive, so this should work.
577 * We'll still send the cookies back in the same case we got them, though.
578 */
579 $index = strtoupper($name);
580 if( isset( $this->cookie[$index] ) ) {
581 $this->cookie[$index]->set( $value, $attr );
582 } else {
583 $this->cookie[$index] = new Cookie( $name, $value, $attr );
584 }
585 }
586
587 /**
588 * @see Cookie::serializeToHttpRequest
589 */
590 public function serializeToHttpRequest( $path, $domain ) {
591 $cookies = array();
592
593 foreach( $this->cookie as $c ) {
594 $serialized = $c->serializeToHttpRequest( $path, $domain );
595 if ( $serialized ) $cookies[] = $serialized;
596 }
597
598 return implode("; ", $cookies);
599 }
600
601 /**
602 * Parse the content of an Set-Cookie HTTP Response header.
603 * @param $cookie string
604 */
605 public function parseCookieResponseHeader ( $cookie, $domain ) {
606 $len = strlen( "Set-Cookie:" );
607 if ( substr_compare( "Set-Cookie:", $cookie, 0, $len, TRUE ) === 0 ) {
608 $cookie = substr( $cookie, $len );
609 }
610
611 $bit = array_map( 'trim', explode( ";", $cookie ) );
612 if ( count($bit) >= 1 ) {
613 list($name, $value) = explode( "=", array_shift( $bit ), 2 );
614 $attr = array();
615 foreach( $bit as $piece ) {
616 $parts = explode( "=", $piece );
617 if( count( $parts ) > 1 ) {
618 $attr[strtolower( $parts[0] )] = $parts[1];
619 } else {
620 $attr[strtolower( $parts[0] )] = true;
621 }
622 }
623
624 if( !isset( $attr['domain'] ) ) {
625 $attr['domain'] = $domain;
626 } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) {
627 return null;
628 }
629
630 $this->setCookie( $name, $value, $attr );
631 }
632 }
633 }
634
635
636 /**
637 * HttpRequest implemented using internal curl compiled into PHP
638 */
639 class CurlHttpRequest extends HttpRequest {
640 static $curlMessageMap = array(
641 6 => 'http-host-unreachable',
642 28 => 'http-timed-out'
643 );
644
645 protected $curlOptions = array();
646 protected $headerText = "";
647
648 protected function readHeader( $fh, $content ) {
649 $this->headerText .= $content;
650 return strlen( $content );
651 }
652
653 public function execute() {
654 parent::execute();
655 if ( !$this->status->isOK() ) {
656 return $this->status;
657 }
658 $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
659 $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
660 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
661 $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
662 $this->curlOptions[CURLOPT_HEADERFUNCTION] = array($this, "readHeader");
663 $this->curlOptions[CURLOPT_FOLLOWLOCATION] = $this->followRedirects;
664 $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
665
666 /* not sure these two are actually necessary */
667 if(isset($this->reqHeaders['Referer'])) {
668 $this->curlOptions[CURLOPT_REFERER] = $this->reqHeaders['Referer'];
669 }
670 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
671
672 if ( $this->sslVerifyHost ) {
673 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost;
674 }
675
676 if ( $this->caInfo ) {
677 $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
678 }
679
680 if ( $this->headersOnly ) {
681 $this->curlOptions[CURLOPT_NOBODY] = true;
682 $this->curlOptions[CURLOPT_HEADER] = true;
683 } elseif ( $this->method == 'POST' ) {
684 $this->curlOptions[CURLOPT_POST] = true;
685 $this->curlOptions[CURLOPT_POSTFIELDS] = $this->postData;
686 // Suppress 'Expect: 100-continue' header, as some servers
687 // will reject it with a 417 and Curl won't auto retry
688 // with HTTP 1.0 fallback
689 $this->reqHeaders['Expect'] = '';
690 } else {
691 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
692 }
693
694 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
695
696 $curlHandle = curl_init( $this->url );
697 curl_setopt_array( $curlHandle, $this->curlOptions );
698
699 if ( false === curl_exec( $curlHandle ) ) {
700 $code = curl_error( $curlHandle );
701
702 if ( isset( self::$curlMessageMap[$code] ) ) {
703 $this->status->fatal( self::$curlMessageMap[$code] );
704 } else {
705 $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
706 }
707 } else {
708 $this->headerList = explode("\r\n", $this->headerText);
709 }
710
711 curl_close( $curlHandle );
712
713 $this->parseHeader();
714 return $this->status;
715 }
716 }
717
718 class PhpHttpRequest extends HttpRequest {
719 protected function urlToTcp( $url ) {
720 $parsedUrl = parse_url( $url );
721
722 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
723 }
724
725 public function execute() {
726 if ( $this->parsedUrl['scheme'] != 'http' ) {
727 $this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] );
728 }
729
730 parent::execute();
731 if ( !$this->status->isOK() ) {
732 return $this->status;
733 }
734
735 $this->reqHeaders['Accept'] = "*/*";
736 if ( $this->method == 'POST' ) {
737 // Required for HTTP 1.0 POSTs
738 $this->reqHeaders['Content-Length'] = strlen( $this->postData );
739 $this->reqHeaders['Content-type'] = "application/x-www-form-urlencoded";
740 }
741
742 $options = array();
743 if ( $this->proxy && !$this->noProxy ) {
744 $options['proxy'] = $this->urlToTCP( $this->proxy );
745 $options['request_fulluri'] = true;
746 }
747
748 if ( !$this->followRedirects ) {
749 $options['max_redirects'] = 0;
750 } else {
751 $options['max_redirects'] = $this->maxRedirects;
752 }
753
754 $options['method'] = $this->method;
755 $options['timeout'] = $this->timeout;
756 $options['header'] = implode("\r\n", $this->getHeaderList());
757 // Note that at some future point we may want to support
758 // HTTP/1.1, but we'd have to write support for chunking
759 // in version of PHP < 5.3.1
760 $options['protocol_version'] = "1.0";
761
762 if ( $this->postData ) {
763 $options['content'] = $this->postData;
764 }
765
766 $oldTimeout = false;
767 if ( version_compare( '5.2.1', phpversion(), '>' ) ) {
768 $oldTimeout = ini_set('default_socket_timeout', $this->timeout);
769 }
770
771 $context = stream_context_create( array( 'http' => $options ) );
772 wfSuppressWarnings();
773 $fh = fopen( $this->url, "r", false, $context );
774 wfRestoreWarnings();
775 if ( $oldTimeout !== false ) {
776 ini_set('default_socket_timeout', $oldTimeout);
777 }
778 if ( $fh === false ) {
779 $this->status->fatal( 'http-request-error' );
780 return $this->status;
781 }
782
783 $result = stream_get_meta_data( $fh );
784 if ( $result['timed_out'] ) {
785 $this->status->fatal( 'http-timed-out', $this->url );
786 return $this->status;
787 }
788 $this->headerList = $result['wrapper_data'];
789
790 while ( !feof( $fh ) ) {
791 $buf = fread( $fh, 8192 );
792 if ( $buf === false ) {
793 $this->status->fatal( 'http-read-error' );
794 break;
795 }
796 if ( strlen( $buf ) ) {
797 call_user_func( $this->callback, $fh, $buf );
798 }
799 }
800 fclose( $fh );
801
802 $this->parseHeader();
803 return $this->status;
804 }
805 }