62 wfDebug(
"HTTP: $method: $url\n" );
65 $options[
'method'] = strtoupper( $method );
67 if ( !isset(
$options[
'timeout'] ) ) {
70 if ( !isset(
$options[
'connectTimeout'] ) ) {
71 $options[
'connectTimeout'] =
'default';
75 $status = $req->execute();
78 if ( $status->isOK() ) {
79 $content = $req->getContent();
81 $errors = $status->getErrorsByType(
'error' );
82 $logger = LoggerFactory::getInstance(
'http' );
83 $logger->warning( $status->getWikiText(),
array(
'caller' => $caller ) );
98 public static function get( $url, $timeout =
'default',
$options =
array() ) {
130 if ( preg_match(
'!^http://([\w.-]+)[/:].*$!', $url,
$matches ) ) {
133 $domainParts = explode(
'.', $host );
135 $domainParts = array_reverse( $domainParts );
138 $countParts = count( $domainParts );
139 for ( $i = 0; $i < $countParts; $i++ ) {
140 $domainPart = $domainParts[$i];
142 $domain = $domainPart;
144 $domain = $domainPart .
'.' . $domain;
147 if ( $wgConf->isLocalVHost( $domain ) ) {
162 return "MediaWiki/$wgVersion";
179 '/^https?:\/\/[^\/\s]\S*$/D',
229 global $wgHTTPTimeout, $wgHTTPConnectTimeout;
241 $this->timeout =
$options[
'timeout'];
243 $this->timeout = $wgHTTPTimeout;
245 if ( isset(
$options[
'connectTimeout'] ) &&
$options[
'connectTimeout'] !=
'default' ) {
246 $this->connectTimeout =
$options[
'connectTimeout'];
248 $this->connectTimeout = $wgHTTPConnectTimeout;
250 if ( isset(
$options[
'userAgent'] ) ) {
254 $members =
array(
"postData",
"proxy",
"noProxy",
"sslVerifyHost",
"caInfo",
255 "method",
"followRedirects",
"maxRedirects",
"sslVerifyCert",
"callback" );
257 foreach ( $members
as $o ) {
261 if ( $o ==
'method' ) {
268 if ( $this->noProxy ) {
279 return function_exists(
'curl_init' ) ||
wfIniGetBool(
'allow_url_fopen' );
294 throw new MWException( __METHOD__ .
': curl (http://php.net/curl) is not installed, but' .
295 ' Http::$httpEngine is set to "curl"' );
303 throw new MWException( __METHOD__ .
': allow_url_fopen ' .
304 'needs to be enabled for pure PHP http requests to ' .
305 'work. If possible, curl should be used instead. See ' .
306 'http://php.net/curl.'
311 throw new MWException( __METHOD__ .
': The setting of Http::$httpEngine is not valid.' );
331 $this->postData =
$args;
343 if ( $this->proxy && !$this->noProxy ) {
351 } elseif ( $wgHTTPProxy ) {
352 $this->proxy = $wgHTTPProxy;
381 if ( $this->cookieJar ) {
382 $this->reqHeaders[
'Cookie'] =
383 $this->cookieJar->serializeToHttpRequest(
384 $this->parsedUrl[
'path'],
385 $this->parsedUrl[
'host']
390 $list[] =
"$name: $value";
416 throw new MWException(
'Invalid MwHttpRequest callback' );
444 if ( strtoupper( $this->method ) ==
"HEAD" ) {
445 $this->headersOnly =
true;
450 if ( !$this->callback ) {
454 if ( !isset( $this->reqHeaders[
'User-Agent'] ) ) {
471 foreach ( $this->headerList
as $header ) {
472 if ( preg_match(
"#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
473 $this->respVersion = $match[1];
474 $this->respStatus = $match[2];
475 } elseif ( preg_match(
"#^[ \t]#", $header ) ) {
476 $last = count( $this->respHeaders[$lastname] ) - 1;
477 $this->respHeaders[$lastname][
$last] .=
"\r\n$header";
478 } elseif ( preg_match(
"#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
479 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
480 $lastname = strtolower( $match[1] );
498 if ( !$this->respHeaders ) {
502 if ( (
int)$this->respStatus > 399 ) {
503 list( $code, $message ) = explode(
" ", $this->respStatus, 2 );
504 $this->status->fatal(
"http-bad-status", $code, $message );
516 if ( !$this->respHeaders ) {
529 if ( !$this->respHeaders ) {
533 $status = (int)$this->respStatus;
551 if ( !$this->respHeaders ) {
565 if ( !$this->respHeaders ) {
569 if ( isset( $this->respHeaders[strtolower( $header )] ) ) {
570 $v = $this->respHeaders[strtolower( $header )];
571 return $v[count( $v ) - 1];
583 $this->cookieJar = $jar;
592 if ( !$this->respHeaders ) {
609 if ( !$this->cookieJar ) {
622 if ( !$this->cookieJar ) {
626 if ( isset( $this->respHeaders[
'set-cookie'] ) ) {
628 foreach ( $this->respHeaders[
'set-cookie']
as $cookie ) {
629 $this->cookieJar->parseCookieResponseHeader( $cookie,
$url[
'host'] );
656 if ( isset( $headers[
'location'] ) ) {
657 $locations = $headers[
'location'];
659 $foundRelativeURI =
false;
660 $countLocations = count( $locations );
662 for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
663 $url = parse_url( $locations[$i] );
665 if ( isset(
$url[
'host'] ) ) {
666 $domain =
$url[
'scheme'] .
'://' .
$url[
'host'];
669 $foundRelativeURI =
true;
673 if ( $foundRelativeURI ) {
675 return $domain . $locations[$countLocations - 1];
677 $url = parse_url( $this->url );
678 if ( isset(
$url[
'host'] ) ) {
679 return $url[
'scheme'] .
'://' .
$url[
'host'] .
680 $locations[$countLocations - 1];
684 return $locations[$countLocations - 1];
725 if ( !$this->status->isOK() ) {
734 if ( defined(
'CURLOPT_CONNECTTIMEOUT_MS' ) ) {
735 $this->curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = $this->connectTimeout * 1000;
738 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
740 $this->curlOptions[CURLOPT_HEADERFUNCTION] =
array( $this,
"readHeader" );
742 $this->curlOptions[CURLOPT_ENCODING] =
""; # Enable compression
744 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders[
'User-Agent'];
746 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost ? 2 : 0;
749 if ( $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;
764 if ( defined(
'CURLOPT_SAFE_UPLOAD' ) ) {
765 $this->curlOptions[CURLOPT_SAFE_UPLOAD] =
true;
772 $this->curlOptions[CURLOPT_POSTFIELDS] =
$postData;
777 $this->reqHeaders[
'Expect'] =
'';
782 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->
getHeaderList();
784 $curlHandle = curl_init( $this->url );
786 if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
788 throw new MWException(
"Error setting curl options." );
793 if ( ! curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION,
true ) ) {
794 wfDebug( __METHOD__ .
": Couldn't set CURLOPT_FOLLOWLOCATION. " .
795 "Probably safe_mode or open_basedir is set.\n" );
802 $curlRes = curl_exec( $curlHandle );
803 if ( curl_errno( $curlHandle ) == CURLE_OPERATION_TIMEOUTED ) {
804 $this->status->fatal(
'http-timed-out', $this->url );
805 } elseif ( $curlRes ===
false ) {
806 $this->status->fatal(
'http-curl-error', curl_error( $curlHandle ) );
808 $this->headerList = explode(
"\r\n", $this->headerText );
811 curl_close( $curlHandle );
825 if ( strval( ini_get(
'open_basedir' ) ) !==
'' ||
wfIniGetBool(
'safe_mode' ) ) {
826 wfDebug(
"Cannot follow redirects in safe mode\n" );
830 if ( !defined(
'CURLOPT_REDIR_PROTOCOLS' ) ) {
831 wfDebug(
"Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
861 $certOptions =
array();
862 $certLocations =
array();
863 if ( $this->caInfo ) {
864 $certLocations =
array(
'manual' => $this->caInfo );
865 } elseif ( version_compare( PHP_VERSION,
'5.6.0',
'<' ) ) {
870 $certLocations = array_filter(
array(
871 getenv(
'SSL_CERT_DIR' ),
872 getenv(
'SSL_CERT_PATH' ),
873 '/etc/pki/tls/certs/ca-bundle.crt', # Fedora et al
874 '/etc/ssl/certs', #
Debian et al
875 '/etc/pki/tls/certs/ca-bundle.trust.crt',
876 '/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem',
877 '/System/Library/OpenSSL', # OSX
881 foreach( $certLocations
as $key => $cert ) {
882 if ( is_dir( $cert ) ) {
883 $certOptions[
'capath'] = $cert;
885 } elseif ( is_file( $cert ) ) {
886 $certOptions[
'cafile'] = $cert;
888 } elseif ( $key ===
'manual' ) {
890 throw new DomainException(
"Invalid CA info passed: $cert" );
903 $n = count( $this->fopenErrors ) + 1;
904 $this->fopenErrors +=
array(
"errno$n" => $errno,
"errstr$n" => $errstr );
912 if ( is_array( $this->postData ) ) {
916 if ( $this->parsedUrl[
'scheme'] !=
'http'
917 && $this->parsedUrl[
'scheme'] !=
'https' ) {
918 $this->status->fatal(
'http-invalid-scheme', $this->parsedUrl[
'scheme'] );
921 $this->reqHeaders[
'Accept'] =
"*/*";
922 $this->reqHeaders[
'Connection'] =
'Close';
923 if ( $this->method ==
'POST' ) {
925 $this->reqHeaders[
'Content-Length'] = strlen( $this->postData );
926 if ( !isset( $this->reqHeaders[
'Content-Type'] ) ) {
927 $this->reqHeaders[
'Content-Type'] =
"application/x-www-form-urlencoded";
934 'method' => $this->method,
936 'protocol_version' =>
'1.1',
937 'max_redirects' => $this->followRedirects ? $this->maxRedirects : 0,
938 'ignore_errors' =>
true,
939 'timeout' => $this->timeout,
941 'curl_verify_ssl_host' => $this->sslVerifyHost ? 2 : 0,
942 'curl_verify_ssl_peer' => $this->sslVerifyCert,
945 'verify_peer' => $this->sslVerifyCert,
946 'SNI_enabled' =>
true,
950 if ( $this->proxy ) {
951 $options[
'http'][
'proxy'] = $this->urlToTCP( $this->proxy );
952 $options[
'http'][
'request_fulluri'] =
true;
955 if ( $this->postData ) {
959 if ( $this->sslVerifyHost ) {
962 if ( version_compare( PHP_VERSION,
'5.6.0',
'>=' ) ) {
963 $options[
'ssl'][
'peer_name'] = $this->parsedUrl[
'host'];
965 $options[
'ssl'][
'CN_match'] = $this->parsedUrl[
'host'];
971 $context = stream_context_create(
$options );
973 $this->headerList =
array();
981 $this->fopenErrors =
array();
982 set_error_handler(
array( $this,
'errorHandler' ) );
983 $fh = fopen(
$url,
"r",
false, $context );
984 restore_error_handler();
992 if ( isset(
$options[
'ssl'][
'CN_match'] )
993 && (
$options[
'ssl'][
'CN_match'] ===
'commons.wikimedia.org'
994 ||
$options[
'ssl'][
'CN_match'] ===
'upload.wikimedia.org' )
996 $options[
'ssl'][
'CN_match'] =
'en.wikipedia.org';
997 $context = stream_context_create(
$options );
1003 $result = stream_get_meta_data( $fh );
1004 $this->headerList =
$result[
'wrapper_data'];
1007 if ( !$this->followRedirects ) {
1011 # Handle manual redirection
1012 if ( !$this->
isRedirect() || $reqCount > $this->maxRedirects ) {
1015 # Check security of URL
1019 wfDebug( __METHOD__ .
": insecure redirection\n" );
1026 if ( $fh ===
false ) {
1027 if ( $this->fopenErrors ) {
1028 LoggerFactory::getInstance(
'http' )->warning( __CLASS__
1029 .
': error opening connection: {errstr1}', $this->fopenErrors );
1031 $this->status->fatal(
'http-request-error' );
1037 $this->status->fatal(
'http-timed-out', $this->url );
1044 if ( $this->status->isOK() || (int)$this->respStatus >= 300 ) {
1045 while ( !feof( $fh ) ) {
1046 $buf = fread( $fh, 8192 );
1048 if ( $buf ===
false ) {
1049 $this->status->fatal(
'http-read-error' );
1053 if ( strlen( $buf ) ) {
1054 call_user_func( $this->callback, $fh, $buf );