+ 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 || $manuallyRedirect ) {
+ $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;
+
+ 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 ( !$manuallyRedirect || !$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;
+ }