Page MenuHomePhorge

abstract class BaseHTTPFuture
libphutil Technical Documentation (Futures)

Execute HTTP requests with a future-oriented API. For example:

$future = new HTTPFuture('http://www.example.com/');
list($status, $body, $headers) = $future->resolve();

This is an abstract base class which defines the API that HTTP futures conform to. Concrete implementations are available in HTTPFuture and HTTPSFuture. All futures return a <status, body, header> tuple when resolved; status is an object of class HTTPFutureResponseStatus and may represent any of a wide variety of errors in the transport layer, a support library, or the actual HTTP exchange.

Tasks

Creating a New Request

  • final public function __construct($uri, $data) — Build a new future which will make an HTTP request to a given URI, with some optional data payload. Since this class is abstract you can't actually instantiate it; instead, build a new @{class:HTTPFuture} or @{class:HTTPSFuture}.

Configuring the Request

  • public function setTimeout($timeout) — Set a timeout for the service call. If the request hasn't resolved yet, the future will resolve with a status that indicates the request timed out. You can determine if a status is a timeout status by calling isTimeout() on the status object.
  • public function getTimeout() — Get the currently configured timeout.
  • final public function setMethod($method) — Select the HTTP method (e.g., "GET", "POST", "PUT") to use for the request. By default, requests use "GET".
  • final public function getMethod() — Get the HTTP method the request will use.
  • public function setURI($uri) — Set the URI to send the request to. Note that this is also a constructor parameter.
  • public function getURI() — Get the fully-qualified URI the request will be made to.
  • public function setData($data) — Provide data to send along with the request. Note that this is also a constructor parameter; it may be more convenient to provide it there. Data must be a string (in which case it will be sent raw) or an array (in which case it will be encoded and sent as 'application/x-www-form-urlencoded').
  • public function getData() — Get the data which will be sent with the request.
  • public function addHeader($name, $value) — Add an HTTP header to the request. The same header name can be specified more than once, which will cause multiple headers to be sent.
  • public function getHeaders($filter) — Get headers which will be sent with the request. Optionally, you can provide a filter, which will return only headers with that name. For example:
  • public function setExpectStatus($status_codes) — Set the status codes that are expected in the response. If set, isError on the status object will return true for status codes that are not in the input array. Otherwise, isError will be true for any HTTP status code outside the 2xx range (notwithstanding other errors such as connection or transport issues).
  • public function setHTTPBasicAuthCredentials($username, $password) — Add a HTTP basic authentication header to the request.

Resolving the Request

  • final public function resolvex() — Exception-oriented @{method:resolve}. Throws if the status indicates an error occurred.
  • public static function getHeader($headers, $search) — Find value of the first header with given name.

Internals

  • protected function parseRawHTTPResponse($raw_response) — Parse a raw HTTP response into a <status, body, headers> tuple.
  • protected function parseHeaders($head_raw) — Parse an HTTP header block.
  • protected function buildMalformedResult($raw_response) — Build a result tuple indicating a parse error resulting from a malformed HTTP response.

Other Methods

Methods

public function __get($name)
Inherited

This method is not documented.
Parameters
$name
Return
wild

public function __set($name, $value)
Inherited

This method is not documented.
Parameters
$name
$value
Return
wild

public function current()
Inherited

This method is not documented.
Return
wild

public function key()
Inherited

This method is not documented.
Return
wild

public function next()
Inherited

This method is not documented.
Return
wild

public function rewind()
Inherited

This method is not documented.
Return
wild

public function valid()
Inherited

This method is not documented.
Return
wild

private function throwOnAttemptedIteration()
Inherited

This method is not documented.
Return
wild

public function getPhobjectClassConstant($key, $byte_limit)
Inherited

Phobject

Read the value of a class constant.

This is the same as just typing self::CONSTANTNAME, but throws a more useful message if the constant is not defined and allows the constant to be limited to a maximum length.

Parameters
string$keyName of the constant.
int|null$byte_limitMaximum number of bytes permitted in the value.
Return
stringValue of the constant.

abstract public function isReady()
Inherited

Future

Is this future's process complete? Specifically, can this future be resolved without blocking?

Return
boolIf true, the external process is complete and resolving this future will not block.

public function resolve()
Inherited

Future

Resolve a future and return its result, blocking until the result is ready if necessary.

Return
wildFuture result.

final public function updateFuture()
Inherited

This method is not documented.
Return
wild

private function startServiceProfiler()
Inherited

This method is not documented.
Return
wild

private function endServiceProfiler()
Inherited

This method is not documented.
Return
wild

protected function getServiceProfilerStartParameters()
Inherited

This method is not documented.
Return
wild

protected function getServiceProfilerResultParameters()
Inherited

This method is not documented.
Return
wild

public function getReadSockets()
Inherited

Future

Retrieve a list of sockets which we can wait to become readable while a future is resolving. If your future has sockets which can be select()ed, return them here (or in getWriteSockets()) to make the resolve loop do a select(). If you do not return sockets in either case, you'll get a busy wait.

Return
listA list of sockets which we expect to become readable.

public function getWriteSockets()
Inherited

Future

Retrieve a list of sockets which we can wait to become writable while a future is resolving. See getReadSockets().

Return
listA list of sockets which we expect to become writable.

public function getDefaultWait()
Inherited

Future

Default amount of time to wait on stream select for this future. Normally 1 second is fine, but if the future has a timeout sooner than that it should return the amount of time left before the timeout.

Return
wild

public function start()
Inherited

This method is not documented.
Return
wild

final protected function getResult()
Inherited

Future

Retrieve the final result of the future.

Return
wildFinal resolution of this future.

final protected function setResult($result)
Inherited

This method is not documented.
Parameters
$result
Return
wild

final public function hasResult()
Inherited

This method is not documented.
Return
wild

private function setException($exception)
Inherited

This method is not documented.
Parameters
$exception
Return
wild

private function getException()
Inherited

This method is not documented.
Return
wild

final public function hasException()
Inherited

This method is not documented.
Return
wild

final public function setFutureKey($key)
Inherited

This method is not documented.
Parameters
$key
Return
wild

final public function getFutureKey()
Inherited

This method is not documented.
Return
wild

final public function setRaiseExceptionOnStart($raise)
Inherited

This method is not documented.
Parameters
$raise
Return
wild

final public function getHasFutureStarted()
Inherited

This method is not documented.
Return
wild

final public function canResolve()
Inherited

This method is not documented.
Return
wild

private function endFuture()
Inherited

This method is not documented.
Return
wild

final public function __construct($uri, $data)

Build a new future which will make an HTTP request to a given URI, with some optional data payload. Since this class is abstract you can't actually instantiate it; instead, build a new HTTPFuture or HTTPSFuture.

Parameters
string$uriFully-qualified URI to send a request to.
mixed$dataString or array to include in the request. Strings will be transmitted raw; arrays will be encoded and sent as 'application/x-www-form-urlencoded'.
Return
this//Implicit.//

public function setTimeout($timeout)

Set a timeout for the service call. If the request hasn't resolved yet, the future will resolve with a status that indicates the request timed out. You can determine if a status is a timeout status by calling isTimeout() on the status object.

Parameters
float$timeoutMaximum timeout, in seconds.
Return
this

public function getTimeout()

Get the currently configured timeout.

Return
floatMaximum number of seconds the request will execute for.

final public function setMethod($method)

Select the HTTP method (e.g., "GET", "POST", "PUT") to use for the request. By default, requests use "GET".

Parameters
string$methodHTTP method name.
Return
this

final public function getMethod()

Get the HTTP method the request will use.

Return
stringHTTP method name, like "GET".

public function setURI($uri)

Set the URI to send the request to. Note that this is also a constructor parameter.

Parameters
string$uriURI to send the request to.
Return
this

public function getURI()

Get the fully-qualified URI the request will be made to.

Return
stringURI the request will be sent to.

public function setData($data)

Provide data to send along with the request. Note that this is also a constructor parameter; it may be more convenient to provide it there. Data must be a string (in which case it will be sent raw) or an array (in which case it will be encoded and sent as 'application/x-www-form-urlencoded').

Parameters
mixed$dataData to send with the request.
Return
this

public function getData()

Get the data which will be sent with the request.

Return
mixedData which will be sent.

public function addHeader($name, $value)

Add an HTTP header to the request. The same header name can be specified more than once, which will cause multiple headers to be sent.

Parameters
string$nameHeader name, like "Accept-Language".
string$valueHeader value, like "en-us".
Return
this

public function getHeaders($filter)

Get headers which will be sent with the request. Optionally, you can provide a filter, which will return only headers with that name. For example:

$all_headers = $future->getHeaders();
$just_user_agent = $future->getHeaders('User-Agent');

In either case, an array with all (or all matching) headers is returned.

Parameters
string|null$filterOptional filter, which selects only headers with that name if provided.
Return
arrayList of all (or all matching) headers.

public function setExpectStatus($status_codes)

Set the status codes that are expected in the response. If set, isError on the status object will return true for status codes that are not in the input array. Otherwise, isError will be true for any HTTP status code outside the 2xx range (notwithstanding other errors such as connection or transport issues).

Parameters
array|null$status_codesList of expected HTTP status codes.
Return
this

public function getExpectStatus()

Return list of expected status codes, or null if not set.

Return
array|nullList of expected status codes.

public function setHTTPBasicAuthCredentials($username, $password)

Add a HTTP basic authentication header to the request.

Parameters
string$usernameUsername to authenticate with.
PhutilOpaqueEnvelope$passwordPassword to authenticate with.
Return
this

public function getHTTPRequestByteLength()

This method is not documented.
Return
wild

public function setDisableContentDecoding($disable_decoding)

This method is not documented.
Parameters
$disable_decoding
Return
wild

public function getDisableContentDecoding()

This method is not documented.
Return
wild

final public function resolvex()

Exception-oriented resolve(). Throws if the status indicates an error occurred.

Return
tupleHTTP request result <body, headers> tuple.

protected function parseRawHTTPResponse($raw_response)

Parse a raw HTTP response into a <status, body, headers> tuple.

Parameters
string$raw_responseRaw HTTP response.
Return
tupleValid resolution tuple.

protected function parseHeaders($head_raw)

Parse an HTTP header block.

Parameters
string$head_rawRaw HTTP headers.
Return
listList of HTTP header tuples.

public static function getHeader($headers, $search)

Find value of the first header with given name.

Parameters
list$headersList of headers from `resolve()`.
string$searchCase insensitive header name.
Return
stringValue of the header or null if not found.

protected function buildMalformedResult($raw_response)

Build a result tuple indicating a parse error resulting from a malformed HTTP response.

Parameters
$raw_response
Return
tupleValid resolution tuple.