7 * Various HTTP related functions
12 * Perform an HTTP request
13 * @param $method string HTTP method. Usually GET/POST
14 * @param $url string Full URL to act on
15 * @param $options options to pass to HttpRequest object
16 * @returns mixed (bool)false on failure or a string on success
18 public static function request( $method, $url, $options = array() ) {
19 $options['method'] = strtoupper( $method );
20 if ( !isset( $options['timeout'] ) ) {
21 $options['timeout'] = 'default';
23 $req = HttpRequest
::factory( $url, $options );
24 $status = $req->execute();
25 if ( $status->isOK() ) {
26 return $req->getContent();
33 * Simple wrapper for Http::request( 'GET' )
34 * @see Http::request()
36 public static function get( $url, $timeout = 'default', $options = array() ) {
37 $options['timeout'] = $timeout;
38 return Http
::request( 'GET', $url, $options );
42 * Simple wrapper for Http::request( 'POST' )
43 * @see Http::request()
45 public static function post( $url, $options = array() ) {
46 return Http
::request( 'POST', $url, $options );
50 * Check if the URL can be served by localhost
51 * @param $url string Full url to check
54 public static function isLocalURL( $url ) {
55 global $wgCommandLineMode, $wgConf;
56 if ( $wgCommandLineMode ) {
62 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
65 $domainParts = explode( '.', $host );
66 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
67 $domainParts = array_reverse( $domainParts );
68 for ( $i = 0; $i < count( $domainParts ); $i++
) {
69 $domainPart = $domainParts[$i];
71 $domain = $domainPart;
73 $domain = $domainPart . '.' . $domain;
75 if ( $wgConf->isLocalVHost( $domain ) ) {
84 * A standard user-agent we can use for external requests.
87 public static function userAgent() {
89 return "MediaWiki/$wgVersion";
93 * Checks that the given URI is a valid one
94 * @param $uri Mixed: URI to check for validity
97 public static function isValidURI( $uri ) {
99 '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
107 * This wrapper class will call out to curl (if available) or fallback
108 * to regular PHP if necessary for handling internal HTTP requests.
112 protected $timeout = 'default';
113 protected $headersOnly = null;
114 protected $postData = null;
115 protected $proxy = null;
116 protected $noProxy = false;
117 protected $sslVerifyHost = true;
118 protected $caInfo = null;
119 protected $method = "GET";
120 protected $reqHeaders = array();
122 protected $parsedUrl;
127 * @param $url string url to use
128 * @param $options array (optional) extra params to pass
129 * Possible keys for the array:
140 function __construct( $url, $options = array() ) {
141 global $wgHTTPTimeout;
144 $this->parsedUrl
= parse_url( $url );
146 if ( !Http
::isValidURI( $this->url
) ) {
147 $this->status
= Status
::newFromFatal('http-invalid-url');
149 $this->status
= Status
::newGood( 100 ); // continue
152 if ( isset($options['timeout']) && $options['timeout'] != 'default' ) {
153 $this->timeout
= $options['timeout'];
155 $this->timeout
= $wgHTTPTimeout;
158 $members = array( "targetFilePath", "requestKey", "postData",
159 "proxy", "noProxy", "sslVerifyHost", "caInfo", "method" );
160 foreach ( $members as $o ) {
161 if ( isset($options[$o]) ) {
162 $this->$o = $options[$o];
168 * Generate a new request object
169 * @see HttpRequest::__construct
171 public static function factory( $url, $options ) {
172 global $wgHTTPEngine;
173 $engine = $wgHTTPEngine;
175 if ( !$wgHTTPEngine ) {
176 $wgHTTPEngine = function_exists( 'curl_init' ) ?
'curl' : 'php';
177 } elseif ( $wgHTTPEngine == 'curl' && !function_exists( 'curl_init' ) ) {
178 throw new MWException( __METHOD__
.': curl (http://php.net/curl) is not installed, but $wgHTTPEngine is set to "curl"' );
181 switch( $wgHTTPEngine ) {
183 return new CurlHttpRequest( $url, $options );
185 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
186 throw new MWException( __METHOD__
.': allow_url_fopen needs to be enabled for pure PHP http requests to work. '.
187 'If possible, curl should be used instead. See http://php.net/curl.' );
189 return new PhpHttpRequest( $url, $options );
191 throw new MWException( __METHOD__
.': The setting of $wgHTTPEngine is not valid.' );
196 * Get the body, or content, of the response to the request
199 public function getContent() {
200 return $this->content
;
204 * Take care of setting up the proxy
205 * (override in subclass)
208 public function proxySetup() {
212 if ( $this->proxy
) {
215 if ( Http
::isLocalURL( $this->url
) ) {
216 $this->proxy
= 'http://localhost:80/';
217 } elseif ( $wgHTTPProxy ) {
218 $this->proxy
= $wgHTTPProxy ;
223 * Set the refererer header
225 public function setReferer( $url ) {
226 $this->setHeader('Referer', $url);
232 public function setUserAgent( $UA ) {
233 $this->setHeader('User-Agent', $UA);
237 * Set an arbitrary header
239 public function setHeader($name, $value) {
240 // I feel like I should normalize the case here...
241 $this->reqHeaders
[$name] = $value;
245 * Get an array of the headers
247 public function getHeaderList() {
250 foreach($this->reqHeaders
as $name => $value) {
251 $list[] = "$name: $value";
258 * @param $callback callback
260 public function setCallback( $callback ) {
261 $this->callback
= $callback;
265 * A generic callback to read in the response from a remote server
267 * @param $content string
269 public function read( $fh, $content ) {
270 $this->content
.= $content;
271 return strlen( $content );
275 * Take care of whatever is necessary to perform the URI request.
278 public function execute() {
281 if( strtoupper($this->method
) == "HEAD" ) {
282 $this->headersOnly
= true;
285 if ( is_array( $this->postData
) ) {
286 $this->postData
= wfArrayToCGI( $this->postData
);
289 if ( is_object( $wgTitle ) && !isset($this->reqHeaders
['Referer']) ) {
290 $this->setReferer( $wgTitle->getFullURL() );
293 if ( !$this->noProxy
) {
297 if ( !$this->callback
) {
298 $this->setCallback( array( $this, 'read' ) );
301 if ( !isset($this->reqHeaders
['User-Agent']) ) {
302 $this->setUserAgent(Http
::userAgent());
308 * HttpRequest implemented using internal curl compiled into PHP
310 class CurlHttpRequest
extends HttpRequest
{
311 protected $curlOptions = array();
313 public function execute() {
315 if ( !$this->status
->isOK() ) {
316 return $this->status
;
319 // A lot of the action up front should probably be in
320 // set* methods, but we'll leave that for another time.
322 $this->curlOptions
[CURLOPT_PROXY
] = $this->proxy
;
323 $this->curlOptions
[CURLOPT_TIMEOUT
] = $this->timeout
;
324 $this->curlOptions
[CURLOPT_HTTP_VERSION
] = CURL_HTTP_VERSION_1_0
;
325 $this->curlOptions
[CURLOPT_WRITEFUNCTION
] = $this->callback
;
327 /* not sure these two are actually necessary */
328 if(isset($this->reqHeaders
['Referer'])) {
329 $this->curlOptions
[CURLOPT_REFERER
] = $this->reqHeaders
['Referer'];
331 $this->curlOptions
[CURLOPT_USERAGENT
] = $this->reqHeaders
['User-Agent'];
333 if ( $this->sslVerifyHost
) {
334 $this->curlOptions
[CURLOPT_SSL_VERIFYHOST
] = $this->sslVerifyHost
;
337 if ( $this->caInfo
) {
338 $this->curlOptions
[CURLOPT_CAINFO
] = $this->caInfo
;
341 if ( $this->headersOnly
) {
342 $this->curlOptions
[CURLOPT_NOBODY
] = true;
343 $this->curlOptions
[CURLOPT_HEADER
] = true;
344 } elseif ( $this->method
== 'POST' ) {
345 $this->curlOptions
[CURLOPT_POST
] = true;
346 $this->curlOptions
[CURLOPT_POSTFIELDS
] = $this->postData
;
347 // Suppress 'Expect: 100-continue' header, as some servers
348 // will reject it with a 417 and Curl won't auto retry
349 // with HTTP 1.0 fallback
350 $this->reqHeaders
['Expect'] = '';
352 $this->curlOptions
[CURLOPT_CUSTOMREQUEST
] = $this->method
;
355 $this->curlOptions
[CURLOPT_HTTPHEADER
] = $this->getHeaderList();
357 $curlHandle = curl_init( $this->url
);
358 curl_setopt_array( $curlHandle, $this->curlOptions
);
360 if ( false === curl_exec( $curlHandle ) ) {
361 // re-using already translated error messages
362 $this->status
->fatal( 'upload-curl-error'.curl_errno( $curlHandle ).'-text' );
365 curl_close( $curlHandle );
367 return $this->status
;
371 class PhpHttpRequest
extends HttpRequest
{
374 protected function urlToTcp( $url ) {
375 $parsedUrl = parse_url( $url );
377 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
380 public function execute() {
381 if ( $this->parsedUrl
['scheme'] != 'http' ) {
382 $this->status
->fatal( 'http-invalid-scheme', $this->parsedURL
['scheme'] );
386 if ( !$this->status
->isOK() ) {
387 return $this->status
;
390 // A lot of the action up front should probably be in
391 // set* methods, but we'll leave that for another time.
393 $this->reqHeaders
['Accept'] = "*/*";
394 if ( $this->method
== 'POST' ) {
395 // Required for HTTP 1.0 POSTs
396 $this->reqHeaders
['Content-Length'] = strlen( $this->postData
);
397 $this->reqHeaders
['Content-type'] = "application/x-www-form-urlencoded";
401 if ( $this->proxy
&& !$this->noProxy
) {
402 $options['proxy'] = $this->urlToTCP( $this->proxy
);
403 $options['request_fulluri'] = true;
406 $options['method'] = $this->method
;
407 $options['timeout'] = $this->timeout
;
408 $options['header'] = implode("\r\n", $this->getHeaderList());
409 // FOR NOW: Force everyone to HTTP 1.0
410 /* if ( version_compare( "5.3.0", phpversion(), ">" ) ) { */
411 $options['protocol_version'] = "1.0";
413 /* $options['protocol_version'] = "1.1"; */
416 if ( $this->postData
) {
417 $options['content'] = $this->postData
;
420 $context = stream_context_create( array( 'http' => $options ) );
422 $this->fh
= fopen( $this->url
, "r", false, $context );
423 } catch ( Exception
$e ) {
424 $this->status
->fatal( $e->getMessage() ); /* need some l10n help */
425 return $this->status
;
428 $result = stream_get_meta_data( $this->fh
);
429 if ( $result['timed_out'] ) {
430 $this->status
->fatal( 'http-timed-out', $this->url
);
431 return $this->status
;
434 $this->headers
= $result['wrapper_data'];
438 $contents = fread( $this->fh
, 8192 );
441 $size = call_user_func_array( $this->callback
, array( $this->fh
, $contents ) );
443 $end = ( $size == 0 ) ||
feof( $this->fh
);
447 return $this->status
;