+ }
+
+ /**
+ * 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
+ * @param $options Array: (optional) extra params to pass (see Http::request())
+ * @see MWHttpRequest::__construct
+ */
+ public static function factory( $url, $options = null ) {
+ if ( !Http::$httpEngine ) {
+ Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
+ } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
+ throw new MWException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
+ ' Http::$httpEngine is set to "curl"' );
+ }
+
+ switch( Http::$httpEngine ) {
+ case 'curl':
+ return new CurlHttpRequest( $url, $options );
+ case 'php':
+ if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
+ throw new MWException( __METHOD__ . ': allow_url_fopen needs to be enabled for pure PHP' .
+ ' http requests to work. If possible, curl should be used instead. See http://php.net/curl.' );
+ }
+ return new PhpHttpRequest( $url, $options );
+ default:
+ throw new MWException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
+ }
+ }
+
+ /**
+ * Get the body, or content, of the response to the request
+ *
+ * @return String
+ */
+ public function getContent() {
+ return $this->content;
+ }
+
+ /**
+ * Set the parameters of the request
+
+ * @param $args Array
+ * @todo overload the args param
+ */
+ public function setData( $args ) {
+ $this->postData = $args;
+ }
+
+ /**
+ * Take care of setting up the proxy
+ * (override in subclass)
+ *
+ * @return String
+ */
+ public function proxySetup() {
+ global $wgHTTPProxy;
+
+ if ( $this->proxy ) {
+ return;
+ }
+
+ if ( Http::isLocalURL( $this->url ) ) {
+ $this->proxy = 'http://localhost:80/';
+ } elseif ( $wgHTTPProxy ) {
+ $this->proxy = $wgHTTPProxy ;
+ } elseif ( getenv( "http_proxy" ) ) {
+ $this->proxy = getenv( "http_proxy" );
+ }
+ }
+
+ /**
+ * Set the refererer header
+ */
+ public function setReferer( $url ) {
+ $this->setHeader( 'Referer', $url );
+ }
+
+ /**
+ * Set the user agent
+ */
+ public function setUserAgent( $UA ) {
+ $this->setHeader( 'User-Agent', $UA );
+ }
+
+ /**
+ * Set an arbitrary header
+ */
+ public function setHeader( $name, $value ) {
+ // I feel like I should normalize the case here...
+ $this->reqHeaders[$name] = $value;
+ }
+
+ /**
+ * Get an array of the headers
+ */
+ public function getHeaderList() {
+ $list = array();
+
+ if ( $this->cookieJar ) {
+ $this->reqHeaders['Cookie'] =
+ $this->cookieJar->serializeToHttpRequest(
+ $this->parsedUrl['path'],
+ $this->parsedUrl['host']
+ );
+ }