View Source cowmachine_util (cowmachine v1.13.1)

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

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

choose_charset(CSets, AccCharHdr)

-spec choose_charset(CSets, AccCharHdr) -> Result
                        when CSets :: [binary()], AccCharHdr :: binary(), Result :: none | binary().

Select the best fitting character set or none

choose_encoding(Encs, AccEncHdr)

-spec choose_encoding(Encs, AccEncHdr) -> Result
                         when Encs :: [binary()], AccEncHdr :: binary(), Result :: none | binary().

Select the best fitting encoding or none

choose_media_type_accepted(ContentTypesAccepted, ReqHeader)

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

choose_media_type_provided(Provided, AcceptHead)

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

convert_request_date(Date)

-spec convert_request_date(Date) -> Result when Date :: binary(), Result :: calendar:datetime().

Parse the HTTP date (IMF-fixdate, rfc850, asctime).

format_content_type(MediaType)

-spec format_content_type(MediaType) -> Result
                             when MediaType :: cow_http_hd:media_type(), Result :: binary().

is_media_type_accepted(ContentTypesAccepted, ContentTypeReqHeader)

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

is_valid_header(Header)

-spec is_valid_header(Header) -> Result when Header :: binary(), Result :: boolean().

Check header valid characters, see rfc822

is_valid_header_value(Header)

-spec is_valid_header_value(Header) -> Result when Header :: binary(), Result :: boolean().

Check if the given value is acceptaple for a http header value.

normalize_content_type(Type)

-spec normalize_content_type(Type) -> Result
                                when
                                    Type :: cowmachine_req:media_type(),
                                    Result :: cow_http_hd:media_type().

parse_header(String)

-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_qs(String)

-spec parse_qs(String) -> Result when String :: binary(), Result :: [{binary(), binary()}].

Parse an application/x-www-form-urlencoded string.


See also specification.

quoted_string(String)

-spec quoted_string(String) -> Result when String :: binary(), Result :: binary().

split_quoted_strings(String)

-spec split_quoted_strings(String) -> Result when String :: binary(), Result :: [binary()].

valid_location(Location)

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