139de3072SChristopher Lenz%% @author Bob Ippolito <bob@mochimedia.com>
239de3072SChristopher Lenz%% @copyright 2007 Mochi Media, Inc.
339de3072SChristopher Lenz
439de3072SChristopher Lenz%% @doc Case preserving (but case insensitive) HTTP Header dictionary.
539de3072SChristopher Lenz
639de3072SChristopher Lenz-module(mochiweb_headers).
739de3072SChristopher Lenz-author('bob@mochimedia.com').
839de3072SChristopher Lenz-export([empty/0, from_list/1, insert/3, enter/3, get_value/2, lookup/2]).
9a104cbfcSFilipe David Borba Manana-export([delete_any/2, get_primary_value/2, get_combined_value/2]).
1039de3072SChristopher Lenz-export([default/3, enter_from_list/2, default_from_list/2]).
1139de3072SChristopher Lenz-export([to_list/1, make/1]).
1222c551bbSPaul Joseph Davis-export([from_binary/1]).
1339de3072SChristopher Lenz
1439de3072SChristopher Lenz%% @type headers().
1539de3072SChristopher Lenz%% @type key() = atom() | binary() | string().
1639de3072SChristopher Lenz%% @type value() = atom() | binary() | string() | integer().
1739de3072SChristopher Lenz
1839de3072SChristopher Lenz%% @spec empty() -> headers()
1939de3072SChristopher Lenz%% @doc Create an empty headers structure.
2039de3072SChristopher Lenzempty() ->
2139de3072SChristopher Lenz    gb_trees:empty().
2239de3072SChristopher Lenz
2339de3072SChristopher Lenz%% @spec make(headers() | [{key(), value()}]) -> headers()
2439de3072SChristopher Lenz%% @doc Construct a headers() from the given list.
2539de3072SChristopher Lenzmake(L) when is_list(L) ->
2639de3072SChristopher Lenz    from_list(L);
27a104cbfcSFilipe David Borba Manana%% assume a non-list is already mochiweb_headers.
28a104cbfcSFilipe David Borba Mananamake(T) ->
2939de3072SChristopher Lenz    T.
3039de3072SChristopher Lenz
314b0948ddSRobert Newson%% @spec from_binary(iolist()) -> headers()
3222c551bbSPaul Joseph Davis%% @doc Transforms a raw HTTP header into a mochiweb headers structure.
3322c551bbSPaul Joseph Davis%%
3422c551bbSPaul Joseph Davis%%      The given raw HTTP header can be one of the following:
3522c551bbSPaul Joseph Davis%%
364b0948ddSRobert Newson%%      1) A string or a binary representing a full HTTP header ending with
3722c551bbSPaul Joseph Davis%%         double CRLF.
3822c551bbSPaul Joseph Davis%%         Examples:
394b0948ddSRobert Newson%%         ```
4022c551bbSPaul Joseph Davis%%         "Content-Length: 47\r\nContent-Type: text/plain\r\n\r\n"
414b0948ddSRobert Newson%%         <<"Content-Length: 47\r\nContent-Type: text/plain\r\n\r\n">>'''
4222c551bbSPaul Joseph Davis%%
434b0948ddSRobert Newson%%      2) A list of binaries or strings where each element represents a raw
4422c551bbSPaul Joseph Davis%%         HTTP header line ending with a single CRLF.
4522c551bbSPaul Joseph Davis%%         Examples:
464b0948ddSRobert Newson%%         ```
474b0948ddSRobert Newson%%         [<<"Content-Length: 47\r\n">>, <<"Content-Type: text/plain\r\n">>]
484b0948ddSRobert Newson%%         ["Content-Length: 47\r\n", "Content-Type: text/plain\r\n"]
494b0948ddSRobert Newson%%         ["Content-Length: 47\r\n", <<"Content-Type: text/plain\r\n">>]'''
5022c551bbSPaul Joseph Davis%%
5122c551bbSPaul Joseph Davisfrom_binary(RawHttpHeader) when is_binary(RawHttpHeader) ->
5222c551bbSPaul Joseph Davis    from_binary(RawHttpHeader, []);
5322c551bbSPaul Joseph Davisfrom_binary(RawHttpHeaderList) ->
5422c551bbSPaul Joseph Davis    from_binary(list_to_binary([RawHttpHeaderList, "\r\n"])).
5522c551bbSPaul Joseph Davis
5622c551bbSPaul Joseph Davisfrom_binary(RawHttpHeader, Acc) ->
5722c551bbSPaul Joseph Davis    case erlang:decode_packet(httph, RawHttpHeader, []) of
584b0948ddSRobert Newson        {ok, {http_header, _, H, _, V}, Rest} ->
5922c551bbSPaul Joseph Davis            from_binary(Rest, [{H, V} | Acc]);
6022c551bbSPaul Joseph Davis        _ ->
6122c551bbSPaul Joseph Davis            make(Acc)
6222c551bbSPaul Joseph Davis    end.
6322c551bbSPaul Joseph Davis
6439de3072SChristopher Lenz%% @spec from_list([{key(), value()}]) -> headers()
6539de3072SChristopher Lenz%% @doc Construct a headers() from the given list.
6639de3072SChristopher Lenzfrom_list(List) ->
6739de3072SChristopher Lenz    lists:foldl(fun ({K, V}, T) -> insert(K, V, T) end, empty(), List).
6839de3072SChristopher Lenz
6939de3072SChristopher Lenz%% @spec enter_from_list([{key(), value()}], headers()) -> headers()
7039de3072SChristopher Lenz%% @doc Insert pairs into the headers, replace any values for existing keys.
7139de3072SChristopher Lenzenter_from_list(List, T) ->
7239de3072SChristopher Lenz    lists:foldl(fun ({K, V}, T1) -> enter(K, V, T1) end, T, List).
7339de3072SChristopher Lenz
7439de3072SChristopher Lenz%% @spec default_from_list([{key(), value()}], headers()) -> headers()
7539de3072SChristopher Lenz%% @doc Insert pairs into the headers for keys that do not already exist.
7639de3072SChristopher Lenzdefault_from_list(List, T) ->
7739de3072SChristopher Lenz    lists:foldl(fun ({K, V}, T1) -> default(K, V, T1) end, T, List).
7839de3072SChristopher Lenz
7939de3072SChristopher Lenz%% @spec to_list(headers()) -> [{key(), string()}]
8039de3072SChristopher Lenz%% @doc Return the contents of the headers. The keys will be the exact key
81282b96ddSNoah Slater%%      that was first inserted (e.g. may be an atom or binary, case is
8239de3072SChristopher Lenz%%      preserved).
8339de3072SChristopher Lenzto_list(T) ->
8439de3072SChristopher Lenz    F = fun ({K, {array, L}}, Acc) ->
852f40a05fSChristopher Lenz                L1 = lists:reverse(L),
862f40a05fSChristopher Lenz                lists:foldl(fun (V, Acc1) -> [{K, V} | Acc1] end, Acc, L1);
872f40a05fSChristopher Lenz            (Pair, Acc) ->
882f40a05fSChristopher Lenz                [Pair | Acc]
892f40a05fSChristopher Lenz        end,
9039de3072SChristopher Lenz    lists:reverse(lists:foldl(F, [], gb_trees:values(T))).
9139de3072SChristopher Lenz
9239de3072SChristopher Lenz%% @spec get_value(key(), headers()) -> string() | undefined
9339de3072SChristopher Lenz%% @doc Return the value of the given header using a case insensitive search.
9439de3072SChristopher Lenz%%      undefined will be returned for keys that are not present.
9539de3072SChristopher Lenzget_value(K, T) ->
9639de3072SChristopher Lenz    case lookup(K, T) of
972f40a05fSChristopher Lenz        {value, {_, V}} ->
982f40a05fSChristopher Lenz            expand(V);
992f40a05fSChristopher Lenz        none ->
1002f40a05fSChristopher Lenz            undefined
10139de3072SChristopher Lenz    end.
10239de3072SChristopher Lenz
10339de3072SChristopher Lenz%% @spec get_primary_value(key(), headers()) -> string() | undefined
10439de3072SChristopher Lenz%% @doc Return the value of the given header up to the first semicolon using
10539de3072SChristopher Lenz%%      a case insensitive search. undefined will be returned for keys
10639de3072SChristopher Lenz%%      that are not present.
10739de3072SChristopher Lenzget_primary_value(K, T) ->
10839de3072SChristopher Lenz    case get_value(K, T) of
10939de3072SChristopher Lenz        undefined ->
11039de3072SChristopher Lenz            undefined;
11139de3072SChristopher Lenz        V ->
11239de3072SChristopher Lenz            lists:takewhile(fun (C) -> C =/= $; end, V)
11339de3072SChristopher Lenz    end.
11439de3072SChristopher Lenz
115a104cbfcSFilipe David Borba Manana%% @spec get_combined_value(key(), headers()) -> string() | undefined
116a104cbfcSFilipe David Borba Manana%% @doc Return the value from the given header using a case insensitive search.
117a104cbfcSFilipe David Borba Manana%%      If the value of the header is a comma-separated list where holds values
118a104cbfcSFilipe David Borba Manana%%      are all identical, the identical value will be returned.
119a104cbfcSFilipe David Borba Manana%%      undefined will be returned for keys that are not present or the
120a104cbfcSFilipe David Borba Manana%%      values in the list are not the same.
121a104cbfcSFilipe David Borba Manana%%
122a104cbfcSFilipe David Borba Manana%%      NOTE: The process isn't designed for a general purpose. If you need
123a104cbfcSFilipe David Borba Manana%%            to access all values in the combined header, please refer to
124a104cbfcSFilipe David Borba Manana%%            '''tokenize_header_value/1'''.
125a104cbfcSFilipe David Borba Manana%%
126a104cbfcSFilipe David Borba Manana%%      Section 4.2 of the RFC 2616 (HTTP 1.1) describes multiple message-header
127a104cbfcSFilipe David Borba Manana%%      fields with the same field-name may be present in a message if and only
128a104cbfcSFilipe David Borba Manana%%      if the entire field-value for that header field is defined as a
129a104cbfcSFilipe David Borba Manana%%      comma-separated list [i.e., #(values)].
130a104cbfcSFilipe David Borba Mananaget_combined_value(K, T) ->
131a104cbfcSFilipe David Borba Manana    case get_value(K, T) of
132a104cbfcSFilipe David Borba Manana        undefined ->
133a104cbfcSFilipe David Borba Manana            undefined;
134a104cbfcSFilipe David Borba Manana        V ->
135a104cbfcSFilipe David Borba Manana            case sets:to_list(sets:from_list(tokenize_header_value(V))) of
136a104cbfcSFilipe David Borba Manana                [Val] ->
137a104cbfcSFilipe David Borba Manana                    Val;
138a104cbfcSFilipe David Borba Manana                _ ->
139a104cbfcSFilipe David Borba Manana                    undefined
140a104cbfcSFilipe David Borba Manana            end
141a104cbfcSFilipe David Borba Manana    end.
142a104cbfcSFilipe David Borba Manana
14339de3072SChristopher Lenz%% @spec lookup(key(), headers()) -> {value, {key(), string()}} | none
14439de3072SChristopher Lenz%% @doc Return the case preserved key and value for the given header using
14539de3072SChristopher Lenz%%      a case insensitive search. none will be returned for keys that are
14639de3072SChristopher Lenz%%      not present.
14739de3072SChristopher Lenzlookup(K, T) ->
14839de3072SChristopher Lenz    case gb_trees:lookup(normalize(K), T) of
1492f40a05fSChristopher Lenz        {value, {K0, V}} ->
1502f40a05fSChristopher Lenz            {value, {K0, expand(V)}};
1512f40a05fSChristopher Lenz        none ->
1522f40a05fSChristopher Lenz            none
15339de3072SChristopher Lenz    end.
15439de3072SChristopher Lenz
15539de3072SChristopher Lenz%% @spec default(key(), value(), headers()) -> headers()
15639de3072SChristopher Lenz%% @doc Insert the pair into the headers if it does not already exist.
15739de3072SChristopher Lenzdefault(K, V, T) ->
15839de3072SChristopher Lenz    K1 = normalize(K),
15939de3072SChristopher Lenz    V1 = any_to_list(V),
16039de3072SChristopher Lenz    try gb_trees:insert(K1, {K, V1}, T)
16139de3072SChristopher Lenz    catch
1622f40a05fSChristopher Lenz        error:{key_exists, _} ->
1632f40a05fSChristopher Lenz            T
16439de3072SChristopher Lenz    end.
16539de3072SChristopher Lenz
16639de3072SChristopher Lenz%% @spec enter(key(), value(), headers()) -> headers()
16739de3072SChristopher Lenz%% @doc Insert the pair into the headers, replacing any pre-existing key.
16839de3072SChristopher Lenzenter(K, V, T) ->
16939de3072SChristopher Lenz    K1 = normalize(K),
17039de3072SChristopher Lenz    V1 = any_to_list(V),
17139de3072SChristopher Lenz    gb_trees:enter(K1, {K, V1}, T).
17239de3072SChristopher Lenz
17339de3072SChristopher Lenz%% @spec insert(key(), value(), headers()) -> headers()
17439de3072SChristopher Lenz%% @doc Insert the pair into the headers, merging with any pre-existing key.
17539de3072SChristopher Lenz%%      A merge is done with Value = V0 ++ ", " ++ V1.
17639de3072SChristopher Lenzinsert(K, V, T) ->
17739de3072SChristopher Lenz    K1 = normalize(K),
17839de3072SChristopher Lenz    V1 = any_to_list(V),
17939de3072SChristopher Lenz    try gb_trees:insert(K1, {K, V1}, T)
18039de3072SChristopher Lenz    catch
1812f40a05fSChristopher Lenz        error:{key_exists, _} ->
1822f40a05fSChristopher Lenz            {K0, V0} = gb_trees:get(K1, T),
1832f40a05fSChristopher Lenz            V2 = merge(K1, V1, V0),
1842f40a05fSChristopher Lenz            gb_trees:update(K1, {K0, V2}, T)
18539de3072SChristopher Lenz    end.
18639de3072SChristopher Lenz
1871ba7a12cSChristopher Lenz%% @spec delete_any(key(), headers()) -> headers()
1881ba7a12cSChristopher Lenz%% @doc Delete the header corresponding to key if it is present.
1891ba7a12cSChristopher Lenzdelete_any(K, T) ->
1901ba7a12cSChristopher Lenz    K1 = normalize(K),
1911ba7a12cSChristopher Lenz    gb_trees:delete_any(K1, T).
1921ba7a12cSChristopher Lenz
19339de3072SChristopher Lenz%% Internal API
19439de3072SChristopher Lenz
195a104cbfcSFilipe David Borba Mananatokenize_header_value(undefined) ->
196a104cbfcSFilipe David Borba Manana    undefined;
197a104cbfcSFilipe David Borba Mananatokenize_header_value(V) ->
198a104cbfcSFilipe David Borba Manana    reversed_tokens(trim_and_reverse(V, false), [], []).
199a104cbfcSFilipe David Borba Manana
200a104cbfcSFilipe David Borba Mananatrim_and_reverse([S | Rest], Reversed) when S=:=$ ; S=:=$\n; S=:=$\t ->
201a104cbfcSFilipe David Borba Manana    trim_and_reverse(Rest, Reversed);
202a104cbfcSFilipe David Borba Mananatrim_and_reverse(V, false) ->
203a104cbfcSFilipe David Borba Manana    trim_and_reverse(lists:reverse(V), true);
204a104cbfcSFilipe David Borba Mananatrim_and_reverse(V, true) ->
205a104cbfcSFilipe David Borba Manana    V.
206a104cbfcSFilipe David Borba Manana
207a104cbfcSFilipe David Borba Mananareversed_tokens([], [], Acc) ->
208a104cbfcSFilipe David Borba Manana    Acc;
209a104cbfcSFilipe David Borba Mananareversed_tokens([], Token, Acc) ->
210a104cbfcSFilipe David Borba Manana    [Token | Acc];
211a104cbfcSFilipe David Borba Mananareversed_tokens("\"" ++ Rest, [], Acc) ->
212a104cbfcSFilipe David Borba Manana    case extract_quoted_string(Rest, []) of
213a104cbfcSFilipe David Borba Manana        {String, NewRest} ->
214a104cbfcSFilipe David Borba Manana            reversed_tokens(NewRest, [], [String | Acc]);
215a104cbfcSFilipe David Borba Manana        undefined ->
216a104cbfcSFilipe David Borba Manana            undefined
217a104cbfcSFilipe David Borba Manana    end;
218a104cbfcSFilipe David Borba Mananareversed_tokens("\"" ++ _Rest, _Token, _Acc) ->
219a104cbfcSFilipe David Borba Manana    undefined;
220a104cbfcSFilipe David Borba Mananareversed_tokens([C | Rest], [], Acc) when C=:=$ ;C=:=$\n;C=:=$\t;C=:=$, ->
221a104cbfcSFilipe David Borba Manana    reversed_tokens(Rest, [], Acc);
222a104cbfcSFilipe David Borba Mananareversed_tokens([C | Rest], Token, Acc) when C=:=$ ;C=:=$\n;C=:=$\t;C=:=$, ->
223a104cbfcSFilipe David Borba Manana    reversed_tokens(Rest, [], [Token | Acc]);
224a104cbfcSFilipe David Borba Mananareversed_tokens([C | Rest], Token, Acc) ->
225a104cbfcSFilipe David Borba Manana    reversed_tokens(Rest, [C | Token], Acc);
226a104cbfcSFilipe David Borba Mananareversed_tokens(_, _, _) ->
227a104cbfcSFilipe David Borba Manana    undefeined.
228a104cbfcSFilipe David Borba Manana
229a104cbfcSFilipe David Borba Mananaextract_quoted_string([], _Acc) ->
230a104cbfcSFilipe David Borba Manana    undefined;
231a104cbfcSFilipe David Borba Mananaextract_quoted_string("\"\\" ++ Rest, Acc) ->
232a104cbfcSFilipe David Borba Manana    extract_quoted_string(Rest, "\"" ++ Acc);
233a104cbfcSFilipe David Borba Mananaextract_quoted_string("\"" ++ Rest, Acc) ->
234a104cbfcSFilipe David Borba Manana    {Acc, Rest};
235a104cbfcSFilipe David Borba Mananaextract_quoted_string([C | Rest], Acc) ->
236a104cbfcSFilipe David Borba Manana    extract_quoted_string(Rest, [C | Acc]).
237a104cbfcSFilipe David Borba Manana
23839de3072SChristopher Lenzexpand({array, L}) ->
23939de3072SChristopher Lenz    mochiweb_util:join(lists:reverse(L), ", ");
24039de3072SChristopher Lenzexpand(V) ->
24139de3072SChristopher Lenz    V.
24239de3072SChristopher Lenz
24339de3072SChristopher Lenzmerge("set-cookie", V1, {array, L}) ->
24439de3072SChristopher Lenz    {array, [V1 | L]};
24539de3072SChristopher Lenzmerge("set-cookie", V1, V0) ->
24639de3072SChristopher Lenz    {array, [V1, V0]};
24739de3072SChristopher Lenzmerge(_, V1, V0) ->
24839de3072SChristopher Lenz    V0 ++ ", " ++ V1.
24939de3072SChristopher Lenz
25039de3072SChristopher Lenznormalize(K) when is_list(K) ->
251678512dcSChristopher Lenz    string:to_lower(K);
25239de3072SChristopher Lenznormalize(K) when is_atom(K) ->
25339de3072SChristopher Lenz    normalize(atom_to_list(K));
25439de3072SChristopher Lenznormalize(K) when is_binary(K) ->
25539de3072SChristopher Lenz    normalize(binary_to_list(K)).
25639de3072SChristopher Lenz
25739de3072SChristopher Lenzany_to_list(V) when is_list(V) ->
25839de3072SChristopher Lenz    V;
25939de3072SChristopher Lenzany_to_list(V) when is_atom(V) ->
26039de3072SChristopher Lenz    atom_to_list(V);
26139de3072SChristopher Lenzany_to_list(V) when is_binary(V) ->
26239de3072SChristopher Lenz    binary_to_list(V);
26339de3072SChristopher Lenzany_to_list(V) when is_integer(V) ->
26439de3072SChristopher Lenz    integer_to_list(V).
26539de3072SChristopher Lenz
2664b0948ddSRobert Newson%%
2674b0948ddSRobert Newson%% Tests.
2684b0948ddSRobert Newson%%
2694b0948ddSRobert Newson-ifdef(TEST).
270a104cbfcSFilipe David Borba Manana-include_lib("eunit/include/eunit.hrl").
2714b0948ddSRobert Newson
2724b0948ddSRobert Newsonmake_test() ->
2734b0948ddSRobert Newson    Identity = make([{hdr, foo}]),
2744b0948ddSRobert Newson    ?assertEqual(
2754b0948ddSRobert Newson       Identity,
2764b0948ddSRobert Newson       make(Identity)).
2774b0948ddSRobert Newson
2784b0948ddSRobert Newsonenter_from_list_test() ->
2794b0948ddSRobert Newson    H = make([{hdr, foo}]),
2804b0948ddSRobert Newson    ?assertEqual(
2814b0948ddSRobert Newson       [{baz, "wibble"}, {hdr, "foo"}],
2824b0948ddSRobert Newson       to_list(enter_from_list([{baz, wibble}], H))),
2834b0948ddSRobert Newson    ?assertEqual(
2844b0948ddSRobert Newson       [{hdr, "bar"}],
2854b0948ddSRobert Newson       to_list(enter_from_list([{hdr, bar}], H))),
2864b0948ddSRobert Newson    ok.
2874b0948ddSRobert Newson
2884b0948ddSRobert Newsondefault_from_list_test() ->
2894b0948ddSRobert Newson    H = make([{hdr, foo}]),
2904b0948ddSRobert Newson    ?assertEqual(
2914b0948ddSRobert Newson       [{baz, "wibble"}, {hdr, "foo"}],
2924b0948ddSRobert Newson       to_list(default_from_list([{baz, wibble}], H))),
2934b0948ddSRobert Newson    ?assertEqual(
2944b0948ddSRobert Newson       [{hdr, "foo"}],
2954b0948ddSRobert Newson       to_list(default_from_list([{hdr, bar}], H))),
2964b0948ddSRobert Newson    ok.
2974b0948ddSRobert Newson
2984b0948ddSRobert Newsonget_primary_value_test() ->
2994b0948ddSRobert Newson    H = make([{hdr, foo}, {baz, <<"wibble;taco">>}]),
3004b0948ddSRobert Newson    ?assertEqual(
3014b0948ddSRobert Newson       "foo",
3024b0948ddSRobert Newson       get_primary_value(hdr, H)),
3034b0948ddSRobert Newson    ?assertEqual(
3044b0948ddSRobert Newson       undefined,
3054b0948ddSRobert Newson       get_primary_value(bar, H)),
3064b0948ddSRobert Newson    ?assertEqual(
3074b0948ddSRobert Newson       "wibble",
3084b0948ddSRobert Newson       get_primary_value(<<"baz">>, H)),
3094b0948ddSRobert Newson    ok.
3104b0948ddSRobert Newson
311a104cbfcSFilipe David Borba Mananaget_combined_value_test() ->
312a104cbfcSFilipe David Borba Manana    H = make([{hdr, foo}, {baz, <<"wibble,taco">>}, {content_length, "123, 123"},
313a104cbfcSFilipe David Borba Manana              {test, " 123,  123,     123  , 123,123 "},
314a104cbfcSFilipe David Borba Manana              {test2, "456,  123,     123  , 123"},
315a104cbfcSFilipe David Borba Manana              {test3, "123"}, {test4, " 123, "}]),
316a104cbfcSFilipe David Borba Manana    ?assertEqual(
317a104cbfcSFilipe David Borba Manana       "foo",
318a104cbfcSFilipe David Borba Manana       get_combined_value(hdr, H)),
319a104cbfcSFilipe David Borba Manana    ?assertEqual(
320a104cbfcSFilipe David Borba Manana       undefined,
321a104cbfcSFilipe David Borba Manana       get_combined_value(bar, H)),
322a104cbfcSFilipe David Borba Manana    ?assertEqual(
323a104cbfcSFilipe David Borba Manana       undefined,
324a104cbfcSFilipe David Borba Manana       get_combined_value(<<"baz">>, H)),
325a104cbfcSFilipe David Borba Manana    ?assertEqual(
326a104cbfcSFilipe David Borba Manana       "123",
327a104cbfcSFilipe David Borba Manana       get_combined_value(<<"content_length">>, H)),
328a104cbfcSFilipe David Borba Manana    ?assertEqual(
329a104cbfcSFilipe David Borba Manana       "123",
330a104cbfcSFilipe David Borba Manana       get_combined_value(<<"test">>, H)),
331a104cbfcSFilipe David Borba Manana    ?assertEqual(
332a104cbfcSFilipe David Borba Manana       undefined,
333a104cbfcSFilipe David Borba Manana       get_combined_value(<<"test2">>, H)),
334a104cbfcSFilipe David Borba Manana    ?assertEqual(
335a104cbfcSFilipe David Borba Manana       "123",
336a104cbfcSFilipe David Borba Manana       get_combined_value(<<"test3">>, H)),
337a104cbfcSFilipe David Borba Manana    ?assertEqual(
338a104cbfcSFilipe David Borba Manana       "123",
339a104cbfcSFilipe David Borba Manana       get_combined_value(<<"test4">>, H)),
340a104cbfcSFilipe David Borba Manana    ok.
341a104cbfcSFilipe David Borba Manana
3424b0948ddSRobert Newsonset_cookie_test() ->
3434b0948ddSRobert Newson    H = make([{"set-cookie", foo}, {"set-cookie", bar}, {"set-cookie", baz}]),
3444b0948ddSRobert Newson    ?assertEqual(
3454b0948ddSRobert Newson       [{"set-cookie", "foo"}, {"set-cookie", "bar"}, {"set-cookie", "baz"}],
3464b0948ddSRobert Newson       to_list(H)),
3474b0948ddSRobert Newson    ok.
3484b0948ddSRobert Newson
3494b0948ddSRobert Newsonheaders_test() ->
3504b0948ddSRobert Newson    H = ?MODULE:make([{hdr, foo}, {"Hdr", "bar"}, {'Hdr', 2}]),
3514b0948ddSRobert Newson    [{hdr, "foo, bar, 2"}] = ?MODULE:to_list(H),
3524b0948ddSRobert Newson    H1 = ?MODULE:insert(taco, grande, H),
3534b0948ddSRobert Newson    [{hdr, "foo, bar, 2"}, {taco, "grande"}] = ?MODULE:to_list(H1),
3544b0948ddSRobert Newson    H2 = ?MODULE:make([{"Set-Cookie", "foo"}]),
3554b0948ddSRobert Newson    [{"Set-Cookie", "foo"}] = ?MODULE:to_list(H2),
3564b0948ddSRobert Newson    H3 = ?MODULE:insert("Set-Cookie", "bar", H2),
3574b0948ddSRobert Newson    [{"Set-Cookie", "foo"}, {"Set-Cookie", "bar"}] = ?MODULE:to_list(H3),
3584b0948ddSRobert Newson    "foo, bar" = ?MODULE:get_value("set-cookie", H3),
3594b0948ddSRobert Newson    {value, {"Set-Cookie", "foo, bar"}} = ?MODULE:lookup("set-cookie", H3),
3604b0948ddSRobert Newson    undefined = ?MODULE:get_value("shibby", H3),
3614b0948ddSRobert Newson    none = ?MODULE:lookup("shibby", H3),
3624b0948ddSRobert Newson    H4 = ?MODULE:insert("content-type",
3634b0948ddSRobert Newson                        "application/x-www-form-urlencoded; charset=utf8",
3644b0948ddSRobert Newson                        H3),
3654b0948ddSRobert Newson    "application/x-www-form-urlencoded" = ?MODULE:get_primary_value(
3664b0948ddSRobert Newson                                             "content-type", H4),
3674b0948ddSRobert Newson    H4 = ?MODULE:delete_any("nonexistent-header", H4),
3684b0948ddSRobert Newson    H3 = ?MODULE:delete_any("content-type", H4),
3694b0948ddSRobert Newson    HB = <<"Content-Length: 47\r\nContent-Type: text/plain\r\n\r\n">>,
3704b0948ddSRobert Newson    H_HB = ?MODULE:from_binary(HB),
3714b0948ddSRobert Newson    H_HB = ?MODULE:from_binary(binary_to_list(HB)),
3724b0948ddSRobert Newson    "47" = ?MODULE:get_value("Content-Length", H_HB),
3734b0948ddSRobert Newson    "text/plain" = ?MODULE:get_value("Content-Type", H_HB),
3744b0948ddSRobert Newson    L_H_HB = ?MODULE:to_list(H_HB),
3754b0948ddSRobert Newson    2 = length(L_H_HB),
3764b0948ddSRobert Newson    true = lists:member({'Content-Length', "47"}, L_H_HB),
3774b0948ddSRobert Newson    true = lists:member({'Content-Type', "text/plain"}, L_H_HB),
3784b0948ddSRobert Newson    HL = [ <<"Content-Length: 47\r\n">>, <<"Content-Type: text/plain\r\n">> ],
3794b0948ddSRobert Newson    HL2 = [ "Content-Length: 47\r\n", <<"Content-Type: text/plain\r\n">> ],
3804b0948ddSRobert Newson    HL3 = [ <<"Content-Length: 47\r\n">>, "Content-Type: text/plain\r\n" ],
3814b0948ddSRobert Newson    H_HL = ?MODULE:from_binary(HL),
3824b0948ddSRobert Newson    H_HL = ?MODULE:from_binary(HL2),
3834b0948ddSRobert Newson    H_HL = ?MODULE:from_binary(HL3),
3844b0948ddSRobert Newson    "47" = ?MODULE:get_value("Content-Length", H_HL),
3854b0948ddSRobert Newson    "text/plain" = ?MODULE:get_value("Content-Type", H_HL),
3864b0948ddSRobert Newson    L_H_HL = ?MODULE:to_list(H_HL),
3874b0948ddSRobert Newson    2 = length(L_H_HL),
3884b0948ddSRobert Newson    true = lists:member({'Content-Length', "47"}, L_H_HL),
3894b0948ddSRobert Newson    true = lists:member({'Content-Type', "text/plain"}, L_H_HL),
3904b0948ddSRobert Newson    [] = ?MODULE:to_list(?MODULE:from_binary(<<>>)),
3914b0948ddSRobert Newson    [] = ?MODULE:to_list(?MODULE:from_binary(<<"">>)),
3924b0948ddSRobert Newson    [] = ?MODULE:to_list(?MODULE:from_binary(<<"\r\n">>)),
3934b0948ddSRobert Newson    [] = ?MODULE:to_list(?MODULE:from_binary(<<"\r\n\r\n">>)),
3944b0948ddSRobert Newson    [] = ?MODULE:to_list(?MODULE:from_binary("")),
3954b0948ddSRobert Newson    [] = ?MODULE:to_list(?MODULE:from_binary([<<>>])),
3964b0948ddSRobert Newson    [] = ?MODULE:to_list(?MODULE:from_binary([<<"">>])),
3974b0948ddSRobert Newson    [] = ?MODULE:to_list(?MODULE:from_binary([<<"\r\n">>])),
3984b0948ddSRobert Newson    [] = ?MODULE:to_list(?MODULE:from_binary([<<"\r\n\r\n">>])),
3994b0948ddSRobert Newson    ok.
40039de3072SChristopher Lenz
401a104cbfcSFilipe David Borba Mananatokenize_header_value_test() ->
402a104cbfcSFilipe David Borba Manana    ?assertEqual(["a quote in a \"quote\"."],
403a104cbfcSFilipe David Borba Manana                 tokenize_header_value("\"a quote in a \\\"quote\\\".\"")),
404a104cbfcSFilipe David Borba Manana    ?assertEqual(["abc"], tokenize_header_value("abc")),
405a104cbfcSFilipe David Borba Manana    ?assertEqual(["abc", "def"], tokenize_header_value("abc def")),
406a104cbfcSFilipe David Borba Manana    ?assertEqual(["abc", "def"], tokenize_header_value("abc , def")),
407a104cbfcSFilipe David Borba Manana    ?assertEqual(["abc", "def"], tokenize_header_value(",abc ,, def,,")),
408a104cbfcSFilipe David Borba Manana    ?assertEqual(["abc def"], tokenize_header_value("\"abc def\"      ")),
409a104cbfcSFilipe David Borba Manana    ?assertEqual(["abc, def"], tokenize_header_value("\"abc, def\"")),
410a104cbfcSFilipe David Borba Manana    ?assertEqual(["\\a\\$"], tokenize_header_value("\"\\a\\$\"")),
411a104cbfcSFilipe David Borba Manana    ?assertEqual(["abc def", "foo, bar", "12345", ""],
412a104cbfcSFilipe David Borba Manana                 tokenize_header_value("\"abc def\" \"foo, bar\" , 12345, \"\"")),
413a104cbfcSFilipe David Borba Manana    ?assertEqual(undefined,
414a104cbfcSFilipe David Borba Manana                 tokenize_header_value(undefined)),
415a104cbfcSFilipe David Borba Manana    ?assertEqual(undefined,
416a104cbfcSFilipe David Borba Manana                 tokenize_header_value("umatched quote\"")),
417a104cbfcSFilipe David Borba Manana    ?assertEqual(undefined,
418a104cbfcSFilipe David Borba Manana                 tokenize_header_value("\"unmatched quote")).
419a104cbfcSFilipe David Borba Manana
4204b0948ddSRobert Newson-endif.