Remove superfluous re- from confirmemail_body_set
[mediawiki.git] / includes / HttpFunctions.php
blob444857a08517f00524860d3a3655ed2297c3d8c0
1 <?php
2 /**
3 * Various HTTP related functions.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup HTTP
24 /**
25 * @defgroup HTTP HTTP
28 /**
29 * Various HTTP related functions
30 * @ingroup HTTP
32 class Http {
33 static $httpEngine = false;
35 /**
36 * Perform an HTTP request
38 * @param string $method HTTP method. Usually GET/POST
39 * @param string $url full URL to act on. If protocol-relative, will be expanded to an http:// URL
40 * @param array $options options to pass to MWHttpRequest object.
41 * Possible keys for the array:
42 * - timeout Timeout length in seconds
43 * - connectTimeout Timeout for connection, in seconds (curl only)
44 * - postData An array of key-value pairs or a url-encoded form data
45 * - proxy The proxy to use.
46 * Otherwise it will use $wgHTTPProxy (if set)
47 * Otherwise it will use the environment variable "http_proxy" (if set)
48 * - noProxy Don't use any proxy at all. Takes precedence over proxy value(s).
49 * - sslVerifyHost Verify hostname against certificate
50 * - sslVerifyCert Verify SSL certificate
51 * - caInfo Provide CA information
52 * - maxRedirects Maximum number of redirects to follow (defaults to 5)
53 * - followRedirects Whether to follow redirects (defaults to false).
54 * Note: this should only be used when the target URL is trusted,
55 * to avoid attacks on intranet services accessible by HTTP.
56 * - userAgent A user agent, if you want to override the default
57 * MediaWiki/$wgVersion
58 * @return Mixed: (bool)false on failure or a string on success
60 public static function request( $method, $url, $options = array() ) {
61 wfDebug( "HTTP: $method: $url\n" );
62 wfProfileIn( __METHOD__ . "-$method" );
64 $options['method'] = strtoupper( $method );
66 if ( !isset( $options['timeout'] ) ) {
67 $options['timeout'] = 'default';
69 if( !isset( $options['connectTimeout'] ) ) {
70 $options['connectTimeout'] = 'default';
73 $req = MWHttpRequest::factory( $url, $options );
74 $status = $req->execute();
76 $content = false;
77 if ( $status->isOK() ) {
78 $content = $req->getContent();
80 wfProfileOut( __METHOD__ . "-$method" );
81 return $content;
84 /**
85 * Simple wrapper for Http::request( 'GET' )
86 * @see Http::request()
88 * @param $url
89 * @param $timeout string
90 * @param $options array
91 * @return string
93 public static function get( $url, $timeout = 'default', $options = array() ) {
94 $options['timeout'] = $timeout;
95 return Http::request( 'GET', $url, $options );
98 /**
99 * Simple wrapper for Http::request( 'POST' )
100 * @see Http::request()
102 * @param $url
103 * @param $options array
104 * @return string
106 public static function post( $url, $options = array() ) {
107 return Http::request( 'POST', $url, $options );
111 * Check if the URL can be served by localhost
113 * @param string $url full url to check
114 * @return Boolean
116 public static function isLocalURL( $url ) {
117 global $wgCommandLineMode, $wgConf;
119 if ( $wgCommandLineMode ) {
120 return false;
123 // Extract host part
124 $matches = array();
125 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
126 $host = $matches[1];
127 // Split up dotwise
128 $domainParts = explode( '.', $host );
129 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
130 $domainParts = array_reverse( $domainParts );
132 $domain = '';
133 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
134 $domainPart = $domainParts[$i];
135 if ( $i == 0 ) {
136 $domain = $domainPart;
137 } else {
138 $domain = $domainPart . '.' . $domain;
141 if ( $wgConf->isLocalVHost( $domain ) ) {
142 return true;
147 return false;
151 * A standard user-agent we can use for external requests.
152 * @return String
154 public static function userAgent() {
155 global $wgVersion;
156 return "MediaWiki/$wgVersion";
160 * Checks that the given URI is a valid one. Hardcoding the
161 * protocols, because we only want protocols that both cURL
162 * and php support.
164 * file:// should not be allowed here for security purpose (r67684)
166 * @todo FIXME this is wildly inaccurate and fails to actually check most stuff
168 * @param $uri Mixed: URI to check for validity
169 * @return Boolean
171 public static function isValidURI( $uri ) {
172 return preg_match(
173 '/^https?:\/\/[^\/\s]\S*$/D',
174 $uri
180 * This wrapper class will call out to curl (if available) or fallback
181 * to regular PHP if necessary for handling internal HTTP requests.
183 * Renamed from HttpRequest to MWHttpRequest to avoid conflict with
184 * PHP's HTTP extension.
186 class MWHttpRequest {
187 const SUPPORTS_FILE_POSTS = false;
189 protected $content;
190 protected $timeout = 'default';
191 protected $headersOnly = null;
192 protected $postData = null;
193 protected $proxy = null;
194 protected $noProxy = false;
195 protected $sslVerifyHost = true;
196 protected $sslVerifyCert = true;
197 protected $caInfo = null;
198 protected $method = "GET";
199 protected $reqHeaders = array();
200 protected $url;
201 protected $parsedUrl;
202 protected $callback;
203 protected $maxRedirects = 5;
204 protected $followRedirects = false;
207 * @var CookieJar
209 protected $cookieJar;
211 protected $headerList = array();
212 protected $respVersion = "0.9";
213 protected $respStatus = "200 Ok";
214 protected $respHeaders = array();
216 public $status;
219 * @param string $url url to use. If protocol-relative, will be expanded to an http:// URL
220 * @param array $options (optional) extra params to pass (see Http::request())
222 protected function __construct( $url, $options = array() ) {
223 global $wgHTTPTimeout, $wgHTTPConnectTimeout;
225 $this->url = wfExpandUrl( $url, PROTO_HTTP );
226 $this->parsedUrl = wfParseUrl( $this->url );
228 if ( !$this->parsedUrl || !Http::isValidURI( $this->url ) ) {
229 $this->status = Status::newFatal( 'http-invalid-url' );
230 } else {
231 $this->status = Status::newGood( 100 ); // continue
234 if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
235 $this->timeout = $options['timeout'];
236 } else {
237 $this->timeout = $wgHTTPTimeout;
239 if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) {
240 $this->connectTimeout = $options['connectTimeout'];
241 } else {
242 $this->connectTimeout = $wgHTTPConnectTimeout;
244 if ( isset( $options['userAgent'] ) ) {
245 $this->setUserAgent( $options['userAgent'] );
248 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
249 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
251 foreach ( $members as $o ) {
252 if ( isset( $options[$o] ) ) {
253 // ensure that MWHttpRequest::method is always
254 // uppercased. Bug 36137
255 if ( $o == 'method' ) {
256 $options[$o] = strtoupper( $options[$o] );
258 $this->$o = $options[$o];
262 if ( $this->noProxy ) {
263 $this->proxy = ''; // noProxy takes precedence
268 * Simple function to test if we can make any sort of requests at all, using
269 * cURL or fopen()
270 * @return bool
272 public static function canMakeRequests() {
273 return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
277 * Generate a new request object
278 * @param string $url url to use
279 * @param array $options (optional) extra params to pass (see Http::request())
280 * @throws MWException
281 * @return CurlHttpRequest|PhpHttpRequest
282 * @see MWHttpRequest::__construct
284 public static function factory( $url, $options = null ) {
285 if ( !Http::$httpEngine ) {
286 Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
287 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
288 throw new MWException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
289 ' Http::$httpEngine is set to "curl"' );
292 switch ( Http::$httpEngine ) {
293 case 'curl':
294 return new CurlHttpRequest( $url, $options );
295 case 'php':
296 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
297 throw new MWException( __METHOD__ . ': allow_url_fopen needs to be enabled for pure PHP' .
298 ' http requests to work. If possible, curl should be used instead. See http://php.net/curl.' );
300 return new PhpHttpRequest( $url, $options );
301 default:
302 throw new MWException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
307 * Get the body, or content, of the response to the request
309 * @return String
311 public function getContent() {
312 return $this->content;
316 * Set the parameters of the request
318 * @param $args Array
319 * @todo overload the args param
321 public function setData( $args ) {
322 $this->postData = $args;
326 * Take care of setting up the proxy (do nothing if "noProxy" is set)
328 * @return void
330 public function proxySetup() {
331 global $wgHTTPProxy;
333 // If there is an explicit proxy set and proxies are not disabled, then use it
334 if ( $this->proxy && !$this->noProxy ) {
335 return;
338 // Otherwise, fallback to $wgHTTPProxy/http_proxy (when set) if this is not a machine
339 // local URL and proxies are not disabled
340 if ( Http::isLocalURL( $this->url ) || $this->noProxy ) {
341 $this->proxy = '';
342 } elseif ( $wgHTTPProxy ) {
343 $this->proxy = $wgHTTPProxy;
344 } elseif ( getenv( "http_proxy" ) ) {
345 $this->proxy = getenv( "http_proxy" );
350 * Set the referrer header
352 public function setReferer( $url ) {
353 $this->setHeader( 'Referer', $url );
357 * Set the user agent
358 * @param $UA string
360 public function setUserAgent( $UA ) {
361 $this->setHeader( 'User-Agent', $UA );
365 * Set an arbitrary header
366 * @param $name
367 * @param $value
369 public function setHeader( $name, $value ) {
370 // I feel like I should normalize the case here...
371 $this->reqHeaders[$name] = $value;
375 * Get an array of the headers
376 * @return array
378 public function getHeaderList() {
379 $list = array();
381 if ( $this->cookieJar ) {
382 $this->reqHeaders['Cookie'] =
383 $this->cookieJar->serializeToHttpRequest(
384 $this->parsedUrl['path'],
385 $this->parsedUrl['host']
389 foreach ( $this->reqHeaders as $name => $value ) {
390 $list[] = "$name: $value";
393 return $list;
397 * Set a read callback to accept data read from the HTTP request.
398 * By default, data is appended to an internal buffer which can be
399 * retrieved through $req->getContent().
401 * To handle data as it comes in -- especially for large files that
402 * would not fit in memory -- you can instead set your own callback,
403 * in the form function($resource, $buffer) where the first parameter
404 * is the low-level resource being read (implementation specific),
405 * and the second parameter is the data buffer.
407 * You MUST return the number of bytes handled in the buffer; if fewer
408 * bytes are reported handled than were passed to you, the HTTP fetch
409 * will be aborted.
411 * @param $callback Callback
412 * @throws MWException
414 public function setCallback( $callback ) {
415 if ( !is_callable( $callback ) ) {
416 throw new MWException( 'Invalid MwHttpRequest callback' );
418 $this->callback = $callback;
422 * A generic callback to read the body of the response from a remote
423 * server.
425 * @param $fh handle
426 * @param $content String
427 * @return int
429 public function read( $fh, $content ) {
430 $this->content .= $content;
431 return strlen( $content );
435 * Take care of whatever is necessary to perform the URI request.
437 * @return Status
439 public function execute() {
440 global $wgTitle;
442 wfProfileIn( __METHOD__ );
444 $this->content = "";
446 if ( strtoupper( $this->method ) == "HEAD" ) {
447 $this->headersOnly = true;
450 if ( is_object( $wgTitle ) && !isset( $this->reqHeaders['Referer'] ) ) {
451 $this->setReferer( wfExpandUrl( $wgTitle->getFullURL(), PROTO_CURRENT ) );
454 $this->proxySetup(); // set up any proxy as needed
456 if ( !$this->callback ) {
457 $this->setCallback( array( $this, 'read' ) );
460 if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
461 $this->setUserAgent( Http::userAgent() );
464 wfProfileOut( __METHOD__ );
468 * Parses the headers, including the HTTP status code and any
469 * Set-Cookie headers. This function expects the headers to be
470 * found in an array in the member variable headerList.
472 protected function parseHeader() {
473 wfProfileIn( __METHOD__ );
475 $lastname = "";
477 foreach ( $this->headerList as $header ) {
478 if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
479 $this->respVersion = $match[1];
480 $this->respStatus = $match[2];
481 } elseif ( preg_match( "#^[ \t]#", $header ) ) {
482 $last = count( $this->respHeaders[$lastname] ) - 1;
483 $this->respHeaders[$lastname][$last] .= "\r\n$header";
484 } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
485 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
486 $lastname = strtolower( $match[1] );
490 $this->parseCookies();
492 wfProfileOut( __METHOD__ );
496 * Sets HTTPRequest status member to a fatal value with the error
497 * message if the returned integer value of the status code was
498 * not successful (< 300) or a redirect (>=300 and < 400). (see
499 * RFC2616, section 10,
500 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a
501 * list of status codes.)
503 protected function setStatus() {
504 if ( !$this->respHeaders ) {
505 $this->parseHeader();
508 if ( (int)$this->respStatus > 399 ) {
509 list( $code, $message ) = explode( " ", $this->respStatus, 2 );
510 $this->status->fatal( "http-bad-status", $code, $message );
515 * Get the integer value of the HTTP status code (e.g. 200 for "200 Ok")
516 * (see RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
517 * for a list of status codes.)
519 * @return Integer
521 public function getStatus() {
522 if ( !$this->respHeaders ) {
523 $this->parseHeader();
526 return (int)$this->respStatus;
530 * Returns true if the last status code was a redirect.
532 * @return Boolean
534 public function isRedirect() {
535 if ( !$this->respHeaders ) {
536 $this->parseHeader();
539 $status = (int)$this->respStatus;
541 if ( $status >= 300 && $status <= 303 ) {
542 return true;
545 return false;
549 * Returns an associative array of response headers after the
550 * request has been executed. Because some headers
551 * (e.g. Set-Cookie) can appear more than once the, each value of
552 * the associative array is an array of the values given.
554 * @return Array
556 public function getResponseHeaders() {
557 if ( !$this->respHeaders ) {
558 $this->parseHeader();
561 return $this->respHeaders;
565 * Returns the value of the given response header.
567 * @param $header String
568 * @return String
570 public function getResponseHeader( $header ) {
571 if ( !$this->respHeaders ) {
572 $this->parseHeader();
575 if ( isset( $this->respHeaders[strtolower( $header )] ) ) {
576 $v = $this->respHeaders[strtolower( $header )];
577 return $v[count( $v ) - 1];
580 return null;
584 * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
586 * @param $jar CookieJar
588 public function setCookieJar( $jar ) {
589 $this->cookieJar = $jar;
593 * Returns the cookie jar in use.
595 * @return CookieJar
597 public function getCookieJar() {
598 if ( !$this->respHeaders ) {
599 $this->parseHeader();
602 return $this->cookieJar;
606 * Sets a cookie. Used before a request to set up any individual
607 * cookies. Used internally after a request to parse the
608 * Set-Cookie headers.
609 * @see Cookie::set
610 * @param $name
611 * @param $value null
612 * @param $attr null
614 public function setCookie( $name, $value = null, $attr = null ) {
615 if ( !$this->cookieJar ) {
616 $this->cookieJar = new CookieJar;
619 $this->cookieJar->setCookie( $name, $value, $attr );
623 * Parse the cookies in the response headers and store them in the cookie jar.
625 protected function parseCookies() {
626 wfProfileIn( __METHOD__ );
628 if ( !$this->cookieJar ) {
629 $this->cookieJar = new CookieJar;
632 if ( isset( $this->respHeaders['set-cookie'] ) ) {
633 $url = parse_url( $this->getFinalUrl() );
634 foreach ( $this->respHeaders['set-cookie'] as $cookie ) {
635 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
639 wfProfileOut( __METHOD__ );
643 * Returns the final URL after all redirections.
645 * Relative values of the "Location" header are incorrect as stated in RFC, however they do happen and modern browsers support them.
646 * This function loops backwards through all locations in order to build the proper absolute URI - Marooned at wikia-inc.com
648 * Note that the multiple Location: headers are an artifact of CURL -- they
649 * shouldn't actually get returned this way. Rewrite this when bug 29232 is
650 * taken care of (high-level redirect handling rewrite).
652 * @return string
654 public function getFinalUrl() {
655 $headers = $this->getResponseHeaders();
657 //return full url (fix for incorrect but handled relative location)
658 if ( isset( $headers['location'] ) ) {
659 $locations = $headers['location'];
660 $domain = '';
661 $foundRelativeURI = false;
662 $countLocations = count( $locations );
664 for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
665 $url = parse_url( $locations[$i] );
667 if ( isset( $url['host'] ) ) {
668 $domain = $url['scheme'] . '://' . $url['host'];
669 break; //found correct URI (with host)
670 } else {
671 $foundRelativeURI = true;
675 if ( $foundRelativeURI ) {
676 if ( $domain ) {
677 return $domain . $locations[$countLocations - 1];
678 } else {
679 $url = parse_url( $this->url );
680 if ( isset( $url['host'] ) ) {
681 return $url['scheme'] . '://' . $url['host'] . $locations[$countLocations - 1];
684 } else {
685 return $locations[$countLocations - 1];
689 return $this->url;
693 * Returns true if the backend can follow redirects. Overridden by the
694 * child classes.
695 * @return bool
697 public function canFollowRedirects() {
698 return true;
703 * MWHttpRequest implemented using internal curl compiled into PHP
705 class CurlHttpRequest extends MWHttpRequest {
706 const SUPPORTS_FILE_POSTS = true;
708 protected $curlOptions = array();
709 protected $headerText = "";
712 * @param $fh
713 * @param $content
714 * @return int
716 protected function readHeader( $fh, $content ) {
717 $this->headerText .= $content;
718 return strlen( $content );
721 public function execute() {
722 wfProfileIn( __METHOD__ );
724 parent::execute();
726 if ( !$this->status->isOK() ) {
727 wfProfileOut( __METHOD__ );
728 return $this->status;
731 $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
732 $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
733 $this->curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = $this->connectTimeout * 1000;
734 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
735 $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
736 $this->curlOptions[CURLOPT_HEADERFUNCTION] = array( $this, "readHeader" );
737 $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
738 $this->curlOptions[CURLOPT_ENCODING] = ""; # Enable compression
740 /* not sure these two are actually necessary */
741 if ( isset( $this->reqHeaders['Referer'] ) ) {
742 $this->curlOptions[CURLOPT_REFERER] = $this->reqHeaders['Referer'];
744 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
746 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost ? 2 : 0;
747 $this->curlOptions[CURLOPT_SSL_VERIFYPEER] = $this->sslVerifyCert;
749 if ( $this->caInfo ) {
750 $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
753 if ( $this->headersOnly ) {
754 $this->curlOptions[CURLOPT_NOBODY] = true;
755 $this->curlOptions[CURLOPT_HEADER] = true;
756 } elseif ( $this->method == 'POST' ) {
757 $this->curlOptions[CURLOPT_POST] = true;
758 $this->curlOptions[CURLOPT_POSTFIELDS] = $this->postData;
759 // Suppress 'Expect: 100-continue' header, as some servers
760 // will reject it with a 417 and Curl won't auto retry
761 // with HTTP 1.0 fallback
762 $this->reqHeaders['Expect'] = '';
763 } else {
764 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
767 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
769 $curlHandle = curl_init( $this->url );
771 if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
772 wfProfileOut( __METHOD__ );
773 throw new MWException( "Error setting curl options." );
776 if ( $this->followRedirects && $this->canFollowRedirects() ) {
777 wfSuppressWarnings();
778 if ( ! curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
779 wfDebug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
780 "Probably safe_mode or open_basedir is set.\n" );
781 // Continue the processing. If it were in curl_setopt_array,
782 // processing would have halted on its entry
784 wfRestoreWarnings();
787 $curlRes = curl_exec( $curlHandle );
788 if ( curl_errno( $curlHandle ) == CURLE_OPERATION_TIMEOUTED ) {
789 $this->status->fatal( 'http-timed-out', $this->url );
790 } elseif ( $curlRes === false ) {
791 $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
792 } else {
793 $this->headerList = explode( "\r\n", $this->headerText );
796 curl_close( $curlHandle );
798 $this->parseHeader();
799 $this->setStatus();
801 wfProfileOut( __METHOD__ );
803 return $this->status;
807 * @return bool
809 public function canFollowRedirects() {
810 if ( strval( ini_get( 'open_basedir' ) ) !== '' || wfIniGetBool( 'safe_mode' ) ) {
811 wfDebug( "Cannot follow redirects in safe mode\n" );
812 return false;
815 if ( !defined( 'CURLOPT_REDIR_PROTOCOLS' ) ) {
816 wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
817 return false;
820 return true;
824 class PhpHttpRequest extends MWHttpRequest {
827 * @param $url string
828 * @return string
830 protected function urlToTcp( $url ) {
831 $parsedUrl = parse_url( $url );
833 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
836 public function execute() {
837 wfProfileIn( __METHOD__ );
839 parent::execute();
841 if ( is_array( $this->postData ) ) {
842 $this->postData = wfArrayToCgi( $this->postData );
845 if ( $this->parsedUrl['scheme'] != 'http' &&
846 $this->parsedUrl['scheme'] != 'https' ) {
847 $this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] );
850 $this->reqHeaders['Accept'] = "*/*";
851 if ( $this->method == 'POST' ) {
852 // Required for HTTP 1.0 POSTs
853 $this->reqHeaders['Content-Length'] = strlen( $this->postData );
854 if ( !isset( $this->reqHeaders['Content-Type'] ) ) {
855 $this->reqHeaders['Content-Type'] = "application/x-www-form-urlencoded";
859 $options = array();
860 if ( $this->proxy ) {
861 $options['proxy'] = $this->urlToTCP( $this->proxy );
862 $options['request_fulluri'] = true;
865 if ( !$this->followRedirects ) {
866 $options['max_redirects'] = 0;
867 } else {
868 $options['max_redirects'] = $this->maxRedirects;
871 $options['method'] = $this->method;
872 $options['header'] = implode( "\r\n", $this->getHeaderList() );
873 // Note that at some future point we may want to support
874 // HTTP/1.1, but we'd have to write support for chunking
875 // in version of PHP < 5.3.1
876 $options['protocol_version'] = "1.0";
878 // This is how we tell PHP we want to deal with 404s (for example) ourselves.
879 // Only works on 5.2.10+
880 $options['ignore_errors'] = true;
882 if ( $this->postData ) {
883 $options['content'] = $this->postData;
886 $options['timeout'] = $this->timeout;
888 if ( $this->sslVerifyHost ) {
889 $options['CN_match'] = $this->parsedUrl['host'];
891 if ( $this->sslVerifyCert ) {
892 $options['verify_peer'] = true;
895 if ( is_dir( $this->caInfo ) ) {
896 $options['capath'] = $this->caInfo;
897 } elseif ( is_file( $this->caInfo ) ) {
898 $options['cafile'] = $this->caInfo;
899 } elseif ( $this->caInfo ) {
900 throw new MWException( "Invalid CA info passed: {$this->caInfo}" );
903 $scheme = $this->parsedUrl['scheme'];
904 $context = stream_context_create( array( "$scheme" => $options ) );
906 $this->headerList = array();
907 $reqCount = 0;
908 $url = $this->url;
910 $result = array();
912 do {
913 $reqCount++;
914 wfSuppressWarnings();
915 $fh = fopen( $url, "r", false, $context );
916 wfRestoreWarnings();
918 if ( !$fh ) {
919 break;
922 $result = stream_get_meta_data( $fh );
923 $this->headerList = $result['wrapper_data'];
924 $this->parseHeader();
926 if ( !$this->followRedirects ) {
927 break;
930 # Handle manual redirection
931 if ( !$this->isRedirect() || $reqCount > $this->maxRedirects ) {
932 break;
934 # Check security of URL
935 $url = $this->getResponseHeader( "Location" );
937 if ( !Http::isValidURI( $url ) ) {
938 wfDebug( __METHOD__ . ": insecure redirection\n" );
939 break;
941 } while ( true );
943 $this->setStatus();
945 if ( $fh === false ) {
946 $this->status->fatal( 'http-request-error' );
947 wfProfileOut( __METHOD__ );
948 return $this->status;
951 if ( $result['timed_out'] ) {
952 $this->status->fatal( 'http-timed-out', $this->url );
953 wfProfileOut( __METHOD__ );
954 return $this->status;
957 // If everything went OK, or we received some error code
958 // get the response body content.
959 if ( $this->status->isOK() || (int)$this->respStatus >= 300 ) {
960 while ( !feof( $fh ) ) {
961 $buf = fread( $fh, 8192 );
963 if ( $buf === false ) {
964 $this->status->fatal( 'http-read-error' );
965 break;
968 if ( strlen( $buf ) ) {
969 call_user_func( $this->callback, $fh, $buf );
973 fclose( $fh );
975 wfProfileOut( __METHOD__ );
977 return $this->status;