* Perform an HTTP request
*
* @param $method String: HTTP method. Usually GET/POST
- * @param $url String: full URL to act on
+ * @param $url String: full URL to act on. If protocol-relative, will be expanded to an http:// URL
* @param $options Array: options to pass to MWHttpRequest object.
* Possible keys for the array:
* - timeout Timeout length in seconds
* @return Mixed: (bool)false on failure or a string on success
*/
public static function request( $method, $url, $options = array() ) {
- $url = wfExpandUrl( $url );
wfDebug( "HTTP: $method: $url\n" );
$options['method'] = strtoupper( $method );
/**
* Simple wrapper for Http::request( 'GET' )
* @see Http::request()
+ *
+ * @return string
*/
public static function get( $url, $timeout = 'default', $options = array() ) {
$options['timeout'] = $timeout;
/**
* Simple wrapper for Http::request( 'POST' )
* @see Http::request()
+ *
+ * @return string
*/
public static function post( $url, $options = array() ) {
return Http::request( 'POST', $url, $options );
* protocols, because we only want protocols that both cURL
* and php support.
*
+ * @fixme this is wildly inaccurate and fails to actually check most stuff
+ *
* @param $uri Mixed: URI to check for validity
* @returns Boolean
*/
public static function isValidURI( $uri ) {
return preg_match(
- '/^(f|ht)tps?:\/\/[^\/\s]\S*$/D',
+ '/^https?:\/\/[^\/\s]\S*$/D',
$uri
);
}
public $status;
/**
- * @param $url String: url to use
+ * @param $url String: url to use. If protocol-relative, will be expanded to an http:// URL
* @param $options Array: (optional) extra params to pass (see Http::request())
*/
function __construct( $url, $options = array() ) {
global $wgHTTPTimeout;
- $this->url = $url;
- $this->parsedUrl = parse_url( $url );
+ $this->url = wfExpandUrl( $url, PROTO_HTTP );
+ $this->parsedUrl = wfParseUrl( $this->url );
- if ( !Http::isValidURI( $this->url ) ) {
+ if ( !$this->parsedUrl || !Http::isValidURI( $this->url ) ) {
$this->status = Status::newFatal( 'http-invalid-url' );
} else {
$this->status = Status::newGood( 100 ); // continue
}
}
+ /**
+ * Simple function to test if we can make any sort of requests at all, using
+ * cURL or fopen()
+ * @return bool
+ */
+ public static function canMakeRequests() {
+ return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
+ }
+
/**
* Generate a new request object
* @param $url String: url to use
}
/**
- * Set the callback
+ * Set a read callback to accept data read from the HTTP request.
+ * By default, data is appended to an internal buffer which can be
+ * retrieved through $req->getContent().
+ *
+ * To handle data as it comes in -- especially for large files that
+ * would not fit in memory -- you can instead set your own callback,
+ * in the form function($resource, $buffer) where the first parameter
+ * is the low-level resource being read (implementation specific),
+ * and the second parameter is the data buffer.
+ *
+ * You MUST return the number of bytes handled in the buffer; if fewer
+ * bytes are reported handled than were passed to you, the HTTP fetch
+ * will be aborted.
*
* @param $callback Callback
*/
public function setCallback( $callback ) {
+ if ( !is_callable( $callback ) ) {
+ throw new MWException( 'Invalid MwHttpRequest callback' );
+ }
$this->callback = $callback;
}
}
if ( is_object( $wgTitle ) && !isset( $this->reqHeaders['Referer'] ) ) {
- $this->setReferer( $wgTitle->getFullURL() );
+ $this->setReferer( wfExpandUrl( $wgTitle->getFullURL(), PROTO_CURRENT ) );
}
if ( !$this->noProxy ) {
/**
* Returns the final URL after all redirections.
*
- * @return String
+ * Relative values of the "Location" header are incorrect as stated in RFC, however they do happen and modern browsers support them.
+ * This function loops backwards through all locations in order to build the proper absolute URI - Marooned at wikia-inc.com
+ *
+ * @returns string
*/
public function getFinalUrl() {
- $location = $this->getResponseHeader( "Location" );
+ $headers = $this->getResponseHeaders();
+
+ //return full url (fix for incorrect but handled relative location)
+ if ( isset( $headers[ 'location' ] ) ) {
+ $locations = $headers[ 'location' ];
+ $domain = '';
+ $foundRelativeURI = false;
+ $countLocations = count($locations);
- if ( $location ) {
- return $location;
+ for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
+ $url = parse_url( $locations[ $i ] );
+
+ if ( isset($url[ 'host' ]) ) {
+ $domain = $url[ 'scheme' ] . '://' . $url[ 'host' ];
+ break; //found correct URI (with host)
+ } else {
+ $foundRelativeURI = true;
+ }
+ }
+
+ if ( $foundRelativeURI ) {
+ if ( $domain ) {
+ return $domain . $locations[ $countLocations - 1 ];
+ } else {
+ $url = parse_url( $this->url );
+ if ( isset($url[ 'host' ]) ) {
+ return $url[ 'scheme' ] . '://' . $url[ 'host' ] . $locations[ $countLocations - 1 ];
+ }
+ }
+ } else {
+ return $locations[ $countLocations - 1 ];
+ }
}
return $this->url;
$this->postData = wfArrayToCGI( $this->postData );
}
- // At least on Centos 4.8 with PHP 5.1.6, using max_redirects to follow redirects
- // causes a segfault
- $manuallyRedirect = version_compare( phpversion(), '5.1.7', '<' );
-
- if ( $this->parsedUrl['scheme'] != 'http' ) {
+ if ( $this->parsedUrl['scheme'] != 'http' &&
+ $this->parsedUrl['scheme'] != 'https' ) {
$this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] );
}
$options['request_fulluri'] = true;
}
- if ( !$this->followRedirects || $manuallyRedirect ) {
+ if ( !$this->followRedirects ) {
$options['max_redirects'] = 0;
} else {
$options['max_redirects'] = $this->maxRedirects;
$options['content'] = $this->postData;
}
- $oldTimeout = false;
- if ( version_compare( '5.2.1', phpversion(), '>' ) ) {
- $oldTimeout = ini_set( 'default_socket_timeout', $this->timeout );
- } else {
- $options['timeout'] = $this->timeout;
- }
+ $options['timeout'] = $this->timeout;
$context = stream_context_create( array( 'http' => $options ) );
$this->headerList = $result['wrapper_data'];
$this->parseHeader();
- if ( !$manuallyRedirect || !$this->followRedirects ) {
+ if ( !$this->followRedirects ) {
break;
}
}
} while ( true );
- if ( $oldTimeout !== false ) {
- ini_set( 'default_socket_timeout', $oldTimeout );
- }
-
$this->setStatus();
if ( $fh === false ) {
return $this->status;
}
- if ( $this->status->isOK() ) {
+ // If everything went OK, or we recieved some error code
+ // get the response body content.
+ if ( $this->status->isOK()
+ || (int)$this->respStatus >= 300) {
while ( !feof( $fh ) ) {
$buf = fread( $fh, 8192 );