14. Protocols

14.1. HTTP

Module Protocols.HTTP


Constant response_codes

constant Protocols.HTTP.response_codes

Description

Mapping from StatusCode to descriptive string.

See also

StatusCode


Method delete_url

.Querydelete_url(string|Standards.URIurl, void|mapping(string:int|string|array(string)) query_variables, void|mapping(string:string|array(string)|int) request_headers, void|Protocols.HTTP.Querycon)

Description

Sends a HTTP DELETE request to the server in the URL and returns the created and initialized Query object. 0 is returned upon failure. If a query object having request_headers->Connection=="Keep-Alive" from a previous request is provided and the already established server connection can be used for the next request, you may gain some performance.


Method do_async_method

voiddo_async_method(stringmethod, string|Standards.URIurl, void|mapping(string:int|string|array(string)) query_variables, void|mapping(string:string|array(string)|int) request_headers, Protocols.HTTP.Querycon, void|stringdata)

Description

Low level asynchronous HTTP call method.

Parameter method

The HTTP method to use, e.g. "GET".

Parameter url

The URL to perform method on. Should be a complete URL, including protocol, e.g. "https://pike.lysator.liu.se/".

Parameter query_variables

Calls http_encode_query and appends the result to the URL.

Parameter request_headers

The HTTP headers to be added to the request. By default the headers User-agent, Host and, if needed by the url, Authorization will be added, with generated contents. Providing these headers will override the default. Setting the value to 0 will remove that header from the request.

Parameter con

Previously initialized connection object. In particular the callbacks must have been set (Query.set_callbacks()).

Parameter data

Data payload to be transmitted in the request.

See also

do_method(), Query.set_callbacks()


Method do_async_proxied_method

voiddo_async_proxied_method(string|Standards.URIproxy, stringuser, stringpassword, stringmethod, string|Standards.URIurl, void|mapping(string:int|string|array(string)) query_variables, void|mapping(string:string|array(string)|int) request_headers, Protocols.HTTP.Querycon, void|stringdata)

Description

Low level asynchronous proxied HTTP call method.

Makes an HTTP request through a proxy.

Parameter proxy

URL for the proxy.

Parameter user
Parameter password

Proxy authentication credentials.

Parameter method

The HTTP method to use, e.g. "GET".

Parameter url

The URL to perform method on. Should be a complete URL, including protocol, e.g. "https://pike.lysator.liu.se/".

Parameter query_variables

Calls http_encode_query and appends the result to the URL.

Parameter request_headers

The HTTP headers to be added to the request. By default the headers User-agent, Host and, if needed by the url, Authorization will be added, with generated contents. Providing these headers will override the default. Setting the value to 0 will remove that header from the request.

Parameter con

Previously initialized connection object. In particular the callbacks must have been set (Query.set_callbacks()).

Parameter data

Data payload to be transmitted in the request.

See also

do_async_method(), do_proxied_method(), Query.set_callbacks()


Method do_method

.Query|zerodo_method(stringmethod, string|Standards.URIurl, void|mapping(string:int|string|array(string)) query_variables, void|mapping(string:string|array(string)|int) request_headers, void|Protocols.HTTP.Querycon, void|stringdata)

Description

Low level HTTP call method.

Parameter method

The HTTP method to use, e.g. "GET".

Parameter url

The URL to perform method on. Should be a complete URL, including protocol, e.g. "https://pike.lysator.liu.se/".

Parameter query_variables

Calls http_encode_query and appends the result to the URL.

Parameter request_headers

The HTTP headers to be added to the request. By default the headers User-agent, Host and, if needed by the url, Authorization will be added, with generated contents. Providing these headers will override the default. Setting the value to 0 will remove that header from the request.

Parameter con

Old connection object.

Parameter data

Data payload to be transmitted in the request.

See also

do_async_method()


Method do_proxied_method

.Querydo_proxied_method(string|Standards.URIproxy, stringuser, stringpassword, stringmethod, string|Standards.URIurl, void|mapping(string:int|string|array(string)) query_variables, void|mapping(string:string|array(string)|int) request_headers, void|Protocols.HTTP.Querycon, void|stringdata)

Description

Makes an HTTP request through a proxy.

Parameter proxy

URL for the proxy.

Parameter user
Parameter password

Proxy authentication credentials.

Parameter method
Parameter url
Parameter query_variables
Parameter request_headers
Parameter con
Parameter data

The remaining arguments are identical to do_method().

See also

do_method(), do_async_proxied_method()


Method get_url

.Queryget_url(string|Standards.URIurl, void|mapping(string:int|string|array(string)) query_variables, void|mapping(string:string|array(string)|int) request_headers, void|Protocols.HTTP.Querycon)

Description

Sends a HTTP GET request to the server in the URL and returns the created and initialized Query object. 0 is returned upon failure. If a query object having request_headers->Connection=="Keep-Alive" from a previous request is provided and the already established server connection can be used for the next request, you may gain some performance.


Method get_url_data

stringget_url_data(string|Standards.URIurl, void|mapping(string:int|string|array(string)) query_variables, void|mapping(string:string|array(string)|int) request_headers, void|Protocols.HTTP.Querycon)

Description

Returns the returned data after calling the requested server for information through HTTP GET. 0 is returned upon failure. Redirects (HTTP 302) are automatically followed.


Method get_url_nice

array(string)|zeroget_url_nice(string|Standards.URIurl, void|mapping(string:int|string|array(string)) query_variables, void|mapping(string:string|array(string)|int) request_headers, void|Protocols.HTTP.Querycon)

Description

Returns an array of ({content_type, data}) after calling the requested server for the information. 0 is returned upon failure. Redirects (HTTP 302) are automatically followed.


Method http_encode_query

stringhttp_encode_query(mapping(string:int|string|array(string)) variables)

Description

Encodes a query mapping to a string; this protects odd - in http perspective - characters like '&' and '#' and control characters, and packs the result together in a HTTP query string.

Example:

	> Protocols.HTTP.http_encode_query( (["anna":"eva","lilith":"blue"]) );
     Result: "lilith=blue&anna=eva"
     > Protocols.HTTP.http_encode_query( (["&":"&","'=\"":"\0\0\0\u0434"]) );
     Result: "%27%3D%22=%00%00%00%D0%B4&%26amp%3B=%26"
	

Method iri_encode

stringiri_encode(strings)

Description

Encodes the given string using %XX encoding to be used as a component part in an IRI (Internationalized Resource Identifier, see RFC 3987). This means that all chars outside the IRI iunreserved set are encoded, i.e. this function encodes equivalently to uri_encode except that all 8-bit and wider characters are left as-is.

Bugs

This function currently does not encode chars in the Unicode private ranges, although that is strictly speaking required in some but not all IRI components. That could change if it turns out to be a problem.

See also

percent_decode, uri_encode


Method percent_decode

stringpercent_decode(strings)

Description

Decodes URI-style %XX encoded chars in the given string.

See also

percent_encode, uri_decode

Bugs

This function currently does not accept wide string input, which is necessary to work as the reverse of iri_encode.


Method percent_encode

stringpercent_encode(strings)

Description

Encodes the given string using %XX encoding, except that URI unreserved chars are not encoded. The unreserved chars are A-Z, a-z, 0-9, -, ., _, and ~ (see RFC 2396 section 2.3).

8-bit chars are encoded straight, and wider chars are not allowed. That means this encoding is applicable if s is a binary octet string. If it is a character string then uri_encode should be used instead.

It is also slightly faster than uri_encode if s is known to contain only US-ASCII.


Method post_url

.Querypost_url(string|Standards.URIurl, mapping(string:int|string|array(string))|stringquery_variables, void|mapping(string:string|array(string)|int) request_headers, void|Protocols.HTTP.Querycon)

Description

Similar to get_url, except that query variables is sent as a POST request instead of a GET request. If query_variables is a simple string, it is assumed to contain the verbatim body of the POST request; Content-Type must be properly specified manually, in this case.


Method post_url_data

stringpost_url_data(string|Standards.URIurl, mapping(string:int|string|array(string))|stringquery_variables, void|mapping(string:string|array(string)|int) request_headers, void|Protocols.HTTP.Querycon)

Description

Similar to get_url_data, except that query variables is sent as a POST request instead of a GET request.


Method post_url_nice

array(string) post_url_nice(string|Standards.URIurl, mapping(string:int|string|array(string))|stringquery_variables, void|mapping(string:string|array(string)|int) request_headers, void|Protocols.HTTP.Querycon)

Description

Similar to get_url_nice, except that query variables is sent as a POST request instead of a GET request.


Method put_url

.Queryput_url(string|Standards.URIurl, void|stringfile, void|mapping(string:int|string|array(string)) query_variables, void|mapping(string:string|array(string)|int) request_headers, void|Protocols.HTTP.Querycon)

Description

Sends a HTTP PUT request to the server in the URL and returns the created and initialized Query object. 0 is returned upon failure. If a query object having request_headers->Connection=="Keep-Alive" from a previous request is provided and the already established server connection can be used for the next request, you may gain some performance.


Method quoted_string_decode

stringquoted_string_decode(strings)

Description

Decodes the given string which has been encoded as a quoted-string according to RFC 2616 section 2.2. s is assumed to not include the surrounding " chars.

See also

quoted_string_encode


Method quoted_string_encode

stringquoted_string_encode(strings)

Description

Encodes the given string quoted to be used as content inside a quoted-string according to RFC 2616 section 2.2. The returned string does not include the surrounding " chars.

Note

The quoted-string quoting rules in RFC 2616 have several problems:

  • Quoting is inconsistent since " is quoted as \", but \ does not need to be quoted. This is resolved in the HTTP bis update to mandate quoting of \ too, which this function performs.

  • Many characters are not quoted sufficiently to make the result safe to use in an HTTP header, so this quoting is not enough if s contains NUL, CR, LF, or any 8-bit or wider character.

See also

quoted_string_decode


Method uri_decode

stringuri_decode(strings)

Description

Decodes URI-style %XX encoded chars in the given string, and then UTF-8 decodes the result. This is the reverse of uri_encode and uri_encode_invalids.

See also

uri_encode, uri_encode_invalids


Method uri_encode

stringuri_encode(strings)

Description

Encodes the given string using %XX encoding to be used as a component part in a URI. This means that all URI reserved and excluded characters are encoded, i.e. everything except A-Z, a-z, 0-9, -, ., _, and ~ (see RFC 2396 section 2.3).

8-bit chars and wider are encoded using UTF-8 followed by percent-encoding. This follows RFC 3986 section 2.5, the IRI-to-URI conversion method in the IRI standard (RFC 3987) and appendix B.2 in the HTML 4.01 standard. It should work regardless of the charset used in the XML document the URI might be inserted into.

See also

uri_decode, uri_encode_invalids, iri_encode


Method uri_encode_invalids

stringuri_encode_invalids(strings)

Description

Encodes all "dangerous" chars in the given string using %XX encoding, so that it can be included as a URI in an HTTP message or header field. This includes control chars, space and various delimiter chars except those in the URI reserved set (RFC 2396 section 2.2).

Since this function doesn't touch the URI reserved chars nor the escape char %, it can be used on a complete formatted URI or IRI.

8-bit chars and wider are encoded using UTF-8 followed by percent-encoding. This follows RFC 3986 section 2.5, the IRI standard (RFC 3987) and appendix B.2 in the HTML 4.01 standard.

Note

The characters in the URI reserved set are: :, /, ?, #, [, ], @, !, $, &, ', (, ), *, +, ,, ;, =. In addition, this function doesn't touch the escape char %.

See also

uri_decode, uri_encode

Enum Protocols.HTTP.StatusCode

Description

HTTP Status codes.

See also

response_codes, https://www.iana.org/assignments/http-status-codes/http-status-codes.txt


Constant DAV_ALREADY_REPORTED

constant Protocols.HTTP.DAV_ALREADY_REPORTED

Description

RFC 5842 section 7.1: 208 Already Reported


Constant DAV_FAILED_DEP

constant Protocols.HTTP.DAV_FAILED_DEP

Description

RFC 2518 section 10.5: 424 Failed Dependency


Constant DAV_LOCKED

constant Protocols.HTTP.DAV_LOCKED

Description

RFC 2518 section 10.4: 423 Locked


Constant DAV_LOOP_DETECTED

constant Protocols.HTTP.DAV_LOOP_DETECTED

Description

RFC 5842 section 7.2: 508 Loop Detected


Constant DAV_MULTISTATUS

constant Protocols.HTTP.DAV_MULTISTATUS

Description

RFC 2518 section 10.2: 207 Multi-Status


Constant DAV_PROCESSING

constant Protocols.HTTP.DAV_PROCESSING

Description

RFC 2518 section 10.1: 102 Processing


Constant DAV_STORAGE_FULL

constant Protocols.HTTP.DAV_STORAGE_FULL

Description

RFC 2518 section 10.6: 507 Insufficient Storage


Constant DAV_UNPROCESSABLE

constant Protocols.HTTP.DAV_UNPROCESSABLE

Description

RFC 2518 section 10.3: 422 Unprocessable Entry


Constant DELTA_HTTP_IM_USED

constant Protocols.HTTP.DELTA_HTTP_IM_USED

Description

RFC 3229 section 10.4.1: 226 IM Used


Constant HTCPCP_TEAPOT

constant Protocols.HTTP.HTCPCP_TEAPOT

Description

RFC 2324 section 2.3.2: 418 I'm a teapot


Constant HTTP_ACCEPTED

constant Protocols.HTTP.HTTP_ACCEPTED

Description

RFC 2616 section 10.2.3: 202 Accepted


Constant HTTP_BAD

constant Protocols.HTTP.HTTP_BAD

Description

RFC 2616 section 10.4.1: 400 Bad Request


Constant HTTP_BAD_GW

constant Protocols.HTTP.HTTP_BAD_GW

Description

RFC 2616 section 10.5.3: 502 Bad Gateway


Constant HTTP_BAD_RANGE

constant Protocols.HTTP.HTTP_BAD_RANGE

Description

RFC 2616 section 10.4.17: 416 Requested Range Not Satisfiable


Constant HTTP_CONFLICT

constant Protocols.HTTP.HTTP_CONFLICT

Description

RFC 2616 section 10.4.10: 409 Conflict


Constant HTTP_CONTINUE

constant Protocols.HTTP.HTTP_CONTINUE

Description

RFC 2616 section 10.1.1: 100 Continue


Constant HTTP_CREATED

constant Protocols.HTTP.HTTP_CREATED

Description

RFC 2616 section 10.2.2: 201 Created


Constant HTTP_EARLY_HINTS

constant Protocols.HTTP.HTTP_EARLY_HINTS

Description

RFC 8297 section 2: 103 Early Hints


Constant HTTP_EXPECT_FAILED

constant Protocols.HTTP.HTTP_EXPECT_FAILED

Description

RFC 2616 section 10.4.18: 417 Expectation Failed


Constant HTTP_FORBIDDEN

constant Protocols.HTTP.HTTP_FORBIDDEN

Description

RFC 2616 section 10.4.4: 403 Forbidden


Constant HTTP_FOUND

constant Protocols.HTTP.HTTP_FOUND

Description

RFC 2616 section 10.3.3: 302 Found


Constant HTTP_GONE

constant Protocols.HTTP.HTTP_GONE

Description

RFC 2616 section 10.4.11: 410 Gone


Constant HTTP_GW_TIMEOUT

constant Protocols.HTTP.HTTP_GW_TIMEOUT

Description

RFC 2616 section 10.5.5: 504 Gateway Timeout


Constant HTTP_HEADERS_TOO_LARGE

constant Protocols.HTTP.HTTP_HEADERS_TOO_LARGE

Description

RFC 6585 section 5: 431 Request Header Fields Too Large


Constant HTTP_INTERNAL_ERR

constant Protocols.HTTP.HTTP_INTERNAL_ERR

Description

RFC 2616 section 10.5.1: 500 Internal Server Error


Constant HTTP_LEGALLY_RESTRICTED

constant Protocols.HTTP.HTTP_LEGALLY_RESTRICTED

Description

RFC 7725 section 3: 451 Unavailable For Legal Reasons


Constant HTTP_LENGTH_REQ

constant Protocols.HTTP.HTTP_LENGTH_REQ

Description

RFC 2616 section 10.4.12: 411 Length Required


Constant HTTP_METHOD_INVALID

constant Protocols.HTTP.HTTP_METHOD_INVALID

Description

RFC 2616 section 10.4.6: 405 Method Not Allowed


Constant HTTP_MISDIRECTED_REQ

constant Protocols.HTTP.HTTP_MISDIRECTED_REQ

Description

RFC 7540 section 9.1.2: 421 Misdirected Request


Constant HTTP_MOVED_PERM

constant Protocols.HTTP.HTTP_MOVED_PERM

Description

RFC 2616 section 10.3.2: 301 Moved Permanently


Constant HTTP_MULTIPLE

constant Protocols.HTTP.HTTP_MULTIPLE

Description

RFC 2616 section 10.3.1: 300 Multiple Choices


Constant HTTP_NET_AUTH_REQUIRED

constant Protocols.HTTP.HTTP_NET_AUTH_REQUIRED

Description

RFC 6585 section 6: 511 Network Authentication Required


Constant HTTP_NONAUTHORATIVE

constant Protocols.HTTP.HTTP_NONAUTHORATIVE

Description

RFC 2616 section 10.2.4: 203 Non-Authorative Information


Constant HTTP_NOT_ACCEPTABLE

constant Protocols.HTTP.HTTP_NOT_ACCEPTABLE

Description

RFC 2616 section 10.4.7: 406 Not Acceptable


Constant HTTP_NOT_EXTENDED

constant Protocols.HTTP.HTTP_NOT_EXTENDED

Description

RFC 2774 section 7: 510 Not Extended (obsolete)


Constant HTTP_NOT_FOUND

constant Protocols.HTTP.HTTP_NOT_FOUND

Description

RFC 2616 section 10.4.5: 404 Not Found


Constant HTTP_NOT_IMPL

constant Protocols.HTTP.HTTP_NOT_IMPL

Description

RFC 2616 section 10.5.2: 501 Not Implemented


Constant HTTP_NOT_MODIFIED

constant Protocols.HTTP.HTTP_NOT_MODIFIED

Description

RFC 2616 section 10.3.5: 304 Not Modified


Constant HTTP_NO_CONTENT

constant Protocols.HTTP.HTTP_NO_CONTENT

Description

RFC 2616 section 10.2.5: 204 No Content


Constant HTTP_OK

constant Protocols.HTTP.HTTP_OK

Description

RFC 2616 section 10.2.1: 200 OK


Constant HTTP_PARTIAL_CONTENT

constant Protocols.HTTP.HTTP_PARTIAL_CONTENT

Description

RFC 2616 section 10.2.7: 206 Partial Content


Constant HTTP_PAY

constant Protocols.HTTP.HTTP_PAY

Description

RFC 2616 section 10.4.3: 402 Payment Required


Constant HTTP_PERM_REDIRECT

constant Protocols.HTTP.HTTP_PERM_REDIRECT

Description

RFC 7538 section 3: 308 Permanent Redirect


Constant HTTP_PRECOND_FAILED

constant Protocols.HTTP.HTTP_PRECOND_FAILED

Description

RFC 2616 section 10.4.13: 412 Precondition Failed


Constant HTTP_PRECOND_REQUIRED

constant Protocols.HTTP.HTTP_PRECOND_REQUIRED

Description

RFC 6585 section 3: 428 Precondition required


Constant HTTP_PROXY_AUTH_REQ

constant Protocols.HTTP.HTTP_PROXY_AUTH_REQ

Description

RFC 2616 section 10.4.8: 407 Proxy Authentication Required


Constant HTTP_REQ_TOO_LARGE

constant Protocols.HTTP.HTTP_REQ_TOO_LARGE

Description

RFC 2616 section 10.4.14: 413 Request Entity Too Large


Constant HTTP_RESET_CONTENT

constant Protocols.HTTP.HTTP_RESET_CONTENT

Description

RFC 2616 section 10.2.6: 205 Reset Content


Constant HTTP_SEE_OTHER

constant Protocols.HTTP.HTTP_SEE_OTHER

Description

RFC 2616 section 10.3.4: 303 See Other


Constant HTTP_SWITCH_PROT

constant Protocols.HTTP.HTTP_SWITCH_PROT

Description

RFC 2616 section 10.1.2: 101 Switching protocols


Constant HTTP_TEMP_REDIRECT

constant Protocols.HTTP.HTTP_TEMP_REDIRECT

Description

RFC 2616 section 10.3.8: 307 Temporary Redirect


Constant HTTP_TIMEOUT

constant Protocols.HTTP.HTTP_TIMEOUT

Description

RFC 2616 section 10.4.9: 408 Request Timeout


Constant HTTP_TOO_MANY_REQUESTS

constant Protocols.HTTP.HTTP_TOO_MANY_REQUESTS

Description

RFC 6585 section 4: 429 Too Many Requests


Constant HTTP_UNAUTH

constant Protocols.HTTP.HTTP_UNAUTH

Description

RFC 2616 section 10.4.2: 401 Unauthorized


Constant HTTP_UNAVAIL

constant Protocols.HTTP.HTTP_UNAVAIL

Description

RFC 2616 section 10.5.4: 503 Service Unavailable


Constant HTTP_UNSUPP_MEDIA

constant Protocols.HTTP.HTTP_UNSUPP_MEDIA

Description

RFC 2616 section 10.4.16: 415 Unsupported Media Type


Constant HTTP_UNSUPP_VERSION

constant Protocols.HTTP.HTTP_UNSUPP_VERSION

Description

RFC 2616 section 10.5.6: 505 HTTP Version Not Supported


Constant HTTP_URI_TOO_LONG

constant Protocols.HTTP.HTTP_URI_TOO_LONG

Description

RFC 2616 section 10.4.15: 414 Request-URI Too Long


Constant HTTP_USE_PROXY

constant Protocols.HTTP.HTTP_USE_PROXY

Description

RFC 2616 section 10.3.6: 305 Use Proxy


Constant TCN_VARIANT_NEGOTIATES

constant Protocols.HTTP.TCN_VARIANT_NEGOTIATES

Description

RFC 2295 section 8.1: 506 Variant Also Negotiates


Constant TLS_TOO_EARLY

constant Protocols.HTTP.TLS_TOO_EARLY

Description

RFC 8470 section 5.2: 425 Too Early


Constant TLS_UPGRADE_REQUIRED

constant Protocols.HTTP.TLS_UPGRADE_REQUIRED

Description

RFC 2817 section 4.2: 426 Upgrade Required

Class Protocols.HTTP.Query

class Protocols.HTTP.Query
Description

Open and execute an HTTP query.

Example

HTTP.Query o=HTTP.Query();

void ok() { write("ok...\n"); write("%O\n", o->headers); exit(0); }

void fail() { write("fail\n"); exit(0); }

int main() { o->set_callbacks(ok, fail); o->async_request("pike.lysator.liu.se", 80, "HEAD / HTTP/1.0"); return -1; }


Variable buf
Variable headerbuf

string Protocols.HTTP.Query.buf
string Protocols.HTTP.Query.headerbuf

Description

Input buffers.


Variable con

Stdio.File|SSL.File|Stdio.UDP Protocols.HTTP.Query.con

Description

Connection to HTTP(s) server.


Variable errno

int Protocols.HTTP.Query.errno

Description

Errno copied from the connection or simulated for async operations.

Note

In Pike 7.8 and earlier hardcoded Linux values were used in async operations, 110 instead of System.ETIMEDOUT and 113 instead of System.EHOSTUNREACH.


Variable request_ok
Variable request_fail
Variable extra_args

localfunction(:void)|zero Protocols.HTTP.Query.request_ok
localfunction(:void)|zero Protocols.HTTP.Query.request_fail
array Protocols.HTTP.Query.extra_args

Description

Callbacks and arguments set by set_callbacks().


Variable headers

mapping Protocols.HTTP.Query.headers

Description

Headers as a mapping. All header names are in lower case, for convinience.


Variable host
Variable real_host
Variable port

string Protocols.HTTP.Query.host
string Protocols.HTTP.Query.real_host
int Protocols.HTTP.Query.port

Description

Connected host and port.

Used to detect whether keep-alive can be used.


Variable hostname_cache

mapping(string:array(string)) Protocols.HTTP.Query.hostname_cache

Description

Set this to a global mapping if you want to use a cache, prior of calling *request().


Variable timeout
Variable maxtime

int Protocols.HTTP.Query.timeout
int Protocols.HTTP.Query.maxtime

Description

timeout is the time to wait in seconds on connection and/or data. If data is fetched asynchronously the watchdog will be reset every time data is received. Defaults to 120 seconds. maxtime is the time the entire operation is allowed to take, no matter if the connection and data fetching is successful. This is by default indefinitely.

Note

These values only have effect in asynchroneous calls


Variable ok

int Protocols.HTTP.Query.ok

Description

Tells if the connection is successfull.


Variable protocol

string Protocols.HTTP.Query.protocol

Description

Protocol string, ie "HTTP/1.0".


Variable status
Variable status_desc

int Protocols.HTTP.Query.status
string Protocols.HTTP.Query.status_desc

Description

Status number and description (eg 200 and "ok").


Method `()

int res = Protocols.HTTP.Query()()

Description

Wait for connection to complete.

Returns

Returns 1 on successfull connection, 0 on failure.


Method async_fetch

voidasync_fetch(function(:void) callback, mixed ... extra)

Description

Fetch all data in background.

See also

timed_async_fetch(), async_request(), set_callbacks()


Method set_callbacks
Method async_request

Protocols.HTTP.Queryset_callbacks(function(:void)|zerorequest_ok, function(:void)|zerorequest_fail, mixed ... extra)
Protocols.HTTP.Queryasync_request(stringserver, intport, stringquery)
Protocols.HTTP.Queryasync_request(stringserver, intport, stringquery, mappingheaders, string|voiddata)

Description

Setup and run an asynchronous request, otherwise similar to thread_request().

request_ok(Protocols.HTTP.Query httpquery,...extra args) will be called when connection is complete, and headers are parsed.

request_fail(Protocols.HTTP.Query httpquery,...extra args) is called if the connection fails.

Returns

Returns the called object


Method cast

(array)Protocols.HTTP.Query()

Returns
Array
mapping0

Headers

string1

Data

string2

Protocol

int3

Status

string4

Status description


Method cast

(mapping)Protocols.HTTP.Query()

Returns

The header mapping ORed with the following mapping.

"protocol" : string

The protocol.

"status" : int

The status code.

"status_desc" : string

The status description.

"data" : string

The returned data.


Method cast

(string)Protocols.HTTP.Query()

Description

Gives back the answer as a string.


Method close

voidclose()

Description

Close all associated file descriptors.


Method data

stringdata(int|voidmax_length)

Description

Gives back the data as a string.


Method datafile

Protocols.HTTP.Query.PseudoFiledatafile()

Description

Gives back a pseudo-file object, with the methods read() and close(). This could be used to copy the file to disc at a proper tempo.

datafile() doesn't give the complete request, just the data.

See also

file()


Method downloaded_bytes

intdownloaded_bytes()

Description

Gives back the number of downloaded bytes.


Method file

Protocols.HTTP.Query.PseudoFilefile()
Protocols.HTTP.Query.PseudoFilefile(mappingnewheaders, void|mappingremoveheaders)

Description

Gives back a pseudo-file object, with the methods read() and close(). This could be used to copy the file to disc at a proper tempo.

newheaders, removeheaders is applied as: (oldheaders|newheaders))-removeheaders Make sure all new and remove-header indices are lower case.

See also

datafile()


Method ponder_answer

protectedintponder_answer(int|voidstart_position)

Description

Attempt to read the result header block from buf and con.

Returns
0

Returns 0 if the header block has not yet been received.

1

Sets headerbuf to the raw header block and headers to the parsed header block, calls the request_ok callback (if any) and returns 1 on successful read of the header block.

Note

The data part of the result may still be pending on 1 return.


Method thread_request

Protocols.HTTP.Querythread_request(stringserver, intport, stringquery)
Protocols.HTTP.Querythread_request(stringserver, intport, stringquery, mappingheaders, void|stringdata)

Description

Create a new query object and begin the query.

The query is executed in a background thread; call `() in the object to wait for the request to complete.

query is the first line sent to the HTTP server; for instance "GET /index.html HTTP/1.1".

headers will be encoded and sent after the first line, and data will be sent after the headers.

Returns

Returns the called object.


Method timed_async_fetch

voidtimed_async_fetch(function(this_program, __unknown__ ... :void) ok_callback, function(this_program, __unknown__ ... :void) fail_callback, mixed ... extra)

Description

Like async_fetch(), except with a timeout and a corresponding fail callback function.

See also

async_fetch(), async_request(), set_callbacks()


Method total_bytes

inttotal_bytes()

Description

Gives back the size of a file if a content-length header is present and parsed at the time of evaluation. Otherwise returns -1.


Method unicode_data

stringunicode_data()

Description

Gives back data, but decoded according to the content-type character set.

See also

data

Class Protocols.HTTP.Query.PseudoFile

class Protocols.HTTP.Query.PseudoFile
Description

Minimal simulation of a Stdio.File object.

Objects of this class are returned by file() and datafile().

Note

Do not attempt further queries using this Query object before having read all data.


Method close

voidclose()


Method read

stringread(intn, bool|voidnot_all)

Class Protocols.HTTP.Session

class Protocols.HTTP.Session

Typedef URL

typedefstring|Standards.URI|SessionURL Protocols.HTTP.Session.URL

Description

A URL which is either a string a Standards.URI or a SessionURL.


Variable default_headers

mapping Protocols.HTTP.Session.default_headers

Description

Default HTTP headers.


Variable follow_redirects

int Protocols.HTTP.Session.follow_redirects

Description

The number of redirects to follow, if any. This is the default to the created Request objects.

A redirect automatically turns into a GET request, and all header, query, post or put information is dropped.

Default is 20 redirects. A negative number will mean infinity.

Bugs

Loops will currently not be detected, only the limit works to stop loops.

See also

Request.follow_redirects


Variable hostname_cache

mapping Protocols.HTTP.Session.hostname_cache

Description

Cache of hostname to IP lookups. Given to and used by the Query objects.


Variable maximum_connection_reuse

int Protocols.HTTP.Session.maximum_connection_reuse

Description

Maximum times a connection is reused. Defaults to 1000000. <2 means no reuse at all.


Variable maximum_connections_per_server

int Protocols.HTTP.Session.maximum_connections_per_server

Description

Maximum number of connections to the same server. Used only by async requests. Defaults to 10 connections.


Variable maximum_total_connections

int Protocols.HTTP.Session.maximum_total_connections

Description

Maximum total number of connections. Limits only async requests, and the number of kept-alive connections (live connections + kept-alive connections <= this number) Defaults to 50 connections.


Variable time_to_keep_unused_connections

int|float Protocols.HTTP.Session.time_to_keep_unused_connections

Description

The time to keep unused connections in seconds. Set to zero to never save any kept-alive connections. (Might be good in a for instance totaly synchroneous script that keeps the backend thread busy and never will get call_outs.) Defaults to 10 seconds.


Method async_get_url
Method async_put_url
Method async_delete_url
Method async_post_url

Requestasync_get_url(URLurl, void|mappingquery_variables, function(:void)|zerocallback_headers_ok, function(:void)|zerocallback_data_ok, function(:void)|zerocallback_fail, mixed ... callback_arguments)
Requestasync_put_url(URLurl, void|stringfile, void|mappingquery_variables, function(:void)|zerocallback_headers_ok, function(:void)|zerocallback_data_ok, function(:void)|zerocallback_fail, mixed ... callback_arguments)
Requestasync_delete_url(URLurl, void|mappingquery_variables, function(:void)|zerocallback_headers_ok, function(:void)|zerocallback_data_ok, function(:void)|zerocallback_fail, mixed ... callback_arguments)
Requestasync_post_url(URLurl, mappingquery_variables, function(:void)|zerocallback_headers_ok, function(:void)|zerocallback_data_ok, function(:void)|zerocallback_fail, mixed ... callback_arguments)

Description

Sends a HTTP GET, POST, PUT or DELETE request to the server in the URL asynchroneously, and call the corresponding callbacks when result arrives (or not). The callbacks will receive the created Request object as first argument, then the given callback_arguments, if any.

callback_headers_ok is called when the HTTP request has received headers.

callback_data_ok is called when the HTTP request has been received completely, data and all.

callback_fail is called when the HTTP request has failed, on a TCP/IP or DNS level, or has received a forced timeout.

The created Request object is returned.


Method encode_cookies
Method decode_cookies

stringencode_cookies()
voiddecode_cookies(stringdata, voidno_clear)

Description

Dump all cookies to a string and read them back. This is useful to store cookies in between sessions (on disk, for instance). decode_cookies will throw an error upon parse failures. Also note, decode_cookies will clear out any previously learned cookies from the Session object, unless no_clear is given and true.


Method get_url
Method post_url
Method put_url
Method delete_url

Requestget_url(URLurl, void|mappingquery_variables)
Requestpost_url(URLurl, mapping|stringquery_variables)
Requestput_url(URLurl, stringfile, void|mappingquery_variables)
Requestdelete_url(URLurl, void|mappingquery_variables)

Description

Sends a HTTP GET, POST, PUT or DELETE request to the server in the URL and returns the created and initialized Request object. 0 is returned upon failure.


Method get_cookies

array(string) get_cookies(Standards.URI|SessionURLfor_url, void|boolno_delete)

Description

Get the cookies that we should send to this server, for this url. They are presented in the form suitable for HTTP headers (as an array). This will also take in count expiration of cookies, and delete expired cookies from the Session unless no_delete is true.


Method get_url_nice
Method get_url_data
Method post_url_nice
Method post_url_data

array(string) get_url_nice(URLurl, mappingquery_variables)
stringget_url_data(URLurl, mappingquery_variables)
array(string) post_url_nice(URLurl, mapping|stringquery_variables)
stringpost_url_data(URLurl, mapping|stringquery_variables)

Description

Returns an array of ({content_type,data}) and just the data string respective, after calling the requested server for the information. 0 is returned upon failure.

post* is similar to the get_url() class of functions, except that the query variables is sent as a POST request instead of as a GET.


Method give_me_connection

Querygive_me_connection(Standards.URIurl)

Description