HttpMessage

class HttpMessage

An HTTP HttpMessage which represents common functionality between requests and responses

method: str
headers: Headers

The HTTP headers for this HttpMessage

body: str

the contents of the body

http_version: str
socket: table

The socket to send/receive on

trailers: Headers or nil

The HTTP trailers

new(socket)
Parameters:

socket (any)

source(source)

Construct a HttpMessage from a source function

Parameters:

source (SourceFn)

Return type:

T or nil

Returns:

preamble or error

Return type:

table or str

tcp_source(socket)

Create a HttpMessage from a lua socket tcp socket

Parameters:

socket (table) – tcp socket

Return type:

T or nil

Return type:

nil or str

udp_source(socket)

Create a response from a lua socket udp socket

Parameters:

socket (table) – udp socket

Return type:

T or nil

Return type:

nil or str

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 note: This is only intended for use with chunk-encoding any other encoding scheme will end up ignoring these values

Parameters:
  • key (str) – The Header’s key

  • value (str) – The Header’s value

Return type:

HttpMessage

add_trailer(key, value)

Add a trailer to the internal map of trailers

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:

HttpMessage

replace_header(key, value)

Replace or append a header to the internal headers map

note: this is not additive, any existing value will be lost

Parameters:
  • key (str)

  • value (any) – If not a string will call tostring

Return type:

HttpMessage

replace_trailer(key, value)

Replace or append a trailer to the internal trailers map

note: This is not additive, any existing value will be lost note: This is only intended for use with chunk-encoding any other encoding scheme will end up ignoring these values

Parameters:
  • key (str)

  • value (any) – If not a string will call tostring

Return type:

HttpMessage

set_content_type(ct)

Set the Content-Type header for this HttpMessage

convenience wrapper around self:replace_header(‘content_type’, len)

Parameters:

ct (str) – The mime type to add as the Content-Type header’s value

Return type:

HttpMessage or nil

Return type:

nil or str

set_content_length(len)

Set the Content-Length header for this HttpMessage

Parameters:

len (number) – The length of the content that will be sent

Return type:

HttpMessage or nil

Return type:

nil or str

set_transfer_encoding(te, chunk_size)

Set the Transfer-Encoding header for this request by default this will be length encoding

Parameters:
  • te (str) – The transfer encoding

  • chunk_size (integer or nil) – if te is “chunked” the size of the chunk to send defaults to 1024

Return type:

T

append_body(s)

Append text to the body

Parameters:

s (str) – the text to append

Return type:

HttpMessage

read_header(key)
Parameters:

key (str or nil) – The header map key to use, defaults to “headers”

Return type:

boolean or nil

Return type:

nil or str

fill_headers()
Return type:

str or nil

get_content_length()
body_type()

Determine what type of body we are dealing with

Return type:

table or nil

Return type:

nil or str

fill_fixed_length_body(len)

fill a body based on content-length

Parameters:

len (integer)

Return type:

str or nil

Return type:

nil or str

Return type:

nil or str

fill_closed_body()

fill a body by reading until the socket is closed

Return type:

str or nil

Return type:

nil or str

Return type:

nil or str

fill_chunked_body_step()
Return type:

str or nil

Return type:

nil or str

Return type:

nil or str

fill_chunked_body()
Return type:

str or nil

Return type:

nil or str

Return type:

nil or str

check_for_trailers()

Check for trailers and add them to the headers if present

this should only be called when chunked encoding has been detected

fill_body()

attempt recieve the full body and set self.body to the bytes from the SourceFn

on error any partial bytes should be included in self.body

Return type:

nil or str

get_body()
Return type:

str or nil

Return type:

nil or str

get_headers()
Return type:

Headers or nil

Return type:

nil or str

serialize()

Serailize the provide Request or Response into a string with new lines

Returns:

result The serialized string if nil an error occured

Return type:

str or nil

Returns:

err If not nil the error

Return type:

nil or str

chunked_oubtbound_body_iter()

build and iterator for outbound chunked encoding

Return type:

Iterator or nil

Return type:

str or nil

normal_body_iter()

build and iterator for outbound non-chunked encoding

Return type:

Iterator or nil

Return type:

nil or str

send_preamble()

Send the first line of the Request|Response

Return type:

integer or nil

Return type:

str or nil

build_chunk(max)

Collect the preamble and headers to the provided limit

Parameters:

max (integer)

Return type:

str

Return type:

integer

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:

nil or 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:

nil or str

send(bytes, skip_length)

Final send of a request or response

Parameters:
  • bytes (str or nil)

  • skip_length (boolean or nil)