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;
767 if ( defined(
'CURLOPT_SAFE_UPLOAD' ) ) {
768 $this->curlOptions[CURLOPT_SAFE_UPLOAD] =
true;
775 $this->curlOptions[CURLOPT_POSTFIELDS] =
$postData;
780 $this->reqHeaders[
'Expect'] =
'';
785 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->
getHeaderList();
787 $curlHandle = curl_init( $this->url );
789 if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
791 throw new MWException(
"Error setting curl options." );
796 if ( ! curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION,
true ) ) {
797 wfDebug( __METHOD__ .
": Couldn't set CURLOPT_FOLLOWLOCATION. " .
798 "Probably safe_mode or open_basedir is set.\n" );
805 $curlRes = curl_exec( $curlHandle );
806 if ( curl_errno( $curlHandle ) == CURLE_OPERATION_TIMEOUTED ) {
807 $this->status->fatal(
'http-timed-out', $this->url );
808 } elseif ( $curlRes ===
false ) {
809 $this->status->fatal(
'http-curl-error', curl_error( $curlHandle ) );
811 $this->headerList = explode(
"\r\n", $this->headerText );
814 curl_close( $curlHandle );
828 if ( strval( ini_get(
'open_basedir' ) ) !==
'' ||
wfIniGetBool(
'safe_mode' ) ) {
829 wfDebug(
"Cannot follow redirects in safe mode\n" );
833 if ( !defined(
'CURLOPT_REDIR_PROTOCOLS' ) ) {
834 wfDebug(
"Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
864 $certOptions =
array();
865 $certLocations =
array();
866 if ( $this->caInfo ) {
867 $certLocations =
array(
'manual' => $this->caInfo );
868 } elseif ( version_compare( PHP_VERSION,
'5.6.0',
'<' ) ) {
873 $certLocations = array_filter(
array(
874 getenv(
'SSL_CERT_DIR' ),
875 getenv(
'SSL_CERT_PATH' ),
876 '/etc/pki/tls/certs/ca-bundle.crt', # Fedora et al
877 '/etc/ssl/certs', #
Debian et al
878 '/etc/pki/tls/certs/ca-bundle.trust.crt',
879 '/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem',
880 '/System/Library/OpenSSL', # OSX
884 foreach( $certLocations
as $key => $cert ) {
885 if ( is_dir( $cert ) ) {
886 $certOptions[
'capath'] = $cert;
888 } elseif ( is_file( $cert ) ) {
889 $certOptions[
'cafile'] = $cert;
891 } elseif ( $key ===
'manual' ) {
893 throw new DomainException(
"Invalid CA info passed: $cert" );
906 $n = count( $this->fopenErrors ) + 1;
907 $this->fopenErrors +=
array(
"errno$n" => $errno,
"errstr$n" => $errstr );
915 if ( is_array( $this->postData ) ) {
919 if ( $this->parsedUrl[
'scheme'] !=
'http'
920 && $this->parsedUrl[
'scheme'] !=
'https' ) {
921 $this->status->fatal(
'http-invalid-scheme', $this->parsedUrl[
'scheme'] );
924 $this->reqHeaders[
'Accept'] =
"*/*";
925 $this->reqHeaders[
'Connection'] =
'Close';
926 if ( $this->method ==
'POST' ) {
928 $this->reqHeaders[
'Content-Length'] = strlen( $this->postData );
929 if ( !isset( $this->reqHeaders[
'Content-Type'] ) ) {
930 $this->reqHeaders[
'Content-Type'] =
"application/x-www-form-urlencoded";
937 'method' => $this->method,
939 'protocol_version' =>
'1.1',
940 'max_redirects' => $this->followRedirects ? $this->maxRedirects : 0,
941 'ignore_errors' =>
true,
942 'timeout' => $this->timeout,
944 'curl_verify_ssl_host' => $this->sslVerifyHost ? 2 : 0,
945 'curl_verify_ssl_peer' => $this->sslVerifyCert,
948 'verify_peer' => $this->sslVerifyCert,
949 'SNI_enabled' =>
true,
953 if ( $this->proxy ) {
954 $options[
'http'][
'proxy'] = $this->urlToTCP( $this->proxy );
955 $options[
'http'][
'request_fulluri'] =
true;
958 if ( $this->postData ) {
962 if ( $this->sslVerifyHost ) {
965 if ( version_compare( PHP_VERSION,
'5.6.0',
'>=' ) ) {
966 $options[
'ssl'][
'peer_name'] = $this->parsedUrl[
'host'];
968 $options[
'ssl'][
'CN_match'] = $this->parsedUrl[
'host'];
974 $context = stream_context_create(
$options );
976 $this->headerList =
array();
984 $this->fopenErrors =
array();
985 set_error_handler(
array( $this,
'errorHandler' ) );
986 $fh = fopen(
$url,
"r",
false, $context );
987 restore_error_handler();
995 if ( isset(
$options[
'ssl'][
'CN_match'] )
996 && (
$options[
'ssl'][
'CN_match'] ===
'commons.wikimedia.org'
997 ||
$options[
'ssl'][
'CN_match'] ===
'upload.wikimedia.org' )
999 $options[
'ssl'][
'CN_match'] =
'en.wikipedia.org';
1000 $context = stream_context_create(
$options );
1006 $result = stream_get_meta_data( $fh );
1007 $this->headerList =
$result[
'wrapper_data'];
1010 if ( !$this->followRedirects ) {
1014 # Handle manual redirection
1015 if ( !$this->
isRedirect() || $reqCount > $this->maxRedirects ) {
1018 # Check security of URL
1022 wfDebug( __METHOD__ .
": insecure redirection\n" );
1029 if ( $fh ===
false ) {
1030 if ( $this->fopenErrors ) {
1031 LoggerFactory::getInstance(
'http' )->warning( __CLASS__
1032 .
': error opening connection: {errstr1}', $this->fopenErrors );
1034 $this->status->fatal(
'http-request-error' );
1040 $this->status->fatal(
'http-timed-out', $this->url );
1047 if ( $this->status->isOK() || (int)$this->respStatus >= 300 ) {
1048 while ( !feof( $fh ) ) {
1049 $buf = fread( $fh, 8192 );
1051 if ( $buf ===
false ) {
1052 $this->status->fatal(
'http-read-error' );
1056 if ( strlen( $buf ) ) {
1057 call_user_func( $this->callback, $fh, $buf );