module Athena::Routing::HeaderUtils

Overview

Includes various HTTP header utility methods.

Defined in:

header_utils.cr

Class Method Summary

Class Method Detail

def self.make_disposition(disposition : ART::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.

ART::HeaderUtils.make_disposition :attachment, "download.txt"         # => attachment; filename="download.txt"
ART::HeaderUtils.make_disposition :attachment, "föö.html"             # => attachment; filename="f__.html"; filename*=UTF-8''f%C3%B6%C3%B6.html
ART::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.

ART::Response.new(
  file_contents,
  headers: HTTP::Headers{"content-disposition" => ART::HeaderUtils.make_disposition(:attachment, "foo.pdf")}
)

TODO Add link to StaticFileListener cookbook recipe.


[View source]
def self.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]
def self.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.

ART::HeaderUtils.to_string({"foo" => "bar", "key" => true}, ", ")          # => foo=bar, key
ART::HeaderUtils.to_string({"foo" => %q("foo\ bar"), "key" => true}, ", ") # => foo=\"foo\\\ bar\", key

[View source]
def self.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]