10 * Various HTTP related functions
16 * Simple wrapper for Http::request( 'GET' )
17 * @see Http::request()
19 static function get( $url, $timeout = 'default', $opts = array() ) {
20 return Http
::request( "GET", $url, $timeout, $opts );
24 * Simple wrapper for Http::request( 'POST' )
25 * @see Http::request()
27 static function post( $url, $timeout = 'default', $opts = array() ) {
28 return Http
::request( "POST", $url, $timeout, $opts );
32 * Get the contents of a file by HTTP
33 * @param $method string HTTP method. Usually GET/POST
34 * @param $url string Full URL to act on
35 * @param $timeout int Seconds to timeout. 'default' falls to $wgHTTPTimeout
36 * @param $curlOptions array Optional array of extra params to pass
39 static function request( $method, $url, $timeout = 'default', $curlOptions = array() ) {
40 global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle;
42 // Go ahead and set the timeout if not otherwise specified
43 if ( $timeout == 'default' ) {
44 $timeout = $wgHTTPTimeout;
47 wfDebug( __METHOD__
. ": $method $url\n" );
48 # Use curl if available
49 if ( function_exists( 'curl_init' ) ) {
50 $c = curl_init( $url );
51 if ( self
::isLocalURL( $url ) ) {
52 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
53 } else if ($wgHTTPProxy) {
54 curl_setopt($c, CURLOPT_PROXY
, $wgHTTPProxy);
57 curl_setopt( $c, CURLOPT_TIMEOUT
, $timeout );
58 curl_setopt( $c, CURLOPT_USERAGENT
, "MediaWiki/$wgVersion" );
59 if ( $method == 'POST' )
60 curl_setopt( $c, CURLOPT_POST
, true );
62 curl_setopt( $c, CURLOPT_CUSTOMREQUEST
, $method );
64 # Set the referer to $wgTitle, even in command-line mode
65 # This is useful for interwiki transclusion, where the foreign
66 # server wants to know what the referring page is.
67 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
69 if ( is_object( $wgTitle ) ) {
70 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
73 if ( is_array( $curlOptions ) ) {
74 foreach( $curlOptions as $option => $value ) {
75 curl_setopt( $c, $option, $value );
81 $text = ob_get_contents();
84 # Don't return the text of error messages, return false on error
85 $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE
);
86 if ( $retcode != 200 ) {
87 wfDebug( __METHOD__
. ": HTTP return code $retcode\n" );
90 # Don't return truncated output
91 $errno = curl_errno( $c );
92 if ( $errno != CURLE_OK
) {
93 $errstr = curl_error( $c );
94 wfDebug( __METHOD__
. ": CURL error code $errno: $errstr\n" );
99 # Otherwise use file_get_contents...
100 # This doesn't have local fetch capabilities...
102 $headers = array( "User-Agent: MediaWiki/$wgVersion" );
103 if( strcasecmp( $method, 'post' ) == 0 ) {
104 // Required for HTTP 1.0 POSTs
105 $headers[] = "Content-Length: 0";
110 'header' => implode( "\r\n", $headers ),
111 'timeout' => $timeout ) );
112 $ctx = stream_context_create($opts);
114 $text = file_get_contents( $url, false, $ctx );
120 * Check if the URL can be served by localhost
121 * @param $url string Full url to check
124 static function isLocalURL( $url ) {
125 global $wgCommandLineMode, $wgConf;
126 if ( $wgCommandLineMode ) {
132 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
135 $domainParts = explode( '.', $host );
136 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
137 $domainParts = array_reverse( $domainParts );
138 for ( $i = 0; $i < count( $domainParts ); $i++
) {
139 $domainPart = $domainParts[$i];
141 $domain = $domainPart;
143 $domain = $domainPart . '.' . $domain;
145 if ( $wgConf->isLocalVHost( $domain ) ) {