/accept

Accept* headers for Erlang/Elixir

Primary LanguageErlangMIT LicenseMIT

Accept header(s) for Erlang/Elixir

Copyright (c) 2016 Ilya Khaprov <i.khaprov@gmail.com>.

Version: 0.3.5

Hex.pm Hex.pm Build Status Coverage Status

TOC

Headers

Accept

RFC

Parsing

1> accept_header:parse("text/*;q=0.3, text/html;q=0.7, text/html;level=1,"
                       "text/html;level=2;q=0.4, */*;q=0.5").
[{media_range,"text","*",0.3,[]},
 {media_range,"text","html",0.7,[]},
 {media_range,"text","html",1,[{"level","1"}]},
 {media_range,"text","html",0.4,[{"level","2"}]},
 {media_range,"*","*",0.5,[]}]

Content Negotiation

2> accept_header:negotiate("text/*;q=0.3, text/html;q=0.7, text/html;level=1,"
                           "text/html;level=2;q=0.4, */*;q=0.5",
                           ["text/html;level=2", "text/html;level-3"]).
"text/html;level-3"

"text/html;level-3" returned because "text/html;level=2" matches to text/html;level=2;q=0.4 with score 0.4 and most specific match for "text/html;level-3" is text/html;q=0.7 with score 0.7.

3> accept_header:negotiate("application/xml,application/xhtml+xml,"
3>                         "text/html;q=0.9,text/plain;q=0.8,image/png,image/*;q=0.9,*/*;q=0.5",
3>                         ["text/n3",
3>                          "application/rdf+xml"]).
"text/n3"

Negotiate preserves user-defined order for equally scored alternatives.

Accept-Encoding

RFC

Parsing

1> accept_encoding_header:parse("gzip;q=1.0, identity; q=0.5, *;q=0").
[{content_coding,"gzip",1.0,[]},
 {content_coding,"identity",0.5,[]},
 {content_coding,"*",0,[]}]

Content Negotiation

1> accept_encoding_header:negotiate("compress, gzip",
1>                                  ["identity", "compress"]).
"compress"

2> accept_encoding_header:negotiate("gzip;q=1.0, identity; q=0.5, *;q=0",
2>                                  ["identity", "sdc", "gzip", "compress"]).
"gzip"

3> accept_encoding_header:negotiate("compress, gzip, *;q=0",
3>                                  ["qwe"]).
undefined

Negotiate preserves user-defined order for equally scored alternatives.

Contributing

Sections order:

Types -> Macros -> Callbacks -> Public API -> Deprecations -> Private Parts

install git precommit hook:

   ./bin/pre-commit.sh install

Pre-commit check can be skipped passing --no-verify option to git commit.

License

MIT

Modules

accept_encoding_header
accept_header