3 * Various HTTP related functions.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
29 * Various HTTP related functions
33 static public $httpEngine = false;
36 * Perform an HTTP request
38 * @param string $method HTTP method. Usually GET/POST
39 * @param string $url Full URL to act on. If protocol-relative, will be expanded to an http:// URL
40 * @param array $options Options to pass to MWHttpRequest object.
41 * Possible keys for the array:
42 * - timeout Timeout length in seconds
43 * - connectTimeout Timeout for connection, in seconds (curl only)
44 * - postData An array of key-value pairs or a url-encoded form data
45 * - proxy The proxy to use.
46 * Otherwise it will use $wgHTTPProxy (if set)
47 * Otherwise it will use the environment variable "http_proxy" (if set)
48 * - noProxy Don't use any proxy at all. Takes precedence over proxy value(s).
49 * - sslVerifyHost Verify hostname against certificate
50 * - sslVerifyCert Verify SSL certificate
51 * - caInfo Provide CA information
52 * - maxRedirects Maximum number of redirects to follow (defaults to 5)
53 * - followRedirects Whether to follow redirects (defaults to false).
54 * Note: this should only be used when the target URL is trusted,
55 * to avoid attacks on intranet services accessible by HTTP.
56 * - userAgent A user agent, if you want to override the default
57 * MediaWiki/$wgVersion
58 * @return string|bool (bool)false on failure or a string on success
60 public static function request( $method, $url, $options = array() ) {
61 wfDebug( "HTTP: $method: $url\n" );
62 wfProfileIn( __METHOD__
. "-$method" );
64 $options['method'] = strtoupper( $method );
66 if ( !isset( $options['timeout'] ) ) {
67 $options['timeout'] = 'default';
69 if ( !isset( $options['connectTimeout'] ) ) {
70 $options['connectTimeout'] = 'default';
73 $req = MWHttpRequest
::factory( $url, $options );
74 $status = $req->execute();
77 if ( $status->isOK() ) {
78 $content = $req->getContent();
80 wfProfileOut( __METHOD__
. "-$method" );
85 * Simple wrapper for Http::request( 'GET' )
86 * @see Http::request()
89 * @param string $timeout
90 * @param array $options
93 public static function get( $url, $timeout = 'default', $options = array() ) {
94 $options['timeout'] = $timeout;
95 return Http
::request( 'GET', $url, $options );
99 * Simple wrapper for Http::request( 'POST' )
100 * @see Http::request()
103 * @param array $options
106 public static function post( $url, $options = array() ) {
107 return Http
::request( 'POST', $url, $options );
111 * Check if the URL can be served by localhost
113 * @param string $url Full url to check
116 public static function isLocalURL( $url ) {
117 global $wgCommandLineMode, $wgLocalVirtualHosts, $wgConf;
119 if ( $wgCommandLineMode ) {
125 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
128 $domainParts = explode( '.', $host );
129 // Check if this domain or any superdomain is listed as a local virtual host
130 $domainParts = array_reverse( $domainParts );
133 $countParts = count( $domainParts );
134 for ( $i = 0; $i < $countParts; $i++
) {
135 $domainPart = $domainParts[$i];
137 $domain = $domainPart;
139 $domain = $domainPart . '.' . $domain;
142 if ( in_array( $domain, $wgLocalVirtualHosts )
143 ||
$wgConf->isLocalVHost( $domain )
154 * A standard user-agent we can use for external requests.
157 public static function userAgent() {
159 return "MediaWiki/$wgVersion";
163 * Checks that the given URI is a valid one. Hardcoding the
164 * protocols, because we only want protocols that both cURL
167 * file:// should not be allowed here for security purpose (r67684)
169 * @todo FIXME this is wildly inaccurate and fails to actually check most stuff
171 * @param string $uri URI to check for validity
174 public static function isValidURI( $uri ) {
176 '/^https?:\/\/[^\/\s]\S*$/D',
183 * This wrapper class will call out to curl (if available) or fallback
184 * to regular PHP if necessary for handling internal HTTP requests.
186 * Renamed from HttpRequest to MWHttpRequest to avoid conflict with
187 * PHP's HTTP extension.
189 class MWHttpRequest
{
190 const SUPPORTS_FILE_POSTS
= false;
193 protected $timeout = 'default';
194 protected $headersOnly = null;
195 protected $postData = null;
196 protected $proxy = null;
197 protected $noProxy = false;
198 protected $sslVerifyHost = true;
199 protected $sslVerifyCert = true;
200 protected $caInfo = null;
201 protected $method = "GET";
202 protected $reqHeaders = array();
204 protected $parsedUrl;
206 protected $maxRedirects = 5;
207 protected $followRedirects = false;
212 protected $cookieJar;
214 protected $headerList = array();
215 protected $respVersion = "0.9";
216 protected $respStatus = "200 Ok";
217 protected $respHeaders = array();
222 * @param string $url Url to use. If protocol-relative, will be expanded to an http:// URL
223 * @param array $options (optional) extra params to pass (see Http::request())
225 protected function __construct( $url, $options = array() ) {
226 global $wgHTTPTimeout, $wgHTTPConnectTimeout;
228 $this->url
= wfExpandUrl( $url, PROTO_HTTP
);
229 $this->parsedUrl
= wfParseUrl( $this->url
);
231 if ( !$this->parsedUrl ||
!Http
::isValidURI( $this->url
) ) {
232 $this->status
= Status
::newFatal( 'http-invalid-url' );
234 $this->status
= Status
::newGood( 100 ); // continue
237 if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
238 $this->timeout
= $options['timeout'];
240 $this->timeout
= $wgHTTPTimeout;
242 if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) {
243 $this->connectTimeout
= $options['connectTimeout'];
245 $this->connectTimeout
= $wgHTTPConnectTimeout;
247 if ( isset( $options['userAgent'] ) ) {
248 $this->setUserAgent( $options['userAgent'] );
251 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
252 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
254 foreach ( $members as $o ) {
255 if ( isset( $options[$o] ) ) {
256 // ensure that MWHttpRequest::method is always
257 // uppercased. Bug 36137
258 if ( $o == 'method' ) {
259 $options[$o] = strtoupper( $options[$o] );
261 $this->$o = $options[$o];
265 if ( $this->noProxy
) {
266 $this->proxy
= ''; // noProxy takes precedence
271 * Simple function to test if we can make any sort of requests at all, using
275 public static function canMakeRequests() {
276 return function_exists( 'curl_init' ) ||
wfIniGetBool( 'allow_url_fopen' );
280 * Generate a new request object
281 * @param string $url Url to use
282 * @param array $options (optional) extra params to pass (see Http::request())
283 * @throws MWException
284 * @return CurlHttpRequest|PhpHttpRequest
285 * @see MWHttpRequest::__construct
287 public static function factory( $url, $options = null ) {
288 if ( !Http
::$httpEngine ) {
289 Http
::$httpEngine = function_exists( 'curl_init' ) ?
'curl' : 'php';
290 } elseif ( Http
::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
291 throw new MWException( __METHOD__
. ': curl (http://php.net/curl) is not installed, but' .
292 ' Http::$httpEngine is set to "curl"' );
295 switch ( Http
::$httpEngine ) {
297 return new CurlHttpRequest( $url, $options );
299 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
300 throw new MWException( __METHOD__
. ': allow_url_fopen ' .
301 'needs to be enabled for pure PHP http requests to ' .
302 'work. If possible, curl should be used instead. See ' .
303 'http://php.net/curl.'
306 return new PhpHttpRequest( $url, $options );
308 throw new MWException( __METHOD__
. ': The setting of Http::$httpEngine is not valid.' );
313 * Get the body, or content, of the response to the request
317 public function getContent() {
318 return $this->content
;
322 * Set the parameters of the request
325 * @todo overload the args param
327 public function setData( $args ) {
328 $this->postData
= $args;
332 * Take care of setting up the proxy (do nothing if "noProxy" is set)
336 public function proxySetup() {
339 // If there is an explicit proxy set and proxies are not disabled, then use it
340 if ( $this->proxy
&& !$this->noProxy
) {
344 // Otherwise, fallback to $wgHTTPProxy/http_proxy (when set) if this is not a machine
345 // local URL and proxies are not disabled
346 if ( Http
::isLocalURL( $this->url
) ||
$this->noProxy
) {
348 } elseif ( $wgHTTPProxy ) {
349 $this->proxy
= $wgHTTPProxy;
350 } elseif ( getenv( "http_proxy" ) ) {
351 $this->proxy
= getenv( "http_proxy" );
359 public function setUserAgent( $UA ) {
360 $this->setHeader( 'User-Agent', $UA );
364 * Set an arbitrary header
365 * @param string $name
366 * @param string $value
368 public function setHeader( $name, $value ) {
369 // I feel like I should normalize the case here...
370 $this->reqHeaders
[$name] = $value;
374 * Get an array of the headers
377 public function getHeaderList() {
380 if ( $this->cookieJar
) {
381 $this->reqHeaders
['Cookie'] =
382 $this->cookieJar
->serializeToHttpRequest(
383 $this->parsedUrl
['path'],
384 $this->parsedUrl
['host']
388 foreach ( $this->reqHeaders
as $name => $value ) {
389 $list[] = "$name: $value";
396 * Set a read callback to accept data read from the HTTP request.
397 * By default, data is appended to an internal buffer which can be
398 * retrieved through $req->getContent().
400 * To handle data as it comes in -- especially for large files that
401 * would not fit in memory -- you can instead set your own callback,
402 * in the form function($resource, $buffer) where the first parameter
403 * is the low-level resource being read (implementation specific),
404 * and the second parameter is the data buffer.
406 * You MUST return the number of bytes handled in the buffer; if fewer
407 * bytes are reported handled than were passed to you, the HTTP fetch
410 * @param callable $callback
411 * @throws MWException
413 public function setCallback( $callback ) {
414 if ( !is_callable( $callback ) ) {
415 throw new MWException( 'Invalid MwHttpRequest callback' );
417 $this->callback
= $callback;
421 * A generic callback to read the body of the response from a remote
424 * @param resource $fh
425 * @param string $content
428 public function read( $fh, $content ) {
429 $this->content
.= $content;
430 return strlen( $content );
434 * Take care of whatever is necessary to perform the URI request.
438 public function execute() {
439 wfProfileIn( __METHOD__
);
443 if ( strtoupper( $this->method
) == "HEAD" ) {
444 $this->headersOnly
= true;
447 $this->proxySetup(); // set up any proxy as needed
449 if ( !$this->callback
) {
450 $this->setCallback( array( $this, 'read' ) );
453 if ( !isset( $this->reqHeaders
['User-Agent'] ) ) {
454 $this->setUserAgent( Http
::userAgent() );
457 wfProfileOut( __METHOD__
);
461 * Parses the headers, including the HTTP status code and any
462 * Set-Cookie headers. This function expects the headers to be
463 * found in an array in the member variable headerList.
465 protected function parseHeader() {
466 wfProfileIn( __METHOD__
);
470 foreach ( $this->headerList
as $header ) {
471 if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
472 $this->respVersion
= $match[1];
473 $this->respStatus
= $match[2];
474 } elseif ( preg_match( "#^[ \t]#", $header ) ) {
475 $last = count( $this->respHeaders
[$lastname] ) - 1;
476 $this->respHeaders
[$lastname][$last] .= "\r\n$header";
477 } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
478 $this->respHeaders
[strtolower( $match[1] )][] = $match[2];
479 $lastname = strtolower( $match[1] );
483 $this->parseCookies();
485 wfProfileOut( __METHOD__
);
489 * Sets HTTPRequest status member to a fatal value with the error
490 * message if the returned integer value of the status code was
491 * not successful (< 300) or a redirect (>=300 and < 400). (see
492 * RFC2616, section 10,
493 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a
494 * list of status codes.)
496 protected function setStatus() {
497 if ( !$this->respHeaders
) {
498 $this->parseHeader();
501 if ( (int)$this->respStatus
> 399 ) {
502 list( $code, $message ) = explode( " ", $this->respStatus
, 2 );
503 $this->status
->fatal( "http-bad-status", $code, $message );
508 * Get the integer value of the HTTP status code (e.g. 200 for "200 Ok")
509 * (see RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
510 * for a list of status codes.)
514 public function getStatus() {
515 if ( !$this->respHeaders
) {
516 $this->parseHeader();
519 return (int)$this->respStatus
;
523 * Returns true if the last status code was a redirect.
527 public function isRedirect() {
528 if ( !$this->respHeaders
) {
529 $this->parseHeader();
532 $status = (int)$this->respStatus
;
534 if ( $status >= 300 && $status <= 303 ) {
542 * Returns an associative array of response headers after the
543 * request has been executed. Because some headers
544 * (e.g. Set-Cookie) can appear more than once the, each value of
545 * the associative array is an array of the values given.
549 public function getResponseHeaders() {
550 if ( !$this->respHeaders
) {
551 $this->parseHeader();
554 return $this->respHeaders
;
558 * Returns the value of the given response header.
560 * @param string $header
563 public function getResponseHeader( $header ) {
564 if ( !$this->respHeaders
) {
565 $this->parseHeader();
568 if ( isset( $this->respHeaders
[strtolower( $header )] ) ) {
569 $v = $this->respHeaders
[strtolower( $header )];
570 return $v[count( $v ) - 1];
577 * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
579 * @param CookieJar $jar
581 public function setCookieJar( $jar ) {
582 $this->cookieJar
= $jar;
586 * Returns the cookie jar in use.
590 public function getCookieJar() {
591 if ( !$this->respHeaders
) {
592 $this->parseHeader();
595 return $this->cookieJar
;
599 * Sets a cookie. Used before a request to set up any individual
600 * cookies. Used internally after a request to parse the
601 * Set-Cookie headers.
603 * @param string $name
604 * @param mixed $value
607 public function setCookie( $name, $value = null, $attr = null ) {
608 if ( !$this->cookieJar
) {
609 $this->cookieJar
= new CookieJar
;
612 $this->cookieJar
->setCookie( $name, $value, $attr );
616 * Parse the cookies in the response headers and store them in the cookie jar.
618 protected function parseCookies() {
619 wfProfileIn( __METHOD__
);
621 if ( !$this->cookieJar
) {
622 $this->cookieJar
= new CookieJar
;
625 if ( isset( $this->respHeaders
['set-cookie'] ) ) {
626 $url = parse_url( $this->getFinalUrl() );
627 foreach ( $this->respHeaders
['set-cookie'] as $cookie ) {
628 $this->cookieJar
->parseCookieResponseHeader( $cookie, $url['host'] );
632 wfProfileOut( __METHOD__
);
636 * Returns the final URL after all redirections.
638 * Relative values of the "Location" header are incorrect as
639 * stated in RFC, however they do happen and modern browsers
640 * support them. This function loops backwards through all
641 * locations in order to build the proper absolute URI - Marooned
644 * Note that the multiple Location: headers are an artifact of
645 * CURL -- they shouldn't actually get returned this way. Rewrite
646 * this when bug 29232 is taken care of (high-level redirect
651 public function getFinalUrl() {
652 $headers = $this->getResponseHeaders();
654 //return full url (fix for incorrect but handled relative location)
655 if ( isset( $headers['location'] ) ) {
656 $locations = $headers['location'];
658 $foundRelativeURI = false;
659 $countLocations = count( $locations );
661 for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
662 $url = parse_url( $locations[$i] );
664 if ( isset( $url['host'] ) ) {
665 $domain = $url['scheme'] . '://' . $url['host'];
666 break; //found correct URI (with host)
668 $foundRelativeURI = true;
672 if ( $foundRelativeURI ) {
674 return $domain . $locations[$countLocations - 1];
676 $url = parse_url( $this->url
);
677 if ( isset( $url['host'] ) ) {
678 return $url['scheme'] . '://' . $url['host'] .
679 $locations[$countLocations - 1];
683 return $locations[$countLocations - 1];
691 * Returns true if the backend can follow redirects. Overridden by the
695 public function canFollowRedirects() {
701 * MWHttpRequest implemented using internal curl compiled into PHP
703 class CurlHttpRequest
extends MWHttpRequest
{
704 const SUPPORTS_FILE_POSTS
= true;
706 protected $curlOptions = array();
707 protected $headerText = "";
710 * @param resource $fh
711 * @param string $content
714 protected function readHeader( $fh, $content ) {
715 $this->headerText
.= $content;
716 return strlen( $content );
719 public function execute() {
720 wfProfileIn( __METHOD__
);
724 if ( !$this->status
->isOK() ) {
725 wfProfileOut( __METHOD__
);
726 return $this->status
;
729 $this->curlOptions
[CURLOPT_PROXY
] = $this->proxy
;
730 $this->curlOptions
[CURLOPT_TIMEOUT
] = $this->timeout
;
732 // Only supported in curl >= 7.16.2
733 if ( defined( 'CURLOPT_CONNECTTIMEOUT_MS' ) ) {
734 $this->curlOptions
[CURLOPT_CONNECTTIMEOUT_MS
] = $this->connectTimeout
* 1000;
737 $this->curlOptions
[CURLOPT_HTTP_VERSION
] = CURL_HTTP_VERSION_1_0
;
738 $this->curlOptions
[CURLOPT_WRITEFUNCTION
] = $this->callback
;
739 $this->curlOptions
[CURLOPT_HEADERFUNCTION
] = array( $this, "readHeader" );
740 $this->curlOptions
[CURLOPT_MAXREDIRS
] = $this->maxRedirects
;
741 $this->curlOptions
[CURLOPT_ENCODING
] = ""; # Enable compression
743 $this->curlOptions
[CURLOPT_USERAGENT
] = $this->reqHeaders
['User-Agent'];
745 $this->curlOptions
[CURLOPT_SSL_VERIFYHOST
] = $this->sslVerifyHost ?
2 : 0;
746 $this->curlOptions
[CURLOPT_SSL_VERIFYPEER
] = $this->sslVerifyCert
;
748 if ( $this->caInfo
) {
749 $this->curlOptions
[CURLOPT_CAINFO
] = $this->caInfo
;
752 if ( $this->headersOnly
) {
753 $this->curlOptions
[CURLOPT_NOBODY
] = true;
754 $this->curlOptions
[CURLOPT_HEADER
] = true;
755 } elseif ( $this->method
== 'POST' ) {
756 $this->curlOptions
[CURLOPT_POST
] = true;
757 $this->curlOptions
[CURLOPT_POSTFIELDS
] = $this->postData
;
758 // Suppress 'Expect: 100-continue' header, as some servers
759 // will reject it with a 417 and Curl won't auto retry
760 // with HTTP 1.0 fallback
761 $this->reqHeaders
['Expect'] = '';
763 $this->curlOptions
[CURLOPT_CUSTOMREQUEST
] = $this->method
;
766 $this->curlOptions
[CURLOPT_HTTPHEADER
] = $this->getHeaderList();
768 $curlHandle = curl_init( $this->url
);
770 if ( !curl_setopt_array( $curlHandle, $this->curlOptions
) ) {
771 wfProfileOut( __METHOD__
);
772 throw new MWException( "Error setting curl options." );
775 if ( $this->followRedirects
&& $this->canFollowRedirects() ) {
776 wfSuppressWarnings();
777 if ( !curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION
, true ) ) {
778 wfDebug( __METHOD__
. ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
779 "Probably safe_mode or open_basedir is set.\n" );
780 // Continue the processing. If it were in curl_setopt_array,
781 // processing would have halted on its entry
786 $curlRes = curl_exec( $curlHandle );
787 if ( curl_errno( $curlHandle ) == CURLE_OPERATION_TIMEOUTED
) {
788 $this->status
->fatal( 'http-timed-out', $this->url
);
789 } elseif ( $curlRes === false ) {
790 $this->status
->fatal( 'http-curl-error', curl_error( $curlHandle ) );
792 $this->headerList
= explode( "\r\n", $this->headerText
);
795 curl_close( $curlHandle );
797 $this->parseHeader();
800 wfProfileOut( __METHOD__
);
802 return $this->status
;
808 public function canFollowRedirects() {
809 if ( strval( ini_get( 'open_basedir' ) ) !== '' ||
wfIniGetBool( 'safe_mode' ) ) {
810 wfDebug( "Cannot follow redirects in safe mode\n" );
814 $curlVersionInfo = curl_version();
815 if ( $curlVersionInfo['version_number'] < 0x071304 ) {
816 wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
824 class PhpHttpRequest
extends MWHttpRequest
{
830 protected function urlToTcp( $url ) {
831 $parsedUrl = parse_url( $url );
833 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
836 public function execute() {
837 wfProfileIn( __METHOD__
);
841 if ( is_array( $this->postData
) ) {
842 $this->postData
= wfArrayToCgi( $this->postData
);
845 if ( $this->parsedUrl
['scheme'] != 'http'
846 && $this->parsedUrl
['scheme'] != 'https' ) {
847 $this->status
->fatal( 'http-invalid-scheme', $this->parsedUrl
['scheme'] );
850 $this->reqHeaders
['Accept'] = "*/*";
851 $this->reqHeaders
['Connection'] = 'Close';
852 if ( $this->method
== 'POST' ) {
853 // Required for HTTP 1.0 POSTs
854 $this->reqHeaders
['Content-Length'] = strlen( $this->postData
);
855 if ( !isset( $this->reqHeaders
['Content-Type'] ) ) {
856 $this->reqHeaders
['Content-Type'] = "application/x-www-form-urlencoded";
860 // Set up PHP stream context
863 'method' => $this->method
,
864 'header' => implode( "\r\n", $this->getHeaderList() ),
865 'protocol_version' => '1.1',
866 'max_redirects' => $this->followRedirects ?
$this->maxRedirects
: 0,
867 'ignore_errors' => true,
868 'timeout' => $this->timeout
,
869 // Curl options in case curlwrappers are installed
870 'curl_verify_ssl_host' => $this->sslVerifyHost ?
2 : 0,
871 'curl_verify_ssl_peer' => $this->sslVerifyCert
,
874 'verify_peer' => $this->sslVerifyCert
,
875 'SNI_enabled' => true,
879 if ( $this->proxy
) {
880 $options['http']['proxy'] = $this->urlToTCP( $this->proxy
);
881 $options['http']['request_fulluri'] = true;
884 if ( $this->postData
) {
885 $options['http']['content'] = $this->postData
;
888 if ( $this->sslVerifyHost
) {
889 $options['ssl']['CN_match'] = $this->parsedUrl
['host'];
892 if ( is_dir( $this->caInfo
) ) {
893 $options['ssl']['capath'] = $this->caInfo
;
894 } elseif ( is_file( $this->caInfo
) ) {
895 $options['ssl']['cafile'] = $this->caInfo
;
896 } elseif ( $this->caInfo
) {
897 throw new MWException( "Invalid CA info passed: {$this->caInfo}" );
900 $context = stream_context_create( $options );
902 $this->headerList
= array();
910 wfSuppressWarnings();
911 $fh = fopen( $url, "r", false, $context );
918 $result = stream_get_meta_data( $fh );
919 $this->headerList
= $result['wrapper_data'];
920 $this->parseHeader();
922 if ( !$this->followRedirects
) {
926 # Handle manual redirection
927 if ( !$this->isRedirect() ||
$reqCount > $this->maxRedirects
) {
930 # Check security of URL
931 $url = $this->getResponseHeader( "Location" );
933 if ( !Http
::isValidURI( $url ) ) {
934 wfDebug( __METHOD__
. ": insecure redirection\n" );
941 if ( $fh === false ) {
942 $this->status
->fatal( 'http-request-error' );
943 wfProfileOut( __METHOD__
);
944 return $this->status
;
947 if ( $result['timed_out'] ) {
948 $this->status
->fatal( 'http-timed-out', $this->url
);
949 wfProfileOut( __METHOD__
);
950 return $this->status
;
953 // If everything went OK, or we received some error code
954 // get the response body content.
955 if ( $this->status
->isOK() ||
(int)$this->respStatus
>= 300 ) {
956 while ( !feof( $fh ) ) {
957 $buf = fread( $fh, 8192 );
959 if ( $buf === false ) {
960 $this->status
->fatal( 'http-read-error' );
964 if ( strlen( $buf ) ) {
965 call_user_func( $this->callback
, $fh, $buf );
971 wfProfileOut( __METHOD__
);
973 return $this->status
;