module Athena::Framework::HeaderUtils
#
Includes various HTTP
header utility methods.
Class methods#
.combine(parts : Enumerable) : Hash(String, String | Bool)
#
Combines a 2D array of parts into a single Hash.
Each child array should have one or two elements, with the first representing the key and the second representing the value.
If there is no second value, true
will be used.
The keys of the resulting hash are all downcased.
ATH::HeaderUtils.combine [["foo", "abc"], ["bar"]] # => {"foo" => "abc", "bar" => true}
.make_disposition(disposition : ATH::BinaryFileResponse::ContentDisposition, filename : String, fallback_filename : String | Nil = nil) : String
#
Generates a HTTP
content-disposition header value with the provided disposition and filename.
If filename contains non ASCII
characters, a sanitized version will be used as part of the filename
directive,
while an encoded version of it will be used as the filename*
directive.
The fallback_filename argument can be used to customize the filename
directive value in this case.
ATH::HeaderUtils.make_disposition :attachment, "download.txt" # => attachment; filename="download.txt"
ATH::HeaderUtils.make_disposition :attachment, "föö.html" # => attachment; filename="f__.html"; filename*=utf-8''f%C3%B6%C3%B6.html
ATH::HeaderUtils.make_disposition :attachment, "föö.html", "foo.html" # => attachment; filename="foo.html"; filename*=utf-8''f%C3%B6%C3%B6.html
This method can be used to enable downloads of dynamically generated files. I.e. that can't be handled via a static file event listener.
ATH::Response.new(
file_contents,
headers: HTTP::Headers{"content-disposition" => ATH::HeaderUtils.make_disposition(:attachment, "foo.pdf")}
)
Tip
Checkout the Getting Started docs for an example of how to serve static files.
.split(header : String, separators : String) : Array
#
Splits an HTTP header by one or more separators, provided in priority order. Returns an array with as many levels as there are separators.
# First splits on `,`, then `;` as defined via the order of the separators.
ATH::HeaderUtils.split "da, en-gb;q=0.8", ",;" # => [["da"], ["en-gb", "q=0.8"]]
ATH::HeaderUtils.split "da, en-gb;q=0.8", ";," # => [["da", "en-gb"], ["q=0.8"]]]
.to_string(io : IO, collection : Hash, separator : String | Char) : Nil
#
Joins a key/value pair collection for use within an HTTP
header; writing the data to the provided io.
The key and value of each entry is joined with =
, quoting the value if needed.
All entries are then joined by the provided separator.
.to_string(collection : Hash, separator : String | Char) : String
#
Joins a key/value pair collection into a string for use within an HTTP
header.
The key and value of each entry is joined with =
, quoting the value if needed.
All entries are then joined by the provided separator.
ATH::HeaderUtils.to_string({"foo" => "bar", "key" => true}, ", ") # => foo=bar, key
ATH::HeaderUtils.to_string({"foo" => %q("foo\ bar"), "key" => true}, ", ") # => foo=\"foo\\\ bar\", key
.to_string(separator : String | Char, **parts) : String
#
Joins the provided key/value parts into a string for use within an HTTP
header.
The key and value of each entry is joined with =
, quoting the value if needed.
All entries are then joined by the provided separator.