MediaWiki REL1_28
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...

Inheritance diagram for MWHttpRequest:
Collaboration diagram for MWHttpRequest:

Public Member Functions

 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.
 
 getHeaderList ()
 Get an array of the headers.
 
 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.
 
 proxySetup ()
 Take care of setting up the proxy (do nothing if "noProxy" is set)
 
 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=null, $attr=null)
 Sets a cookie.
 
 setCookieJar ( $jar)
 Tells the MWHttpRequest object to use this pre-loaded CookieJar.
 
 setData ( $args)
 Set the parameters of the request.
 
 setHeader ( $name, $value)
 Set an arbitrary header.
 
 setLogger (LoggerInterface $logger)
 
 setUserAgent ( $UA)
 Set the user agent.
 

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 factory ( $url, $options=null, $caller=__METHOD__)
 Generate a new request object.
 

Public Attributes

 $logger
 
 $status
 
const SUPPORTS_FILE_POSTS = false
 

Protected Member Functions

 __construct ( $url, $options=[], $caller=__METHOD__, $profiler=null)
 
 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.
 
 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 (< 300) or a redirect (>=300 and < 400).
 

Protected Attributes

 $caInfo = null
 
 $callback
 
 $content
 
CookieJar $cookieJar
 
 $followRedirects = false
 
 $headerList = []
 
 $headersOnly = null
 
 $maxRedirects = 5
 
 $method = "GET"
 
 $noProxy = false
 
 $parsedUrl
 
 $postData = null
 
string $profileName
 
Profiler $profiler
 
 $proxy = null
 
 $reqHeaders = []
 
 $respHeaders = []
 
 $respStatus = "200 Ok"
 
 $respVersion = "0.9"
 
 $sslVerifyCert = true
 
 $sslVerifyHost = true
 
 $timeout = 'default'
 
 $url
 
 LoggerInterface
 

Static Private Member Functions

static isLocalURL ( $url)
 Check if the URL can be served by localhost.
 

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 33 of file MWHttpRequest.php.

Constructor & Destructor Documentation

◆ __construct()

MWHttpRequest::__construct (   $url,
  $options = [],
  $caller = __METHOD__,
  $profiler = null 
)
protected
Parameters
string$urlUrl to use. If protocol-relative, will be expanded to an http:// URL
array$options(optional) extra params to pass (see Http::request())
string$callerThe method making this request, for profiling
Profiler$profilerAn instance of the profiler for profiling, or null

Definition at line 86 of file MWHttpRequest.php.

References $options, $url, $wgHTTPConnectTimeout, $wgHTTPTimeout, as, global, PROTO_HTTP, setUserAgent(), wfExpandUrl(), and wfParseUrl().

Member Function Documentation

◆ canFollowRedirects()

MWHttpRequest::canFollowRedirects ( )

Returns true if the backend can follow redirects.

Overridden by the child classes.

Returns
bool

Reimplemented in CurlHttpRequest.

Definition at line 615 of file MWHttpRequest.php.

◆ canMakeRequests()

static 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 155 of file MWHttpRequest.php.

References wfIniGetBool().

◆ execute()

MWHttpRequest::execute ( )

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

Returns
Status

Reimplemented in CurlHttpRequest, and PhpHttpRequest.

Definition at line 364 of file MWHttpRequest.php.

References content, proxySetup(), setCallback(), and setUserAgent().

◆ factory()

static MWHttpRequest::factory (   $url,
  $options = null,
  $caller = __METHOD__ 
)
static

Generate a new request object.

Parameters
string$urlUrl to use
array$options(optional) extra params to pass (see Http::request())
string$callerThe method making this request, for profiling
Exceptions
MWException
Returns
CurlHttpRequest|PhpHttpRequest
See also
MWHttpRequest::__construct

Reimplemented in MWHttpRequestTester.

Definition at line 168 of file MWHttpRequest.php.

References $options, $url, Profiler\instance(), and wfIniGetBool().

Referenced by MediaWiki\Tidy\Html5Depurate\tidy().

◆ getContent()

MWHttpRequest::getContent ( )

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

Returns
string

Definition at line 206 of file MWHttpRequest.php.

References $content.

◆ getCookieJar()

MWHttpRequest::getCookieJar ( )

Returns the cookie jar in use.

Returns
CookieJar

Definition at line 512 of file MWHttpRequest.php.

References parseHeader().

◆ getFinalUrl()

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 bug 29232 is taken care of (high-level redirect handling rewrite).

Returns
string

Definition at line 571 of file MWHttpRequest.php.

References $url, and getResponseHeaders().

Referenced by parseCookies().

◆ getHeaderList()

MWHttpRequest::getHeaderList ( )

Get an array of the headers.

Returns
array

Definition at line 303 of file MWHttpRequest.php.

References $name, $value, and as.

Referenced by CurlHttpRequest\execute(), and PhpHttpRequest\execute().

◆ getResponseHeader()

MWHttpRequest::getResponseHeader (   $header)

Returns the value of the given response header.

Parameters
string$header
Returns
string|null

Definition at line 485 of file MWHttpRequest.php.

References $header, and parseHeader().

Referenced by PhpHttpRequest\execute().

◆ getResponseHeaders()

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.

Returns
array

Definition at line 471 of file MWHttpRequest.php.

References parseHeader().

Referenced by getFinalUrl().

◆ getStatus()

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 436 of file MWHttpRequest.php.

References parseHeader().

◆ isLocalURL()

static MWHttpRequest::isLocalURL (   $url)
staticprivate

Check if the URL can be served by localhost.

Parameters
string$urlFull url to check
Returns
bool

Definition at line 246 of file MWHttpRequest.php.

References $matches, $url, $wgCommandLineMode, $wgLocalVirtualHosts, and global.

◆ isRedirect()

MWHttpRequest::isRedirect ( )

Returns true if the last status code was a redirect.

Returns
bool

Definition at line 449 of file MWHttpRequest.php.

References $status, and parseHeader().

Referenced by PhpHttpRequest\execute().

◆ parseCookies()

MWHttpRequest::parseCookies ( )
protected

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

Definition at line 540 of file MWHttpRequest.php.

References $url, as, and getFinalUrl().

Referenced by parseHeader().

◆ parseHeader()

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.

Definition at line 389 of file MWHttpRequest.php.

References $header, $last, as, and parseCookies().

Referenced by CurlHttpRequest\execute(), PhpHttpRequest\execute(), getCookieJar(), getResponseHeader(), getResponseHeaders(), getStatus(), isRedirect(), and setStatus().

◆ proxySetup()

MWHttpRequest::proxySetup ( )

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

Returns
void

Definition at line 225 of file MWHttpRequest.php.

Referenced by execute().

◆ read()

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

Definition at line 354 of file MWHttpRequest.php.

References $content, and content.

◆ setCallback()

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$callback
Exceptions
MWException

Definition at line 339 of file MWHttpRequest.php.

References $callback.

Referenced by execute().

◆ setCookie()

MWHttpRequest::setCookie (   $name,
  $value = null,
  $attr = null 
)

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
mixed$value
array$attr

Definition at line 529 of file MWHttpRequest.php.

References $name, $value, and CookieJar\setCookie().

◆ setCookieJar()

MWHttpRequest::setCookieJar (   $jar)

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

Parameters
CookieJar$jar

Definition at line 503 of file MWHttpRequest.php.

◆ setData()

MWHttpRequest::setData (   $args)

Set the parameters of the request.

Parameters
array$args
Todo:
overload the args param

Definition at line 216 of file MWHttpRequest.php.

References $args.

◆ setHeader()

MWHttpRequest::setHeader (   $name,
  $value 
)

Set an arbitrary header.

Parameters
string$name
string$value

Definition at line 294 of file MWHttpRequest.php.

References $name, and $value.

Referenced by setUserAgent().

◆ setLogger()

MWHttpRequest::setLogger ( LoggerInterface  $logger)
Parameters
LoggerInterface$logger

Definition at line 146 of file MWHttpRequest.php.

◆ setStatus()

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 (< 300) or a redirect (>=300 and < 400).

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

Definition at line 418 of file MWHttpRequest.php.

References $code, list, and parseHeader().

Referenced by CurlHttpRequest\execute(), and PhpHttpRequest\execute().

◆ setUserAgent()

MWHttpRequest::setUserAgent (   $UA)

Set the user agent.

Parameters
string$UA

Definition at line 285 of file MWHttpRequest.php.

References setHeader().

Referenced by __construct(), and execute().

Member Data Documentation

◆ $caInfo

MWHttpRequest::$caInfo = null
protected

Definition at line 44 of file MWHttpRequest.php.

Referenced by CurlHttpRequest\execute(), and PhpHttpRequest\getCertOptions().

◆ $callback

MWHttpRequest::$callback
protected

Definition at line 49 of file MWHttpRequest.php.

Referenced by CurlHttpRequest\execute(), and setCallback().

◆ $content

MWHttpRequest::$content
protected

Definition at line 36 of file MWHttpRequest.php.

Referenced by CurlHttpRequest\readHeader().

◆ $cookieJar

CookieJar MWHttpRequest::$cookieJar
protected

Definition at line 56 of file MWHttpRequest.php.

◆ $followRedirects

MWHttpRequest::$followRedirects = false
protected

Definition at line 51 of file MWHttpRequest.php.

◆ $headerList

MWHttpRequest::$headerList = []
protected

Definition at line 58 of file MWHttpRequest.php.

◆ $headersOnly

MWHttpRequest::$headersOnly = null
protected

Definition at line 38 of file MWHttpRequest.php.

◆ $logger

MWHttpRequest::$logger

Definition at line 78 of file MWHttpRequest.php.

◆ $maxRedirects

MWHttpRequest::$maxRedirects = 5
protected

Definition at line 50 of file MWHttpRequest.php.

Referenced by CurlHttpRequest\execute().

◆ $method

MWHttpRequest::$method = "GET"
protected

Definition at line 45 of file MWHttpRequest.php.

Referenced by CurlHttpRequest\execute(), and PhpHttpRequest\execute().

◆ $noProxy

MWHttpRequest::$noProxy = false
protected

Definition at line 41 of file MWHttpRequest.php.

◆ $parsedUrl

MWHttpRequest::$parsedUrl
protected

Definition at line 48 of file MWHttpRequest.php.

Referenced by PhpHttpRequest\urlToTcp().

◆ $postData

MWHttpRequest::$postData = null
protected

Definition at line 39 of file MWHttpRequest.php.

Referenced by CurlHttpRequest\execute(), and PhpHttpRequest\execute().

◆ $profileName

string MWHttpRequest::$profileName
protected

Definition at line 73 of file MWHttpRequest.php.

◆ $profiler

Profiler MWHttpRequest::$profiler
protected

Definition at line 68 of file MWHttpRequest.php.

◆ $proxy

MWHttpRequest::$proxy = null
protected

Definition at line 40 of file MWHttpRequest.php.

Referenced by CurlHttpRequest\execute().

◆ $reqHeaders

MWHttpRequest::$reqHeaders = []
protected

Definition at line 46 of file MWHttpRequest.php.

◆ $respHeaders

MWHttpRequest::$respHeaders = []
protected

Definition at line 61 of file MWHttpRequest.php.

◆ $respStatus

MWHttpRequest::$respStatus = "200 Ok"
protected

Definition at line 60 of file MWHttpRequest.php.

◆ $respVersion

MWHttpRequest::$respVersion = "0.9"
protected

Definition at line 59 of file MWHttpRequest.php.

◆ $sslVerifyCert

MWHttpRequest::$sslVerifyCert = true
protected

Definition at line 43 of file MWHttpRequest.php.

Referenced by CurlHttpRequest\execute(), and PhpHttpRequest\execute().

◆ $sslVerifyHost

MWHttpRequest::$sslVerifyHost = true
protected

Definition at line 42 of file MWHttpRequest.php.

◆ $status

MWHttpRequest::$status

Definition at line 63 of file MWHttpRequest.php.

Referenced by CurlHttpRequest\execute(), PhpHttpRequest\execute(), and isRedirect().

◆ $timeout

MWHttpRequest::$timeout = 'default'
protected

Definition at line 37 of file MWHttpRequest.php.

Referenced by CurlHttpRequest\execute(), and PhpHttpRequest\execute().

◆ $url

◆ LoggerInterface

MWHttpRequest::LoggerInterface
protected

Definition at line 78 of file MWHttpRequest.php.

◆ SUPPORTS_FILE_POSTS

const MWHttpRequest::SUPPORTS_FILE_POSTS = false

Definition at line 34 of file MWHttpRequest.php.


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