7 * Various HTTP related functions
11 static $httpEngine = false;
14 * Perform an HTTP request
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
34 public static function request( $method, $url, $options = array() ) {
35 $url = wfExpandUrl( $url );
36 wfDebug( "HTTP: $method: $url\n" );
37 $options['method'] = strtoupper( $method );
39 if ( !isset( $options['timeout'] ) ) {
40 $options['timeout'] = 'default';
43 $req = MWHttpRequest
::factory( $url, $options );
44 $status = $req->execute();
46 if ( $status->isOK() ) {
47 return $req->getContent();
54 * Simple wrapper for Http::request( 'GET' )
55 * @see Http::request()
57 public static function get( $url, $timeout = 'default', $options = array() ) {
58 $options['timeout'] = $timeout;
59 return Http
::request( 'GET', $url, $options );
63 * Simple wrapper for Http::request( 'POST' )
64 * @see Http::request()
66 public static function post( $url, $options = array() ) {
67 return Http
::request( 'POST', $url, $options );
71 * Check if the URL can be served by localhost
73 * @param $url String: full url to check
76 public static function isLocalURL( $url ) {
77 global $wgCommandLineMode, $wgConf;
79 if ( $wgCommandLineMode ) {
85 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
88 $domainParts = explode( '.', $host );
89 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
90 $domainParts = array_reverse( $domainParts );
93 for ( $i = 0; $i < count( $domainParts ); $i++
) {
94 $domainPart = $domainParts[$i];
96 $domain = $domainPart;
98 $domain = $domainPart . '.' . $domain;
101 if ( $wgConf->isLocalVHost( $domain ) ) {
111 * A standard user-agent we can use for external requests.
114 public static function userAgent() {
116 return "MediaWiki/$wgVersion";
120 * Checks that the given URI is a valid one. Hardcoding the
121 * protocols, because we only want protocols that both cURL
124 * @param $uri Mixed: URI to check for validity
127 public static function isValidURI( $uri ) {
129 '/^(f|ht)tps?:\/\/[^\/\s]\S*$/D',
136 * This wrapper class will call out to curl (if available) or fallback
137 * to regular PHP if necessary for handling internal HTTP requests.
139 * Renamed from HttpRequest to MWHttpRequst to avoid conflict with
140 * php's HTTP extension.
142 class MWHttpRequest
{
143 const SUPPORTS_FILE_POSTS
= false;
146 protected $timeout = 'default';
147 protected $headersOnly = null;
148 protected $postData = null;
149 protected $proxy = null;
150 protected $noProxy = false;
151 protected $sslVerifyHost = true;
152 protected $sslVerifyCert = true;
153 protected $caInfo = null;
154 protected $method = "GET";
155 protected $reqHeaders = array();
157 protected $parsedUrl;
159 protected $maxRedirects = 5;
160 protected $followRedirects = false;
165 protected $cookieJar;
167 protected $headerList = array();
168 protected $respVersion = "0.9";
169 protected $respStatus = "200 Ok";
170 protected $respHeaders = array();
175 * @param $url String: url to use
176 * @param $options Array: (optional) extra params to pass (see Http::request())
178 function __construct( $url, $options = array() ) {
179 global $wgHTTPTimeout;
182 $this->parsedUrl
= parse_url( $url );
184 if ( !Http
::isValidURI( $this->url
) ) {
185 $this->status
= Status
::newFatal( 'http-invalid-url' );
187 $this->status
= Status
::newGood( 100 ); // continue
190 if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
191 $this->timeout
= $options['timeout'];
193 $this->timeout
= $wgHTTPTimeout;
196 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
197 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
199 foreach ( $members as $o ) {
200 if ( isset( $options[$o] ) ) {
201 $this->$o = $options[$o];
207 * Generate a new request object
208 * @param $url String: url to use
209 * @param $options Array: (optional) extra params to pass (see Http::request())
210 * @see MWHttpRequest::__construct
212 public static function factory( $url, $options = null ) {
213 if ( !Http
::$httpEngine ) {
214 Http
::$httpEngine = function_exists( 'curl_init' ) ?
'curl' : 'php';
215 } elseif ( Http
::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
216 throw new MWException( __METHOD__
. ': curl (http://php.net/curl) is not installed, but' .
217 ' Http::$httpEngine is set to "curl"' );
220 switch( Http
::$httpEngine ) {
222 return new CurlHttpRequest( $url, $options );
224 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
225 throw new MWException( __METHOD__
. ': allow_url_fopen needs to be enabled for pure PHP' .
226 ' http requests to work. If possible, curl should be used instead. See http://php.net/curl.' );
228 return new PhpHttpRequest( $url, $options );
230 throw new MWException( __METHOD__
. ': The setting of Http::$httpEngine is not valid.' );
235 * Get the body, or content, of the response to the request
239 public function getContent() {
240 return $this->content
;
244 * Set the parameters of the request
247 * @todo overload the args param
249 public function setData( $args ) {
250 $this->postData
= $args;
254 * Take care of setting up the proxy
255 * (override in subclass)
259 public function proxySetup() {
262 if ( $this->proxy
) {
266 if ( Http
::isLocalURL( $this->url
) ) {
267 $this->proxy
= 'http://localhost:80/';
268 } elseif ( $wgHTTPProxy ) {
269 $this->proxy
= $wgHTTPProxy ;
270 } elseif ( getenv( "http_proxy" ) ) {
271 $this->proxy
= getenv( "http_proxy" );
276 * Set the refererer header
278 public function setReferer( $url ) {
279 $this->setHeader( 'Referer', $url );
285 public function setUserAgent( $UA ) {
286 $this->setHeader( 'User-Agent', $UA );
290 * Set an arbitrary header
292 public function setHeader( $name, $value ) {
293 // I feel like I should normalize the case here...
294 $this->reqHeaders
[$name] = $value;
298 * Get an array of the headers
300 public function getHeaderList() {
303 if ( $this->cookieJar
) {
304 $this->reqHeaders
['Cookie'] =
305 $this->cookieJar
->serializeToHttpRequest(
306 $this->parsedUrl
['path'],
307 $this->parsedUrl
['host']
311 foreach ( $this->reqHeaders
as $name => $value ) {
312 $list[] = "$name: $value";
321 * @param $callback Callback
323 public function setCallback( $callback ) {
324 $this->callback
= $callback;
328 * A generic callback to read the body of the response from a remote
332 * @param $content String
334 public function read( $fh, $content ) {
335 $this->content
.= $content;
336 return strlen( $content );
340 * Take care of whatever is necessary to perform the URI request.
344 public function execute() {
349 if ( strtoupper( $this->method
) == "HEAD" ) {
350 $this->headersOnly
= true;
353 if ( is_object( $wgTitle ) && !isset( $this->reqHeaders
['Referer'] ) ) {
354 $this->setReferer( $wgTitle->getFullURL() );
357 if ( !$this->noProxy
) {
361 if ( !$this->callback
) {
362 $this->setCallback( array( $this, 'read' ) );
365 if ( !isset( $this->reqHeaders
['User-Agent'] ) ) {
366 $this->setUserAgent( Http
::userAgent() );
371 * Parses the headers, including the HTTP status code and any
372 * Set-Cookie headers. This function expectes the headers to be
373 * found in an array in the member variable headerList.
377 protected function parseHeader() {
380 foreach ( $this->headerList
as $header ) {
381 if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
382 $this->respVersion
= $match[1];
383 $this->respStatus
= $match[2];
384 } elseif ( preg_match( "#^[ \t]#", $header ) ) {
385 $last = count( $this->respHeaders
[$lastname] ) - 1;
386 $this->respHeaders
[$lastname][$last] .= "\r\n$header";
387 } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
388 $this->respHeaders
[strtolower( $match[1] )][] = $match[2];
389 $lastname = strtolower( $match[1] );
393 $this->parseCookies();
397 * Sets HTTPRequest status member to a fatal value with the error
398 * message if the returned integer value of the status code was
399 * not successful (< 300) or a redirect (>=300 and < 400). (see
400 * RFC2616, section 10,
401 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a
402 * list of status codes.)
406 protected function setStatus() {
407 if ( !$this->respHeaders
) {
408 $this->parseHeader();
411 if ( (int)$this->respStatus
> 399 ) {
412 list( $code, $message ) = explode( " ", $this->respStatus
, 2 );
413 $this->status
->fatal( "http-bad-status", $code, $message );
418 * Get the integer value of the HTTP status code (e.g. 200 for "200 Ok")
419 * (see RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
420 * for a list of status codes.)
424 public function getStatus() {
425 if ( !$this->respHeaders
) {
426 $this->parseHeader();
429 return (int)$this->respStatus
;
434 * Returns true if the last status code was a redirect.
438 public function isRedirect() {
439 if ( !$this->respHeaders
) {
440 $this->parseHeader();
443 $status = (int)$this->respStatus
;
445 if ( $status >= 300 && $status <= 303 ) {
453 * Returns an associative array of response headers after the
454 * request has been executed. Because some headers
455 * (e.g. Set-Cookie) can appear more than once the, each value of
456 * the associative array is an array of the values given.
460 public function getResponseHeaders() {
461 if ( !$this->respHeaders
) {
462 $this->parseHeader();
465 return $this->respHeaders
;
469 * Returns the value of the given response header.
471 * @param $header String
474 public function getResponseHeader( $header ) {
475 if ( !$this->respHeaders
) {
476 $this->parseHeader();
479 if ( isset( $this->respHeaders
[strtolower ( $header ) ] ) ) {
480 $v = $this->respHeaders
[strtolower ( $header ) ];
481 return $v[count( $v ) - 1];
488 * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
490 * @param $jar CookieJar
492 public function setCookieJar( $jar ) {
493 $this->cookieJar
= $jar;
497 * Returns the cookie jar in use.
501 public function getCookieJar() {
502 if ( !$this->respHeaders
) {
503 $this->parseHeader();
506 return $this->cookieJar
;
510 * Sets a cookie. Used before a request to set up any individual
511 * cookies. Used internally after a request to parse the
512 * Set-Cookie headers.
515 public function setCookie( $name, $value = null, $attr = null ) {
516 if ( !$this->cookieJar
) {
517 $this->cookieJar
= new CookieJar
;
520 $this->cookieJar
->setCookie( $name, $value, $attr );
524 * Parse the cookies in the response headers and store them in the cookie jar.
526 protected function parseCookies() {
527 if ( !$this->cookieJar
) {
528 $this->cookieJar
= new CookieJar
;
531 if ( isset( $this->respHeaders
['set-cookie'] ) ) {
532 $url = parse_url( $this->getFinalUrl() );
533 foreach ( $this->respHeaders
['set-cookie'] as $cookie ) {
534 $this->cookieJar
->parseCookieResponseHeader( $cookie, $url['host'] );
540 * Returns the final URL after all redirections.
544 public function getFinalUrl() {
545 $location = $this->getResponseHeader( "Location" );
555 * Returns true if the backend can follow redirects. Overridden by the
558 public function canFollowRedirects() {
570 protected $isSessionKey = true;
571 // TO IMPLEMENT protected $secure
572 // TO IMPLEMENT? protected $maxAge (add onto expires)
573 // TO IMPLEMENT? protected $version
574 // TO IMPLEMENT? protected $comment
576 function __construct( $name, $value, $attr ) {
578 $this->set( $value, $attr );
582 * Sets a cookie. Used before a request to set up any individual
583 * cookies. Used internally after a request to parse the
584 * Set-Cookie headers.
586 * @param $value String: the value of the cookie
587 * @param $attr Array: possible key/values:
588 * expires A date string
589 * path The path this cookie is used on
590 * domain Domain this cookie is used on
592 public function set( $value, $attr ) {
593 $this->value
= $value;
595 if ( isset( $attr['expires'] ) ) {
596 $this->isSessionKey
= false;
597 $this->expires
= strtotime( $attr['expires'] );
600 if ( isset( $attr['path'] ) ) {
601 $this->path
= $attr['path'];
606 if ( isset( $attr['domain'] ) ) {
607 if ( self
::validateCookieDomain( $attr['domain'] ) ) {
608 $this->domain
= $attr['domain'];
611 throw new MWException( "You must specify a domain." );
616 * Return the true if the cookie is valid is valid. Otherwise,
617 * false. The uses a method similar to IE cookie security
619 * http://kuza55.blogspot.com/2008/02/understanding-cookie-security.html
620 * A better method might be to use a blacklist like
621 * http://publicsuffix.org/
623 * @param $domain String: the domain to validate
624 * @param $originDomain String: (optional) the domain the cookie originates from
627 public static function validateCookieDomain( $domain, $originDomain = null ) {
628 // Don't allow a trailing dot
629 if ( substr( $domain, -1 ) == "." ) {
633 $dc = explode( ".", $domain );
635 // Only allow full, valid IP addresses
636 if ( preg_match( '/^[0-9.]+$/', $domain ) ) {
637 if ( count( $dc ) != 4 ) {
641 if ( ip2long( $domain ) === false ) {
645 if ( $originDomain == null ||
$originDomain == $domain ) {
651 // Don't allow cookies for "co.uk" or "gov.uk", etc, but allow "supermarket.uk"
652 if ( strrpos( $domain, "." ) - strlen( $domain ) == -3 ) {
653 if ( ( count( $dc ) == 2 && strlen( $dc[0] ) <= 2 )
654 ||
( count( $dc ) == 3 && strlen( $dc[0] ) == "" && strlen( $dc[1] ) <= 2 ) ) {
657 if ( ( count( $dc ) == 2 ||
( count( $dc ) == 3 && $dc[0] == "" ) )
658 && preg_match( '/(com|net|org|gov|edu)\...$/', $domain ) ) {
663 if ( $originDomain != null ) {
664 if ( substr( $domain, 0, 1 ) != "." && $domain != $originDomain ) {
668 if ( substr( $domain, 0, 1 ) == "."
669 && substr_compare( $originDomain, $domain, -strlen( $domain ),
670 strlen( $domain ), TRUE ) != 0 ) {
679 * Serialize the cookie jar into a format useful for HTTP Request headers.
681 * @param $path String: the path that will be used. Required.
682 * @param $domain String: the domain that will be used. Required.
685 public function serializeToHttpRequest( $path, $domain ) {
688 if ( $this->canServeDomain( $domain )
689 && $this->canServePath( $path )
690 && $this->isUnExpired() ) {
691 $ret = $this->name
. "=" . $this->value
;
697 protected function canServeDomain( $domain ) {
698 if ( $domain == $this->domain
699 ||
( strlen( $domain ) > strlen( $this->domain
)
700 && substr( $this->domain
, 0, 1 ) == "."
701 && substr_compare( $domain, $this->domain
, -strlen( $this->domain
),
702 strlen( $this->domain
), TRUE ) == 0 ) ) {
709 protected function canServePath( $path ) {
710 if ( $this->path
&& substr_compare( $this->path
, $path, 0, strlen( $this->path
) ) == 0 ) {
717 protected function isUnExpired() {
718 if ( $this->isSessionKey ||
$this->expires
> time() ) {
727 private $cookie = array();
730 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
733 public function setCookie ( $name, $value, $attr ) {
734 /* cookies: case insensitive, so this should work.
735 * We'll still send the cookies back in the same case we got them, though.
737 $index = strtoupper( $name );
739 if ( isset( $this->cookie
[$index] ) ) {
740 $this->cookie
[$index]->set( $value, $attr );
742 $this->cookie
[$index] = new Cookie( $name, $value, $attr );
747 * @see Cookie::serializeToHttpRequest
749 public function serializeToHttpRequest( $path, $domain ) {
752 foreach ( $this->cookie
as $c ) {
753 $serialized = $c->serializeToHttpRequest( $path, $domain );
756 $cookies[] = $serialized;
760 return implode( "; ", $cookies );
764 * Parse the content of an Set-Cookie HTTP Response header.
766 * @param $cookie String
767 * @param $domain String: cookie's domain
769 public function parseCookieResponseHeader ( $cookie, $domain ) {
770 $len = strlen( "Set-Cookie:" );
772 if ( substr_compare( "Set-Cookie:", $cookie, 0, $len, TRUE ) === 0 ) {
773 $cookie = substr( $cookie, $len );
776 $bit = array_map( 'trim', explode( ";", $cookie ) );
778 if ( count( $bit ) >= 1 ) {
779 list( $name, $value ) = explode( "=", array_shift( $bit ), 2 );
782 foreach ( $bit as $piece ) {
783 $parts = explode( "=", $piece );
784 if ( count( $parts ) > 1 ) {
785 $attr[strtolower( $parts[0] )] = $parts[1];
787 $attr[strtolower( $parts[0] )] = true;
791 if ( !isset( $attr['domain'] ) ) {
792 $attr['domain'] = $domain;
793 } elseif ( !Cookie
::validateCookieDomain( $attr['domain'], $domain ) ) {
797 $this->setCookie( $name, $value, $attr );
803 * MWHttpRequest implemented using internal curl compiled into PHP
805 class CurlHttpRequest
extends MWHttpRequest
{
806 const SUPPORTS_FILE_POSTS
= true;
808 static $curlMessageMap = array(
809 6 => 'http-host-unreachable',
810 28 => 'http-timed-out'
813 protected $curlOptions = array();
814 protected $headerText = "";
816 protected function readHeader( $fh, $content ) {
817 $this->headerText
.= $content;
818 return strlen( $content );
821 public function execute() {
824 if ( !$this->status
->isOK() ) {
825 return $this->status
;
828 $this->curlOptions
[CURLOPT_PROXY
] = $this->proxy
;
829 $this->curlOptions
[CURLOPT_TIMEOUT
] = $this->timeout
;
830 $this->curlOptions
[CURLOPT_HTTP_VERSION
] = CURL_HTTP_VERSION_1_0
;
831 $this->curlOptions
[CURLOPT_WRITEFUNCTION
] = $this->callback
;
832 $this->curlOptions
[CURLOPT_HEADERFUNCTION
] = array( $this, "readHeader" );
833 $this->curlOptions
[CURLOPT_MAXREDIRS
] = $this->maxRedirects
;
834 $this->curlOptions
[CURLOPT_ENCODING
] = ""; # Enable compression
836 /* not sure these two are actually necessary */
837 if ( isset( $this->reqHeaders
['Referer'] ) ) {
838 $this->curlOptions
[CURLOPT_REFERER
] = $this->reqHeaders
['Referer'];
840 $this->curlOptions
[CURLOPT_USERAGENT
] = $this->reqHeaders
['User-Agent'];
842 if ( isset( $this->sslVerifyHost
) ) {
843 $this->curlOptions
[CURLOPT_SSL_VERIFYHOST
] = $this->sslVerifyHost
;
846 if ( isset( $this->sslVerifyCert
) ) {
847 $this->curlOptions
[CURLOPT_SSL_VERIFYPEER
] = $this->sslVerifyCert
;
850 if ( $this->caInfo
) {
851 $this->curlOptions
[CURLOPT_CAINFO
] = $this->caInfo
;
854 if ( $this->headersOnly
) {
855 $this->curlOptions
[CURLOPT_NOBODY
] = true;
856 $this->curlOptions
[CURLOPT_HEADER
] = true;
857 } elseif ( $this->method
== 'POST' ) {
858 $this->curlOptions
[CURLOPT_POST
] = true;
859 $this->curlOptions
[CURLOPT_POSTFIELDS
] = $this->postData
;
860 // Suppress 'Expect: 100-continue' header, as some servers
861 // will reject it with a 417 and Curl won't auto retry
862 // with HTTP 1.0 fallback
863 $this->reqHeaders
['Expect'] = '';
865 $this->curlOptions
[CURLOPT_CUSTOMREQUEST
] = $this->method
;
868 $this->curlOptions
[CURLOPT_HTTPHEADER
] = $this->getHeaderList();
870 $curlHandle = curl_init( $this->url
);
872 if ( !curl_setopt_array( $curlHandle, $this->curlOptions
) ) {
873 throw new MWException( "Error setting curl options." );
876 if ( $this->followRedirects
&& $this->canFollowRedirects() ) {
877 wfSuppressWarnings();
878 if ( ! curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION
, true ) ) {
879 wfDebug( __METHOD__
. ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
880 "Probably safe_mode or open_basedir is set.\n" );
881 // Continue the processing. If it were in curl_setopt_array,
882 // processing would have halted on its entry
887 if ( false === curl_exec( $curlHandle ) ) {
888 $code = curl_error( $curlHandle );
890 if ( isset( self
::$curlMessageMap[$code] ) ) {
891 $this->status
->fatal( self
::$curlMessageMap[$code] );
893 $this->status
->fatal( 'http-curl-error', curl_error( $curlHandle ) );
896 $this->headerList
= explode( "\r\n", $this->headerText
);
899 curl_close( $curlHandle );
901 $this->parseHeader();
904 return $this->status
;
907 public function canFollowRedirects() {
908 if ( strval( ini_get( 'open_basedir' ) ) !== '' ||
wfIniGetBool( 'safe_mode' ) ) {
909 wfDebug( "Cannot follow redirects in safe mode\n" );
913 if ( !defined( 'CURLOPT_REDIR_PROTOCOLS' ) ) {
914 wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
922 class PhpHttpRequest
extends MWHttpRequest
{
923 protected function urlToTcp( $url ) {
924 $parsedUrl = parse_url( $url );
926 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
929 public function execute() {
932 if ( is_array( $this->postData
) ) {
933 $this->postData
= wfArrayToCGI( $this->postData
);
936 // At least on Centos 4.8 with PHP 5.1.6, using max_redirects to follow redirects
938 $manuallyRedirect = version_compare( phpversion(), '5.1.7', '<' );
940 if ( $this->parsedUrl
['scheme'] != 'http' ) {
941 $this->status
->fatal( 'http-invalid-scheme', $this->parsedUrl
['scheme'] );
944 $this->reqHeaders
['Accept'] = "*/*";
945 if ( $this->method
== 'POST' ) {
946 // Required for HTTP 1.0 POSTs
947 $this->reqHeaders
['Content-Length'] = strlen( $this->postData
);
948 $this->reqHeaders
['Content-type'] = "application/x-www-form-urlencoded";
952 if ( $this->proxy
&& !$this->noProxy
) {
953 $options['proxy'] = $this->urlToTCP( $this->proxy
);
954 $options['request_fulluri'] = true;
957 if ( !$this->followRedirects ||
$manuallyRedirect ) {
958 $options['max_redirects'] = 0;
960 $options['max_redirects'] = $this->maxRedirects
;
963 $options['method'] = $this->method
;
964 $options['header'] = implode( "\r\n", $this->getHeaderList() );
965 // Note that at some future point we may want to support
966 // HTTP/1.1, but we'd have to write support for chunking
967 // in version of PHP < 5.3.1
968 $options['protocol_version'] = "1.0";
970 // This is how we tell PHP we want to deal with 404s (for example) ourselves.
971 // Only works on 5.2.10+
972 $options['ignore_errors'] = true;
974 if ( $this->postData
) {
975 $options['content'] = $this->postData
;
979 if ( version_compare( '5.2.1', phpversion(), '>' ) ) {
980 $oldTimeout = ini_set( 'default_socket_timeout', $this->timeout
);
982 $options['timeout'] = $this->timeout
;
985 $context = stream_context_create( array( 'http' => $options ) );
987 $this->headerList
= array();
995 wfSuppressWarnings();
996 $fh = fopen( $url, "r", false, $context );
1003 $result = stream_get_meta_data( $fh );
1004 $this->headerList
= $result['wrapper_data'];
1005 $this->parseHeader();
1007 if ( !$manuallyRedirect ||
!$this->followRedirects
) {
1011 # Handle manual redirection
1012 if ( !$this->isRedirect() ||
$reqCount > $this->maxRedirects
) {
1015 # Check security of URL
1016 $url = $this->getResponseHeader( "Location" );
1018 if ( substr( $url, 0, 7 ) !== 'http://' ) {
1019 wfDebug( __METHOD__
. ": insecure redirection\n" );
1024 if ( $oldTimeout !== false ) {
1025 ini_set( 'default_socket_timeout', $oldTimeout );
1030 if ( $fh === false ) {
1031 $this->status
->fatal( 'http-request-error' );
1032 return $this->status
;
1035 if ( $result['timed_out'] ) {
1036 $this->status
->fatal( 'http-timed-out', $this->url
);
1037 return $this->status
;
1040 if ( $this->status
->isOK() ) {
1041 while ( !feof( $fh ) ) {
1042 $buf = fread( $fh, 8192 );
1044 if ( $buf === false ) {
1045 $this->status
->fatal( 'http-read-error' );
1049 if ( strlen( $buf ) ) {
1050 call_user_func( $this->callback
, $fh, $buf );
1056 return $this->status
;