turbo.httputil – Utilities for the HTTP protocol

The httputil namespace contains the HTTPHeader class and POST data parsers, which is a integral part of the HTTPServer class.

HTTPParser class

Used to parse HTTP headers. Parsing is done through Ryan Dahls HTTP Parser via the FFI. It is very fast and contains various protection against attacks. The .so is compiled when Turbo is installed with make install. Note that this class has sanity checking for input parameters. If they are of wrong type or contains bad data they will raise a error.

HTTPParser(hdr_str, hdr_t)

Create a new HTTPParser class instance.

hdr_t available:

  • turbo.httputil.hdr_t.HTTP_RESPONSE,
  • turbo.httputil.hdr_t.HTTP_REQUEST,
  • turbo.httputil.hdr_t.HTTP_BOTH
Parameters:
  • hdr_str (String) – (optional) Raw header, including ending double CLRF, if you want the class to parse headers on construction.
  • hdr_t (Number) – (optional) If hdr_str is defined this should also be defined. It is used to specify what kind of header you want to parse.
Return type:

HTTPParser object

HTTPParser:get_url()

Get URL.

Return type:String or nil
HTTPParser:get_url_field(UF_prop)

Get specified URL segment. If segment does not exist, nil is returned. Will throw error if no URL has been parsed. UF_prop available:

  • turbo.httputil.UF.SCHEMA,
  • turbo.httputil.UF.HOST,
  • turbo.httputil.UF.PORT,
  • turbo.httputil.UF.PATH,
  • turbo.httputil.UF.PATH,
  • turbo.httputil.QUERY,
  • turbo.httputil.UF.FRAGMENT,
  • turbo.httputil.UF.USERINFO
Parameters:UF_prop (Number) – Segment to return, values defined in turbo.httputil.UF.
Return type:String or nil
HTTPParser:parse_url(url)

Parse standalone URL and populate class instance with values. HTTPParser.get_url_field must be used to read out value.

Parameters:url (String) – URL string.
HTTPParser:get_method()

Get current URL request method.

Return type:String or nil
HTTPParser:get_status_code()

Get the current HTTP status code.

Return type:Number or nil
HTTPParser:get(key, caseinsensitive)

Get given key from header key value section.

Parameters:
  • key (String) – Value to get, e.g “Content-Encoding”.
  • caseinsensitive (Boolean) – If true then the key will be matched without regard for case sensitivity.
Return type:

The value of the key in String form, or nil if not existing. May return a table if multiple keys are set.

HTTPParser:get_argument(name)

Get a argument from the query section of parsed URL. (e.g ?param1=myvalue) Note that this method only gets one argument. If there are multiple arguments with same name use HTTPParser.get_arguments()

Parameters:name (String) – The name of the argument.
Return type:String or nil
HTTPParser:get_arguments()

Get all URL query arguments of parsed URL. Support multiple values with same name.

Return type:Table
HTTPParser:parse_response_header(raw_headers)

Parse HTTP response headers. Populates the class with all data in headers. Will throw error on parsing failure.

Parameters:raw_headers (String) – Raw HTTP response header in string form.
HTTPParser:parse_request_header(raw_headers)

Parse HTTP request headers. Populates the class with all data in headers. Will throw error on parsing failure.

Parameters:raw_headers (String) – Raw HTTP request header in string form.

HTTPHeaders class

Used to compile HTTP headers. Note that this class has sanity checking for input parameters. If they are of wrong type or contains bad data they will raise a error.

HTTPHeaders()

Create a new HTTPHeaders class instance.

Return type:HTTPHeaders object

Manipulation

HTTPHeaders:set_uri(uri)

Set URI. Mostly usefull when building up request headers, NOT when parsing response headers. Parsing should be done with HTTPHeaders:parse_url.

Parameters:uri (String) – URI string to set.
HTTPHeaders:get_uri()

Get current URI.

Return type:String or nil
HTTPHeaders:set_method(method)

Set URL request method. E.g “POST” or “GET”.

Parameters:method (String) – Method to set.
HTTPHeaders:get_method()

Get current URL request method.

Return type:String or nil
HTTPHeaders:set_version(version)

Set HTTP protocol version.

Parameters:version (String) – Version string to set.
HTTPHeaders:get_version()

Get current HTTP protocol version.

Return type:String or nil
HTTPHeaders:set_status_code(code)

Set HTTP status code. The code is validated against all known.

Parameters:code (Number) – The code to set.
HTTPHeaders:get_status_code()

Get the current HTTP status code.

Return type:Number or nil
HTTPHeaders:get(key, caseinsensitive)

Get given key from header key value section.

Parameters:
  • key (String) – Value to get, e.g “Content-Encoding”.
  • caseinsensitive (Boolean) – If true then the key will be matched without regard for case sensitivity.
Return type:

The value of the key in String form, or nil if not existing. May return a table if multiple keys are set.

HTTPHeaders:add(key, value)

Add a key with value to the headers. Supports adding multiple values to one key. E.g mutiple “Set-Cookie” header fields.

Parameters:
  • key (String) – Key to add to headers. Must be string or error is raised.
  • value (String) – Value to associate with the key.
HTTPHeaders:set(key, value, caseinsensitive)

Set a key with value to the headers. Overwiting existing key.

Parameters:
  • key (String) – The key to set.
  • value (String) – Value to associate with the key.
  • caseinsensitive (Boolean) – If true then the existing keys will be matched without regard for case sensitivity and overwritten.
HTTPHeaders:remove(key, caseinsensitive)

Remove a key value combination from the headers.

Parameters:
  • key (String) – Key to remove.
  • caseinsensitive (Boolean) – If true then the existing keys will be matched without regard for case sensitivity and overwritten.

Stringifiers

HTTPHeaders:stringify_as_request()

Stringify data set in class as a HTTP request header.

Return type:String. HTTP header string excluding final delimiter.
HTTPHeaders:stringify_as_response()

Stringify data set in class as a HTTP response header. If not “Date” field is set, it will be generated automatically.

Return type:String. HTTP header string excluding final delimiter.
HTTPHeaders:__tostring()

Convinience method to return HTTPHeaders:stringify_as_response on string conversion.

Return type:String. HTTP header string excluding final delimiter.

Functions

parse_multipart_data(data)

Parse multipart form data.

Parameters:data (String) – Multi-part form data in string form.
Return type:Table of keys with corresponding values. Each key may hold multiple values if there were found multiple values for one key.
parse_post_arguments(data)

Parse ? and & separated key value fields.

Parameters:data (String) – Form data in string form.
Return type:Table of keys with corresponding values. Each key may hold multiple values if there were found multiple values for one key.