Split HttpFunctions.php into separate files
[lhc/web/wiklou.git] / includes / http / Http.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 use MediaWiki\Logger\LoggerFactory;
22
23 /**
24 * Various HTTP related functions
25 * @ingroup HTTP
26 */
27 class Http {
28 static public $httpEngine = false;
29
30 /**
31 * Perform an HTTP request
32 *
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 * @param string $caller The method making this request, for profiling
54 * @return string|bool (bool)false on failure or a string on success
55 */
56 public static function request( $method, $url, $options = [], $caller = __METHOD__ ) {
57 wfDebug( "HTTP: $method: $url\n" );
58
59 $options['method'] = strtoupper( $method );
60
61 if ( !isset( $options['timeout'] ) ) {
62 $options['timeout'] = 'default';
63 }
64 if ( !isset( $options['connectTimeout'] ) ) {
65 $options['connectTimeout'] = 'default';
66 }
67
68 $req = MWHttpRequest::factory( $url, $options, $caller );
69 $status = $req->execute();
70
71 if ( $status->isOK() ) {
72 return $req->getContent();
73 } else {
74 $errors = $status->getErrorsByType( 'error' );
75 $logger = LoggerFactory::getInstance( 'http' );
76 $logger->warning( $status->getWikiText( false, false, 'en' ),
77 [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
78 return false;
79 }
80 }
81
82 /**
83 * Simple wrapper for Http::request( 'GET' )
84 * @see Http::request()
85 * @since 1.25 Second parameter $timeout removed. Second parameter
86 * is now $options which can be given a 'timeout'
87 *
88 * @param string $url
89 * @param array $options
90 * @param string $caller The method making this request, for profiling
91 * @return string|bool false on error
92 */
93 public static function get( $url, $options = [], $caller = __METHOD__ ) {
94 $args = func_get_args();
95 if ( isset( $args[1] ) && ( is_string( $args[1] ) || is_numeric( $args[1] ) ) ) {
96 // Second was used to be the timeout
97 // And third parameter used to be $options
98 wfWarn( "Second parameter should not be a timeout.", 2 );
99 $options = isset( $args[2] ) && is_array( $args[2] ) ?
100 $args[2] : [];
101 $options['timeout'] = $args[1];
102 $caller = __METHOD__;
103 }
104 return Http::request( 'GET', $url, $options, $caller );
105 }
106
107 /**
108 * Simple wrapper for Http::request( 'POST' )
109 * @see Http::request()
110 *
111 * @param string $url
112 * @param array $options
113 * @param string $caller The method making this request, for profiling
114 * @return string|bool false on error
115 */
116 public static function post( $url, $options = [], $caller = __METHOD__ ) {
117 return Http::request( 'POST', $url, $options, $caller );
118 }
119
120 /**
121 * A standard user-agent we can use for external requests.
122 * @return string
123 */
124 public static function userAgent() {
125 global $wgVersion;
126 return "MediaWiki/$wgVersion";
127 }
128
129 /**
130 * Checks that the given URI is a valid one. Hardcoding the
131 * protocols, because we only want protocols that both cURL
132 * and php support.
133 *
134 * file:// should not be allowed here for security purpose (r67684)
135 *
136 * @todo FIXME this is wildly inaccurate and fails to actually check most stuff
137 *
138 * @param string $uri URI to check for validity
139 * @return bool
140 */
141 public static function isValidURI( $uri ) {
142 return preg_match(
143 '/^https?:\/\/[^\/\s]\S*$/D',
144 $uri
145 );
146 }
147
148 /**
149 * Gets the relevant proxy from $wgHTTPProxy
150 *
151 * @return mixed The proxy address or an empty string if not set.
152 */
153 public static function getProxy() {
154 global $wgHTTPProxy;
155
156 if ( $wgHTTPProxy ) {
157 return $wgHTTPProxy;
158 }
159
160 return "";
161 }
162 }