30use Psr\Log\LoggerInterface;
56 $this->options = $options;
57 $this->logger = $logger;
95 public function create( $url, array $options = [], $caller = __METHOD__ ) {
96 if ( !isset( $options[
'logger'] ) ) {
97 $options[
'logger'] = $this->logger;
99 $options[
'timeout'] = $this->normalizeTimeout(
100 $options[
'timeout'] ??
null,
101 $options[
'maxTimeout'] ??
null,
105 $options[
'connectTimeout'] = $this->normalizeTimeout(
106 $options[
'connectTimeout'] ??
null,
107 $options[
'maxConnectTimeout'] ??
null,
125 private function normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured ) {
126 if ( $parameter ===
'default' || $parameter ===
null ) {
127 if ( !is_numeric( $default ) ) {
128 throw new \InvalidArgumentException(
129 '$wgHTTPTimeout and $wgHTTPConnectTimeout must be set to a number' );
135 if ( $maxParameter !==
null ) {
136 $max = $maxParameter;
138 $max = $maxConfigured;
140 if ( $max && $value > $max ) {
153 return function_exists(
'curl_init' ) ||
wfIniGetBool(
'allow_url_fopen' );
167 public function request( $method, $url, array $options = [], $caller = __METHOD__ ) {
168 $logger = LoggerFactory::getInstance(
'http' );
169 $logger->debug(
"$method: $url" );
171 $options[
'method'] = strtoupper( $method );
173 $req = $this->
create( $url, $options, $caller );
174 $status = $req->execute();
176 if ( $status->isOK() ) {
177 return $req->getContent();
179 $errors = $status->getErrorsByType(
'error' );
180 $logger->warning( Status::wrap( $status )->getWikiText(
false,
false,
'en' ),
181 [
'error' => $errors,
'caller' => $caller,
'content' => $req->getContent() ] );
195 public function get( $url, array $options = [], $caller = __METHOD__ ) {
196 return $this->
request(
'GET', $url, $options, $caller );
208 public function post( $url, array $options = [], $caller = __METHOD__ ) {
209 return $this->
request(
'POST', $url, $options, $caller );
232 $options[
'reqTimeout'] = $this->normalizeTimeout(
233 $options[
'reqTimeout'] ?? $options[
'timeout'] ??
null,
234 $options[
'maxReqTimeout'] ?? $options[
'maxTimeout'] ??
null,
238 $options[
'connTimeout'] = $this->normalizeTimeout(
239 $options[
'connTimeout'] ?? $options[
'connectTimeout'] ??
null,
240 $options[
'maxConnTimeout'] ?? $options[
'maxConnectTimeout'] ??
null,
249 'logger' => $this->logger,
270 $config[
'timeout'] = $this->normalizeTimeout(
271 $config[
'timeout'] ?? null,
272 $config[
'maxTimeout'] ?? null,
277 $config[
'connect_timeout'] = $this->normalizeTimeout(
278 $config[
'connect_timeout'] ??
null,
279 $config[
'maxConnectTimeout'] ??
null,
284 if ( !isset( $config[
'headers'][
'User-Agent'] ) ) {
285 $config[
'headers'][
'User-Agent'] = $this->
getUserAgent();
288 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()
Class to handle multiple HTTP requests.
Profiler base class that defines the interface and some shared functionality.
static instance()
Singleton.
Generic operation result class Has warning/error list, boolean status and arbitrary value.