mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-16 05:00:24 +00:00
Better handle content negotiation when accept contains charsets
Thanks to Philip Witty for help figuring this out.
This commit is contained in:
parent
d4dff21055
commit
399b6a16b4
6 changed files with 231 additions and 19 deletions
22
test/handlers/charset_in_content_types_provided_h.erl
Normal file
22
test/handlers/charset_in_content_types_provided_h.erl
Normal file
|
@ -0,0 +1,22 @@
|
|||
%% This module has a media type provided with an explicit charset.
|
||||
|
||||
-module(charset_in_content_types_provided_h).
|
||||
|
||||
-export([init/2]).
|
||||
-export([content_types_provided/2]).
|
||||
-export([charsets_provided/2]).
|
||||
-export([get_text_plain/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
{cowboy_rest, Req, Opts}.
|
||||
|
||||
content_types_provided(Req, State) ->
|
||||
{[
|
||||
{{<<"text">>, <<"plain">>, [{<<"charset">>, <<"utf-8">>}]}, get_text_plain}
|
||||
], Req, State}.
|
||||
|
||||
charsets_provided(Req, State) ->
|
||||
{[<<"utf-16">>, <<"iso-8861-1">>], Req, State}.
|
||||
|
||||
get_text_plain(Req, State) ->
|
||||
{<<"This is REST!">>, Req, State}.
|
|
@ -0,0 +1,24 @@
|
|||
%% This module has a media type provided with a wildcard
|
||||
%% and a list of charsets that is limited.
|
||||
|
||||
-module(charset_in_content_types_provided_implicit_h).
|
||||
|
||||
-export([init/2]).
|
||||
-export([content_types_provided/2]).
|
||||
-export([charsets_provided/2]).
|
||||
-export([get_text_plain/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
{cowboy_rest, Req, Opts}.
|
||||
|
||||
content_types_provided(Req, State) ->
|
||||
{[
|
||||
{{<<"text">>, <<"plain">>, '*'}, get_text_plain}
|
||||
], Req, State}.
|
||||
|
||||
charsets_provided(Req, State) ->
|
||||
{[<<"utf-8">>, <<"utf-16">>], Req, State}.
|
||||
|
||||
get_text_plain(Req, State) ->
|
||||
{<<"This is REST!">>, Req, State}.
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
%% This module has a media type provided with a wildcard
|
||||
%% and lacks a charsets_provided callback.
|
||||
|
||||
-module(charset_in_content_types_provided_implicit_no_callback_h).
|
||||
|
||||
-export([init/2]).
|
||||
-export([content_types_provided/2]).
|
||||
-export([get_text_plain/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
{cowboy_rest, Req, Opts}.
|
||||
|
||||
content_types_provided(Req, State) ->
|
||||
{[
|
||||
{{<<"text">>, <<"plain">>, '*'}, get_text_plain}
|
||||
], Req, State}.
|
||||
|
||||
get_text_plain(Req, State) ->
|
||||
{<<"This is REST!">>, Req, State}.
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue