Response

class Response

An HTTP Response

headers: Headers

The HTTP headers for this response

body: str

the contents of the response body

status: number

The HTTP status 3 digit number

http_version: str
socket: table

The socket to send/receive on

_source: function

(pat:string|nil):string ltn12 source

_parsed_headers: boolean
_received_body: boolean
static source(source)

Create a request parser from an ltn12 source function

Parameters

source (fun():str) –

Return type

Response

Returns

if return 1 is nil the error string

Return type

str

static tcp_source(socket)

Create a response from a lua socket tcp socket

Parameters

socket (table) – tcp socket

Return type

Response

static udp_source(socket)

Create a response from a lua socket udp socket

Parameters

socket (table) – udp socket

Return type

Response

get_headers()
get_content_length()

Attempt to get the value from Content-Length header

Returns

when not nil the Content-Length

Return type

number or nil

Returns

when not nil the error message

Return type

str or nil

next_line()

Get the next line from an incoming request, checking first

if we have reached the end of the content

Return type

str or nil

Return type

str or nil

get_body()
static new(status_code, socket)

Create a new response for building in memory

Parameters
  • status_code (number) –

  • socket (table) – luasocket for sending

add_header(key, value)

Append a header to the internal headers map

note: this is additive, though the _last_ value is used during serialization

Parameters
  • key (str) –

  • value (str) –

Return type

Response

set_content_type(s)

Set the Content-Type of the outbound request

Parameters

s (str) – the mime type for this request

Return type

Response

set_content_length(len)

Set the Content-Length header of the outbound response

Parameters

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

Return type

Response

serialize()

Serialize this full response into a string

Return type

str

append_body(s)

Append text to the body

Parameters

s (str) – the text to append

Return type

Response

set_status(n)

Set the status for this outgoing request

Parameters

n (number) – the 3 digit status

Return type

Response

iter()

Creates a lua iterator returning a line (with new line characters)

for this Response

Return type

function

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

has_sent()
_sending_body()