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()
59 public static function get( $url, $timeout = 'default', $options = array() ) {
60 $options['timeout'] = $timeout;
61 return Http
::request( 'GET', $url, $options );
65 * Simple wrapper for Http::request( 'POST' )
66 * @see Http::request()
70 public static function post( $url, $options = array() ) {
71 return Http
::request( 'POST', $url, $options );
75 * Check if the URL can be served by localhost
77 * @param $url String: full url to check
80 public static function isLocalURL( $url ) {
81 global $wgCommandLineMode, $wgConf;
83 if ( $wgCommandLineMode ) {
89 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
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 );
97 for ( $i = 0; $i < count( $domainParts ); $i++
) {
98 $domainPart = $domainParts[$i];
100 $domain = $domainPart;
102 $domain = $domainPart . '.' . $domain;
105 if ( $wgConf->isLocalVHost( $domain ) ) {
115 * A standard user-agent we can use for external requests.
118 public static function userAgent() {
120 return "MediaWiki/$wgVersion";
124 * Checks that the given URI is a valid one. Hardcoding the
125 * protocols, because we only want protocols that both cURL
128 * @fixme this is wildly inaccurate and fails to actually check most stuff
130 * @param $uri Mixed: URI to check for validity
133 public static function isValidURI( $uri ) {
135 '/^https?:\/\/[^\/\s]\S*$/D',
142 * This wrapper class will call out to curl (if available) or fallback
143 * to regular PHP if necessary for handling internal HTTP requests.
145 * Renamed from HttpRequest to MWHttpRequest to avoid conflict with
146 * PHP's HTTP extension.
148 class MWHttpRequest
{
149 const SUPPORTS_FILE_POSTS
= false;
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();
163 protected $parsedUrl;
165 protected $maxRedirects = 5;
166 protected $followRedirects = false;
171 protected $cookieJar;
173 protected $headerList = array();
174 protected $respVersion = "0.9";
175 protected $respStatus = "200 Ok";
176 protected $respHeaders = array();
181 * @param $url String: url to use
182 * @param $options Array: (optional) extra params to pass (see Http::request())
184 function __construct( $url, $options = array() ) {
185 global $wgHTTPTimeout;
188 $this->parsedUrl
= parse_url( $url );
190 if ( !Http
::isValidURI( $this->url
) ) {
191 $this->status
= Status
::newFatal( 'http-invalid-url' );
193 $this->status
= Status
::newGood( 100 ); // continue
196 if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
197 $this->timeout
= $options['timeout'];
199 $this->timeout
= $wgHTTPTimeout;
202 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
203 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
205 foreach ( $members as $o ) {
206 if ( isset( $options[$o] ) ) {
207 $this->$o = $options[$o];
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
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"' );
226 switch( Http
::$httpEngine ) {
228 return new CurlHttpRequest( $url, $options );
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.' );
234 return new PhpHttpRequest( $url, $options );
236 throw new MWException( __METHOD__
. ': The setting of Http::$httpEngine is not valid.' );
241 * Get the body, or content, of the response to the request
245 public function getContent() {
246 return $this->content
;
250 * Set the parameters of the request
253 * @todo overload the args param
255 public function setData( $args ) {
256 $this->postData
= $args;
260 * Take care of setting up the proxy
261 * (override in subclass)
265 public function proxySetup() {
268 if ( $this->proxy
) {
272 if ( Http
::isLocalURL( $this->url
) ) {
273 $this->proxy
= 'http://localhost:80/';
274 } elseif ( $wgHTTPProxy ) {
275 $this->proxy
= $wgHTTPProxy ;
276 } elseif ( $this->useProxy( $this->url
) ) {
277 $this->proxy
= $this->useProxy( $this->url
);
282 * Determine HTTP proxy from environment settings respecting
283 * 'http_proxy' and 'no_proxy' environment variables
285 public static function useProxy( $url ) {
286 if ( $proxy = getenv( "http_proxy" ) ) {
288 if ( $url && ( $noproxy = preg_split( "#\s*,\s*#is", getenv( "no_proxy" ) ) ) ) {
289 foreach ( $noproxy as $n ) {
290 if ( preg_match('#(\d+)\.(\d+)\.(\d+)\.(\d+)/(\d+)#s', $n, $m) &&
291 preg_match('#^[a-z0-9_]+://(?:[^/]*:[^/]*@)?([^/@]+)(?:/|$|\?)#is', $url, $ip) ) {
293 max( 0x100 - ( 1 << max( 8-$m[5], 0 ) ), 0 ),
294 max( 0x100 - ( 1 << max( 16-$m[5], 0 ) ), 0 ),
295 max( 0x100 - ( 1 << max( 24-$m[5], 0 ) ), 0 ),
296 max( 0x100 - ( 1 << max( 32-$m[5], 0 ) ), 0 ),
298 $ip = @gethostbyname
( $ip[1] );
299 if ( preg_match( '#(\d+)\.(\d+)\.(\d+)\.(\d+)#s', $ip, $ipm ) &&
300 ( intval( $ipm[1] ) & $mask[0] ) == intval( $m[1] ) &&
301 ( intval( $ipm[2] ) & $mask[1] ) == intval( $m[2] ) &&
302 ( intval( $ipm[3] ) & $mask[2] ) == intval( $m[3] ) &&
303 ( intval( $ipm[4] ) & $mask[3] ) == intval( $m[4] ) ) {
308 $n = preg_replace( '/#.*$/is', '', $n );
309 $n = preg_quote( $n );
310 $n = str_replace( '\\*', '.*', $n );
311 if ( preg_match( '#'.$n.'#is', $url ) ) {
319 $proxy = preg_replace( '#^http://#is', '', $proxy );
320 $proxy = preg_replace( '#/*$#is', '', $proxy );
331 * Set the refererer header
333 public function setReferer( $url ) {
334 $this->setHeader( 'Referer', $url );
340 public function setUserAgent( $UA ) {
341 $this->setHeader( 'User-Agent', $UA );
345 * Set an arbitrary header
347 public function setHeader( $name, $value ) {
348 // I feel like I should normalize the case here...
349 $this->reqHeaders
[$name] = $value;
353 * Get an array of the headers
355 public function getHeaderList() {
358 if ( $this->cookieJar
) {
359 $this->reqHeaders
['Cookie'] =
360 $this->cookieJar
->serializeToHttpRequest(
361 $this->parsedUrl
['path'],
362 $this->parsedUrl
['host']
366 foreach ( $this->reqHeaders
as $name => $value ) {
367 $list[] = "$name: $value";
374 * Set a read callback to accept data read from the HTTP request.
375 * By default, data is appended to an internal buffer which can be
376 * retrieved through $req->getContent().
378 * To handle data as it comes in -- especially for large files that
379 * would not fit in memory -- you can instead set your own callback,
380 * in the form function($resource, $buffer) where the first parameter
381 * is the low-level resource being read (implementation specific),
382 * and the second parameter is the data buffer.
384 * You MUST return the number of bytes handled in the buffer; if fewer
385 * bytes are reported handled than were passed to you, the HTTP fetch
388 * @param $callback Callback
390 public function setCallback( $callback ) {
391 if ( !is_callable( $callback ) ) {
392 throw new MWException( 'Invalid MwHttpRequest callback' );
394 $this->callback
= $callback;
398 * A generic callback to read the body of the response from a remote
402 * @param $content String
404 public function read( $fh, $content ) {
405 $this->content
.= $content;
406 return strlen( $content );
410 * Take care of whatever is necessary to perform the URI request.
414 public function execute() {
419 if ( strtoupper( $this->method
) == "HEAD" ) {
420 $this->headersOnly
= true;
423 if ( is_object( $wgTitle ) && !isset( $this->reqHeaders
['Referer'] ) ) {
424 $this->setReferer( $wgTitle->getFullURL() );
427 if ( !$this->noProxy
) {
431 if ( !$this->callback
) {
432 $this->setCallback( array( $this, 'read' ) );
435 if ( !isset( $this->reqHeaders
['User-Agent'] ) ) {
436 $this->setUserAgent( Http
::userAgent() );
441 * Parses the headers, including the HTTP status code and any
442 * Set-Cookie headers. This function expectes the headers to be
443 * found in an array in the member variable headerList.
447 protected function parseHeader() {
450 foreach ( $this->headerList
as $header ) {
451 if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
452 $this->respVersion
= $match[1];
453 $this->respStatus
= $match[2];
454 } elseif ( preg_match( "#^[ \t]#", $header ) ) {
455 $last = count( $this->respHeaders
[$lastname] ) - 1;
456 $this->respHeaders
[$lastname][$last] .= "\r\n$header";
457 } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
458 $this->respHeaders
[strtolower( $match[1] )][] = $match[2];
459 $lastname = strtolower( $match[1] );
463 $this->parseCookies();
467 * Sets HTTPRequest status member to a fatal value with the error
468 * message if the returned integer value of the status code was
469 * not successful (< 300) or a redirect (>=300 and < 400). (see
470 * RFC2616, section 10,
471 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a
472 * list of status codes.)
476 protected function setStatus() {
477 if ( !$this->respHeaders
) {
478 $this->parseHeader();
481 if ( (int)$this->respStatus
> 399 ) {
482 list( $code, $message ) = explode( " ", $this->respStatus
, 2 );
483 $this->status
->fatal( "http-bad-status", $code, $message );
488 * Get the integer value of the HTTP status code (e.g. 200 for "200 Ok")
489 * (see RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
490 * for a list of status codes.)
494 public function getStatus() {
495 if ( !$this->respHeaders
) {
496 $this->parseHeader();
499 return (int)$this->respStatus
;
504 * Returns true if the last status code was a redirect.
508 public function isRedirect() {
509 if ( !$this->respHeaders
) {
510 $this->parseHeader();
513 $status = (int)$this->respStatus
;
515 if ( $status >= 300 && $status <= 303 ) {
523 * Returns an associative array of response headers after the
524 * request has been executed. Because some headers
525 * (e.g. Set-Cookie) can appear more than once the, each value of
526 * the associative array is an array of the values given.
530 public function getResponseHeaders() {
531 if ( !$this->respHeaders
) {
532 $this->parseHeader();
535 return $this->respHeaders
;
539 * Returns the value of the given response header.
541 * @param $header String
544 public function getResponseHeader( $header ) {
545 if ( !$this->respHeaders
) {
546 $this->parseHeader();
549 if ( isset( $this->respHeaders
[strtolower ( $header ) ] ) ) {
550 $v = $this->respHeaders
[strtolower ( $header ) ];
551 return $v[count( $v ) - 1];
558 * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
560 * @param $jar CookieJar
562 public function setCookieJar( $jar ) {
563 $this->cookieJar
= $jar;
567 * Returns the cookie jar in use.
571 public function getCookieJar() {
572 if ( !$this->respHeaders
) {
573 $this->parseHeader();
576 return $this->cookieJar
;
580 * Sets a cookie. Used before a request to set up any individual
581 * cookies. Used internally after a request to parse the
582 * Set-Cookie headers.
585 public function setCookie( $name, $value = null, $attr = null ) {
586 if ( !$this->cookieJar
) {
587 $this->cookieJar
= new CookieJar
;
590 $this->cookieJar
->setCookie( $name, $value, $attr );
594 * Parse the cookies in the response headers and store them in the cookie jar.
596 protected function parseCookies() {
597 if ( !$this->cookieJar
) {
598 $this->cookieJar
= new CookieJar
;
601 if ( isset( $this->respHeaders
['set-cookie'] ) ) {
602 $url = parse_url( $this->getFinalUrl() );
603 foreach ( $this->respHeaders
['set-cookie'] as $cookie ) {
604 $this->cookieJar
->parseCookieResponseHeader( $cookie, $url['host'] );
610 * Returns the final URL after all redirections.
614 public function getFinalUrl() {
615 $location = $this->getResponseHeader( "Location" );
625 * Returns true if the backend can follow redirects. Overridden by the
628 public function canFollowRedirects() {
634 * MWHttpRequest implemented using internal curl compiled into PHP
636 class CurlHttpRequest
extends MWHttpRequest
{
637 const SUPPORTS_FILE_POSTS
= true;
639 static $curlMessageMap = array(
640 6 => 'http-host-unreachable',
641 28 => 'http-timed-out'
644 protected $curlOptions = array();
645 protected $headerText = "";
647 protected function readHeader( $fh, $content ) {
648 $this->headerText
.= $content;
649 return strlen( $content );
652 public function execute() {
655 if ( !$this->status
->isOK() ) {
656 return $this->status
;
659 $this->curlOptions
[CURLOPT_PROXY
] = $this->proxy
;
660 $this->curlOptions
[CURLOPT_TIMEOUT
] = $this->timeout
;
661 $this->curlOptions
[CURLOPT_HTTP_VERSION
] = CURL_HTTP_VERSION_1_0
;
662 $this->curlOptions
[CURLOPT_WRITEFUNCTION
] = $this->callback
;
663 $this->curlOptions
[CURLOPT_HEADERFUNCTION
] = array( $this, "readHeader" );
664 $this->curlOptions
[CURLOPT_MAXREDIRS
] = $this->maxRedirects
;
665 $this->curlOptions
[CURLOPT_ENCODING
] = ""; # Enable compression
667 /* not sure these two are actually necessary */
668 if ( isset( $this->reqHeaders
['Referer'] ) ) {
669 $this->curlOptions
[CURLOPT_REFERER
] = $this->reqHeaders
['Referer'];
671 $this->curlOptions
[CURLOPT_USERAGENT
] = $this->reqHeaders
['User-Agent'];
673 if ( isset( $this->sslVerifyHost
) ) {
674 $this->curlOptions
[CURLOPT_SSL_VERIFYHOST
] = $this->sslVerifyHost
;
677 if ( isset( $this->sslVerifyCert
) ) {
678 $this->curlOptions
[CURLOPT_SSL_VERIFYPEER
] = $this->sslVerifyCert
;
681 if ( $this->caInfo
) {
682 $this->curlOptions
[CURLOPT_CAINFO
] = $this->caInfo
;
685 if ( $this->headersOnly
) {
686 $this->curlOptions
[CURLOPT_NOBODY
] = true;
687 $this->curlOptions
[CURLOPT_HEADER
] = true;
688 } elseif ( $this->method
== 'POST' ) {
689 $this->curlOptions
[CURLOPT_POST
] = true;
690 $this->curlOptions
[CURLOPT_POSTFIELDS
] = $this->postData
;
691 // Suppress 'Expect: 100-continue' header, as some servers
692 // will reject it with a 417 and Curl won't auto retry
693 // with HTTP 1.0 fallback
694 $this->reqHeaders
['Expect'] = '';
696 $this->curlOptions
[CURLOPT_CUSTOMREQUEST
] = $this->method
;
699 $this->curlOptions
[CURLOPT_HTTPHEADER
] = $this->getHeaderList();
701 $curlHandle = curl_init( $this->url
);
703 if ( !curl_setopt_array( $curlHandle, $this->curlOptions
) ) {
704 throw new MWException( "Error setting curl options." );
707 if ( $this->followRedirects
&& $this->canFollowRedirects() ) {
708 wfSuppressWarnings();
709 if ( ! curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION
, true ) ) {
710 wfDebug( __METHOD__
. ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
711 "Probably safe_mode or open_basedir is set.\n" );
712 // Continue the processing. If it were in curl_setopt_array,
713 // processing would have halted on its entry
718 if ( false === curl_exec( $curlHandle ) ) {
719 $code = curl_error( $curlHandle );
721 if ( isset( self
::$curlMessageMap[$code] ) ) {
722 $this->status
->fatal( self
::$curlMessageMap[$code] );
724 $this->status
->fatal( 'http-curl-error', curl_error( $curlHandle ) );
727 $this->headerList
= explode( "\r\n", $this->headerText
);
730 curl_close( $curlHandle );
732 $this->parseHeader();
735 return $this->status
;
738 public function canFollowRedirects() {
739 if ( strval( ini_get( 'open_basedir' ) ) !== '' ||
wfIniGetBool( 'safe_mode' ) ) {
740 wfDebug( "Cannot follow redirects in safe mode\n" );
744 if ( !defined( 'CURLOPT_REDIR_PROTOCOLS' ) ) {
745 wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
753 class PhpHttpRequest
extends MWHttpRequest
{
754 protected function urlToTcp( $url ) {
755 $parsedUrl = parse_url( $url );
757 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
760 public function execute() {
763 if ( is_array( $this->postData
) ) {
764 $this->postData
= wfArrayToCGI( $this->postData
);
767 if ( $this->parsedUrl
['scheme'] != 'http' &&
768 $this->parsedUrl
['scheme'] != 'https' ) {
769 $this->status
->fatal( 'http-invalid-scheme', $this->parsedUrl
['scheme'] );
772 $this->reqHeaders
['Accept'] = "*/*";
773 if ( $this->method
== 'POST' ) {
774 // Required for HTTP 1.0 POSTs
775 $this->reqHeaders
['Content-Length'] = strlen( $this->postData
);
776 $this->reqHeaders
['Content-type'] = "application/x-www-form-urlencoded";
780 if ( $this->proxy
&& !$this->noProxy
) {
781 $options['proxy'] = $this->urlToTCP( $this->proxy
);
782 $options['request_fulluri'] = true;
785 if ( !$this->followRedirects
) {
786 $options['max_redirects'] = 0;
788 $options['max_redirects'] = $this->maxRedirects
;
791 $options['method'] = $this->method
;
792 $options['header'] = implode( "\r\n", $this->getHeaderList() );
793 // Note that at some future point we may want to support
794 // HTTP/1.1, but we'd have to write support for chunking
795 // in version of PHP < 5.3.1
796 $options['protocol_version'] = "1.0";
798 // This is how we tell PHP we want to deal with 404s (for example) ourselves.
799 // Only works on 5.2.10+
800 $options['ignore_errors'] = true;
802 if ( $this->postData
) {
803 $options['content'] = $this->postData
;
806 $options['timeout'] = $this->timeout
;
808 $context = stream_context_create( array( 'http' => $options ) );
810 $this->headerList
= array();
818 wfSuppressWarnings();
819 $fh = fopen( $url, "r", false, $context );
826 $result = stream_get_meta_data( $fh );
827 $this->headerList
= $result['wrapper_data'];
828 $this->parseHeader();
830 if ( !$this->followRedirects
) {
834 # Handle manual redirection
835 if ( !$this->isRedirect() ||
$reqCount > $this->maxRedirects
) {
838 # Check security of URL
839 $url = $this->getResponseHeader( "Location" );
841 if ( substr( $url, 0, 7 ) !== 'http://' ) {
842 wfDebug( __METHOD__
. ": insecure redirection\n" );
849 if ( $fh === false ) {
850 $this->status
->fatal( 'http-request-error' );
851 return $this->status
;
854 if ( $result['timed_out'] ) {
855 $this->status
->fatal( 'http-timed-out', $this->url
);
856 return $this->status
;
859 // If everything went OK, or we recieved some error code
860 // get the response body content.
861 if ( $this->status
->isOK()
862 ||
(int)$this->respStatus
>= 300) {
863 while ( !feof( $fh ) ) {
864 $buf = fread( $fh, 8192 );
866 if ( $buf === false ) {
867 $this->status
->fatal( 'http-read-error' );
871 if ( strlen( $buf ) ) {
872 call_user_func( $this->callback
, $fh, $buf );
878 return $this->status
;