Even more documentation in various files
[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 * @param $uri Mixed: URI to check for validity
129 * @returns Boolean
130 */
131 public static function isValidURI( $uri ) {
132 return preg_match(
133 '/^(f|ht)tps?:\/\/[^\/\s]\S*$/D',
134 $uri
135 );
136 }
137 }
138
139 /**
140 * This wrapper class will call out to curl (if available) or fallback
141 * to regular PHP if necessary for handling internal HTTP requests.
142 *
143 * Renamed from HttpRequest to MWHttpRequest to avoid conflict with
144 * PHP's HTTP extension.
145 */
146 class MWHttpRequest {
147 const SUPPORTS_FILE_POSTS = false;
148
149 protected $content;
150 protected $timeout = 'default';
151 protected $headersOnly = null;
152 protected $postData = null;
153 protected $proxy = null;
154 protected $noProxy = false;
155 protected $sslVerifyHost = true;
156 protected $sslVerifyCert = true;
157 protected $caInfo = null;
158 protected $method = "GET";
159 protected $reqHeaders = array();
160 protected $url;
161 protected $parsedUrl;
162 protected $callback;
163 protected $maxRedirects = 5;
164 protected $followRedirects = false;
165
166 /**
167 * @var CookieJar
168 */
169 protected $cookieJar;
170
171 protected $headerList = array();
172 protected $respVersion = "0.9";
173 protected $respStatus = "200 Ok";
174 protected $respHeaders = array();
175
176 public $status;
177
178 /**
179 * @param $url String: url to use
180 * @param $options Array: (optional) extra params to pass (see Http::request())
181 */
182 function __construct( $url, $options = array() ) {
183 global $wgHTTPTimeout;
184
185 $this->url = $url;
186 $this->parsedUrl = parse_url( $url );
187
188 if ( !Http::isValidURI( $this->url ) ) {
189 $this->status = Status::newFatal( 'http-invalid-url' );
190 } else {
191 $this->status = Status::newGood( 100 ); // continue
192 }
193
194 if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
195 $this->timeout = $options['timeout'];
196 } else {
197 $this->timeout = $wgHTTPTimeout;
198 }
199
200 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
201 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
202
203 foreach ( $members as $o ) {
204 if ( isset( $options[$o] ) ) {
205 $this->$o = $options[$o];
206 }
207 }
208 }
209
210 /**
211 * Generate a new request object
212 * @param $url String: url to use
213 * @param $options Array: (optional) extra params to pass (see Http::request())
214 * @see MWHttpRequest::__construct
215 */
216 public static function factory( $url, $options = null ) {
217 if ( !Http::$httpEngine ) {
218 Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
219 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
220 throw new MWException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
221 ' Http::$httpEngine is set to "curl"' );
222 }
223
224 switch( Http::$httpEngine ) {
225 case 'curl':
226 return new CurlHttpRequest( $url, $options );
227 case 'php':
228 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
229 throw new MWException( __METHOD__ . ': allow_url_fopen needs to be enabled for pure PHP' .
230 ' http requests to work. If possible, curl should be used instead. See http://php.net/curl.' );
231 }
232 return new PhpHttpRequest( $url, $options );
233 default:
234 throw new MWException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
235 }
236 }
237
238 /**
239 * Get the body, or content, of the response to the request
240 *
241 * @return String
242 */
243 public function getContent() {
244 return $this->content;
245 }
246
247 /**
248 * Set the parameters of the request
249
250 * @param $args Array
251 * @todo overload the args param
252 */
253 public function setData( $args ) {
254 $this->postData = $args;
255 }
256
257 /**
258 * Take care of setting up the proxy
259 * (override in subclass)
260 *
261 * @return String
262 */
263 public function proxySetup() {
264 global $wgHTTPProxy;
265
266 if ( $this->proxy ) {
267 return;
268 }
269
270 if ( Http::isLocalURL( $this->url ) ) {
271 $this->proxy = 'http://localhost:80/';
272 } elseif ( $wgHTTPProxy ) {
273 $this->proxy = $wgHTTPProxy ;
274 } elseif ( getenv( "http_proxy" ) ) {
275 $this->proxy = getenv( "http_proxy" );
276 }
277 }
278
279 /**
280 * Set the refererer header
281 */
282 public function setReferer( $url ) {
283 $this->setHeader( 'Referer', $url );
284 }
285
286 /**
287 * Set the user agent
288 */
289 public function setUserAgent( $UA ) {
290 $this->setHeader( 'User-Agent', $UA );
291 }
292
293 /**
294 * Set an arbitrary header
295 */
296 public function setHeader( $name, $value ) {
297 // I feel like I should normalize the case here...
298 $this->reqHeaders[$name] = $value;
299 }
300
301 /**
302 * Get an array of the headers
303 */
304 public function getHeaderList() {
305 $list = array();
306
307 if ( $this->cookieJar ) {
308 $this->reqHeaders['Cookie'] =
309 $this->cookieJar->serializeToHttpRequest(
310 $this->parsedUrl['path'],
311 $this->parsedUrl['host']
312 );
313 }
314
315 foreach ( $this->reqHeaders as $name => $value ) {
316 $list[] = "$name: $value";
317 }
318
319 return $list;
320 }
321
322 /**
323 * Set a read callback to accept data read from the HTTP request.
324 * By default, data is appended to an internal buffer which can be
325 * retrieved through $req->getContent().
326 *
327 * To handle data as it comes in -- especially for large files that
328 * would not fit in memory -- you can instead set your own callback,
329 * in the form function($resource, $buffer) where the first parameter
330 * is the low-level resource being read (implementation specific),
331 * and the second parameter is the data buffer.
332 *
333 * You MUST return the number of bytes handled in the buffer; if fewer
334 * bytes are reported handled than were passed to you, the HTTP fetch
335 * will be aborted.
336 *
337 * @param $callback Callback
338 */
339 public function setCallback( $callback ) {
340 if ( !is_callable( $callback ) ) {
341 throw new MWException( 'Invalid MwHttpRequest callback' );
342 }
343 $this->callback = $callback;
344 }
345
346 /**
347 * A generic callback to read the body of the response from a remote
348 * server.
349 *
350 * @param $fh handle
351 * @param $content String
352 */
353 public function read( $fh, $content ) {
354 $this->content .= $content;
355 return strlen( $content );
356 }
357
358 /**
359 * Take care of whatever is necessary to perform the URI request.
360 *
361 * @return Status
362 */
363 public function execute() {
364 global $wgTitle;
365
366 $this->content = "";
367
368 if ( strtoupper( $this->method ) == "HEAD" ) {
369 $this->headersOnly = true;
370 }
371
372 if ( is_object( $wgTitle ) && !isset( $this->reqHeaders['Referer'] ) ) {
373 $this->setReferer( $wgTitle->getFullURL() );
374 }
375
376 if ( !$this->noProxy ) {
377 $this->proxySetup();
378 }
379
380 if ( !$this->callback ) {
381 $this->setCallback( array( $this, 'read' ) );
382 }
383
384 if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
385 $this->setUserAgent( Http::userAgent() );
386 }
387 }
388
389 /**
390 * Parses the headers, including the HTTP status code and any
391 * Set-Cookie headers. This function expectes the headers to be
392 * found in an array in the member variable headerList.
393 *
394 * @return nothing
395 */
396 protected function parseHeader() {
397 $lastname = "";
398
399 foreach ( $this->headerList as $header ) {
400 if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
401 $this->respVersion = $match[1];
402 $this->respStatus = $match[2];
403 } elseif ( preg_match( "#^[ \t]#", $header ) ) {
404 $last = count( $this->respHeaders[$lastname] ) - 1;
405 $this->respHeaders[$lastname][$last] .= "\r\n$header";
406 } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
407 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
408 $lastname = strtolower( $match[1] );
409 }
410 }
411
412 $this->parseCookies();
413 }
414
415 /**
416 * Sets HTTPRequest status member to a fatal value with the error
417 * message if the returned integer value of the status code was
418 * not successful (< 300) or a redirect (>=300 and < 400). (see
419 * RFC2616, section 10,
420 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a
421 * list of status codes.)
422 *
423 * @return nothing
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 Integer
442 */
443 public function getStatus() {
444 if ( !$this->respHeaders ) {
445 $this->parseHeader();
446 }
447
448 return (int)$this->respStatus;
449 }
450
451
452 /**
453 * Returns true if the last status code was a redirect.
454 *
455 * @return Boolean
456 */
457 public function isRedirect() {
458 if ( !$this->respHeaders ) {
459 $this->parseHeader();
460 }
461
462 $status = (int)$this->respStatus;
463
464 if ( $status >= 300 && $status <= 303 ) {
465 return true;
466 }
467
468 return false;
469 }
470
471 /**
472 * Returns an associative array of response headers after the
473 * request has been executed. Because some headers
474 * (e.g. Set-Cookie) can appear more than once the, each value of
475 * the associative array is an array of the values given.
476 *
477 * @return Array
478 */
479 public function getResponseHeaders() {
480 if ( !$this->respHeaders ) {
481 $this->parseHeader();
482 }
483
484 return $this->respHeaders;
485 }
486
487 /**
488 * Returns the value of the given response header.
489 *
490 * @param $header String
491 * @return String
492 */
493 public function getResponseHeader( $header ) {
494 if ( !$this->respHeaders ) {
495 $this->parseHeader();
496 }
497
498 if ( isset( $this->respHeaders[strtolower ( $header ) ] ) ) {
499 $v = $this->respHeaders[strtolower ( $header ) ];
500 return $v[count( $v ) - 1];
501 }
502
503 return null;
504 }
505
506 /**
507 * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
508 *
509 * @param $jar CookieJar
510 */
511 public function setCookieJar( $jar ) {
512 $this->cookieJar = $jar;
513 }
514
515 /**
516 * Returns the cookie jar in use.
517 *
518 * @returns CookieJar
519 */
520 public function getCookieJar() {
521 if ( !$this->respHeaders ) {
522 $this->parseHeader();
523 }
524
525 return $this->cookieJar;
526 }
527
528 /**
529 * Sets a cookie. Used before a request to set up any individual
530 * cookies. Used internally after a request to parse the
531 * Set-Cookie headers.
532 * @see Cookie::set
533 */
534 public function setCookie( $name, $value = null, $attr = null ) {
535 if ( !$this->cookieJar ) {
536 $this->cookieJar = new CookieJar;
537 }
538
539 $this->cookieJar->setCookie( $name, $value, $attr );
540 }
541
542 /**
543 * Parse the cookies in the response headers and store them in the cookie jar.
544 */
545 protected function parseCookies() {
546 if ( !$this->cookieJar ) {
547 $this->cookieJar = new CookieJar;
548 }
549
550 if ( isset( $this->respHeaders['set-cookie'] ) ) {
551 $url = parse_url( $this->getFinalUrl() );
552 foreach ( $this->respHeaders['set-cookie'] as $cookie ) {
553 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
554 }
555 }
556 }
557
558 /**
559 * Returns the final URL after all redirections.
560 *
561 * @return String
562 */
563 public function getFinalUrl() {
564 $location = $this->getResponseHeader( "Location" );
565
566 if ( $location ) {
567 return $location;
568 }
569
570 return $this->url;
571 }
572
573 /**
574 * Returns true if the backend can follow redirects. Overridden by the
575 * child classes.
576 */
577 public function canFollowRedirects() {
578 return true;
579 }
580 }
581
582 /**
583 * MWHttpRequest implemented using internal curl compiled into PHP
584 */
585 class CurlHttpRequest extends MWHttpRequest {
586 const SUPPORTS_FILE_POSTS = true;
587
588 static $curlMessageMap = array(
589 6 => 'http-host-unreachable',
590 28 => 'http-timed-out'
591 );
592
593 protected $curlOptions = array();
594 protected $headerText = "";
595
596 protected function readHeader( $fh, $content ) {
597 $this->headerText .= $content;
598 return strlen( $content );
599 }
600
601 public function execute() {
602 parent::execute();
603
604 if ( !$this->status->isOK() ) {
605 return $this->status;
606 }
607
608 $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
609 $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
610 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
611 $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
612 $this->curlOptions[CURLOPT_HEADERFUNCTION] = array( $this, "readHeader" );
613 $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
614 $this->curlOptions[CURLOPT_ENCODING] = ""; # Enable compression
615
616 /* not sure these two are actually necessary */
617 if ( isset( $this->reqHeaders['Referer'] ) ) {
618 $this->curlOptions[CURLOPT_REFERER] = $this->reqHeaders['Referer'];
619 }
620 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
621
622 if ( isset( $this->sslVerifyHost ) ) {
623 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost;
624 }
625
626 if ( isset( $this->sslVerifyCert ) ) {
627 $this->curlOptions[CURLOPT_SSL_VERIFYPEER] = $this->sslVerifyCert;
628 }
629
630 if ( $this->caInfo ) {
631 $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
632 }
633
634 if ( $this->headersOnly ) {
635 $this->curlOptions[CURLOPT_NOBODY] = true;
636 $this->curlOptions[CURLOPT_HEADER] = true;
637 } elseif ( $this->method == 'POST' ) {
638 $this->curlOptions[CURLOPT_POST] = true;
639 $this->curlOptions[CURLOPT_POSTFIELDS] = $this->postData;
640 // Suppress 'Expect: 100-continue' header, as some servers
641 // will reject it with a 417 and Curl won't auto retry
642 // with HTTP 1.0 fallback
643 $this->reqHeaders['Expect'] = '';
644 } else {
645 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
646 }
647
648 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
649
650 $curlHandle = curl_init( $this->url );
651
652 if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
653 throw new MWException( "Error setting curl options." );
654 }
655
656 if ( $this->followRedirects && $this->canFollowRedirects() ) {
657 wfSuppressWarnings();
658 if ( ! curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
659 wfDebug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
660 "Probably safe_mode or open_basedir is set.\n" );
661 // Continue the processing. If it were in curl_setopt_array,
662 // processing would have halted on its entry
663 }
664 wfRestoreWarnings();
665 }
666
667 if ( false === curl_exec( $curlHandle ) ) {
668 $code = curl_error( $curlHandle );
669
670 if ( isset( self::$curlMessageMap[$code] ) ) {
671 $this->status->fatal( self::$curlMessageMap[$code] );
672 } else {
673 $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
674 }
675 } else {
676 $this->headerList = explode( "\r\n", $this->headerText );
677 }
678
679 curl_close( $curlHandle );
680
681 $this->parseHeader();
682 $this->setStatus();
683
684 return $this->status;
685 }
686
687 public function canFollowRedirects() {
688 if ( strval( ini_get( 'open_basedir' ) ) !== '' || wfIniGetBool( 'safe_mode' ) ) {
689 wfDebug( "Cannot follow redirects in safe mode\n" );
690 return false;
691 }
692
693 if ( !defined( 'CURLOPT_REDIR_PROTOCOLS' ) ) {
694 wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
695 return false;
696 }
697
698 return true;
699 }
700 }
701
702 class PhpHttpRequest extends MWHttpRequest {
703 protected function urlToTcp( $url ) {
704 $parsedUrl = parse_url( $url );
705
706 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
707 }
708
709 public function execute() {
710 parent::execute();
711
712 if ( is_array( $this->postData ) ) {
713 $this->postData = wfArrayToCGI( $this->postData );
714 }
715
716 if ( $this->parsedUrl['scheme'] != 'http' ) {
717 $this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] );
718 }
719
720 $this->reqHeaders['Accept'] = "*/*";
721 if ( $this->method == 'POST' ) {
722 // Required for HTTP 1.0 POSTs
723 $this->reqHeaders['Content-Length'] = strlen( $this->postData );
724 $this->reqHeaders['Content-type'] = "application/x-www-form-urlencoded";
725 }
726
727 $options = array();
728 if ( $this->proxy && !$this->noProxy ) {
729 $options['proxy'] = $this->urlToTCP( $this->proxy );
730 $options['request_fulluri'] = true;
731 }
732
733 if ( !$this->followRedirects ) {
734 $options['max_redirects'] = 0;
735 } else {
736 $options['max_redirects'] = $this->maxRedirects;
737 }
738
739 $options['method'] = $this->method;
740 $options['header'] = implode( "\r\n", $this->getHeaderList() );
741 // Note that at some future point we may want to support
742 // HTTP/1.1, but we'd have to write support for chunking
743 // in version of PHP < 5.3.1
744 $options['protocol_version'] = "1.0";
745
746 // This is how we tell PHP we want to deal with 404s (for example) ourselves.
747 // Only works on 5.2.10+
748 $options['ignore_errors'] = true;
749
750 if ( $this->postData ) {
751 $options['content'] = $this->postData;
752 }
753
754 $options['timeout'] = $this->timeout;
755
756 $context = stream_context_create( array( 'http' => $options ) );
757
758 $this->headerList = array();
759 $reqCount = 0;
760 $url = $this->url;
761
762 $result = array();
763
764 do {
765 $reqCount++;
766 wfSuppressWarnings();
767 $fh = fopen( $url, "r", false, $context );
768 wfRestoreWarnings();
769
770 if ( !$fh ) {
771 break;
772 }
773
774 $result = stream_get_meta_data( $fh );
775 $this->headerList = $result['wrapper_data'];
776 $this->parseHeader();
777
778 if ( !$this->followRedirects ) {
779 break;
780 }
781
782 # Handle manual redirection
783 if ( !$this->isRedirect() || $reqCount > $this->maxRedirects ) {
784 break;
785 }
786 # Check security of URL
787 $url = $this->getResponseHeader( "Location" );
788
789 if ( substr( $url, 0, 7 ) !== 'http://' ) {
790 wfDebug( __METHOD__ . ": insecure redirection\n" );
791 break;
792 }
793 } while ( true );
794
795 $this->setStatus();
796
797 if ( $fh === false ) {
798 $this->status->fatal( 'http-request-error' );
799 return $this->status;
800 }
801
802 if ( $result['timed_out'] ) {
803 $this->status->fatal( 'http-timed-out', $this->url );
804 return $this->status;
805 }
806
807 // If everything went OK, or we recieved some error code
808 // get the response body content.
809 if ( $this->status->isOK()
810 || (int)$this->respStatus >= 300) {
811 while ( !feof( $fh ) ) {
812 $buf = fread( $fh, 8192 );
813
814 if ( $buf === false ) {
815 $this->status->fatal( 'http-read-error' );
816 break;
817 }
818
819 if ( strlen( $buf ) ) {
820 call_user_func( $this->callback, $fh, $buf );
821 }
822 }
823 }
824 fclose( $fh );
825
826 return $this->status;
827 }
828 }