Merge "Remove parameter 'options' from hook 'SkinEditSectionLinks'"
[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 use MediaWiki\MediaWikiServices;
23
24 /**
25 * Various HTTP related functions
26 * @deprecated since 1.34
27 * @ingroup HTTP
28 */
29 class Http {
30 /** @deprecated since 1.34, just use the default engine */
31 public static $httpEngine = null;
32
33 /**
34 * Perform an HTTP request
35 *
36 * @deprecated since 1.34, use HttpRequestFactory::request()
37 *
38 * @param string $method HTTP method. Usually GET/POST
39 * @param string $url Full URL to act on. If protocol-relative, will be expanded to an http:// URL
40 * @param array $options Options to pass to MWHttpRequest object. See HttpRequestFactory::create
41 * docs
42 * @param string $caller The method making this request, for profiling
43 * @return string|bool (bool)false on failure or a string on success
44 */
45 public static function request( $method, $url, array $options = [], $caller = __METHOD__ ) {
46 $ret = MediaWikiServices::getInstance()->getHttpRequestFactory()->request(
47 $method, $url, $options, $caller );
48 return is_string( $ret ) ? $ret : false;
49 }
50
51 /**
52 * Simple wrapper for Http::request( 'GET' )
53 *
54 * @deprecated since 1.34, use HttpRequestFactory::get()
55 *
56 * @since 1.25 Second parameter $timeout removed. Second parameter
57 * is now $options which can be given a 'timeout'
58 *
59 * @param string $url
60 * @param array $options
61 * @param string $caller The method making this request, for profiling
62 * @return string|bool false on error
63 */
64 public static function get( $url, array $options = [], $caller = __METHOD__ ) {
65 $args = func_get_args();
66 if ( isset( $args[1] ) && ( is_string( $args[1] ) || is_numeric( $args[1] ) ) ) {
67 // Second was used to be the timeout
68 // And third parameter used to be $options
69 wfWarn( "Second parameter should not be a timeout.", 2 );
70 $options = isset( $args[2] ) && is_array( $args[2] ) ?
71 $args[2] : [];
72 $options['timeout'] = $args[1];
73 $caller = __METHOD__;
74 }
75 return self::request( 'GET', $url, $options, $caller );
76 }
77
78 /**
79 * Simple wrapper for Http::request( 'POST' )
80 *
81 * @deprecated since 1.34, use HttpRequestFactory::post()
82 *
83 * @param string $url
84 * @param array $options
85 * @param string $caller The method making this request, for profiling
86 * @return string|bool false on error
87 */
88 public static function post( $url, array $options = [], $caller = __METHOD__ ) {
89 return self::request( 'POST', $url, $options, $caller );
90 }
91
92 /**
93 * A standard user-agent we can use for external requests.
94 *
95 * @deprecated since 1.34, use HttpRequestFactory::getUserAgent()
96 * @return string
97 */
98 public static function userAgent() {
99 return MediaWikiServices::getInstance()->getHttpRequestFactory()->getUserAgent();
100 }
101
102 /**
103 * Check that the given URI is a valid one.
104 *
105 * This hardcodes a small set of protocols only, because we want to
106 * deterministically reject protocols not supported by all HTTP-transport
107 * methods.
108 *
109 * "file://" specifically must not be allowed, for security purpose
110 * (see <https://www.mediawiki.org/wiki/Special:Code/MediaWiki/r67684>).
111 *
112 * @todo FIXME this is wildly inaccurate and fails to actually check most stuff
113 *
114 * @deprecated since 1.34, use MWHttpRequest::isValidURI
115 * @param string $uri URI to check for validity
116 * @return bool
117 */
118 public static function isValidURI( $uri ) {
119 return MWHttpRequest::isValidURI( $uri );
120 }
121
122 /**
123 * Gets the relevant proxy from $wgHTTPProxy
124 *
125 * @deprecated since 1.34, use $wgHTTPProxy directly
126 * @return string The proxy address or an empty string if not set.
127 */
128 public static function getProxy() {
129 wfDeprecated( __METHOD__, '1.34' );
130
131 global $wgHTTPProxy;
132 return (string)$wgHTTPProxy;
133 }
134
135 /**
136 * Get a configured MultiHttpClient
137 *
138 * @deprecated since 1.34, construct it directly
139 * @param array $options
140 * @return MultiHttpClient
141 */
142 public static function createMultiClient( array $options = [] ) {
143 wfDeprecated( __METHOD__, '1.34' );
144
145 global $wgHTTPConnectTimeout, $wgHTTPTimeout, $wgHTTPProxy;
146
147 return new MultiHttpClient( $options + [
148 'connTimeout' => $wgHTTPConnectTimeout,
149 'reqTimeout' => $wgHTTPTimeout,
150 'userAgent' => self::userAgent(),
151 'proxy' => $wgHTTPProxy,
152 'logger' => LoggerFactory::getInstance( 'http' )
153 ] );
154 }
155 }