| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace MediaWiki\Http; |
| 21 |
|
| 22 |
use GuzzleHttp\Client; |
| 23 |
use GuzzleHttpRequest; |
| 24 |
use InvalidArgumentException; |
| 25 |
use MediaWiki\Config\ServiceOptions; |
| 26 |
use MediaWiki\Logger\LoggerFactory; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\Status\Status; |
| 29 |
use MWHttpRequest; |
| 30 |
use Profiler; |
| 31 |
use Psr\Log\LoggerInterface; |
| 32 |
use Wikimedia\Http\MultiHttpClient; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class HttpRequestFactory { |
| 38 |
|
| 39 |
private $options; |
| 40 |
|
| 41 |
private $logger; |
| 42 |
|
| 43 |
private $telemetry; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public const CONSTRUCTOR_OPTIONS = [ |
| 49 |
MainConfigNames::HTTPTimeout, |
| 50 |
MainConfigNames::HTTPConnectTimeout, |
| 51 |
MainConfigNames::HTTPMaxTimeout, |
| 52 |
MainConfigNames::HTTPMaxConnectTimeout, |
| 53 |
MainConfigNames::LocalVirtualHosts, |
| 54 |
MainConfigNames::LocalHTTPProxy, |
| 55 |
]; |
| 56 |
|
| 57 |
public function __construct( |
| 58 |
ServiceOptions $options, |
| 59 |
LoggerInterface $logger, |
| 60 |
?Telemetry $telemetry = null |
| 61 |
) { |
| 62 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 63 |
$this->options = $options; |
| 64 |
$this->logger = $logger; |
| 65 |
$this->telemetry = $telemetry; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function create( $url, array $options = [], $caller = __METHOD__ ) { |
| 104 |
if ( !isset( $options['logger'] ) ) { |
| 105 |
$options['logger'] = $this->logger; |
| 106 |
} |
| 107 |
$options['timeout'] = $this->normalizeTimeout( |
| 108 |
$options['timeout'] ?? null, |
| 109 |
$options['maxTimeout'] ?? null, |
| 110 |
$this->options->get( MainConfigNames::HTTPTimeout ), |
| 111 |
$this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF |
| 112 |
); |
| 113 |
$options['connectTimeout'] = $this->normalizeTimeout( |
| 114 |
$options['connectTimeout'] ?? null, |
| 115 |
$options['maxConnectTimeout'] ?? null, |
| 116 |
$this->options->get( MainConfigNames::HTTPConnectTimeout ), |
| 117 |
$this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF |
| 118 |
); |
| 119 |
$client = new GuzzleHttpRequest( $url, $options, $caller, Profiler::instance() ); |
| 120 |
if ( $this->telemetry ) { |
| 121 |
$client->addTelemetry( $this->telemetry ); |
| 122 |
} |
| 123 |
return $client; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 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' ); |
| 141 |
} |
| 142 |
$value = $default; |
| 143 |
} else { |
| 144 |
$value = $parameter; |
| 145 |
} |
| 146 |
$max = $maxParameter ?? $maxConfigured; |
| 147 |
if ( $max && $value > $max ) { |
| 148 |
return $max; |
| 149 |
} |
| 150 |
|
| 151 |
return $value; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function canMakeRequests() { |
| 160 |
return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' ); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function request( $method, $url, array $options = [], $caller = __METHOD__ ) { |
| 175 |
$logger = LoggerFactory::getInstance( 'http' ); |
| 176 |
$logger->debug( "$method: $url" ); |
| 177 |
|
| 178 |
$options['method'] = strtoupper( $method ); |
| 179 |
|
| 180 |
$req = $this->create( $url, $options, $caller ); |
| 181 |
$status = $req->execute(); |
| 182 |
|
| 183 |
if ( $status->isOK() ) { |
| 184 |
return $req->getContent(); |
| 185 |
} else { |
| 186 |
$errors = array_map( fn ( $msg ) => $msg->getKey(), $status->getMessages( 'error' ) ); |
| 187 |
$logger->warning( Status::wrap( $status )->getWikiText( false, false, 'en' ), |
| 188 |
[ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] ); |
| 189 |
return null; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function get( $url, array $options = [], $caller = __METHOD__ ) { |
| 203 |
return $this->request( 'GET', $url, $options, $caller ); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function post( $url, array $options = [], $caller = __METHOD__ ) { |
| 216 |
return $this->request( 'POST', $url, $options, $caller ); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function getUserAgent() { |
| 223 |
return 'MediaWiki/' . MW_VERSION; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public function createMultiClient( $options = [] ) { |
| 239 |
$options['reqTimeout'] = $this->normalizeTimeout( |
| 240 |
$options['reqTimeout'] ?? $options['timeout'] ?? null, |
| 241 |
$options['maxReqTimeout'] ?? $options['maxTimeout'] ?? null, |
| 242 |
$this->options->get( MainConfigNames::HTTPTimeout ), |
| 243 |
$this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF |
| 244 |
); |
| 245 |
$options['connTimeout'] = $this->normalizeTimeout( |
| 246 |
$options['connTimeout'] ?? $options['connectTimeout'] ?? null, |
| 247 |
$options['maxConnTimeout'] ?? $options['maxConnectTimeout'] ?? null, |
| 248 |
$this->options->get( MainConfigNames::HTTPConnectTimeout ), |
| 249 |
$this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF |
| 250 |
); |
| 251 |
$options += [ |
| 252 |
'maxReqTimeout' => $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF, |
| 253 |
'maxConnTimeout' => |
| 254 |
$this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF, |
| 255 |
'userAgent' => $this->getUserAgent(), |
| 256 |
'logger' => $this->logger, |
| 257 |
'localProxy' => $this->options->get( MainConfigNames::LocalHTTPProxy ), |
| 258 |
'localVirtualHosts' => $this->options->get( MainConfigNames::LocalVirtualHosts ), |
| 259 |
'telemetry' => Telemetry::getInstance(), |
| 260 |
]; |
| 261 |
return new MultiHttpClient( $options ); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public function createGuzzleClient( array $config = [] ): Client { |
| 278 |
$config['timeout'] = $this->normalizeTimeout( |
| 279 |
$config['timeout'] ?? null, |
| 280 |
$config['maxTimeout'] ?? null, |
| 281 |
$this->options->get( MainConfigNames::HTTPTimeout ), |
| 282 |
$this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF |
| 283 |
); |
| 284 |
|
| 285 |
$config['connect_timeout'] = $this->normalizeTimeout( |
| 286 |
$config['connect_timeout'] ?? null, |
| 287 |
$config['maxConnectTimeout'] ?? null, |
| 288 |
$this->options->get( MainConfigNames::HTTPConnectTimeout ), |
| 289 |
$this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF |
| 290 |
); |
| 291 |
|
| 292 |
if ( !isset( $config['headers']['User-Agent'] ) ) { |
| 293 |
$config['headers']['User-Agent'] = $this->getUserAgent(); |
| 294 |
} |
| 295 |
if ( $this->telemetry ) { |
| 296 |
$config['headers'] = array_merge( |
| 297 |
$this->telemetry->getRequestHeaders(), $config['headers'] |
| 298 |
); |
| 299 |
} |
| 300 |
|
| 301 |
return new Client( $config ); |
| 302 |
} |
| 303 |
} |