summaryrefslogtreecommitdiff
path: root/includes/HttpFunctions.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/HttpFunctions.php')
-rw-r--r--includes/HttpFunctions.php86
1 files changed, 64 insertions, 22 deletions
diff --git a/includes/HttpFunctions.php b/includes/HttpFunctions.php
index 555a79b7..269d45ff 100644
--- a/includes/HttpFunctions.php
+++ b/includes/HttpFunctions.php
@@ -1,24 +1,48 @@
<?php
/**
+ * @defgroup HTTP HTTP
+ * @file
+ * @ingroup HTTP
+ */
+
+/**
* Various HTTP related functions
+ * @ingroup HTTP
*/
class Http {
- static function get( $url, $timeout = 'default' ) {
- return Http::request( "GET", $url, $timeout );
+
+ /**
+ * Simple wrapper for Http::request( 'GET' )
+ * @see Http::request()
+ */
+ public static function get( $url, $timeout = 'default', $opts = array() ) {
+ return Http::request( "GET", $url, $timeout, $opts );
}
- static function post( $url, $timeout = 'default' ) {
- return Http::request( "POST", $url, $timeout );
+ /**
+ * Simple wrapper for Http::request( 'POST' )
+ * @see Http::request()
+ */
+ public static function post( $url, $timeout = 'default', $opts = array() ) {
+ return Http::request( "POST", $url, $timeout, $opts );
}
/**
* Get the contents of a file by HTTP
- *
- * if $timeout is 'default', $wgHTTPTimeout is used
+ * @param $method string HTTP method. Usually GET/POST
+ * @param $url string Full URL to act on
+ * @param $timeout int Seconds to timeout. 'default' falls to $wgHTTPTimeout
+ * @param $curlOptions array Optional array of extra params to pass
+ * to curl_setopt()
*/
- static function request( $method, $url, $timeout = 'default' ) {
- global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle;
+ public static function request( $method, $url, $timeout = 'default', $curlOptions = array() ) {
+ global $wgHTTPTimeout, $wgHTTPProxy, $wgTitle;
+
+ // Go ahead and set the timeout if not otherwise specified
+ if ( $timeout == 'default' ) {
+ $timeout = $wgHTTPTimeout;
+ }
wfDebug( __METHOD__ . ": $method $url\n" );
# Use curl if available
@@ -30,13 +54,12 @@ class Http {
curl_setopt($c, CURLOPT_PROXY, $wgHTTPProxy);
}
- if ( $timeout == 'default' ) {
- $timeout = $wgHTTPTimeout;
- }
curl_setopt( $c, CURLOPT_TIMEOUT, $timeout );
- curl_setopt( $c, CURLOPT_USERAGENT, "MediaWiki/$wgVersion" );
- if ( $method == 'POST' )
+ curl_setopt( $c, CURLOPT_USERAGENT, self :: userAgent() );
+ if ( $method == 'POST' ) {
curl_setopt( $c, CURLOPT_POST, true );
+ curl_setopt( $c, CURLOPT_POSTFIELDS, '' );
+ }
else
curl_setopt( $c, CURLOPT_CUSTOMREQUEST, $method );
@@ -48,6 +71,12 @@ class Http {
if ( is_object( $wgTitle ) ) {
curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
}
+
+ if ( is_array( $curlOptions ) ) {
+ foreach( $curlOptions as $option => $value ) {
+ curl_setopt( $c, $option, $value );
+ }
+ }
ob_start();
curl_exec( $c );
@@ -55,20 +84,24 @@ class Http {
ob_end_clean();
# Don't return the text of error messages, return false on error
- if ( curl_getinfo( $c, CURLINFO_HTTP_CODE ) != 200 ) {
+ $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE );
+ if ( $retcode != 200 ) {
+ wfDebug( __METHOD__ . ": HTTP return code $retcode\n" );
$text = false;
}
# Don't return truncated output
- if ( curl_errno( $c ) != CURLE_OK ) {
+ $errno = curl_errno( $c );
+ if ( $errno != CURLE_OK ) {
+ $errstr = curl_error( $c );
+ wfDebug( __METHOD__ . ": CURL error code $errno: $errstr\n" );
$text = false;
}
curl_close( $c );
} else {
# Otherwise use file_get_contents...
- # This may take 3 minutes to time out, and doesn't have local fetch capabilities
+ # This doesn't have local fetch capabilities...
- global $wgVersion;
- $headers = array( "User-Agent: MediaWiki/$wgVersion" );
+ $headers = array( "User-Agent: " . self :: userAgent() );
if( strcasecmp( $method, 'post' ) == 0 ) {
// Required for HTTP 1.0 POSTs
$headers[] = "Content-Length: 0";
@@ -76,20 +109,21 @@ class Http {
$opts = array(
'http' => array(
'method' => $method,
- 'header' => implode( "\r\n", $headers ) ) );
+ 'header' => implode( "\r\n", $headers ),
+ 'timeout' => $timeout ) );
$ctx = stream_context_create($opts);
- $url_fopen = ini_set( 'allow_url_fopen', 1 );
$text = file_get_contents( $url, false, $ctx );
- ini_set( 'allow_url_fopen', $url_fopen );
}
return $text;
}
/**
* Check if the URL can be served by localhost
+ * @param $url string Full url to check
+ * @return bool
*/
- static function isLocalURL( $url ) {
+ public static function isLocalURL( $url ) {
global $wgCommandLineMode, $wgConf;
if ( $wgCommandLineMode ) {
return false;
@@ -117,4 +151,12 @@ class Http {
}
return false;
}
+
+ /**
+ * Return a standard user-agent we can use for external requests.
+ */
+ public static function userAgent() {
+ global $wgVersion;
+ return "MediaWiki/$wgVersion";
+ }
}