63 wfDebug(
"HTTP: $method: $url\n" );
66 $options[
'method'] = strtoupper( $method );
68 if ( !isset(
$options[
'timeout'] ) ) {
71 if ( !isset(
$options[
'connectTimeout'] ) ) {
72 $options[
'connectTimeout'] =
'default';
76 $status = $req->execute();
79 if ( $status->isOK() ) {
80 $content = $req->getContent();
82 $errors = $status->getErrorsByType(
'error' );
83 $logger = LoggerFactory::getInstance(
'http' );
84 $logger->warning( $status->getWikiText(),
array(
'caller' => $caller ) );
99 public static function get( $url, $timeout =
'default',
$options =
array() ) {
131 if ( preg_match(
'!^http://([\w.-]+)[/:].*$!', $url,
$matches ) ) {
134 $domainParts = explode(
'.', $host );
136 $domainParts = array_reverse( $domainParts );
139 $countParts = count( $domainParts );
140 for ( $i = 0; $i < $countParts; $i++ ) {
141 $domainPart = $domainParts[$i];
143 $domain = $domainPart;
145 $domain = $domainPart .
'.' . $domain;
148 if ( $wgConf->isLocalVHost( $domain ) ) {
163 return "MediaWiki/$wgVersion";
180 '/^https?:\/\/[^\/\s]\S*$/D',
230 global $wgHTTPTimeout, $wgHTTPConnectTimeout;
242 $this->timeout =
$options[
'timeout'];
244 $this->timeout = $wgHTTPTimeout;
246 if ( isset(
$options[
'connectTimeout'] ) &&
$options[
'connectTimeout'] !=
'default' ) {
247 $this->connectTimeout =
$options[
'connectTimeout'];
249 $this->connectTimeout = $wgHTTPConnectTimeout;
251 if ( isset(
$options[
'userAgent'] ) ) {
255 $members =
array(
"postData",
"proxy",
"noProxy",
"sslVerifyHost",
"caInfo",
256 "method",
"followRedirects",
"maxRedirects",
"sslVerifyCert",
"callback" );
258 foreach ( $members
as $o ) {
262 if ( $o ==
'method' ) {
269 if ( $this->noProxy ) {
280 return function_exists(
'curl_init' ) ||
wfIniGetBool(
'allow_url_fopen' );
295 throw new MWException( __METHOD__ .
': curl (http://php.net/curl) is not installed, but' .
296 ' Http::$httpEngine is set to "curl"' );
304 throw new MWException( __METHOD__ .
': allow_url_fopen ' .
305 'needs to be enabled for pure PHP http requests to ' .
306 'work. If possible, curl should be used instead. See ' .
307 'http://php.net/curl.'
312 throw new MWException( __METHOD__ .
': The setting of Http::$httpEngine is not valid.' );
332 $this->postData =
$args;
344 if ( $this->proxy && !$this->noProxy ) {
352 } elseif ( $wgHTTPProxy ) {
353 $this->proxy = $wgHTTPProxy;
354 } elseif ( getenv(
"http_proxy" ) ) {
355 $this->proxy = getenv(
"http_proxy" );
384 if ( $this->cookieJar ) {
385 $this->reqHeaders[
'Cookie'] =
386 $this->cookieJar->serializeToHttpRequest(
387 $this->parsedUrl[
'path'],
388 $this->parsedUrl[
'host']
393 $list[] =
"$name: $value";
419 throw new MWException(
'Invalid MwHttpRequest callback' );
447 if ( strtoupper( $this->method ) ==
"HEAD" ) {
448 $this->headersOnly =
true;
453 if ( !$this->callback ) {
457 if ( !isset( $this->reqHeaders[
'User-Agent'] ) ) {
474 foreach ( $this->headerList
as $header ) {
475 if ( preg_match(
"#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
476 $this->respVersion = $match[1];
477 $this->respStatus = $match[2];
478 } elseif ( preg_match(
"#^[ \t]#", $header ) ) {
479 $last = count( $this->respHeaders[$lastname] ) - 1;
480 $this->respHeaders[$lastname][
$last] .=
"\r\n$header";
481 } elseif ( preg_match(
"#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
482 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
483 $lastname = strtolower( $match[1] );
501 if ( !$this->respHeaders ) {
505 if ( (
int)$this->respStatus > 399 ) {
506 list( $code, $message ) = explode(
" ", $this->respStatus, 2 );
507 $this->status->fatal(
"http-bad-status", $code, $message );
519 if ( !$this->respHeaders ) {
532 if ( !$this->respHeaders ) {
536 $status = (int)$this->respStatus;
554 if ( !$this->respHeaders ) {
568 if ( !$this->respHeaders ) {
572 if ( isset( $this->respHeaders[strtolower( $header )] ) ) {
573 $v = $this->respHeaders[strtolower( $header )];
574 return $v[count( $v ) - 1];
586 $this->cookieJar = $jar;
595 if ( !$this->respHeaders ) {
612 if ( !$this->cookieJar ) {
625 if ( !$this->cookieJar ) {
629 if ( isset( $this->respHeaders[
'set-cookie'] ) ) {
631 foreach ( $this->respHeaders[
'set-cookie']
as $cookie ) {
632 $this->cookieJar->parseCookieResponseHeader( $cookie,
$url[
'host'] );
659 if ( isset( $headers[
'location'] ) ) {
660 $locations = $headers[
'location'];
662 $foundRelativeURI =
false;
663 $countLocations = count( $locations );
665 for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
666 $url = parse_url( $locations[$i] );
668 if ( isset(
$url[
'host'] ) ) {
669 $domain =
$url[
'scheme'] .
'://' .
$url[
'host'];
672 $foundRelativeURI =
true;
676 if ( $foundRelativeURI ) {
678 return $domain . $locations[$countLocations - 1];
680 $url = parse_url( $this->url );
681 if ( isset(
$url[
'host'] ) ) {
682 return $url[
'scheme'] .
'://' .
$url[
'host'] .
683 $locations[$countLocations - 1];
687 return $locations[$countLocations - 1];
728 if ( !$this->status->isOK() ) {
737 if ( defined(
'CURLOPT_CONNECTTIMEOUT_MS' ) ) {
738 $this->curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = $this->connectTimeout * 1000;
741 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
743 $this->curlOptions[CURLOPT_HEADERFUNCTION] =
array( $this,
"readHeader" );
745 $this->curlOptions[CURLOPT_ENCODING] =
""; # Enable compression
747 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders[
'User-Agent'];
749 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost ? 2 : 0;
752 if ( $this->caInfo ) {
756 if ( $this->headersOnly ) {
757 $this->curlOptions[CURLOPT_NOBODY] =
true;
758 $this->curlOptions[CURLOPT_HEADER] =
true;
759 } elseif ( $this->method ==
'POST' ) {
760 $this->curlOptions[CURLOPT_POST] =
true;
765 $this->reqHeaders[
'Expect'] =
'';
770 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->
getHeaderList();
772 $curlHandle = curl_init( $this->url );
774 if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
776 throw new MWException(
"Error setting curl options." );
781 if ( ! curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION,
true ) ) {
782 wfDebug( __METHOD__ .
": Couldn't set CURLOPT_FOLLOWLOCATION. " .
783 "Probably safe_mode or open_basedir is set.\n" );
790 $curlRes = curl_exec( $curlHandle );
791 if ( curl_errno( $curlHandle ) == CURLE_OPERATION_TIMEOUTED ) {
792 $this->status->fatal(
'http-timed-out', $this->url );
793 } elseif ( $curlRes ===
false ) {
794 $this->status->fatal(
'http-curl-error', curl_error( $curlHandle ) );
796 $this->headerList = explode(
"\r\n", $this->headerText );
799 curl_close( $curlHandle );
813 if ( strval( ini_get(
'open_basedir' ) ) !==
'' ||
wfIniGetBool(
'safe_mode' ) ) {
814 wfDebug(
"Cannot follow redirects in safe mode\n" );
818 if ( !defined(
'CURLOPT_REDIR_PROTOCOLS' ) ) {
819 wfDebug(
"Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
849 $certOptions =
array();
850 $certLocations =
array();
851 if ( $this->caInfo ) {
852 $certLocations =
array(
'manual' => $this->caInfo );
853 } elseif ( version_compare( PHP_VERSION,
'5.6.0',
'<' ) ) {
858 $certLocations = array_filter(
array(
859 getenv(
'SSL_CERT_DIR' ),
860 getenv(
'SSL_CERT_PATH' ),
861 '/etc/pki/tls/certs/ca-bundle.crt', # Fedora et al
862 '/etc/ssl/certs', #
Debian et al
863 '/etc/pki/tls/certs/ca-bundle.trust.crt',
864 '/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem',
865 '/System/Library/OpenSSL', # OSX
869 foreach( $certLocations
as $key => $cert ) {
870 if ( is_dir( $cert ) ) {
871 $certOptions[
'capath'] = $cert;
873 } elseif ( is_file( $cert ) ) {
874 $certOptions[
'cafile'] = $cert;
876 } elseif ( $key ===
'manual' ) {
878 throw new DomainException(
"Invalid CA info passed: $cert" );
891 $n = count( $this->fopenErrors ) + 1;
892 $this->fopenErrors +=
array(
"errno$n" => $errno,
"errstr$n" => $errstr );
900 if ( is_array( $this->postData ) ) {
904 if ( $this->parsedUrl[
'scheme'] !=
'http'
905 && $this->parsedUrl[
'scheme'] !=
'https' ) {
906 $this->status->fatal(
'http-invalid-scheme', $this->parsedUrl[
'scheme'] );
909 $this->reqHeaders[
'Accept'] =
"*/*";
910 $this->reqHeaders[
'Connection'] =
'Close';
911 if ( $this->method ==
'POST' ) {
913 $this->reqHeaders[
'Content-Length'] = strlen( $this->postData );
914 if ( !isset( $this->reqHeaders[
'Content-Type'] ) ) {
915 $this->reqHeaders[
'Content-Type'] =
"application/x-www-form-urlencoded";
922 'method' => $this->method,
924 'protocol_version' =>
'1.1',
925 'max_redirects' => $this->followRedirects ? $this->maxRedirects : 0,
926 'ignore_errors' =>
true,
927 'timeout' => $this->timeout,
929 'curl_verify_ssl_host' => $this->sslVerifyHost ? 2 : 0,
930 'curl_verify_ssl_peer' => $this->sslVerifyCert,
933 'verify_peer' => $this->sslVerifyCert,
934 'SNI_enabled' =>
true,
938 if ( $this->proxy ) {
939 $options[
'http'][
'proxy'] = $this->urlToTCP( $this->proxy );
940 $options[
'http'][
'request_fulluri'] =
true;
943 if ( $this->postData ) {
947 if ( $this->sslVerifyHost ) {
950 if ( version_compare( PHP_VERSION,
'5.6.0',
'>=' ) ) {
951 $options[
'ssl'][
'peer_name'] = $this->parsedUrl[
'host'];
953 $options[
'ssl'][
'CN_match'] = $this->parsedUrl[
'host'];
959 $context = stream_context_create(
$options );
961 $this->headerList =
array();
969 $this->fopenErrors =
array();
970 set_error_handler(
array( $this,
'errorHandler' ) );
971 $fh = fopen(
$url,
"r",
false, $context );
972 restore_error_handler();
980 if ( isset(
$options[
'ssl'][
'CN_match'] )
981 && (
$options[
'ssl'][
'CN_match'] ===
'commons.wikimedia.org'
982 ||
$options[
'ssl'][
'CN_match'] ===
'upload.wikimedia.org' )
984 $options[
'ssl'][
'CN_match'] =
'en.wikipedia.org';
985 $context = stream_context_create(
$options );
991 $result = stream_get_meta_data( $fh );
992 $this->headerList =
$result[
'wrapper_data'];
995 if ( !$this->followRedirects ) {
999 # Handle manual redirection
1000 if ( !$this->
isRedirect() || $reqCount > $this->maxRedirects ) {
1003 # Check security of URL
1007 wfDebug( __METHOD__ .
": insecure redirection\n" );
1014 if ( $fh ===
false ) {
1015 if ( $this->fopenErrors ) {
1016 LoggerFactory::getInstance(
'http' )->warning( __CLASS__
1017 .
': error opening connection: {errstr1}', $this->fopenErrors );
1019 $this->status->fatal(
'http-request-error' );
1025 $this->status->fatal(
'http-timed-out', $this->url );
1032 if ( $this->status->isOK() || (int)$this->respStatus >= 300 ) {
1033 while ( !feof( $fh ) ) {
1034 $buf = fread( $fh, 8192 );
1036 if ( $buf ===
false ) {
1037 $this->status->fatal(
'http-read-error' );
1041 if ( strlen( $buf ) ) {
1042 call_user_func( $this->callback, $fh, $buf );