cowmachine_util (cowmachine v1.16.1)
View SourceUtilities for parsing, quoting, and negotiation.
Summary
Functions
Select the best fitting character set or none
Select the best fitting encoding or none
Match the Accept request header with content_types_provided.
Parse the HTTP date (IMF-fixdate, rfc850, asctime).
Match the Content-Type request header with content_types_accepted against.
Check header valid characters, see rfc822
Check if the given value is acceptaple for a http header value.
Parse a Content-Type like header, return the main Content-Type and a property list of options.
Parse an application/x-www-form-urlencoded string. Returns a list of name/value pairs. If a value is missing then the value is assumed to be the empty string. Default the maximum number of name/value pairs returned is 10_000. More will throw too_many_qs_names. See also specification.
Parse an application/x-www-form-urlencoded string. With a maximum number of parsed name/value pairs. If more than the given amount is parsed then too_many_qs_names is thrown. If a value is missing then the value is assumed to be the empty string. See also specification.
Check if the given location is safe to use as a location header. This is uses as a defense against urls with scripts. The test is quite strict and will drop values that might have been acceptable.
Functions
-spec choose_charset(CSets, AccCharHdr) -> Result when CSets :: [binary()], AccCharHdr :: binary(), Result :: none | binary().
Select the best fitting character set or none
-spec choose_encoding(Encs, AccEncHdr) -> Result when Encs :: [binary()], AccEncHdr :: binary(), Result :: none | binary().
Select the best fitting encoding or none
-spec choose_media_type_accepted(ContentTypesAccepted, ReqHeader) -> Result when ContentTypesAccepted :: [cowmachine_req:media_type()], ReqHeader :: cow_http_hd:media_type(), Result :: cowmachine_req:media_type() | none.
-spec choose_media_type_provided(Provided, AcceptHead) -> Result when Provided :: MediaTypes, MediaTypes :: [MediaType], MediaType :: cow_http_hd:media_type(), AcceptHead :: binary(), Result :: cow_http_hd:media_type() | none.
Match the Accept request header with content_types_provided.
Return the Content-Type for the response.
If there is no acceptable/available match, return the atom none.
AcceptHead is the value of the request's Accept header.
Provided is a list of media types the controller can provide:
- a binary e.g. —
<<"text/html">> - a binary and parameters e.g. —
{<<"text/html">>,[{<<"level">>,<<"1">>}]} - two binaries e.g.
{<<"text">>, <<"html">>} - two binaries and parameters e.g. —
{<<"text">>,<<"html">>,[{<<"level">>,<<"1">>}]}
(the plain string case with no parameters is much more common)
-spec convert_request_date(Date) -> Result when Date :: binary(), Result :: calendar:datetime() | bad_date.
Parse the HTTP date (IMF-fixdate, rfc850, asctime).
-spec format_content_type(MediaType) -> Result when MediaType :: cow_http_hd:media_type(), Result :: binary().
-spec is_media_type_accepted(ContentTypesAccepted, ContentTypeReqHeader) -> Result when ContentTypesAccepted :: list(), ContentTypeReqHeader :: cow_http_hd:media_type(), Result :: boolean().
Match the Content-Type request header with content_types_accepted against.
Check header valid characters, see rfc822
Check if the given value is acceptaple for a http header value.
-spec normalize_content_type(Type) -> Result when Type :: cowmachine_req:media_type(), Result :: cow_http_hd:media_type().
-spec parse_header(String) -> Result when String :: binary(), Result :: {binary(), [{binary(), binary()}]}.
Parse a Content-Type like header, return the main Content-Type and a property list of options.
Parse an application/x-www-form-urlencoded string. Returns a list of name/value pairs. If a value is missing then the value is assumed to be the empty string. Default the maximum number of name/value pairs returned is 10_000. More will throw too_many_qs_names. See also specification.
-spec parse_qs(String, MaxNames) -> Result when String :: binary(), MaxNames :: integer(), Result :: [{binary(), binary()}].
Parse an application/x-www-form-urlencoded string. With a maximum number of parsed name/value pairs. If more than the given amount is parsed then too_many_qs_names is thrown. If a value is missing then the value is assumed to be the empty string. See also specification.
-spec valid_location(Location) -> Result when Location :: binary(), Result :: {true, binary()} | false.
Check if the given location is safe to use as a location header. This is uses as a defense against urls with scripts. The test is quite strict and will drop values that might have been acceptable.