Module restia.negotiator

Handles content negotiation with the client.

Info:

  • License: Unlicense
  • Author: DarkWiiPlayer

Functions

parse (accept) Parses an “accept” header and returns its entries.
patterns (accept) Works like negotiator.parse but adds a pattern field to them.
pick (available) Picks a value from a content-type –> value map respecting an accept header.

Local Functions

escape (pattern) Escapes all the special pattern characters in a string
pattern (accept) Takes a content type string and turns the string into patterns to match said type(s)


Functions

parse (accept)
Parses an “accept” header and returns its entries. Values are returned as: {q = <Q-Value>, s = <Specificity>, type = <content type>} where specificity can be 1 for /, 2 for <type>/* or 3 for <type>/<subtype>

Parameters:

  • accept string The full HTTP Accept header
patterns (accept)
Works like negotiator.parse but adds a pattern field to them.

Parameters:

  • accept
pick (available)
Picks a value from a content-type –> value map respecting an accept header. When handlers are given as a sequence of tuples or strings, it checks them in order and prefers lower indices when more than one element matches. This is to allow prioritizing computationally cheaper content representations when clients can accept both. @tparam string accept A full HTTP Accept header

Parameters:

  • available table A table of content types

Returns:

    type value

Usage:

    -- Check in order and use first match
    type, content = restia.negotiator.pick(headers.accept, {
    	{'text/plain', "Hello!"},
    	{'text/html', "<h1>Hello!</h1>"}
    })
    -- Check out of order and use first match
    type, content = restia.negotiator.pick(headers.accept, {
    	['text/plain'] = "Hello!"
    	['text/html'] = "<h1>Hello!</h1>",
    })
    -- Return a fitting content type
    type = restia.negotiator.pick(headers.accept, {
    	'appliaction/json', 'text/html', 'application/yaml'
    })
    -- Returns a fallback
    type, content =
    	restia.negotiator.pick(headers.accept, {}, "text/plain", "Hello!")

Local Functions

escape (pattern)
Escapes all the special pattern characters in a string

Parameters:

  • pattern string A string to escape
pattern (accept)
Takes a content type string and turns the string into patterns to match said type(s)

Parameters:

  • accept string A single content-type
generated by LDoc 1.4.6 Last updated 2021-01-03 16:45:09