4 * Various HTTP related functions
8 static function get( $url, $timeout = 'default' ) {
9 return Http
::request( "GET", $url, $timeout );
12 static function post( $url, $timeout = 'default' ) {
13 return Http
::request( "POST", $url, $timeout );
17 * Get the contents of a file by HTTP
19 * if $timeout is 'default', $wgHTTPTimeout is used
21 static function request( $method, $url, $timeout = 'default' ) {
22 global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle;
24 wfDebug( __METHOD__
. ": $method $url\n" );
25 # Use curl if available
26 if ( function_exists( 'curl_init' ) ) {
27 $c = curl_init( $url );
28 if ( self
::isLocalURL( $url ) ) {
29 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
30 } else if ($wgHTTPProxy) {
31 curl_setopt($c, CURLOPT_PROXY
, $wgHTTPProxy);
34 if ( $timeout == 'default' ) {
35 $timeout = $wgHTTPTimeout;
37 curl_setopt( $c, CURLOPT_TIMEOUT
, $timeout );
38 curl_setopt( $c, CURLOPT_USERAGENT
, "MediaWiki/$wgVersion" );
39 if ( $method == 'POST' )
40 curl_setopt( $c, CURLOPT_POST
, true );
42 curl_setopt( $c, CURLOPT_CUSTOMREQUEST
, $method );
44 # Set the referer to $wgTitle, even in command-line mode
45 # This is useful for interwiki transclusion, where the foreign
46 # server wants to know what the referring page is.
47 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
49 if ( is_object( $wgTitle ) ) {
50 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
55 $text = ob_get_contents();
58 # Don't return the text of error messages, return false on error
59 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE
) != 200 ) {
62 # Don't return truncated output
63 if ( curl_errno( $c ) != CURLE_OK
) {
68 # Otherwise use file_get_contents...
69 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
72 $headers = array( "User-Agent: MediaWiki/$wgVersion" );
73 if( strcasecmp( $method, 'post' ) == 0 ) {
74 // Required for HTTP 1.0 POSTs
75 $headers[] = "Content-Length: 0";
80 'header' => implode( "\r\n", $headers ) ) );
81 $ctx = stream_context_create($opts);
83 $url_fopen = ini_set( 'allow_url_fopen', 1 );
84 $text = file_get_contents( $url, false, $ctx );
85 ini_set( 'allow_url_fopen', $url_fopen );
91 * Check if the URL can be served by localhost
93 static function isLocalURL( $url ) {
94 global $wgCommandLineMode, $wgConf;
95 if ( $wgCommandLineMode ) {
101 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
104 $domainParts = explode( '.', $host );
105 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
106 $domainParts = array_reverse( $domainParts );
107 for ( $i = 0; $i < count( $domainParts ); $i++
) {
108 $domainPart = $domainParts[$i];
110 $domain = $domainPart;
112 $domain = $domainPart . '.' . $domain;
114 if ( $wgConf->isLocalVHost( $domain ) ) {