class Athena::Framework::Request
inherits Reference
#
Wraps an HTTP::Request instance to provide additional functionality.
Forwards all additional methods to the wrapped HTTP::Request
instance.
Constants#
FORMATS = {"atom" => Set {"application/atom+xml"}, "css" => Set {"text/css"}, "csv" => Set {"text/csv"}, "form" => Set {"application/x-www-form-urlencoded", "multipart/form-data"}, "html" => Set {"text/html", "application/xhtml+xml"}, "js" => Set {"application/javascript", "application/x-javascript", "text/javascript"}, "json" => Set {"application/json", "application/x-json"}, "jsonld" => Set {"application/ld+json"}, "rdf" => Set {"application/rdf+xml"}, "rss" => Set {"application/rss+xml"}, "txt" => Set {"text/plain"}, "xml" => Set {"text/xml", "application/xml", "application/x-xml"}}
#
Represents the supported built in formats; mapping the format name to its valid MIME
type(s).
Additional formats may be registered via .register_format
.
Constructors#
.new(method : String, path : String, headers : HTTP::Headers | Nil = nil, body : String | Bytes | IO | Nil = nil, version : String = "HTTP/1.1") : self
#
Class methods#
.mime_types(format : String) : Set(String)
#
Returns the MIME
types for the provided format.
ATH::Request.mime_types "txt" # => Set{"text/plain"}
.override_trusted_header(header : ATH::Request::ProxyHeader, name : String) : Nil
#
Allows overriding the header name to look for off the request for a given ATH::Request::ProxyHeader
.
In some cases a proxy might not use the exact x-forwarded-*
header name.
See the external documentation for more information.
.register_format(format : String, mime_types : Indexable(String)) : Nil
#
Registers the provided format with the provided mime_types. Can also be used to change the mime_types supported for an existing format.
ATH::Request.register_format "some_format", {"some/mimetype"}
.set_trusted_hosts(host_patterns : Array(Regex)) : Nil
#
Allows setting a list of host_patterns used to whitelist the allowed hostnames of requests. If there is at least one pattern defined, requests whose hostname does NOT match any of the patterns, will receive a 400 response.
See ATH::Bundle:Schema#trusted_hosts for more information.
.set_trusted_proxies(trusted_proxies : Enumerable(String), trusted_header_set : ATH::Request::ProxyHeader) : Nil
#
Allows setting a list of trusted_proxies, and which ATH::Request::ProxyHeader
should be whitelisted.
The provided proxies are expected to be either IPv4 and/or IPv6 addresses.
The special "REMOTE_ADDRESS"
string is also supported that will map to the current request's remote address.
See the external documentation for more information.
.trusted_header_set : ATH::Request::ProxyHeader
#
Returns which ATH::Request::ProxyHeader
s have been whitelisted by the application as set via .set_trusted_proxies
, defaulting to all of them.
.trusted_host_patterns : Array(Regex)
#
Returns the list of trusted host patterns set via .set_trusted_hosts
.
.trusted_proxies : Array(String)
#
Returns the list of trusted proxy IP addresses as set via .set_trusted_proxies
.
Methods#
#action : ATH::ActionBase
#
The ATH::Action
object associated with this request.
Will only be set if a route was able to be resolved
as part of ATH::Listeners::Routing
.
#action? : ATH::ActionBase | ::Nil
#
The ATH::Action
object associated with this request.
Will only be set if a route was able to be resolved
as part of ATH::Listeners::Routing
.
#content_type_format : String | ::Nil
#
Returns the Format of the request based on its content-type
header, or nil
if the header is missing.
#files : Hash(String, Array(ATH::UploadedFile))
#
If enabled, Athena will populate this hash with files from the request body of multipart/form-data
requests.
The keys of the hash map to the name attribute of the related file input control. The value of the hash is an array in order to handle multi-file uploads using the same name.
#format(mime_type : String) : String | Nil
#
Returns the format for the provided mime_type.
request.format "text/plain" # => "txt"
#from_trusted_proxy? : Bool
#
Returns true
if this request originated from a trusted proxy.
See the external documentation for more information.
#host : String | ::Nil
#
Returns the host name the request originated from.
Supports reading from ATH::Request::ProxyHeader::FORWARDED_HOST
, falling back on the "host"
header.
See the external documentation for more information.
#mime_type(format : String) : String | Nil
#
Returns the first MIME
type for the provided format if defined, otherwise returns nil
.
request.mime_type "txt" # => "text/plain"
#port : Int32
#
Returns the port on which the request is made.
Supports reading from both ATH::Request::ProxyHeader::FORWARDED_PORT
and ATH::Request::ProxyHeader::FORWARDED_HOST
, falling back on the "host"
header, then #scheme
.
See the external documentation for more information.
ameba:disable Metrics/CyclomaticComplexity
#request_format(default : String | Nil = "json") : String | Nil
#
Returns the format for this request.
First checks if a format was explicitly set via #request_format=
.
Next, will check for the _format
request #attributes
, finally falling back on the provided default.
#request_format=(request_format : String | Nil)
#
Sets the #request_format
to the explicitly passed format.
#secure? : Bool
#
Returns true
the request was made over HTTPS, otherwise returns false
.
Supports reading from ATH::Request::ProxyHeader::FORWARDED_PROTO
.
See the external documentation for more information.