24use InvalidArgumentException;
31use Psr\Log\LoggerInterface;
60 LoggerInterface $logger,
64 $this->options = $options;
65 $this->logger = $logger;
66 $this->telemetry = $telemetry;
104 public function create(
$url, array $options = [], $caller = __METHOD__ ) {
105 if ( !isset( $options[
'logger'] ) ) {
106 $options[
'logger'] = $this->logger;
108 $options[
'timeout'] = $this->normalizeTimeout(
109 $options[
'timeout'] ??
null,
110 $options[
'maxTimeout'] ??
null,
114 $options[
'connectTimeout'] = $this->normalizeTimeout(
115 $options[
'connectTimeout'] ??
null,
116 $options[
'maxConnectTimeout'] ??
null,
121 if ( $this->telemetry ) {
122 $client->addTelemetry( $this->telemetry );
137 private function normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured ) {
138 if ( $parameter ===
'default' || $parameter ===
null ) {
139 if ( !is_numeric( $default ) ) {
140 throw new InvalidArgumentException(
141 '$wgHTTPTimeout and $wgHTTPConnectTimeout must be set to a number' );
147 $max = $maxParameter ?? $maxConfigured;
148 if ( $max && $value > $max ) {
161 return function_exists(
'curl_init' ) ||
wfIniGetBool(
'allow_url_fopen' );
175 public function request( $method,
$url, array $options = [], $caller = __METHOD__ ) {
176 $logger = LoggerFactory::getInstance(
'http' );
177 $logger->debug(
"$method: $url" );
179 $options[
'method'] = strtoupper( $method );
181 $req = $this->
create(
$url, $options, $caller );
182 $status = $req->execute();
184 if ( $status->isOK() ) {
185 return $req->getContent();
187 $errors = array_map(
static fn ( $msg ) => $msg->getKey(), $status->getMessages(
'error' ) );
188 $logger->warning( Status::wrap( $status )->getWikiText(
false,
false,
'en' ),
189 [
'error' => $errors,
'caller' => $caller,
'content' => $req->getContent() ] );
203 public function get(
$url, array $options = [], $caller = __METHOD__ ) {
204 return $this->
request(
'GET',
$url, $options, $caller );
216 public function post(
$url, array $options = [], $caller = __METHOD__ ) {
217 return $this->
request(
'POST',
$url, $options, $caller );
240 $options[
'reqTimeout'] = $this->normalizeTimeout(
241 $options[
'reqTimeout'] ?? $options[
'timeout'] ??
null,
242 $options[
'maxReqTimeout'] ?? $options[
'maxTimeout'] ??
null,
246 $options[
'connTimeout'] = $this->normalizeTimeout(
247 $options[
'connTimeout'] ?? $options[
'connectTimeout'] ??
null,
248 $options[
'maxConnTimeout'] ?? $options[
'maxConnectTimeout'] ??
null,
257 'logger' => $this->logger,
279 $config[
'timeout'] = $this->normalizeTimeout(
280 $config[
'timeout'] ?? null,
281 $config[
'maxTimeout'] ?? null,
286 $config[
'connect_timeout'] = $this->normalizeTimeout(
287 $config[
'connect_timeout'] ??
null,
288 $config[
'maxConnectTimeout'] ??
null,
293 if ( !isset( $config[
'headers'][
'User-Agent'] ) ) {
294 $config[
'headers'][
'User-Agent'] = $this->
getUserAgent();
296 if ( $this->telemetry ) {
297 $config[
'headers'] = array_merge(
298 $this->telemetry->getRequestHeaders(), $config[
'headers']
302 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.