Request¶
-
class
Request
¶ An HTTP Request
-
method
: str¶ the HTTP method for this request
-
url
: table¶ The parsed url of this request
-
http_version
: str¶ The http version from the request first line
-
headers
: Headers¶ The HTTP headers for this request
-
body
: str¶ The contents of the request’s body
-
socket
: table¶ Lua socket for receiving/sending
-
_source
: fun():str¶ ltn12 source
-
_parsed_headers
: boolean¶
-
_received_body
: boolean¶
-
static
source
(source)¶ Construct a request from a ltn12 source function
- Parameters
source (fun():str) – this should always return a single line when called
- Return type
-
static
tcp_source
(socket)¶ Create a new Request with a lua socket
- Parameters
socket (table) – tcp socket
- Return type
-
static
udp_source
(socket)¶ Create a new Request with a lua socket
- Parameters
socket (table) – udp socket
- Return type
Request or str or nil
-
get_headers
()¶ Get the headers for this request
parsing the incoming stream of headers if not already parsed
- Return type
Headers or str or nil
-
_fill_headers
()¶ read from the socket filling in the headers property
-
get_body
()¶ Get the contents of this request’s body
if not yet received, this will read the body from the socket
- Return type
str or nil or str or nil
-
content_length
()¶ Get the value from the Content-Length header that should be present
for all http requests
- Return type
number or nil or str or nil
-
static
new
(method, url, socket)¶ Construct a request Builder
- Parameters
method (str) – an http method string
url (str or table) – the path for this request as a string or as a net_url table
socket (table) –
- Return type
-
add_header
(key, value)¶ Add a header to the internal map of headers
note: this is additive, so adding X-Forwarded-For twice will cause there to be multiple X-Forwarded-For entries in the serialized headers
- Parameters
key (str) – The Header’s key
value (str) – The Header’s value
- Return type
-
set_content_type
(ct)¶ Set the Content-Type header for this request
convenience wrapper around self:add_header(‘content_type’, len)
- Parameters
ct (str) – The mime type to add as the Content-Type header’s value
- Return type
-
set_content_length
(len)¶ Set the Content-Length header for this request
convenience wrapper around self:add_header(‘content_length’, len)
- Parameters
len (number) – The Expected length of the body
- Return type
-
append_body
(chunk)¶ append the provided chunk to this Request’s body
- Parameters
chunk (str) – The text to add to this request’s body
- Return type
-
serialize
()¶ Serialize this request into a single string
- Return type
str
-
iter
()¶ Serialize this request as a lua iterator that will
provide the next line (including new line characters). This will split the body on any internal new lines as well
- Return type
fun():str
-
send_preamble
()¶ Serialize and pass the first line of this Request into the sink
- Returns
if not nil, success
- Return type
integer
- Returns
if not nil and error message
- Return type
str
-
send_header
()¶ Pass a single header line into the sink functions
- Returns
If not nil, then successfully “sent”
- Return type
integer or nil
- Returns
If not nil, the error message
- Return type
str
-
send_body_chunk
()¶ Slice a chunk of at most 1024 bytes from self.body and pass it to
the sink
- Returns
if not nil, success
- Return type
integer or nil
- Returns
if not nil and error message
- Return type
str
-
send
(bytes)¶ Serialize and pass the request chunks into the sink
- Parameters
bytes (str or nil) – the final bytes to append to the body
- Returns
If not nil sent successfully
- Return type
integer or nil
- Returns
if not nil the error message
- Return type
str
-
_sending_body
()¶
-