Payload¶
This class represent a single HTTP message, which can be either a
request
or a response
.
Transfer Modes¶
HTTP provides two ways to encode the transmission of a message 'body', of any size. This package supports both of them:
-
StreamTransfer. This is used for payload bodies where the exact length is known in advance, including most transfers of files. It is selected by calling
Payload.set_length
with an integer bytecount. Appication buffer sizes determine how much data is fed to the TCP connection at once, but the total amount must match this size. -
ChunkedTransfer. This is used when the payload length can not be known in advance, but can be large. It is selected by calling
Payload.set_length
with a parameter ofNone
. On the TCP link this mode can be detected because there is noContent-Length
header at all, being replaced by theTransfer-Encoding: chunked
header. In addition, the message body is separated into chunks, each with its own bytecount. As withStreamTransfer
mode, transmission can be spread out over time with the difference that it is the original data source that determines the chunk size.
If Payload.set_length
is never called at all, a variation on
StreamTransfer
called OneshotTransfer
is used. In this case, all of
the message body is placed into the message at once, using
Payload.add_chunk
calls. The size will be determined when the message is
submitted for transmission. Care must be taken not to consume too much
memory, especially on a server where there can be multiple messages in
transit at once.
The type of transfer being used by an incoming message can be determined
from its transfer_mode
field, which will be one of the
TransferMode types.
Sequence¶
For example, to send a message of possibly large size:
- Create the message with a call to
Payload.request
orPayload.response
. - Set the
session
field of the message. - Call
Payload.set_length
to indicate the length of the body. - Add any additional headers that may be required, such as
Content-type
. - Submit the message for transmission by calling the either the
HTTPSession.apply
method (in servers) or theHTTPCLient.apply
method in clients. - Wait for the
send_body
notification. - Make any number of calls to
Payload.send_chunk
. - Call
Payload.finish
.
To send a message of small, reasonable size (say, under 20KB), this simplified method can be used instead:
- Create the message with a call to
Payload.request
orPayload.response
. - Set the
session
field of the message. - Add any additional headers that may be required, such as
Content-type
. - Call
add_chunk
one or more times to add body data. - Submit the message for transmission by calling the either the
HTTPSession
.apply
method (in servers) or the HTTPClient.apply
method in clients.
Constructors¶
request¶
Create an HTTP request
message.
Parameters¶
Returns¶
- Payload iso^
response¶
Create an HTTP response
message.
Parameters¶
- status': Status val = reference
Returns¶
- Payload iso^
Public fields¶
var proto: String val¶
The HTTP protocol string
var status: U16 val¶
Internal representation of the response Status.
Will be 0
for HTTP requests.
var method: String val¶
The HTTP Method.
GET
, POST
, DELETE
, OPTIONS
, ...
For HTTP responses this will be the status string,
for a 200
status this will be 200 OK
, for 404
, 404 Not Found
etc..
var url: URL val¶
The HTTP request URL.
It will be used for the HTTP path and the Host
header.
The user
and password
fields are ignored.
For HTTP responses this will be an empty URL.
var transfer_mode: (ChunkedTransfer val | StreamTransfer val | OneshotTransfer val)¶
Determines the transfer mode of this message.
In case of outgoing requests or responses,
use set_length
to control the transfer mode.
In case of incoming requests, this field determines how the request is transferred.
var session: (HTTPSession tag | None val)¶
var username: String val¶
The username extracted from an Authentication
header of an HTTP request
received via HTTPServer.
This is not used and not sent using HTTPClient,
use update
to set an Authentication
header instead.
var password: String val¶
The password extracted from an Authentication
header of an HTTP request
received via HTTPServer.
This is not used and not sent using HTTPClient,
use update
to set an Authentication
header instead.
Public Functions¶
apply¶
Get a header.
Parameters¶
- key: String val
Returns¶
- String val ?
is_safe¶
A request method is "safe" if it does not modify state in the resource. These methods can be guaranteed not to have any body data. Return true for a safe request method, false otherwise.
Returns¶
- Bool val
body¶
Get the body in OneshotTransfer
mode.
In the other modes it raises an error.
Returns¶
set_length¶
Set the body length when known in advance. This determines the transfer mode that will be used. A parameter of 'None' will use Chunked Transfer Encoding. A numeric value will use Streamed transfer. Not calling this function at all will use Oneshot transfer.
Parameters¶
Returns¶
- None val
update¶
Set any header. If we've already received the header, append the value as a comma separated list, as per RFC 2616 section 4.2.
Parameters¶
Returns¶
- Payload ref^
headers¶
Get all the headers.
Returns¶
body_size¶
Get the total intended size of the body.
ServerConnection
accumulates actual size transferred for logging.
Returns¶
add_chunk¶
This is how application code adds data to the body in
OneshotTransfer
mode. For large bodies, call set_length
and use send_chunk
instead.
Parameters¶
Returns¶
- Payload ref^
send_chunk¶
This is how application code sends body data in StreamTransfer
and
ChunkedTransfer
modes. For smaller body lengths, add_chunk
in
Oneshot
mode can be used instead.
Parameters¶
Returns¶
- None val
finish¶
Mark the end of body transmission. This does not do anything, and is unnecessary, in Oneshot mode.
Returns¶
- None val
respond¶
Start sending a response from the server to the client.
Parameters¶
- response': Payload trn
Returns¶
- None val
has_body¶
Determines whether a message has a body portion.
Returns¶
- Bool val