/deps/mochiweb/src/mochiweb_headers.erl

http://github.com/zotonic/zotonic · Erlang · 299 lines · 203 code · 31 blank · 65 comment · 0 complexity · ba6ab9847df5a99468a7e89561de003d MD5 · raw file

  1. %% @author Bob Ippolito <bob@mochimedia.com>
  2. %% @copyright 2007 Mochi Media, Inc.
  3. %% @doc Case preserving (but case insensitive) HTTP Header dictionary.
  4. -module(mochiweb_headers).
  5. -author('bob@mochimedia.com').
  6. -export([empty/0, from_list/1, insert/3, enter/3, get_value/2, lookup/2]).
  7. -export([delete_any/2, get_primary_value/2]).
  8. -export([default/3, enter_from_list/2, default_from_list/2]).
  9. -export([to_list/1, make/1]).
  10. -export([from_binary/1]).
  11. %% @type headers().
  12. %% @type key() = atom() | binary() | string().
  13. %% @type value() = atom() | binary() | string() | integer().
  14. %% @spec empty() -> headers()
  15. %% @doc Create an empty headers structure.
  16. empty() ->
  17. gb_trees:empty().
  18. %% @spec make(headers() | [{key(), value()}]) -> headers()
  19. %% @doc Construct a headers() from the given list.
  20. make(L) when is_list(L) ->
  21. from_list(L);
  22. %% assume a non-list is already mochiweb_headers.
  23. make(T) ->
  24. T.
  25. %% @spec from_binary(iolist()) -> headers()
  26. %% @doc Transforms a raw HTTP header into a mochiweb headers structure.
  27. %%
  28. %% The given raw HTTP header can be one of the following:
  29. %%
  30. %% 1) A string or a binary representing a full HTTP header ending with
  31. %% double CRLF.
  32. %% Examples:
  33. %% ```
  34. %% "Content-Length: 47\r\nContent-Type: text/plain\r\n\r\n"
  35. %% <<"Content-Length: 47\r\nContent-Type: text/plain\r\n\r\n">>'''
  36. %%
  37. %% 2) A list of binaries or strings where each element represents a raw
  38. %% HTTP header line ending with a single CRLF.
  39. %% Examples:
  40. %% ```
  41. %% [<<"Content-Length: 47\r\n">>, <<"Content-Type: text/plain\r\n">>]
  42. %% ["Content-Length: 47\r\n", "Content-Type: text/plain\r\n"]
  43. %% ["Content-Length: 47\r\n", <<"Content-Type: text/plain\r\n">>]'''
  44. %%
  45. from_binary(RawHttpHeader) when is_binary(RawHttpHeader) ->
  46. from_binary(RawHttpHeader, []);
  47. from_binary(RawHttpHeaderList) ->
  48. from_binary(list_to_binary([RawHttpHeaderList, "\r\n"])).
  49. from_binary(RawHttpHeader, Acc) ->
  50. case erlang:decode_packet(httph, RawHttpHeader, []) of
  51. {ok, {http_header, _, H, _, V}, Rest} ->
  52. from_binary(Rest, [{H, V} | Acc]);
  53. _ ->
  54. make(Acc)
  55. end.
  56. %% @spec from_list([{key(), value()}]) -> headers()
  57. %% @doc Construct a headers() from the given list.
  58. from_list(List) ->
  59. lists:foldl(fun ({K, V}, T) -> insert(K, V, T) end, empty(), List).
  60. %% @spec enter_from_list([{key(), value()}], headers()) -> headers()
  61. %% @doc Insert pairs into the headers, replace any values for existing keys.
  62. enter_from_list(List, T) ->
  63. lists:foldl(fun ({K, V}, T1) -> enter(K, V, T1) end, T, List).
  64. %% @spec default_from_list([{key(), value()}], headers()) -> headers()
  65. %% @doc Insert pairs into the headers for keys that do not already exist.
  66. default_from_list(List, T) ->
  67. lists:foldl(fun ({K, V}, T1) -> default(K, V, T1) end, T, List).
  68. %% @spec to_list(headers()) -> [{key(), string()}]
  69. %% @doc Return the contents of the headers. The keys will be the exact key
  70. %% that was first inserted (e.g. may be an atom or binary, case is
  71. %% preserved).
  72. to_list(T) ->
  73. F = fun ({K, {array, L}}, Acc) ->
  74. L1 = lists:reverse(L),
  75. lists:foldl(fun (V, Acc1) -> [{K, V} | Acc1] end, Acc, L1);
  76. (Pair, Acc) ->
  77. [Pair | Acc]
  78. end,
  79. lists:reverse(lists:foldl(F, [], gb_trees:values(T))).
  80. %% @spec get_value(key(), headers()) -> string() | undefined
  81. %% @doc Return the value of the given header using a case insensitive search.
  82. %% undefined will be returned for keys that are not present.
  83. get_value(K, T) ->
  84. case lookup(K, T) of
  85. {value, {_, V}} ->
  86. expand(V);
  87. none ->
  88. undefined
  89. end.
  90. %% @spec get_primary_value(key(), headers()) -> string() | undefined
  91. %% @doc Return the value of the given header up to the first semicolon using
  92. %% a case insensitive search. undefined will be returned for keys
  93. %% that are not present.
  94. get_primary_value(K, T) ->
  95. case get_value(K, T) of
  96. undefined ->
  97. undefined;
  98. V ->
  99. lists:takewhile(fun (C) -> C =/= $; end, V)
  100. end.
  101. %% @spec lookup(key(), headers()) -> {value, {key(), string()}} | none
  102. %% @doc Return the case preserved key and value for the given header using
  103. %% a case insensitive search. none will be returned for keys that are
  104. %% not present.
  105. lookup(K, T) ->
  106. case gb_trees:lookup(normalize(K), T) of
  107. {value, {K0, V}} ->
  108. {value, {K0, expand(V)}};
  109. none ->
  110. none
  111. end.
  112. %% @spec default(key(), value(), headers()) -> headers()
  113. %% @doc Insert the pair into the headers if it does not already exist.
  114. default(K, V, T) ->
  115. K1 = normalize(K),
  116. V1 = any_to_list(V),
  117. try gb_trees:insert(K1, {K, V1}, T)
  118. catch
  119. error:{key_exists, _} ->
  120. T
  121. end.
  122. %% @spec enter(key(), value(), headers()) -> headers()
  123. %% @doc Insert the pair into the headers, replacing any pre-existing key.
  124. enter(K, V, T) ->
  125. K1 = normalize(K),
  126. V1 = any_to_list(V),
  127. gb_trees:enter(K1, {K, V1}, T).
  128. %% @spec insert(key(), value(), headers()) -> headers()
  129. %% @doc Insert the pair into the headers, merging with any pre-existing key.
  130. %% A merge is done with Value = V0 ++ ", " ++ V1.
  131. insert(K, V, T) ->
  132. K1 = normalize(K),
  133. V1 = any_to_list(V),
  134. try gb_trees:insert(K1, {K, V1}, T)
  135. catch
  136. error:{key_exists, _} ->
  137. {K0, V0} = gb_trees:get(K1, T),
  138. V2 = merge(K1, V1, V0),
  139. gb_trees:update(K1, {K0, V2}, T)
  140. end.
  141. %% @spec delete_any(key(), headers()) -> headers()
  142. %% @doc Delete the header corresponding to key if it is present.
  143. delete_any(K, T) ->
  144. K1 = normalize(K),
  145. gb_trees:delete_any(K1, T).
  146. %% Internal API
  147. expand({array, L}) ->
  148. mochiweb_util:join(lists:reverse(L), ", ");
  149. expand(V) ->
  150. V.
  151. merge("set-cookie", V1, {array, L}) ->
  152. {array, [V1 | L]};
  153. merge("set-cookie", V1, V0) ->
  154. {array, [V1, V0]};
  155. merge(_, V1, V0) ->
  156. V0 ++ ", " ++ V1.
  157. normalize(K) when is_list(K) ->
  158. string:to_lower(K);
  159. normalize(K) when is_atom(K) ->
  160. normalize(atom_to_list(K));
  161. normalize(K) when is_binary(K) ->
  162. normalize(binary_to_list(K)).
  163. any_to_list(V) when is_list(V) ->
  164. V;
  165. any_to_list(V) when is_atom(V) ->
  166. atom_to_list(V);
  167. any_to_list(V) when is_binary(V) ->
  168. binary_to_list(V);
  169. any_to_list(V) when is_integer(V) ->
  170. integer_to_list(V).
  171. %%
  172. %% Tests.
  173. %%
  174. -ifdef(TEST).
  175. -include_lib("eunit/include/eunit.hrl").
  176. make_test() ->
  177. Identity = make([{hdr, foo}]),
  178. ?assertEqual(
  179. Identity,
  180. make(Identity)).
  181. enter_from_list_test() ->
  182. H = make([{hdr, foo}]),
  183. ?assertEqual(
  184. [{baz, "wibble"}, {hdr, "foo"}],
  185. to_list(enter_from_list([{baz, wibble}], H))),
  186. ?assertEqual(
  187. [{hdr, "bar"}],
  188. to_list(enter_from_list([{hdr, bar}], H))),
  189. ok.
  190. default_from_list_test() ->
  191. H = make([{hdr, foo}]),
  192. ?assertEqual(
  193. [{baz, "wibble"}, {hdr, "foo"}],
  194. to_list(default_from_list([{baz, wibble}], H))),
  195. ?assertEqual(
  196. [{hdr, "foo"}],
  197. to_list(default_from_list([{hdr, bar}], H))),
  198. ok.
  199. get_primary_value_test() ->
  200. H = make([{hdr, foo}, {baz, <<"wibble;taco">>}]),
  201. ?assertEqual(
  202. "foo",
  203. get_primary_value(hdr, H)),
  204. ?assertEqual(
  205. undefined,
  206. get_primary_value(bar, H)),
  207. ?assertEqual(
  208. "wibble",
  209. get_primary_value(<<"baz">>, H)),
  210. ok.
  211. set_cookie_test() ->
  212. H = make([{"set-cookie", foo}, {"set-cookie", bar}, {"set-cookie", baz}]),
  213. ?assertEqual(
  214. [{"set-cookie", "foo"}, {"set-cookie", "bar"}, {"set-cookie", "baz"}],
  215. to_list(H)),
  216. ok.
  217. headers_test() ->
  218. H = ?MODULE:make([{hdr, foo}, {"Hdr", "bar"}, {'Hdr', 2}]),
  219. [{hdr, "foo, bar, 2"}] = ?MODULE:to_list(H),
  220. H1 = ?MODULE:insert(taco, grande, H),
  221. [{hdr, "foo, bar, 2"}, {taco, "grande"}] = ?MODULE:to_list(H1),
  222. H2 = ?MODULE:make([{"Set-Cookie", "foo"}]),
  223. [{"Set-Cookie", "foo"}] = ?MODULE:to_list(H2),
  224. H3 = ?MODULE:insert("Set-Cookie", "bar", H2),
  225. [{"Set-Cookie", "foo"}, {"Set-Cookie", "bar"}] = ?MODULE:to_list(H3),
  226. "foo, bar" = ?MODULE:get_value("set-cookie", H3),
  227. {value, {"Set-Cookie", "foo, bar"}} = ?MODULE:lookup("set-cookie", H3),
  228. undefined = ?MODULE:get_value("shibby", H3),
  229. none = ?MODULE:lookup("shibby", H3),
  230. H4 = ?MODULE:insert("content-type",
  231. "application/x-www-form-urlencoded; charset=utf8",
  232. H3),
  233. "application/x-www-form-urlencoded" = ?MODULE:get_primary_value(
  234. "content-type", H4),
  235. H4 = ?MODULE:delete_any("nonexistent-header", H4),
  236. H3 = ?MODULE:delete_any("content-type", H4),
  237. HB = <<"Content-Length: 47\r\nContent-Type: text/plain\r\n\r\n">>,
  238. H_HB = ?MODULE:from_binary(HB),
  239. H_HB = ?MODULE:from_binary(binary_to_list(HB)),
  240. "47" = ?MODULE:get_value("Content-Length", H_HB),
  241. "text/plain" = ?MODULE:get_value("Content-Type", H_HB),
  242. L_H_HB = ?MODULE:to_list(H_HB),
  243. 2 = length(L_H_HB),
  244. true = lists:member({'Content-Length', "47"}, L_H_HB),
  245. true = lists:member({'Content-Type', "text/plain"}, L_H_HB),
  246. HL = [ <<"Content-Length: 47\r\n">>, <<"Content-Type: text/plain\r\n">> ],
  247. HL2 = [ "Content-Length: 47\r\n", <<"Content-Type: text/plain\r\n">> ],
  248. HL3 = [ <<"Content-Length: 47\r\n">>, "Content-Type: text/plain\r\n" ],
  249. H_HL = ?MODULE:from_binary(HL),
  250. H_HL = ?MODULE:from_binary(HL2),
  251. H_HL = ?MODULE:from_binary(HL3),
  252. "47" = ?MODULE:get_value("Content-Length", H_HL),
  253. "text/plain" = ?MODULE:get_value("Content-Type", H_HL),
  254. L_H_HL = ?MODULE:to_list(H_HL),
  255. 2 = length(L_H_HL),
  256. true = lists:member({'Content-Length', "47"}, L_H_HL),
  257. true = lists:member({'Content-Type', "text/plain"}, L_H_HL),
  258. [] = ?MODULE:to_list(?MODULE:from_binary(<<>>)),
  259. [] = ?MODULE:to_list(?MODULE:from_binary(<<"">>)),
  260. [] = ?MODULE:to_list(?MODULE:from_binary(<<"\r\n">>)),
  261. [] = ?MODULE:to_list(?MODULE:from_binary(<<"\r\n\r\n">>)),
  262. [] = ?MODULE:to_list(?MODULE:from_binary("")),
  263. [] = ?MODULE:to_list(?MODULE:from_binary([<<>>])),
  264. [] = ?MODULE:to_list(?MODULE:from_binary([<<"">>])),
  265. [] = ?MODULE:to_list(?MODULE:from_binary([<<"\r\n">>])),
  266. [] = ?MODULE:to_list(?MODULE:from_binary([<<"\r\n\r\n">>])),
  267. ok.
  268. -endif.