MediaWiki master
MediaWiki\Http\MWHttpRequest Class Reference

This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for handling internal HTTP requests. More...

Inherits LoggerAwareInterface.

Inherited by MediaWiki\Http\GuzzleHttpRequest.

Collaboration diagram for MediaWiki\Http\MWHttpRequest:

Public Member Functions

 __construct ( $url, array $options, string $caller=__METHOD__, protected readonly ?Profiler $profiler=null,)
 
 addTelemetry (TelemetryHeadersInterface $telemetry)
 Add Telemetry information to the request.
 
 canFollowRedirects ()
 Returns true if the backend can follow redirects.
 
 execute ()
 Take care of whatever is necessary to perform the URI request.
 
 getContent ()
 Get the body, or content, of the response to the request.
 
 getCookieJar ()
 Returns the cookie jar in use.
 
 getFinalUrl ()
 Returns the final URL after all redirections.
 
 getResponseHeader ( $header)
 Returns the value of the given response header.
 
 getResponseHeaders ()
 Returns an associative array of response headers after the request has been executed.
 
 getStatus ()
 Get the integer value of the HTTP status code (e.g.
 
 isRedirect ()
 Returns true if the last status code was a redirect.
 
 read ( $fh, $content)
 A generic callback to read the body of the response from a remote server.
 
 setCallback ( $callback)
 Set a read callback to accept data read from the HTTP request.
 
 setCookie ( $name, $value, array $attr=[])
 Sets a cookie.
 
 setCookieJar (CookieJar $jar)
 Tells the MWHttpRequest object to use this pre-loaded CookieJar.
 
 setData (array $args)
 Set the parameters of the request.
 
 setHeader ( $name, $value)
 Set an arbitrary header.
 
 setLogger (LoggerInterface $logger)
 
 setOriginalRequest ( $originalRequest)
 Set information about the original request.
 
 setUserAgent ( $UA)
 

Static Public Member Functions

static canMakeRequests ()
 Simple function to test if we can make any sort of requests at all, using cURL or fopen()
 
static isValidURI ( $uri)
 Check that the given URI is a valid one.
 

Public Attributes

const SUPPORTS_FILE_POSTS = false
 

Protected Member Functions

 doSetCallback ( $callback)
 Worker function for setting callbacks.
 
 parseCookies ()
 Parse the cookies in the response headers and store them in the cookie jar.
 
 parseHeader ()
 Parses the headers, including the HTTP status code and any Set-Cookie headers.
 
 prepare ()
 
 proxySetup ()
 Take care of setting up the proxy (do nothing if "noProxy" is set)
 
 setReverseProxy (string $proxy)
 Enable use of a reverse proxy in which the hostname is passed as a "Host" header, and the request is sent to the proxy's host:port instead.
 
 setStatus ()
 Sets HTTPRequest status member to a fatal value with the error message if the returned integer value of the status code was not successful (1-299) or a redirect (300-399).
 

Protected Attributes

string null $caInfo = null
 
callable $callback
 
int $connectTimeout
 
string null $content
 
CookieJar $cookieJar
 
bool $followRedirects = false
 
array $headerList = []
 
bool null $headersOnly = null
 
LoggerInterface $logger
 
int $maxRedirects = 5
 
string $method = "GET"
 
bool $noProxy = false
 
array false $parsedUrl
 
array null $postData = null
 
readonly string $profileName
 
string null $proxy = null
 
array $reqHeaders = []
 
string[][] $respHeaders = []
 
string $respStatus = "200 Ok"
 
string $respVersion = "0.9"
 
bool $sslVerifyCert = true
 
bool $sslVerifyHost = true
 
readonly StatusValue $status
 
int string $timeout = 'default'
 
string $url
 

Detailed Description

This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for handling internal HTTP requests.

Renamed from HttpRequest to MWHttpRequest to avoid conflict with PHP's HTTP extension.

Definition at line 31 of file MWHttpRequest.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Http\MWHttpRequest::__construct ( $url,
array $options,
string $caller = __METHOD__,
protected readonly ?Profiler $profiler = null )
Parameters
string$urlUrl to use. If protocol-relative, will be expanded to an http:// URL
array$optionsextra params to pass (see HttpRequestFactory::create())
string$callerThe method making this request, for profiling
Profiler | null$profilerAn instance of the profiler for profiling, or null
Exceptions

\Exception

Definition at line 103 of file MWHttpRequest.php.

References MediaWiki\Http\MWHttpRequest\$url, MediaWiki\MediaWikiServices\getInstance(), PROTO_HTTP, MediaWiki\Http\MWHttpRequest\setHeader(), MediaWiki\Http\MWHttpRequest\setOriginalRequest(), and MediaWiki\Http\MWHttpRequest\setUserAgent().

Member Function Documentation

◆ addTelemetry()

MediaWiki\Http\MWHttpRequest::addTelemetry ( TelemetryHeadersInterface $telemetry)

Add Telemetry information to the request.

Parameters
TelemetryHeadersInterface$telemetry
Returns
void

Definition at line 201 of file MWHttpRequest.php.

◆ canFollowRedirects()

MediaWiki\Http\MWHttpRequest::canFollowRedirects ( )

Returns true if the backend can follow redirects.

Overridden by the child classes.

Returns
bool

Definition at line 643 of file MWHttpRequest.php.

◆ canMakeRequests()

static MediaWiki\Http\MWHttpRequest::canMakeRequests ( )
static

Simple function to test if we can make any sort of requests at all, using cURL or fopen()

Returns
bool

Definition at line 172 of file MWHttpRequest.php.

References wfIniGetBool().

◆ doSetCallback()

MediaWiki\Http\MWHttpRequest::doSetCallback ( $callback)
protected

Worker function for setting callbacks.

Calls can originate both internally and externally via setCallback). Defaults to the internal read callback if $callback is null.

Parameters
callable | null$callback
Exceptions
InvalidArgumentException

Reimplemented in MediaWiki\Http\GuzzleHttpRequest.

Definition at line 353 of file MWHttpRequest.php.

◆ execute()

MediaWiki\Http\MWHttpRequest::execute ( )

Take care of whatever is necessary to perform the URI request.

Returns
Status
Note
currently returns Status for B/C

Reimplemented in MediaWiki\Http\GuzzleHttpRequest.

Definition at line 383 of file MWHttpRequest.php.

◆ getContent()

MediaWiki\Http\MWHttpRequest::getContent ( )

Get the body, or content, of the response to the request.

Returns
string

Definition at line 181 of file MWHttpRequest.php.

◆ getCookieJar()

MediaWiki\Http\MWHttpRequest::getCookieJar ( )

Returns the cookie jar in use.

Returns
CookieJar

Definition at line 540 of file MWHttpRequest.php.

Referenced by MediaWiki\Http\GuzzleHttpRequest\execute().

◆ getFinalUrl()

MediaWiki\Http\MWHttpRequest::getFinalUrl ( )

Returns the final URL after all redirections.

Relative values of the "Location" header are incorrect as stated in RFC, however they do happen and modern browsers support them. This function loops backwards through all locations in order to build the proper absolute URI - Marooned at wikia-inc.com

Note that the multiple Location: headers are an artifact of CURL – they shouldn't actually get returned this way. Rewrite this when T31232 is taken care of (high-level redirect handling rewrite).

Returns
string

Definition at line 601 of file MWHttpRequest.php.

References $url.

◆ getResponseHeader()

MediaWiki\Http\MWHttpRequest::getResponseHeader ( $header)

Returns the value of the given response header.

Parameters
string$headercase-insensitive
Returns
string|null

Definition at line 513 of file MWHttpRequest.php.

◆ getResponseHeaders()

MediaWiki\Http\MWHttpRequest::getResponseHeaders ( )

Returns an associative array of response headers after the request has been executed.

Because some headers (e.g. Set-Cookie) can appear more than once the, each value of the associative array is an array of the values given. Header names are always in lowercase.

Returns
array

Definition at line 499 of file MWHttpRequest.php.

◆ getStatus()

MediaWiki\Http\MWHttpRequest::getStatus ( )

Get the integer value of the HTTP status code (e.g.

200 for "200 Ok") (see RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a list of status codes.)

Returns
int

Definition at line 463 of file MWHttpRequest.php.

◆ isRedirect()

MediaWiki\Http\MWHttpRequest::isRedirect ( )

Returns true if the last status code was a redirect.

Returns
bool

Definition at line 476 of file MWHttpRequest.php.

◆ isValidURI()

static MediaWiki\Http\MWHttpRequest::isValidURI ( $uri)
static

Check that the given URI is a valid one.

This hardcodes a small set of protocols only, because we want to deterministically reject protocols not supported by all HTTP-transport methods.

"file://" specifically must not be allowed, for security reasons (see https://www.mediawiki.org/wiki/Special:Code/MediaWiki/r67684).

Todo
FIXME this is wildly inaccurate and fails to actually check most stuff
Since
1.34
Parameters
string$uriURI to check for validity
Returns
bool

Definition at line 693 of file MWHttpRequest.php.

Referenced by MediaWiki\Upload\UploadFromUrl\canFetchFile().

◆ parseCookies()

MediaWiki\Http\MWHttpRequest::parseCookies ( )
protected

Parse the cookies in the response headers and store them in the cookie jar.

Definition at line 570 of file MWHttpRequest.php.

References $url.

◆ parseHeader()

MediaWiki\Http\MWHttpRequest::parseHeader ( )
protected

Parses the headers, including the HTTP status code and any Set-Cookie headers.

This function expects the headers to be found in an array in the member variable headerList.

Reimplemented in MediaWiki\Http\GuzzleHttpRequest.

Definition at line 411 of file MWHttpRequest.php.

◆ prepare()

MediaWiki\Http\MWHttpRequest::prepare ( )
protected

Reimplemented in MediaWiki\Http\GuzzleHttpRequest.

Definition at line 387 of file MWHttpRequest.php.

◆ proxySetup()

MediaWiki\Http\MWHttpRequest::proxySetup ( )
protected

Take care of setting up the proxy (do nothing if "noProxy" is set)

Returns
void

Definition at line 212 of file MWHttpRequest.php.

◆ read()

MediaWiki\Http\MWHttpRequest::read ( $fh,
$content )

A generic callback to read the body of the response from a remote server.

Parameters
resource$fh
string$content
Returns
int
Access: internal

Definition at line 372 of file MWHttpRequest.php.

◆ setCallback()

MediaWiki\Http\MWHttpRequest::setCallback ( $callback)

Set a read callback to accept data read from the HTTP request.

By default, data is appended to an internal buffer which can be retrieved through $req->getContent().

To handle data as it comes in – especially for large files that would not fit in memory – you can instead set your own callback, in the form function($resource, $buffer) where the first parameter is the low-level resource being read (implementation specific), and the second parameter is the data buffer.

You MUST return the number of bytes handled in the buffer; if fewer bytes are reported handled than were passed to you, the HTTP fetch will be aborted.

Parameters
callable | null$callback
Exceptions
InvalidArgumentException

Reimplemented in MediaWiki\Http\GuzzleHttpRequest.

Definition at line 342 of file MWHttpRequest.php.

◆ setCookie()

MediaWiki\Http\MWHttpRequest::setCookie ( $name,
$value,
array $attr = [] )

Sets a cookie.

Used before a request to set up any individual cookies. Used internally after a request to parse the Set-Cookie headers.

See also
Cookie::set
Parameters
string$name
string$value
array$attr

Definition at line 557 of file MWHttpRequest.php.

References Wikimedia\CookieJar\setCookie().

◆ setCookieJar()

MediaWiki\Http\MWHttpRequest::setCookieJar ( CookieJar $jar)

Tells the MWHttpRequest object to use this pre-loaded CookieJar.

To read response cookies from the jar, getCookieJar must be called first.

Definition at line 531 of file MWHttpRequest.php.

◆ setData()

MediaWiki\Http\MWHttpRequest::setData ( array $args)

Set the parameters of the request.

Parameters
array$args
Todo
overload the args param

Definition at line 191 of file MWHttpRequest.php.

◆ setHeader()

MediaWiki\Http\MWHttpRequest::setHeader ( $name,
$value )

Set an arbitrary header.

Parameters
string$name
string$value

Definition at line 319 of file MWHttpRequest.php.

Referenced by MediaWiki\Http\MWHttpRequest\__construct().

◆ setLogger()

MediaWiki\Http\MWHttpRequest::setLogger ( LoggerInterface $logger)

Definition at line 163 of file MWHttpRequest.php.

◆ setOriginalRequest()

MediaWiki\Http\MWHttpRequest::setOriginalRequest ( $originalRequest)

Set information about the original request.

This can be useful for endpoints/API modules which act as a proxy for some service, and throttling etc. needs to happen in that service. Calling this will result in the X-Forwarded-For and X-Original-User-Agent headers being set.

Parameters
WebRequest | array$originalRequestWhen in array form, it's expected to have the keys 'ip' and 'userAgent'.
Note
IP/user agent is personally identifiable information, and should only be set when the privacy policy of the request target is compatible with that of the MediaWiki installation.

Definition at line 659 of file MWHttpRequest.php.

Referenced by MediaWiki\Http\MWHttpRequest\__construct().

◆ setReverseProxy()

MediaWiki\Http\MWHttpRequest::setReverseProxy ( string $proxy)
protected

Enable use of a reverse proxy in which the hostname is passed as a "Host" header, and the request is sent to the proxy's host:port instead.

Note that any custom port in the request URL will be lost and cookies and redirects may not work properly.

Parameters
string$proxyURL of proxy

Definition at line 249 of file MWHttpRequest.php.

◆ setStatus()

MediaWiki\Http\MWHttpRequest::setStatus ( )
protected

Sets HTTPRequest status member to a fatal value with the error message if the returned integer value of the status code was not successful (1-299) or a redirect (300-399).

See RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a list of status codes.

Definition at line 442 of file MWHttpRequest.php.

Referenced by MediaWiki\Http\GuzzleHttpRequest\execute().

◆ setUserAgent()

MediaWiki\Http\MWHttpRequest::setUserAgent ( $UA)
Parameters
string$UA

Definition at line 310 of file MWHttpRequest.php.

Referenced by MediaWiki\Http\MWHttpRequest\__construct().

Member Data Documentation

◆ $caInfo

string null MediaWiki\Http\MWHttpRequest::$caInfo = null
protected

Definition at line 54 of file MWHttpRequest.php.

Referenced by MediaWiki\Http\GuzzleHttpRequest\execute().

◆ $callback

callable MediaWiki\Http\MWHttpRequest::$callback
protected

◆ $connectTimeout

int MediaWiki\Http\MWHttpRequest::$connectTimeout
protected

Definition at line 70 of file MWHttpRequest.php.

Referenced by MediaWiki\Http\GuzzleHttpRequest\execute().

◆ $content

string null MediaWiki\Http\MWHttpRequest::$content
protected

Definition at line 40 of file MWHttpRequest.php.

◆ $cookieJar

CookieJar MediaWiki\Http\MWHttpRequest::$cookieJar
protected

Definition at line 75 of file MWHttpRequest.php.

◆ $followRedirects

bool MediaWiki\Http\MWHttpRequest::$followRedirects = false
protected

Definition at line 68 of file MWHttpRequest.php.

◆ $headerList

array MediaWiki\Http\MWHttpRequest::$headerList = []
protected

Definition at line 78 of file MWHttpRequest.php.

◆ $headersOnly

bool null MediaWiki\Http\MWHttpRequest::$headersOnly = null
protected

Definition at line 42 of file MWHttpRequest.php.

◆ $logger

LoggerInterface MediaWiki\Http\MWHttpRequest::$logger
protected

Definition at line 90 of file MWHttpRequest.php.

◆ $maxRedirects

int MediaWiki\Http\MWHttpRequest::$maxRedirects = 5
protected

Definition at line 66 of file MWHttpRequest.php.

Referenced by MediaWiki\Http\GuzzleHttpRequest\execute().

◆ $method

string MediaWiki\Http\MWHttpRequest::$method = "GET"
protected

Definition at line 56 of file MWHttpRequest.php.

◆ $noProxy

bool MediaWiki\Http\MWHttpRequest::$noProxy = false
protected

Definition at line 48 of file MWHttpRequest.php.

◆ $parsedUrl

array false MediaWiki\Http\MWHttpRequest::$parsedUrl
protected

Definition at line 62 of file MWHttpRequest.php.

◆ $postData

array null MediaWiki\Http\MWHttpRequest::$postData = null
protected

Definition at line 44 of file MWHttpRequest.php.

Referenced by MediaWiki\Http\GuzzleHttpRequest\execute().

◆ $profileName

readonly string MediaWiki\Http\MWHttpRequest::$profileName
protected

Definition at line 88 of file MWHttpRequest.php.

◆ $proxy

string null MediaWiki\Http\MWHttpRequest::$proxy = null
protected

Definition at line 46 of file MWHttpRequest.php.

Referenced by MediaWiki\Http\GuzzleHttpRequest\execute().

◆ $reqHeaders

array MediaWiki\Http\MWHttpRequest::$reqHeaders = []
protected

Definition at line 58 of file MWHttpRequest.php.

◆ $respHeaders

string [][] MediaWiki\Http\MWHttpRequest::$respHeaders = []
protected

Definition at line 84 of file MWHttpRequest.php.

◆ $respStatus

string MediaWiki\Http\MWHttpRequest::$respStatus = "200 Ok"
protected

Definition at line 82 of file MWHttpRequest.php.

◆ $respVersion

string MediaWiki\Http\MWHttpRequest::$respVersion = "0.9"
protected

Definition at line 80 of file MWHttpRequest.php.

◆ $sslVerifyCert

bool MediaWiki\Http\MWHttpRequest::$sslVerifyCert = true
protected

Definition at line 52 of file MWHttpRequest.php.

◆ $sslVerifyHost

bool MediaWiki\Http\MWHttpRequest::$sslVerifyHost = true
protected

Definition at line 50 of file MWHttpRequest.php.

◆ $status

readonly StatusValue MediaWiki\Http\MWHttpRequest::$status
protected

Definition at line 86 of file MWHttpRequest.php.

◆ $timeout

int string MediaWiki\Http\MWHttpRequest::$timeout = 'default'
protected

Definition at line 37 of file MWHttpRequest.php.

Referenced by MediaWiki\Http\GuzzleHttpRequest\execute().

◆ $url

string MediaWiki\Http\MWHttpRequest::$url
protected

◆ SUPPORTS_FILE_POSTS

const MediaWiki\Http\MWHttpRequest::SUPPORTS_FILE_POSTS = false

Definition at line 32 of file MWHttpRequest.php.


The documentation for this class was generated from the following file: