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 * @param $uri Mixed: URI to check for validity
131 public static function isValidURI( $uri ) {
133 '/^(f|ht)tps?:\/\/[^\/\s]\S*$/D',
140 * This wrapper class will call out to curl (if available) or fallback
141 * to regular PHP if necessary for handling internal HTTP requests.
143 * Renamed from HttpRequest to MWHttpRequest to avoid conflict with
144 * PHP's HTTP extension.
146 class MWHttpRequest
{
147 const SUPPORTS_FILE_POSTS
= false;
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();
161 protected $parsedUrl;
163 protected $maxRedirects = 5;
164 protected $followRedirects = false;
169 protected $cookieJar;
171 protected $headerList = array();
172 protected $respVersion = "0.9";
173 protected $respStatus = "200 Ok";
174 protected $respHeaders = array();
179 * @param $url String: url to use
180 * @param $options Array: (optional) extra params to pass (see Http::request())
182 function __construct( $url, $options = array() ) {
183 global $wgHTTPTimeout;
186 $this->parsedUrl
= parse_url( $url );
188 if ( !Http
::isValidURI( $this->url
) ) {
189 $this->status
= Status
::newFatal( 'http-invalid-url' );
191 $this->status
= Status
::newGood( 100 ); // continue
194 if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
195 $this->timeout
= $options['timeout'];
197 $this->timeout
= $wgHTTPTimeout;
200 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
201 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
203 foreach ( $members as $o ) {
204 if ( isset( $options[$o] ) ) {
205 $this->$o = $options[$o];
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
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"' );
224 switch( Http
::$httpEngine ) {
226 return new CurlHttpRequest( $url, $options );
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.' );
232 return new PhpHttpRequest( $url, $options );
234 throw new MWException( __METHOD__
. ': The setting of Http::$httpEngine is not valid.' );
239 * Get the body, or content, of the response to the request
243 public function getContent() {
244 return $this->content
;
248 * Set the parameters of the request
251 * @todo overload the args param
253 public function setData( $args ) {
254 $this->postData
= $args;
258 * Take care of setting up the proxy
259 * (override in subclass)
263 public function proxySetup() {
266 if ( $this->proxy
) {
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" );
280 * Set the refererer header
282 public function setReferer( $url ) {
283 $this->setHeader( 'Referer', $url );
289 public function setUserAgent( $UA ) {
290 $this->setHeader( 'User-Agent', $UA );
294 * Set an arbitrary header
296 public function setHeader( $name, $value ) {
297 // I feel like I should normalize the case here...
298 $this->reqHeaders
[$name] = $value;
302 * Get an array of the headers
304 public function getHeaderList() {
307 if ( $this->cookieJar
) {
308 $this->reqHeaders
['Cookie'] =
309 $this->cookieJar
->serializeToHttpRequest(
310 $this->parsedUrl
['path'],
311 $this->parsedUrl
['host']
315 foreach ( $this->reqHeaders
as $name => $value ) {
316 $list[] = "$name: $value";
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().
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.
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
337 * @param $callback Callback
339 public function setCallback( $callback ) {
340 if ( !is_callable( $callback ) ) {
341 throw new MWException( 'Invalid MwHttpRequest callback' );
343 $this->callback
= $callback;
347 * A generic callback to read the body of the response from a remote
351 * @param $content String
353 public function read( $fh, $content ) {
354 $this->content
.= $content;
355 return strlen( $content );
359 * Take care of whatever is necessary to perform the URI request.
363 public function execute() {
368 if ( strtoupper( $this->method
) == "HEAD" ) {
369 $this->headersOnly
= true;
372 if ( is_object( $wgTitle ) && !isset( $this->reqHeaders
['Referer'] ) ) {
373 $this->setReferer( $wgTitle->getFullURL() );
376 if ( !$this->noProxy
) {
380 if ( !$this->callback
) {
381 $this->setCallback( array( $this, 'read' ) );
384 if ( !isset( $this->reqHeaders
['User-Agent'] ) ) {
385 $this->setUserAgent( Http
::userAgent() );
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.
396 protected function parseHeader() {
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] );
412 $this->parseCookies();
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.)
425 protected function setStatus() {
426 if ( !$this->respHeaders
) {
427 $this->parseHeader();
430 if ( (int)$this->respStatus
> 399 ) {
431 list( $code, $message ) = explode( " ", $this->respStatus
, 2 );
432 $this->status
->fatal( "http-bad-status", $code, $message );
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.)
443 public function getStatus() {
444 if ( !$this->respHeaders
) {
445 $this->parseHeader();
448 return (int)$this->respStatus
;
453 * Returns true if the last status code was a redirect.
457 public function isRedirect() {
458 if ( !$this->respHeaders
) {
459 $this->parseHeader();
462 $status = (int)$this->respStatus
;
464 if ( $status >= 300 && $status <= 303 ) {
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.
479 public function getResponseHeaders() {
480 if ( !$this->respHeaders
) {
481 $this->parseHeader();
484 return $this->respHeaders
;
488 * Returns the value of the given response header.
490 * @param $header String
493 public function getResponseHeader( $header ) {
494 if ( !$this->respHeaders
) {
495 $this->parseHeader();
498 if ( isset( $this->respHeaders
[strtolower ( $header ) ] ) ) {
499 $v = $this->respHeaders
[strtolower ( $header ) ];
500 return $v[count( $v ) - 1];
507 * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
509 * @param $jar CookieJar
511 public function setCookieJar( $jar ) {
512 $this->cookieJar
= $jar;
516 * Returns the cookie jar in use.
520 public function getCookieJar() {
521 if ( !$this->respHeaders
) {
522 $this->parseHeader();
525 return $this->cookieJar
;
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.
534 public function setCookie( $name, $value = null, $attr = null ) {
535 if ( !$this->cookieJar
) {
536 $this->cookieJar
= new CookieJar
;
539 $this->cookieJar
->setCookie( $name, $value, $attr );
543 * Parse the cookies in the response headers and store them in the cookie jar.
545 protected function parseCookies() {
546 if ( !$this->cookieJar
) {
547 $this->cookieJar
= new CookieJar
;
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'] );
559 * Returns the final URL after all redirections.
563 public function getFinalUrl() {
564 $location = $this->getResponseHeader( "Location" );
574 * Returns true if the backend can follow redirects. Overridden by the
577 public function canFollowRedirects() {
583 * MWHttpRequest implemented using internal curl compiled into PHP
585 class CurlHttpRequest
extends MWHttpRequest
{
586 const SUPPORTS_FILE_POSTS
= true;
588 static $curlMessageMap = array(
589 6 => 'http-host-unreachable',
590 28 => 'http-timed-out'
593 protected $curlOptions = array();
594 protected $headerText = "";
596 protected function readHeader( $fh, $content ) {
597 $this->headerText
.= $content;
598 return strlen( $content );
601 public function execute() {
604 if ( !$this->status
->isOK() ) {
605 return $this->status
;
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
616 /* not sure these two are actually necessary */
617 if ( isset( $this->reqHeaders
['Referer'] ) ) {
618 $this->curlOptions
[CURLOPT_REFERER
] = $this->reqHeaders
['Referer'];
620 $this->curlOptions
[CURLOPT_USERAGENT
] = $this->reqHeaders
['User-Agent'];
622 if ( isset( $this->sslVerifyHost
) ) {
623 $this->curlOptions
[CURLOPT_SSL_VERIFYHOST
] = $this->sslVerifyHost
;
626 if ( isset( $this->sslVerifyCert
) ) {
627 $this->curlOptions
[CURLOPT_SSL_VERIFYPEER
] = $this->sslVerifyCert
;
630 if ( $this->caInfo
) {
631 $this->curlOptions
[CURLOPT_CAINFO
] = $this->caInfo
;
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'] = '';
645 $this->curlOptions
[CURLOPT_CUSTOMREQUEST
] = $this->method
;
648 $this->curlOptions
[CURLOPT_HTTPHEADER
] = $this->getHeaderList();
650 $curlHandle = curl_init( $this->url
);
652 if ( !curl_setopt_array( $curlHandle, $this->curlOptions
) ) {
653 throw new MWException( "Error setting curl options." );
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
667 if ( false === curl_exec( $curlHandle ) ) {
668 $code = curl_error( $curlHandle );
670 if ( isset( self
::$curlMessageMap[$code] ) ) {
671 $this->status
->fatal( self
::$curlMessageMap[$code] );
673 $this->status
->fatal( 'http-curl-error', curl_error( $curlHandle ) );
676 $this->headerList
= explode( "\r\n", $this->headerText
);
679 curl_close( $curlHandle );
681 $this->parseHeader();
684 return $this->status
;
687 public function canFollowRedirects() {
688 if ( strval( ini_get( 'open_basedir' ) ) !== '' ||
wfIniGetBool( 'safe_mode' ) ) {
689 wfDebug( "Cannot follow redirects in safe mode\n" );
693 if ( !defined( 'CURLOPT_REDIR_PROTOCOLS' ) ) {
694 wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
702 class PhpHttpRequest
extends MWHttpRequest
{
703 protected function urlToTcp( $url ) {
704 $parsedUrl = parse_url( $url );
706 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
709 public function execute() {
712 if ( is_array( $this->postData
) ) {
713 $this->postData
= wfArrayToCGI( $this->postData
);
716 if ( $this->parsedUrl
['scheme'] != 'http' ) {
717 $this->status
->fatal( 'http-invalid-scheme', $this->parsedUrl
['scheme'] );
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";
728 if ( $this->proxy
&& !$this->noProxy
) {
729 $options['proxy'] = $this->urlToTCP( $this->proxy
);
730 $options['request_fulluri'] = true;
733 if ( !$this->followRedirects
) {
734 $options['max_redirects'] = 0;
736 $options['max_redirects'] = $this->maxRedirects
;
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";
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;
750 if ( $this->postData
) {
751 $options['content'] = $this->postData
;
754 $options['timeout'] = $this->timeout
;
756 $context = stream_context_create( array( 'http' => $options ) );
758 $this->headerList
= array();
766 wfSuppressWarnings();
767 $fh = fopen( $url, "r", false, $context );
774 $result = stream_get_meta_data( $fh );
775 $this->headerList
= $result['wrapper_data'];
776 $this->parseHeader();
778 if ( !$this->followRedirects
) {
782 # Handle manual redirection
783 if ( !$this->isRedirect() ||
$reqCount > $this->maxRedirects
) {
786 # Check security of URL
787 $url = $this->getResponseHeader( "Location" );
789 if ( substr( $url, 0, 7 ) !== 'http://' ) {
790 wfDebug( __METHOD__
. ": insecure redirection\n" );
797 if ( $fh === false ) {
798 $this->status
->fatal( 'http-request-error' );
799 return $this->status
;
802 if ( $result['timed_out'] ) {
803 $this->status
->fatal( 'http-timed-out', $this->url
);
804 return $this->status
;
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 );
814 if ( $buf === false ) {
815 $this->status
->fatal( 'http-read-error' );
819 if ( strlen( $buf ) ) {
820 call_user_func( $this->callback
, $fh, $buf );
826 return $this->status
;