10use InvalidArgumentException;
17use Psr\Log\LoggerInterface;
46 LoggerInterface $logger,
50 $this->options = $options;
51 $this->logger = $logger;
52 $this->telemetry = $telemetry;
89 public function create(
$url, array $options = [], $caller = __METHOD__ ) {
90 if ( !isset( $options[
'logger'] ) ) {
91 $options[
'logger'] = $this->logger;
93 $options[
'timeout'] = $this->normalizeTimeout(
94 $options[
'timeout'] ??
null,
95 $options[
'maxTimeout'] ??
null,
99 $options[
'connectTimeout'] = $this->normalizeTimeout(
100 $options[
'connectTimeout'] ??
null,
101 $options[
'maxConnectTimeout'] ??
null,
106 if ( $this->telemetry ) {
107 $client->addTelemetry( $this->telemetry );
122 private function normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured ) {
123 if ( $parameter ===
'default' || $parameter ===
null ) {
124 if ( !is_numeric( $default ) ) {
125 throw new InvalidArgumentException(
126 '$wgHTTPTimeout and $wgHTTPConnectTimeout must be set to a number' );
132 $max = $maxParameter ?? $maxConfigured;
133 if ( $max && $value > $max ) {
146 return function_exists(
'curl_init' ) ||
wfIniGetBool(
'allow_url_fopen' );
160 public function request( $method,
$url, array $options = [], $caller = __METHOD__ ) {
161 $logger = LoggerFactory::getInstance(
'http' );
162 $logger->debug(
"$method: $url" );
164 $options[
'method'] = strtoupper( $method );
166 $req = $this->
create(
$url, $options, $caller );
167 $status = $req->execute();
169 if ( $status->isOK() ) {
170 return $req->getContent();
172 $errors = array_map(
static fn ( $msg ) => $msg->getKey(), $status->getMessages(
'error' ) );
173 $logger->warning( Status::wrap( $status )->getWikiText(
false,
false,
'en' ),
174 [
'error' => $errors,
'caller' => $caller,
'content' => $req->getContent() ] );
188 public function get(
$url, array $options = [], $caller = __METHOD__ ) {
189 return $this->
request(
'GET',
$url, $options, $caller );
201 public function post(
$url, array $options = [], $caller = __METHOD__ ) {
202 return $this->
request(
'POST',
$url, $options, $caller );
225 $options[
'reqTimeout'] = $this->normalizeTimeout(
226 $options[
'reqTimeout'] ?? $options[
'timeout'] ??
null,
227 $options[
'maxReqTimeout'] ?? $options[
'maxTimeout'] ??
null,
231 $options[
'connTimeout'] = $this->normalizeTimeout(
232 $options[
'connTimeout'] ?? $options[
'connectTimeout'] ??
null,
233 $options[
'maxConnTimeout'] ?? $options[
'maxConnectTimeout'] ??
null,
242 'logger' => $this->logger,
264 $config[
'timeout'] = $this->normalizeTimeout(
265 $config[
'timeout'] ?? null,
266 $config[
'maxTimeout'] ?? null,
271 $config[
'connect_timeout'] = $this->normalizeTimeout(
272 $config[
'connect_timeout'] ??
null,
273 $config[
'maxConnectTimeout'] ??
null,
278 if ( !isset( $config[
'headers'][
'User-Agent'] ) ) {
279 $config[
'headers'][
'User-Agent'] = $this->
getUserAgent();
281 if ( $this->telemetry ) {
282 $config[
'headers'] = array_merge(
283 $this->telemetry->getRequestHeaders(), $config[
'headers']
287 return new Client( $config );
const MW_VERSION
The running version of MediaWiki.
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.
MWHttpRequest implemented using the Guzzle library.
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
A class containing constants representing the names of configuration variables.
const HTTPConnectTimeout
Name constant for the HTTPConnectTimeout setting, for use with Config::get()
const HTTPTimeout
Name constant for the HTTPTimeout setting, for use with Config::get()
const LocalVirtualHosts
Name constant for the LocalVirtualHosts setting, for use with Config::get()
const HTTPMaxConnectTimeout
Name constant for the HTTPMaxConnectTimeout setting, for use with Config::get()
const HTTPMaxTimeout
Name constant for the HTTPMaxTimeout setting, for use with Config::get()
const LocalHTTPProxy
Name constant for the LocalHTTPProxy setting, for use with Config::get()
Profiler base class that defines the interface and some shared functionality.