Update docs/hooks.txt for ShowSearchHitTitle
[mediawiki.git] / includes / http / Http.php
blob779d60634674b060f65b1e70681098d32c30a275
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\Logger\LoggerFactory;
23 /**
24 * Various HTTP related functions
25 * @ingroup HTTP
27 class Http {
28 static public $httpEngine = false;
30 /**
31 * Perform an HTTP request
33 * @param string $method HTTP method. Usually GET/POST
34 * @param string $url Full URL to act on. If protocol-relative, will be expanded to an http:// URL
35 * @param array $options Options to pass to MWHttpRequest object.
36 * Possible keys for the array:
37 * - timeout Timeout length in seconds
38 * - connectTimeout Timeout for connection, in seconds (curl only)
39 * - postData An array of key-value pairs or a url-encoded form data
40 * - proxy The proxy to use.
41 * Otherwise it will use $wgHTTPProxy (if set)
42 * Otherwise it will use the environment variable "http_proxy" (if set)
43 * - noProxy Don't use any proxy at all. Takes precedence over proxy value(s).
44 * - sslVerifyHost Verify hostname against certificate
45 * - sslVerifyCert Verify SSL certificate
46 * - caInfo Provide CA information
47 * - maxRedirects Maximum number of redirects to follow (defaults to 5)
48 * - followRedirects Whether to follow redirects (defaults to false).
49 * Note: this should only be used when the target URL is trusted,
50 * to avoid attacks on intranet services accessible by HTTP.
51 * - userAgent A user agent, if you want to override the default
52 * MediaWiki/$wgVersion
53 * - logger A \Psr\Logger\LoggerInterface instance for debug logging
54 * - username Username for HTTP Basic Authentication
55 * - password Password for HTTP Basic Authentication
56 * @param string $caller The method making this request, for profiling
57 * @return string|bool (bool)false on failure or a string on success
59 public static function request( $method, $url, $options = [], $caller = __METHOD__ ) {
60 wfDebug( "HTTP: $method: $url\n" );
62 $options['method'] = strtoupper( $method );
64 if ( !isset( $options['timeout'] ) ) {
65 $options['timeout'] = 'default';
67 if ( !isset( $options['connectTimeout'] ) ) {
68 $options['connectTimeout'] = 'default';
71 $req = MWHttpRequest::factory( $url, $options, $caller );
72 $status = $req->execute();
74 if ( $status->isOK() ) {
75 return $req->getContent();
76 } else {
77 $errors = $status->getErrorsByType( 'error' );
78 $logger = LoggerFactory::getInstance( 'http' );
79 $logger->warning( Status::wrap( $status )->getWikiText( false, false, 'en' ),
80 [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
81 return false;
85 /**
86 * Simple wrapper for Http::request( 'GET' )
87 * @see Http::request()
88 * @since 1.25 Second parameter $timeout removed. Second parameter
89 * is now $options which can be given a 'timeout'
91 * @param string $url
92 * @param array $options
93 * @param string $caller The method making this request, for profiling
94 * @return string|bool false on error
96 public static function get( $url, $options = [], $caller = __METHOD__ ) {
97 $args = func_get_args();
98 if ( isset( $args[1] ) && ( is_string( $args[1] ) || is_numeric( $args[1] ) ) ) {
99 // Second was used to be the timeout
100 // And third parameter used to be $options
101 wfWarn( "Second parameter should not be a timeout.", 2 );
102 $options = isset( $args[2] ) && is_array( $args[2] ) ?
103 $args[2] : [];
104 $options['timeout'] = $args[1];
105 $caller = __METHOD__;
107 return Http::request( 'GET', $url, $options, $caller );
111 * Simple wrapper for Http::request( 'POST' )
112 * @see Http::request()
114 * @param string $url
115 * @param array $options
116 * @param string $caller The method making this request, for profiling
117 * @return string|bool false on error
119 public static function post( $url, $options = [], $caller = __METHOD__ ) {
120 return Http::request( 'POST', $url, $options, $caller );
124 * A standard user-agent we can use for external requests.
125 * @return string
127 public static function userAgent() {
128 global $wgVersion;
129 return "MediaWiki/$wgVersion";
133 * Checks that the given URI is a valid one. Hardcoding the
134 * protocols, because we only want protocols that both cURL
135 * and php support.
137 * file:// should not be allowed here for security purpose (r67684)
139 * @todo FIXME this is wildly inaccurate and fails to actually check most stuff
141 * @param string $uri URI to check for validity
142 * @return bool
144 public static function isValidURI( $uri ) {
145 return (bool)preg_match(
146 '/^https?:\/\/[^\/\s]\S*$/D',
147 $uri
152 * Gets the relevant proxy from $wgHTTPProxy
154 * @return mixed The proxy address or an empty string if not set.
156 public static function getProxy() {
157 global $wgHTTPProxy;
159 if ( $wgHTTPProxy ) {
160 return $wgHTTPProxy;
163 return "";