Skip to content

module Athena::Framework::HeaderUtils #

Includes various HTTP header utility methods.

Class methods#

.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.

View source

.parse(header : String) : Hash(String, String | Bool)#

View source

.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.

View source

.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
View source

.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.

View source