10 * Various HTTP related functions
15 public static $lastCurlErrno = 0;
16 public static $lastHttpResponse = 0;
19 * Simple wrapper for Http::request( 'GET' )
20 * @see Http::request()
22 static function get( $url, $timeout = 'default', $opts = array() ) {
23 return Http
::request( "GET", $url, $timeout, $opts );
27 * Simple wrapper for Http::request( 'POST' )
28 * @see Http::request()
30 static function post( $url, $timeout = 'default', $opts = array() ) {
31 return Http
::request( "POST", $url, $timeout, $opts );
35 * Get the contents of a file by HTTP
36 * @param $method string HTTP method. Usually GET/POST
37 * @param $url string Full URL to act on
38 * @param $timeout int Seconds to timeout. 'default' falls to $wgHTTPTimeout
39 * @param $curlOptions array Optional array of extra params to pass
42 static function request( $method, $url, $timeout = 'default', $curlOptions = array() ) {
43 global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle;
45 // Go ahead and set the timeout if not otherwise specified
46 if ( $timeout == 'default' ) {
47 $timeout = $wgHTTPTimeout;
50 wfDebug( __METHOD__
. ": $method $url\n" );
51 # Use curl if available
52 if ( function_exists( 'curl_init' ) ) {
53 $c = curl_init( $url );
54 if ( self
::isLocalURL( $url ) ) {
55 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
56 } else if ($wgHTTPProxy) {
57 curl_setopt($c, CURLOPT_PROXY
, $wgHTTPProxy);
60 curl_setopt( $c, CURLOPT_TIMEOUT
, $timeout );
61 curl_setopt( $c, CURLOPT_USERAGENT
, "MediaWiki/$wgVersion" );
62 if ( $method == 'POST' )
63 curl_setopt( $c, CURLOPT_POST
, true );
65 curl_setopt( $c, CURLOPT_CUSTOMREQUEST
, $method );
67 # Set the referer to $wgTitle, even in command-line mode
68 # This is useful for interwiki transclusion, where the foreign
69 # server wants to know what the referring page is.
70 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
72 if ( is_object( $wgTitle ) ) {
73 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
76 if ( is_array( $curlOptions ) ) {
77 foreach( $curlOptions as $option => $value ) {
78 curl_setopt( $c, $option, $value );
84 $text = ob_get_contents();
87 # Don't return the text of error messages, return false on error
88 self
::$lastHttpResponse = curl_getinfo( $c, CURLINFO_HTTP_CODE
);
89 if ( self
::$lastHttpResponse != 200 ) {
90 wfDebug( __METHOD__
. ": HTTP return code " . self
::$lastHttpResponse . "\n" );
93 # Don't return truncated output
94 self
::$lastCurlErrno = curl_errno( $c );
95 if ( self
::$lastCurlErrno != CURLE_OK
) {
96 $errstr = curl_error( $c );
97 wfDebug( __METHOD__
. ": CURL error code " . self
::$lastCurlErrno . ": $errstr\n" );
102 # Otherwise use file_get_contents...
103 # This doesn't have local fetch capabilities...
105 $headers = array( "User-Agent: MediaWiki/$wgVersion" );
106 if( strcasecmp( $method, 'post' ) == 0 ) {
107 // Required for HTTP 1.0 POSTs
108 $headers[] = "Content-Length: 0";
113 'header' => implode( "\r\n", $headers ),
114 'timeout' => $timeout ) );
115 $ctx = stream_context_create($opts);
117 $text = file_get_contents( $url, false, $ctx );
123 * Check if the URL can be served by localhost
124 * @param $url string Full url to check
127 static function isLocalURL( $url ) {
128 global $wgCommandLineMode, $wgConf;
129 if ( $wgCommandLineMode ) {
135 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
138 $domainParts = explode( '.', $host );
139 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
140 $domainParts = array_reverse( $domainParts );
141 for ( $i = 0; $i < count( $domainParts ); $i++
) {
142 $domainPart = $domainParts[$i];
144 $domain = $domainPart;
146 $domain = $domainPart . '.' . $domain;
148 if ( $wgConf->isLocalVHost( $domain ) ) {