+
+ /**
+ * Returns an associative array of response headers after the
+ * request has been executed. Because some headers
+ * (e.g. Set-Cookie) can appear more than once the, each value of
+ * the associative array is an array of the values given.
+ *
+ * @return Array
+ */
+ public function getResponseHeaders() {
+ if ( !$this->respHeaders ) {
+ $this->parseHeader();
+ }
+
+ return $this->respHeaders;
+ }
+
+ /**
+ * Returns the value of the given response header.
+ *
+ * @param $header String
+ * @return String
+ */
+ public function getResponseHeader( $header ) {
+ if ( !$this->respHeaders ) {
+ $this->parseHeader();
+ }
+
+ if ( isset( $this->respHeaders[strtolower ( $header ) ] ) ) {
+ $v = $this->respHeaders[strtolower ( $header ) ];
+ return $v[count( $v ) - 1];
+ }
+
+ return null;
+ }
+
+ /**
+ * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
+ *
+ * @param $jar CookieJar
+ */
+ public function setCookieJar( $jar ) {
+ $this->cookieJar = $jar;
+ }
+
+ /**
+ * Returns the cookie jar in use.
+ *
+ * @returns CookieJar
+ */
+ public function getCookieJar() {
+ if ( !$this->respHeaders ) {
+ $this->parseHeader();
+ }
+
+ return $this->cookieJar;
+ }
+
+ /**
+ * Sets a cookie. Used before a request to set up any individual
+ * cookies. Used internally after a request to parse the
+ * Set-Cookie headers.
+ * @see Cookie::set
+ */
+ public function setCookie( $name, $value = null, $attr = null ) {
+ if ( !$this->cookieJar ) {
+ $this->cookieJar = new CookieJar;
+ }
+
+ $this->cookieJar->setCookie( $name, $value, $attr );
+ }
+
+ /**
+ * Parse the cookies in the response headers and store them in the cookie jar.
+ */
+ protected function parseCookies() {
+ if ( !$this->cookieJar ) {
+ $this->cookieJar = new CookieJar;
+ }
+
+ if ( isset( $this->respHeaders['set-cookie'] ) ) {
+ $url = parse_url( $this->getFinalUrl() );
+ foreach ( $this->respHeaders['set-cookie'] as $cookie ) {
+ $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
+ }
+ }
+ }
+
+ /**
+ * Returns the final URL after all redirections.
+ *
+ * @return String
+ */
+ public function getFinalUrl() {
+ $location = $this->getResponseHeader( "Location" );
+
+ if ( $location ) {
+ return $location;
+ }
+
+ return $this->url;
+ }
+
+ /**
+ * Returns true if the backend can follow redirects. Overridden by the
+ * child classes.
+ */
+ public function canFollowRedirects() {
+ return true;
+ }
+}
+
+/**
+ * MWHttpRequest implemented using internal curl compiled into PHP
+ */
+class CurlHttpRequest extends MWHttpRequest {
+ const SUPPORTS_FILE_POSTS = true;
+
+ static $curlMessageMap = array(
+ 6 => 'http-host-unreachable',
+ 28 => 'http-timed-out'
+ );
+
+ protected $curlOptions = array();
+ protected $headerText = "";
+
+ protected function readHeader( $fh, $content ) {
+ $this->headerText .= $content;
+ return strlen( $content );
+ }
+
+ public function execute() {
+ parent::execute();
+
+ if ( !$this->status->isOK() ) {
+ return $this->status;
+ }
+
+ $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
+ $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
+ $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
+ $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
+ $this->curlOptions[CURLOPT_HEADERFUNCTION] = array( $this, "readHeader" );
+ $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
+ $this->curlOptions[CURLOPT_ENCODING] = ""; # Enable compression
+
+ /* not sure these two are actually necessary */
+ if ( isset( $this->reqHeaders['Referer'] ) ) {
+ $this->curlOptions[CURLOPT_REFERER] = $this->reqHeaders['Referer'];
+ }
+ $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
+
+ if ( isset( $this->sslVerifyHost ) ) {
+ $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost;
+ }
+
+ if ( isset( $this->sslVerifyCert ) ) {
+ $this->curlOptions[CURLOPT_SSL_VERIFYPEER] = $this->sslVerifyCert;
+ }
+
+ if ( $this->caInfo ) {
+ $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
+ }
+
+ if ( $this->headersOnly ) {
+ $this->curlOptions[CURLOPT_NOBODY] = true;
+ $this->curlOptions[CURLOPT_HEADER] = true;
+ } elseif ( $this->method == 'POST' ) {
+ $this->curlOptions[CURLOPT_POST] = true;
+ $this->curlOptions[CURLOPT_POSTFIELDS] = $this->postData;
+ // Suppress 'Expect: 100-continue' header, as some servers
+ // will reject it with a 417 and Curl won't auto retry
+ // with HTTP 1.0 fallback
+ $this->reqHeaders['Expect'] = '';
+ } else {
+ $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
+ }
+
+ $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
+
+ $curlHandle = curl_init( $this->url );
+
+ if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
+ throw new MWException( "Error setting curl options." );
+ }
+
+ if ( $this->followRedirects && $this->canFollowRedirects() ) {
+ wfSuppressWarnings();
+ if ( ! curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
+ wfDebug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
+ "Probably safe_mode or open_basedir is set.\n" );
+ // Continue the processing. If it were in curl_setopt_array,
+ // processing would have halted on its entry
+ }
+ wfRestoreWarnings();
+ }
+
+ if ( false === curl_exec( $curlHandle ) ) {
+ $code = curl_error( $curlHandle );
+
+ if ( isset( self::$curlMessageMap[$code] ) ) {
+ $this->status->fatal( self::$curlMessageMap[$code] );
+ } else {
+ $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
+ }
+ } else {
+ $this->headerList = explode( "\r\n", $this->headerText );
+ }
+
+ curl_close( $curlHandle );
+
+ $this->parseHeader();
+ $this->setStatus();
+
+ return $this->status;
+ }
+
+ public function canFollowRedirects() {
+ if ( strval( ini_get( 'open_basedir' ) ) !== '' || wfIniGetBool( 'safe_mode' ) ) {
+ wfDebug( "Cannot follow redirects in safe mode\n" );
+ return false;
+ }
+
+ if ( !defined( 'CURLOPT_REDIR_PROTOCOLS' ) ) {
+ wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
+ return false;
+ }
+
+ return true;
+ }
+}
+
+class PhpHttpRequest extends MWHttpRequest {
+ protected function urlToTcp( $url ) {
+ $parsedUrl = parse_url( $url );
+
+ return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
+ }
+
+ public function execute() {
+ parent::execute();
+
+ if ( is_array( $this->postData ) ) {
+ $this->postData = wfArrayToCGI( $this->postData );
+ }
+
+ if ( $this->parsedUrl['scheme'] != 'http' ) {
+ $this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] );
+ }
+
+ $this->reqHeaders['Accept'] = "*/*";
+ if ( $this->method == 'POST' ) {
+ // Required for HTTP 1.0 POSTs
+ $this->reqHeaders['Content-Length'] = strlen( $this->postData );
+ $this->reqHeaders['Content-type'] = "application/x-www-form-urlencoded";
+ }
+
+ $options = array();
+ if ( $this->proxy && !$this->noProxy ) {
+ $options['proxy'] = $this->urlToTCP( $this->proxy );
+ $options['request_fulluri'] = true;
+ }
+
+ if ( !$this->followRedirects ) {
+ $options['max_redirects'] = 0;
+ } else {
+ $options['max_redirects'] = $this->maxRedirects;
+ }
+
+ $options['method'] = $this->method;
+ $options['header'] = implode( "\r\n", $this->getHeaderList() );
+ // Note that at some future point we may want to support
+ // HTTP/1.1, but we'd have to write support for chunking
+ // in version of PHP < 5.3.1
+ $options['protocol_version'] = "1.0";
+
+ // This is how we tell PHP we want to deal with 404s (for example) ourselves.
+ // Only works on 5.2.10+
+ $options['ignore_errors'] = true;
+
+ if ( $this->postData ) {
+ $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;
+ }
+
+ $context = stream_context_create( array( 'http' => $options ) );
+
+ $this->headerList = array();
+ $reqCount = 0;
+ $url = $this->url;
+
+ $result = array();
+
+ do {
+ $reqCount++;
+ wfSuppressWarnings();
+ $fh = fopen( $url, "r", false, $context );
+ wfRestoreWarnings();
+
+ if ( !$fh ) {
+ break;
+ }
+
+ $result = stream_get_meta_data( $fh );
+ $this->headerList = $result['wrapper_data'];
+ $this->parseHeader();
+
+ if ( !$this->followRedirects ) {
+ break;
+ }
+
+ # Handle manual redirection
+ if ( !$this->isRedirect() || $reqCount > $this->maxRedirects ) {
+ break;
+ }
+ # Check security of URL
+ $url = $this->getResponseHeader( "Location" );
+
+ if ( substr( $url, 0, 7 ) !== 'http://' ) {
+ wfDebug( __METHOD__ . ": insecure redirection\n" );
+ break;
+ }
+ } while ( true );
+
+ if ( $oldTimeout !== false ) {
+ ini_set( 'default_socket_timeout', $oldTimeout );
+ }
+
+ $this->setStatus();
+
+ if ( $fh === false ) {
+ $this->status->fatal( 'http-request-error' );
+ return $this->status;
+ }
+
+ if ( $result['timed_out'] ) {
+ $this->status->fatal( 'http-timed-out', $this->url );
+ return $this->status;
+ }
+
+ if ( $this->status->isOK() ) {
+ while ( !feof( $fh ) ) {
+ $buf = fread( $fh, 8192 );
+
+ if ( $buf === false ) {
+ $this->status->fatal( 'http-read-error' );
+ break;
+ }
+
+ if ( strlen( $buf ) ) {
+ call_user_func( $this->callback, $fh, $buf );
+ }
+ }
+ }
+ fclose( $fh );
+
+ return $this->status;
+ }