Per comments on r88145: unlink file if it is broken
[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 *
16 * @param $method String: HTTP method. Usually GET/POST
17 * @param $url String: full URL to act on
18 * @param $options Array: options to pass to MWHttpRequest object.
19 * Possible keys for the array:
20 * - timeout Timeout length in seconds
21 * - postData An array of key-value pairs or a url-encoded form data
22 * - proxy The proxy to use.
23 * Will use $wgHTTPProxy (if set) otherwise.
24 * - noProxy Override $wgHTTPProxy (if set) and don't use any proxy at all.
25 * - sslVerifyHost (curl only) Verify hostname against certificate
26 * - sslVerifyCert (curl only) Verify SSL certificate
27 * - caInfo (curl only) Provide CA information
28 * - maxRedirects Maximum number of redirects to follow (defaults to 5)
29 * - followRedirects Whether to follow redirects (defaults to false).
30 * Note: this should only be used when the target URL is trusted,
31 * to avoid attacks on intranet services accessible by HTTP.
32 * @return Mixed: (bool)false on failure or a string on success
33 */
34 public static function request( $method, $url, $options = array() ) {
35 $url = wfExpandUrl( $url );
36 wfDebug( "HTTP: $method: $url\n" );
37 $options['method'] = strtoupper( $method );
38
39 if ( !isset( $options['timeout'] ) ) {
40 $options['timeout'] = 'default';
41 }
42
43 $req = MWHttpRequest::factory( $url, $options );
44 $status = $req->execute();
45
46 if ( $status->isOK() ) {
47 return $req->getContent();
48 } else {
49 return false;
50 }
51 }
52
53 /**
54 * Simple wrapper for Http::request( 'GET' )
55 * @see Http::request()
56 *
57 * @return string
58 */
59 public static function get( $url, $timeout = 'default', $options = array() ) {
60 $options['timeout'] = $timeout;
61 return Http::request( 'GET', $url, $options );
62 }
63
64 /**
65 * Simple wrapper for Http::request( 'POST' )
66 * @see Http::request()
67 *
68 * @return string
69 */
70 public static function post( $url, $options = array() ) {
71 return Http::request( 'POST', $url, $options );
72 }
73
74 /**
75 * Check if the URL can be served by localhost
76 *
77 * @param $url String: full url to check
78 * @return Boolean
79 */
80 public static function isLocalURL( $url ) {
81 global $wgCommandLineMode, $wgConf;
82
83 if ( $wgCommandLineMode ) {
84 return false;
85 }
86
87 // Extract host part
88 $matches = array();
89 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
90 $host = $matches[1];
91 // Split up dotwise
92 $domainParts = explode( '.', $host );
93 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
94 $domainParts = array_reverse( $domainParts );
95
96 $domain = '';
97 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
98 $domainPart = $domainParts[$i];
99 if ( $i == 0 ) {
100 $domain = $domainPart;
101 } else {
102 $domain = $domainPart . '.' . $domain;
103 }
104
105 if ( $wgConf->isLocalVHost( $domain ) ) {
106 return true;
107 }
108 }
109 }
110
111 return false;
112 }
113
114 /**
115 * A standard user-agent we can use for external requests.
116 * @return String
117 */
118 public static function userAgent() {
119 global $wgVersion;
120 return "MediaWiki/$wgVersion";
121 }
122
123 /**
124 * Checks that the given URI is a valid one. Hardcoding the
125 * protocols, because we only want protocols that both cURL
126 * and php support.
127 *
128 * @fixme this is wildly inaccurate and fails to actually check most stuff
129 *
130 * @param $uri Mixed: URI to check for validity
131 * @returns Boolean
132 */
133 public static function isValidURI( $uri ) {
134 return preg_match(
135 '/^https?:\/\/[^\/\s]\S*$/D',
136 $uri
137 );
138 }
139 }
140
141 /**
142 * This wrapper class will call out to curl (if available) or fallback
143 * to regular PHP if necessary for handling internal HTTP requests.
144 *
145 * Renamed from HttpRequest to MWHttpRequest to avoid conflict with
146 * PHP's HTTP extension.
147 */
148 class MWHttpRequest {
149 const SUPPORTS_FILE_POSTS = false;
150
151 protected $content;
152 protected $timeout = 'default';
153 protected $headersOnly = null;
154 protected $postData = null;
155 protected $proxy = null;
156 protected $noProxy = false;
157 protected $sslVerifyHost = true;
158 protected $sslVerifyCert = true;
159 protected $caInfo = null;
160 protected $method = "GET";
161 protected $reqHeaders = array();
162 protected $url;
163 protected $parsedUrl;
164 protected $callback;
165 protected $maxRedirects = 5;
166 protected $followRedirects = false;
167
168 /**
169 * @var CookieJar
170 */
171 protected $cookieJar;
172
173 protected $headerList = array();
174 protected $respVersion = "0.9";
175 protected $respStatus = "200 Ok";
176 protected $respHeaders = array();
177
178 public $status;
179
180 /**
181 * @param $url String: url to use
182 * @param $options Array: (optional) extra params to pass (see Http::request())
183 */
184 function __construct( $url, $options = array() ) {
185 global $wgHTTPTimeout;
186
187 $this->url = $url;
188 $this->parsedUrl = parse_url( $url );
189
190 if ( !Http::isValidURI( $this->url ) ) {
191 $this->status = Status::newFatal( 'http-invalid-url' );
192 } else {
193 $this->status = Status::newGood( 100 ); // continue
194 }
195
196 if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
197 $this->timeout = $options['timeout'];
198 } else {
199 $this->timeout = $wgHTTPTimeout;
200 }
201
202 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
203 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
204
205 foreach ( $members as $o ) {
206 if ( isset( $options[$o] ) ) {
207 $this->$o = $options[$o];
208 }
209 }
210 }
211
212 /**
213 * Generate a new request object
214 * @param $url String: url to use
215 * @param $options Array: (optional) extra params to pass (see Http::request())
216 * @see MWHttpRequest::__construct
217 */
218 public static function factory( $url, $options = null ) {
219 if ( !Http::$httpEngine ) {
220 Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
221 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
222 throw new MWException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
223 ' Http::$httpEngine is set to "curl"' );
224 }
225
226 switch( Http::$httpEngine ) {
227 case 'curl':
228 return new CurlHttpRequest( $url, $options );
229 case 'php':
230 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
231 throw new MWException( __METHOD__ . ': allow_url_fopen needs to be enabled for pure PHP' .
232 ' http requests to work. If possible, curl should be used instead. See http://php.net/curl.' );
233 }
234 return new PhpHttpRequest( $url, $options );
235 default:
236 throw new MWException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
237 }
238 }
239
240 /**
241 * Get the body, or content, of the response to the request
242 *
243 * @return String
244 */
245 public function getContent() {
246 return $this->content;
247 }
248
249 /**
250 * Set the parameters of the request
251
252 * @param $args Array
253 * @todo overload the args param
254 */
255 public function setData( $args ) {
256 $this->postData = $args;
257 }
258
259 /**
260 * Take care of setting up the proxy
261 * (override in subclass)
262 *
263 * @return String
264 */
265 public function proxySetup() {
266 global $wgHTTPProxy;
267
268 if ( $this->proxy ) {
269 return;
270 }
271
272 if ( Http::isLocalURL( $this->url ) ) {
273 $this->proxy = 'http://localhost:80/';
274 } elseif ( $wgHTTPProxy ) {
275 $this->proxy = $wgHTTPProxy ;
276 } elseif ( getenv( "http_proxy" ) ) {
277 $this->proxy = getenv( "http_proxy" );
278 }
279 }
280
281 /**
282 * Set the refererer header
283 */
284 public function setReferer( $url ) {
285 $this->setHeader( 'Referer', $url );
286 }
287
288 /**
289 * Set the user agent
290 */
291 public function setUserAgent( $UA ) {
292 $this->setHeader( 'User-Agent', $UA );
293 }
294
295 /**
296 * Set an arbitrary header
297 */
298 public function setHeader( $name, $value ) {
299 // I feel like I should normalize the case here...
300 $this->reqHeaders[$name] = $value;
301 }
302
303 /**
304 * Get an array of the headers
305 */
306 public function getHeaderList() {
307 $list = array();
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 $callback Callback
340 */
341 public function setCallback( $callback ) {
342 if ( !is_callable( $callback ) ) {
343 throw new MWException( 'Invalid MwHttpRequest callback' );
344 }
345 $this->callback = $callback;
346 }
347
348 /**
349 * A generic callback to read the body of the response from a remote
350 * server.
351 *
352 * @param $fh handle
353 * @param $content String
354 */
355 public function read( $fh, $content ) {
356 $this->content .= $content;
357 return strlen( $content );
358 }
359
360 /**
361 * Take care of whatever is necessary to perform the URI request.
362 *
363 * @return Status
364 */
365 public function execute() {
366 global $wgTitle;
367
368 $this->content = "";
369
370 if ( strtoupper( $this->method ) == "HEAD" ) {
371 $this->headersOnly = true;
372 }
373
374 if ( is_object( $wgTitle ) && !isset( $this->reqHeaders['Referer'] ) ) {
375 $this->setReferer( $wgTitle->getFullURL() );
376 }
377
378 if ( !$this->noProxy ) {
379 $this->proxySetup();
380 }
381
382 if ( !$this->callback ) {
383 $this->setCallback( array( $this, 'read' ) );
384 }
385
386 if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
387 $this->setUserAgent( Http::userAgent() );
388 }
389 }
390
391 /**
392 * Parses the headers, including the HTTP status code and any
393 * Set-Cookie headers. This function expectes the headers to be
394 * found in an array in the member variable headerList.
395 *
396 * @return nothing
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 * @return nothing
426 */
427 protected function setStatus() {
428 if ( !$this->respHeaders ) {
429 $this->parseHeader();
430 }
431
432 if ( (int)$this->respStatus > 399 ) {
433 list( $code, $message ) = explode( " ", $this->respStatus, 2 );
434 $this->status->fatal( "http-bad-status", $code, $message );
435 }
436 }
437
438 /**
439 * Get the integer value of the HTTP status code (e.g. 200 for "200 Ok")
440 * (see RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
441 * for a list of status codes.)
442 *
443 * @return Integer
444 */
445 public function getStatus() {
446 if ( !$this->respHeaders ) {
447 $this->parseHeader();
448 }
449
450 return (int)$this->respStatus;
451 }
452
453
454 /**
455 * Returns true if the last status code was a redirect.
456 *
457 * @return Boolean
458 */
459 public function isRedirect() {
460 if ( !$this->respHeaders ) {
461 $this->parseHeader();
462 }
463
464 $status = (int)$this->respStatus;
465
466 if ( $status >= 300 && $status <= 303 ) {
467 return true;
468 }
469
470 return false;
471 }
472
473 /**
474 * Returns an associative array of response headers after the
475 * request has been executed. Because some headers
476 * (e.g. Set-Cookie) can appear more than once the, each value of
477 * the associative array is an array of the values given.
478 *
479 * @return Array
480 */
481 public function getResponseHeaders() {
482 if ( !$this->respHeaders ) {
483 $this->parseHeader();
484 }
485
486 return $this->respHeaders;
487 }
488
489 /**
490 * Returns the value of the given response header.
491 *
492 * @param $header String
493 * @return String
494 */
495 public function getResponseHeader( $header ) {
496 if ( !$this->respHeaders ) {
497 $this->parseHeader();
498 }
499
500 if ( isset( $this->respHeaders[strtolower ( $header ) ] ) ) {
501 $v = $this->respHeaders[strtolower ( $header ) ];
502 return $v[count( $v ) - 1];
503 }
504
505 return null;
506 }
507
508 /**
509 * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
510 *
511 * @param $jar CookieJar
512 */
513 public function setCookieJar( $jar ) {
514 $this->cookieJar = $jar;
515 }
516
517 /**
518 * Returns the cookie jar in use.
519 *
520 * @returns CookieJar
521 */
522 public function getCookieJar() {
523 if ( !$this->respHeaders ) {
524 $this->parseHeader();
525 }
526
527 return $this->cookieJar;
528 }
529
530 /**
531 * Sets a cookie. Used before a request to set up any individual
532 * cookies. Used internally after a request to parse the
533 * Set-Cookie headers.
534 * @see Cookie::set
535 */
536 public function setCookie( $name, $value = null, $attr = null ) {
537 if ( !$this->cookieJar ) {
538 $this->cookieJar = new CookieJar;
539 }
540
541 $this->cookieJar->setCookie( $name, $value, $attr );
542 }
543
544 /**
545 * Parse the cookies in the response headers and store them in the cookie jar.
546 */
547 protected function parseCookies() {
548 if ( !$this->cookieJar ) {
549 $this->cookieJar = new CookieJar;
550 }
551
552 if ( isset( $this->respHeaders['set-cookie'] ) ) {
553 $url = parse_url( $this->getFinalUrl() );
554 foreach ( $this->respHeaders['set-cookie'] as $cookie ) {
555 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
556 }
557 }
558 }
559
560 /**
561 * Returns the final URL after all redirections.
562 *
563 * @return String
564 */
565 public function getFinalUrl() {
566 $location = $this->getResponseHeader( "Location" );
567
568 if ( $location ) {
569 return $location;
570 }
571
572 return $this->url;
573 }
574
575 /**
576 * Returns true if the backend can follow redirects. Overridden by the
577 * child classes.
578 */
579 public function canFollowRedirects() {
580 return true;
581 }
582 }
583
584 /**
585 * MWHttpRequest implemented using internal curl compiled into PHP
586 */
587 class CurlHttpRequest extends MWHttpRequest {
588 const SUPPORTS_FILE_POSTS = true;
589
590 static $curlMessageMap = array(
591 6 => 'http-host-unreachable',
592 28 => 'http-timed-out'
593 );
594
595 protected $curlOptions = array();
596 protected $headerText = "";
597
598 protected function readHeader( $fh, $content ) {
599 $this->headerText .= $content;
600 return strlen( $content );
601 }
602
603 public function execute() {
604 parent::execute();
605
606 if ( !$this->status->isOK() ) {
607 return $this->status;
608 }
609
610 $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
611 $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
612 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
613 $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
614 $this->curlOptions[CURLOPT_HEADERFUNCTION] = array( $this, "readHeader" );
615 $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
616 $this->curlOptions[CURLOPT_ENCODING] = ""; # Enable compression
617
618 /* not sure these two are actually necessary */
619 if ( isset( $this->reqHeaders['Referer'] ) ) {
620 $this->curlOptions[CURLOPT_REFERER] = $this->reqHeaders['Referer'];
621 }
622 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
623
624 if ( isset( $this->sslVerifyHost ) ) {
625 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost;
626 }
627
628 if ( isset( $this->sslVerifyCert ) ) {
629 $this->curlOptions[CURLOPT_SSL_VERIFYPEER] = $this->sslVerifyCert;
630 }
631
632 if ( $this->caInfo ) {
633 $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
634 }
635
636 if ( $this->headersOnly ) {
637 $this->curlOptions[CURLOPT_NOBODY] = true;
638 $this->curlOptions[CURLOPT_HEADER] = true;
639 } elseif ( $this->method == 'POST' ) {
640 $this->curlOptions[CURLOPT_POST] = true;
641 $this->curlOptions[CURLOPT_POSTFIELDS] = $this->postData;
642 // Suppress 'Expect: 100-continue' header, as some servers
643 // will reject it with a 417 and Curl won't auto retry
644 // with HTTP 1.0 fallback
645 $this->reqHeaders['Expect'] = '';
646 } else {
647 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
648 }
649
650 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
651
652 $curlHandle = curl_init( $this->url );
653
654 if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
655 throw new MWException( "Error setting curl options." );
656 }
657
658 if ( $this->followRedirects && $this->canFollowRedirects() ) {
659 wfSuppressWarnings();
660 if ( ! curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
661 wfDebug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
662 "Probably safe_mode or open_basedir is set.\n" );
663 // Continue the processing. If it were in curl_setopt_array,
664 // processing would have halted on its entry
665 }
666 wfRestoreWarnings();
667 }
668
669 if ( false === curl_exec( $curlHandle ) ) {
670 $code = curl_error( $curlHandle );
671
672 if ( isset( self::$curlMessageMap[$code] ) ) {
673 $this->status->fatal( self::$curlMessageMap[$code] );
674 } else {
675 $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
676 }
677 } else {
678 $this->headerList = explode( "\r\n", $this->headerText );
679 }
680
681 curl_close( $curlHandle );
682
683 $this->parseHeader();
684 $this->setStatus();
685
686 return $this->status;
687 }
688
689 public function canFollowRedirects() {
690 if ( strval( ini_get( 'open_basedir' ) ) !== '' || wfIniGetBool( 'safe_mode' ) ) {
691 wfDebug( "Cannot follow redirects in safe mode\n" );
692 return false;
693 }
694
695 if ( !defined( 'CURLOPT_REDIR_PROTOCOLS' ) ) {
696 wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
697 return false;
698 }
699
700 return true;
701 }
702 }
703
704 class PhpHttpRequest extends MWHttpRequest {
705 protected function urlToTcp( $url ) {
706 $parsedUrl = parse_url( $url );
707
708 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
709 }
710
711 public function execute() {
712 parent::execute();
713
714 if ( is_array( $this->postData ) ) {
715 $this->postData = wfArrayToCGI( $this->postData );
716 }
717
718 if ( $this->parsedUrl['scheme'] != 'http' &&
719 $this->parsedUrl['scheme'] != 'https' ) {
720 $this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] );
721 }
722
723 $this->reqHeaders['Accept'] = "*/*";
724 if ( $this->method == 'POST' ) {
725 // Required for HTTP 1.0 POSTs
726 $this->reqHeaders['Content-Length'] = strlen( $this->postData );
727 $this->reqHeaders['Content-type'] = "application/x-www-form-urlencoded";
728 }
729
730 $options = array();
731 if ( $this->proxy && !$this->noProxy ) {
732 $options['proxy'] = $this->urlToTCP( $this->proxy );
733 $options['request_fulluri'] = true;
734 }
735
736 if ( !$this->followRedirects ) {
737 $options['max_redirects'] = 0;
738 } else {
739 $options['max_redirects'] = $this->maxRedirects;
740 }
741
742 $options['method'] = $this->method;
743 $options['header'] = implode( "\r\n", $this->getHeaderList() );
744 // Note that at some future point we may want to support
745 // HTTP/1.1, but we'd have to write support for chunking
746 // in version of PHP < 5.3.1
747 $options['protocol_version'] = "1.0";
748
749 // This is how we tell PHP we want to deal with 404s (for example) ourselves.
750 // Only works on 5.2.10+
751 $options['ignore_errors'] = true;
752
753 if ( $this->postData ) {
754 $options['content'] = $this->postData;
755 }
756
757 $options['timeout'] = $this->timeout;
758
759 $context = stream_context_create( array( 'http' => $options ) );
760
761 $this->headerList = array();
762 $reqCount = 0;
763 $url = $this->url;
764
765 $result = array();
766
767 do {
768 $reqCount++;
769 wfSuppressWarnings();
770 $fh = fopen( $url, "r", false, $context );
771 wfRestoreWarnings();
772
773 if ( !$fh ) {
774 break;
775 }
776
777 $result = stream_get_meta_data( $fh );
778 $this->headerList = $result['wrapper_data'];
779 $this->parseHeader();
780
781 if ( !$this->followRedirects ) {
782 break;
783 }
784
785 # Handle manual redirection
786 if ( !$this->isRedirect() || $reqCount > $this->maxRedirects ) {
787 break;
788 }
789 # Check security of URL
790 $url = $this->getResponseHeader( "Location" );
791
792 if ( substr( $url, 0, 7 ) !== 'http://' ) {
793 wfDebug( __METHOD__ . ": insecure redirection\n" );
794 break;
795 }
796 } while ( true );
797
798 $this->setStatus();
799
800 if ( $fh === false ) {
801 $this->status->fatal( 'http-request-error' );
802 return $this->status;
803 }
804
805 if ( $result['timed_out'] ) {
806 $this->status->fatal( 'http-timed-out', $this->url );
807 return $this->status;
808 }
809
810 // If everything went OK, or we recieved some error code
811 // get the response body content.
812 if ( $this->status->isOK()
813 || (int)$this->respStatus >= 300) {
814 while ( !feof( $fh ) ) {
815 $buf = fread( $fh, 8192 );
816
817 if ( $buf === false ) {
818 $this->status->fatal( 'http-read-error' );
819 break;
820 }
821
822 if ( strlen( $buf ) ) {
823 call_user_func( $this->callback, $fh, $buf );
824 }
825 }
826 }
827 fclose( $fh );
828
829 return $this->status;
830 }
831 }