24use InvalidArgumentException;
31use Psr\Log\LoggerInterface;
59 LoggerInterface $logger,
63 $this->options = $options;
64 $this->logger = $logger;
65 $this->telemetry = $telemetry;
103 public function create(
$url, array $options = [], $caller = __METHOD__ ) {
104 if ( !isset( $options[
'logger'] ) ) {
105 $options[
'logger'] = $this->logger;
107 $options[
'timeout'] = $this->normalizeTimeout(
108 $options[
'timeout'] ??
null,
109 $options[
'maxTimeout'] ??
null,
113 $options[
'connectTimeout'] = $this->normalizeTimeout(
114 $options[
'connectTimeout'] ??
null,
115 $options[
'maxConnectTimeout'] ??
null,
120 if ( $this->telemetry ) {
121 $client->addTelemetry( $this->telemetry );
136 private function normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured ) {
137 if ( $parameter ===
'default' || $parameter ===
null ) {
138 if ( !is_numeric( $default ) ) {
139 throw new InvalidArgumentException(
140 '$wgHTTPTimeout and $wgHTTPConnectTimeout must be set to a number' );
146 $max = $maxParameter ?? $maxConfigured;
147 if ( $max && $value > $max ) {
160 return function_exists(
'curl_init' ) ||
wfIniGetBool(
'allow_url_fopen' );
174 public function request( $method,
$url, array $options = [], $caller = __METHOD__ ) {
175 $logger = LoggerFactory::getInstance(
'http' );
176 $logger->debug(
"$method: $url" );
178 $options[
'method'] = strtoupper( $method );
180 $req = $this->
create(
$url, $options, $caller );
181 $status = $req->execute();
183 if ( $status->isOK() ) {
184 return $req->getContent();
186 $errors = $status->getErrorsByType(
'error' );
187 $logger->warning( Status::wrap( $status )->getWikiText(
false,
false,
'en' ),
188 [
'error' => $errors,
'caller' => $caller,
'content' => $req->getContent() ] );
202 public function get(
$url, array $options = [], $caller = __METHOD__ ) {
203 return $this->
request(
'GET',
$url, $options, $caller );
215 public function post(
$url, array $options = [], $caller = __METHOD__ ) {
216 return $this->
request(
'POST',
$url, $options, $caller );
239 $options[
'reqTimeout'] = $this->normalizeTimeout(
240 $options[
'reqTimeout'] ?? $options[
'timeout'] ??
null,
241 $options[
'maxReqTimeout'] ?? $options[
'maxTimeout'] ??
null,
245 $options[
'connTimeout'] = $this->normalizeTimeout(
246 $options[
'connTimeout'] ?? $options[
'connectTimeout'] ??
null,
247 $options[
'maxConnTimeout'] ?? $options[
'maxConnectTimeout'] ??
null,
256 'logger' => $this->logger,
278 $config[
'timeout'] = $this->normalizeTimeout(
279 $config[
'timeout'] ?? null,
280 $config[
'maxTimeout'] ?? null,
285 $config[
'connect_timeout'] = $this->normalizeTimeout(
286 $config[
'connect_timeout'] ??
null,
287 $config[
'maxConnectTimeout'] ??
null,
292 if ( !isset( $config[
'headers'][
'User-Agent'] ) ) {
293 $config[
'headers'][
'User-Agent'] = $this->
getUserAgent();
295 if ( $this->telemetry ) {
296 $config[
'headers'] = array_merge(
297 $this->telemetry->getRequestHeaders(), $config[
'headers']
301 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.