cowboy_http_protocol.erl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
  2. %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. %% @doc HTTP protocol handler.
  16. %%
  17. %% The available options are:
  18. %% <dl>
  19. %% <dt>dispatch</dt><dd>The dispatch list for this protocol.</dd>
  20. %% <dt>max_empty_lines</dt><dd>Max number of empty lines before a request.
  21. %% Defaults to 5.</dd>
  22. %% <dt>timeout</dt><dd>Time in milliseconds before an idle
  23. %% connection is closed. Defaults to 5000 milliseconds.</dd>
  24. %% <dt>urldecode</dt><dd>Function and options argument to use when decoding
  25. %% URL encoded strings. Defaults to `{fun cowboy_http:urldecode/2, crash}'.
  26. %% </dd>
  27. %% </dl>
  28. %%
  29. %% Note that there is no need to monitor these processes when using Cowboy as
  30. %% an application as it already supervises them under the listener supervisor.
  31. %%
  32. %% @see cowboy_dispatcher
  33. %% @see cowboy_http_handler
  34. -module(cowboy_http_protocol).
  35. -behaviour(cowboy_protocol).
  36. -export([start_link/4]). %% API.
  37. -export([init/4, parse_request/1, handler_loop/3]). %% FSM.
  38. -include("include/http.hrl").
  39. -include_lib("eunit/include/eunit.hrl").
  40. -record(state, {
  41. listener :: pid(),
  42. socket :: inet:socket(),
  43. transport :: module(),
  44. dispatch :: cowboy_dispatcher:dispatch_rules(),
  45. handler :: {module(), any()},
  46. urldecode :: {fun((binary(), T) -> binary()), T},
  47. req_empty_lines = 0 :: integer(),
  48. max_empty_lines :: integer(),
  49. max_line_length :: integer(),
  50. timeout :: timeout(),
  51. buffer = <<>> :: binary(),
  52. hibernate = false :: boolean(),
  53. loop_timeout = infinity :: timeout(),
  54. loop_timeout_ref :: undefined | reference()
  55. }).
  56. %% API.
  57. %% @doc Start an HTTP protocol process.
  58. -spec start_link(pid(), inet:socket(), module(), any()) -> {ok, pid()}.
  59. start_link(ListenerPid, Socket, Transport, Opts) ->
  60. Pid = spawn_link(?MODULE, init, [ListenerPid, Socket, Transport, Opts]),
  61. {ok, Pid}.
  62. %% FSM.
  63. %% @private
  64. -spec init(pid(), inet:socket(), module(), any()) -> ok | none().
  65. init(ListenerPid, Socket, Transport, Opts) ->
  66. Dispatch = proplists:get_value(dispatch, Opts, []),
  67. MaxEmptyLines = proplists:get_value(max_empty_lines, Opts, 5),
  68. MaxLineLength = proplists:get_value(max_line_length, Opts, 4096),
  69. Timeout = proplists:get_value(timeout, Opts, 5000),
  70. URLDecDefault = {fun cowboy_http:urldecode/2, crash},
  71. URLDec = proplists:get_value(urldecode, Opts, URLDecDefault),
  72. receive shoot -> ok end,
  73. wait_request(#state{listener=ListenerPid, socket=Socket, transport=Transport,
  74. dispatch=Dispatch, max_empty_lines=MaxEmptyLines,
  75. max_line_length=MaxLineLength, timeout=Timeout, urldecode=URLDec}).
  76. %% @private
  77. -spec parse_request(#state{}) -> ok | none().
  78. %% We limit the length of the Request-line to MaxLength to avoid endlessly
  79. %% reading from the socket and eventually crashing.
  80. parse_request(State=#state{buffer=Buffer, max_line_length=MaxLength}) ->
  81. case erlang:decode_packet(http_bin, Buffer, []) of
  82. {ok, Request, Rest} -> request(Request, State#state{buffer=Rest});
  83. {more, _Length} when byte_size(Buffer) > MaxLength ->
  84. error_terminate(413, State);
  85. {more, _Length} -> wait_request(State);
  86. {error, _Reason} -> error_terminate(400, State)
  87. end.
  88. -spec wait_request(#state{}) -> ok | none().
  89. wait_request(State=#state{socket=Socket, transport=Transport,
  90. timeout=T, buffer=Buffer}) ->
  91. case Transport:recv(Socket, 0, T) of
  92. {ok, Data} -> parse_request(State#state{
  93. buffer= << Buffer/binary, Data/binary >>});
  94. {error, _Reason} -> terminate(State)
  95. end.
  96. -spec request({http_request, http_method(), http_uri(),
  97. http_version()}, #state{}) -> ok | none().
  98. %% @todo We probably want to handle some things differently between versions.
  99. request({http_request, _Method, _URI, Version}, State)
  100. when Version =/= {1, 0}, Version =/= {1, 1} ->
  101. error_terminate(505, State);
  102. %% @todo We need to cleanup the URI properly.
  103. request({http_request, Method, {abs_path, AbsPath}, Version},
  104. State=#state{socket=Socket, transport=Transport,
  105. urldecode={URLDecFun, URLDecArg}=URLDec}) ->
  106. URLDecode = fun(Bin) -> URLDecFun(Bin, URLDecArg) end,
  107. {Path, RawPath, Qs} = cowboy_dispatcher:split_path(AbsPath, URLDecode),
  108. ConnAtom = version_to_connection(Version),
  109. parse_header(#http_req{socket=Socket, transport=Transport,
  110. connection=ConnAtom, method=Method, version=Version,
  111. path=Path, raw_path=RawPath, raw_qs=Qs, urldecode=URLDec}, State);
  112. request({http_request, Method, '*', Version},
  113. State=#state{socket=Socket, transport=Transport, urldecode=URLDec}) ->
  114. ConnAtom = version_to_connection(Version),
  115. parse_header(#http_req{socket=Socket, transport=Transport,
  116. connection=ConnAtom, method=Method, version=Version,
  117. path='*', raw_path= <<"*">>, raw_qs= <<>>, urldecode=URLDec}, State);
  118. request({http_request, _Method, _URI, _Version}, State) ->
  119. error_terminate(501, State);
  120. request({http_error, <<"\r\n">>},
  121. State=#state{req_empty_lines=N, max_empty_lines=N}) ->
  122. error_terminate(400, State);
  123. request({http_error, <<"\r\n">>}, State=#state{req_empty_lines=N}) ->
  124. parse_request(State#state{req_empty_lines=N + 1});
  125. request(_Any, State) ->
  126. error_terminate(400, State).
  127. -spec parse_header(#http_req{}, #state{}) -> ok | none().
  128. parse_header(Req, State=#state{buffer=Buffer, max_line_length=MaxLength}) ->
  129. case erlang:decode_packet(httph_bin, Buffer, []) of
  130. {ok, Header, Rest} -> header(Header, Req, State#state{buffer=Rest});
  131. {more, _Length} when byte_size(Buffer) > MaxLength ->
  132. error_terminate(413, State);
  133. {more, _Length} -> wait_header(Req, State);
  134. {error, _Reason} -> error_terminate(400, State)
  135. end.
  136. -spec wait_header(#http_req{}, #state{}) -> ok | none().
  137. wait_header(Req, State=#state{socket=Socket,
  138. transport=Transport, timeout=T, buffer=Buffer}) ->
  139. case Transport:recv(Socket, 0, T) of
  140. {ok, Data} -> parse_header(Req, State#state{
  141. buffer= << Buffer/binary, Data/binary >>});
  142. {error, timeout} -> error_terminate(408, State);
  143. {error, closed} -> terminate(State)
  144. end.
  145. -spec header({http_header, integer(), http_header(), any(), binary()}
  146. | http_eoh, #http_req{}, #state{}) -> ok | none().
  147. header({http_header, _I, 'Host', _R, RawHost}, Req=#http_req{
  148. transport=Transport, host=undefined}, State) ->
  149. RawHost2 = cowboy_bstr:to_lower(RawHost),
  150. case catch cowboy_dispatcher:split_host(RawHost2) of
  151. {Host, RawHost3, undefined} ->
  152. Port = default_port(Transport:name()),
  153. dispatch(fun parse_header/2, Req#http_req{
  154. host=Host, raw_host=RawHost3, port=Port,
  155. headers=[{'Host', RawHost3}|Req#http_req.headers]}, State);
  156. {Host, RawHost3, Port} ->
  157. dispatch(fun parse_header/2, Req#http_req{
  158. host=Host, raw_host=RawHost3, port=Port,
  159. headers=[{'Host', RawHost3}|Req#http_req.headers]}, State);
  160. {'EXIT', _Reason} ->
  161. error_terminate(400, State)
  162. end;
  163. %% Ignore Host headers if we already have it.
  164. header({http_header, _I, 'Host', _R, _V}, Req, State) ->
  165. parse_header(Req, State);
  166. header({http_header, _I, 'Connection', _R, Connection},
  167. Req=#http_req{headers=Headers}, State) ->
  168. Req2 = Req#http_req{headers=[{'Connection', Connection}|Headers]},
  169. {ConnTokens, Req3}
  170. = cowboy_http_req:parse_header('Connection', Req2),
  171. ConnAtom = cowboy_http:connection_to_atom(ConnTokens),
  172. parse_header(Req3#http_req{connection=ConnAtom}, State);
  173. header({http_header, _I, Field, _R, Value}, Req, State) ->
  174. Field2 = format_header(Field),
  175. parse_header(Req#http_req{headers=[{Field2, Value}|Req#http_req.headers]},
  176. State);
  177. %% The Host header is required in HTTP/1.1.
  178. header(http_eoh, #http_req{version={1, 1}, host=undefined}, State) ->
  179. error_terminate(400, State);
  180. %% It is however optional in HTTP/1.0.
  181. header(http_eoh, Req=#http_req{version={1, 0}, transport=Transport,
  182. host=undefined}, State=#state{buffer=Buffer}) ->
  183. Port = default_port(Transport:name()),
  184. dispatch(fun handler_init/2, Req#http_req{host=[], raw_host= <<>>,
  185. port=Port, buffer=Buffer}, State#state{buffer= <<>>});
  186. header(http_eoh, Req, State=#state{buffer=Buffer}) ->
  187. handler_init(Req#http_req{buffer=Buffer}, State#state{buffer= <<>>});
  188. header({http_error, _Bin}, _Req, State) ->
  189. error_terminate(500, State).
  190. -spec dispatch(fun((#http_req{}, #state{}) -> ok),
  191. #http_req{}, #state{}) -> ok | none().
  192. dispatch(Next, Req=#http_req{host=Host, path=Path},
  193. State=#state{dispatch=Dispatch}) ->
  194. %% @todo We probably want to filter the Host and Path here to allow
  195. %% things like url rewriting.
  196. case cowboy_dispatcher:match(Host, Path, Dispatch) of
  197. {ok, Handler, Opts, Binds, HostInfo, PathInfo} ->
  198. Next(Req#http_req{host_info=HostInfo, path_info=PathInfo,
  199. bindings=Binds}, State#state{handler={Handler, Opts}});
  200. {error, notfound, host} ->
  201. error_terminate(400, State);
  202. {error, notfound, path} ->
  203. error_terminate(404, State)
  204. end.
  205. -spec handler_init(#http_req{}, #state{}) -> ok | none().
  206. handler_init(Req, State=#state{transport=Transport,
  207. handler={Handler, Opts}}) ->
  208. try Handler:init({Transport:name(), http}, Req, Opts) of
  209. {ok, Req2, HandlerState} ->
  210. handler_handle(HandlerState, Req2, State);
  211. {loop, Req2, HandlerState} ->
  212. handler_before_loop(HandlerState, Req2, State);
  213. {loop, Req2, HandlerState, hibernate} ->
  214. handler_before_loop(HandlerState, Req2,
  215. State#state{hibernate=true});
  216. {loop, Req2, HandlerState, Timeout} ->
  217. handler_before_loop(HandlerState, Req2,
  218. State#state{loop_timeout=Timeout});
  219. {loop, Req2, HandlerState, Timeout, hibernate} ->
  220. handler_before_loop(HandlerState, Req2,
  221. State#state{hibernate=true, loop_timeout=Timeout});
  222. {shutdown, Req2, HandlerState} ->
  223. handler_terminate(HandlerState, Req2, State);
  224. %% @todo {upgrade, transport, Module}
  225. {upgrade, protocol, Module} ->
  226. upgrade_protocol(Req, State, Module)
  227. catch Class:Reason ->
  228. error_terminate(500, State),
  229. error_logger:error_msg(
  230. "** Handler ~p terminating in init/3~n"
  231. " for the reason ~p:~p~n"
  232. "** Options were ~p~n"
  233. "** Request was ~p~n** Stacktrace: ~p~n~n",
  234. [Handler, Class, Reason, Opts, Req, erlang:get_stacktrace()])
  235. end.
  236. -spec upgrade_protocol(#http_req{}, #state{}, atom()) -> ok | none().
  237. upgrade_protocol(Req, State=#state{listener=ListenerPid,
  238. handler={Handler, Opts}}, Module) ->
  239. case Module:upgrade(ListenerPid, Handler, Opts, Req) of
  240. {UpgradeRes, Req2} -> next_request(Req2, State, UpgradeRes);
  241. _Any -> terminate(State)
  242. end.
  243. -spec handler_handle(any(), #http_req{}, #state{}) -> ok | none().
  244. handler_handle(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
  245. try Handler:handle(Req, HandlerState) of
  246. {ok, Req2, HandlerState2} ->
  247. terminate_request(HandlerState2, Req2, State)
  248. catch Class:Reason ->
  249. error_logger:error_msg(
  250. "** Handler ~p terminating in handle/2~n"
  251. " for the reason ~p:~p~n"
  252. "** Options were ~p~n** Handler state was ~p~n"
  253. "** Request was ~p~n** Stacktrace: ~p~n~n",
  254. [Handler, Class, Reason, Opts,
  255. HandlerState, Req, erlang:get_stacktrace()]),
  256. handler_terminate(HandlerState, Req, State),
  257. terminate(State)
  258. end.
  259. %% We don't listen for Transport closes because that would force us
  260. %% to receive data and buffer it indefinitely.
  261. -spec handler_before_loop(any(), #http_req{}, #state{}) -> ok | none().
  262. handler_before_loop(HandlerState, Req, State=#state{hibernate=true}) ->
  263. State2 = handler_loop_timeout(State),
  264. erlang:hibernate(?MODULE, handler_loop,
  265. [HandlerState, Req, State2#state{hibernate=false}]);
  266. handler_before_loop(HandlerState, Req, State) ->
  267. State2 = handler_loop_timeout(State),
  268. handler_loop(HandlerState, Req, State2).
  269. %% Almost the same code can be found in cowboy_http_websocket.
  270. -spec handler_loop_timeout(#state{}) -> #state{}.
  271. handler_loop_timeout(State=#state{loop_timeout=infinity}) ->
  272. State#state{loop_timeout_ref=undefined};
  273. handler_loop_timeout(State=#state{loop_timeout=Timeout,
  274. loop_timeout_ref=PrevRef}) ->
  275. _ = case PrevRef of undefined -> ignore; PrevRef ->
  276. erlang:cancel_timer(PrevRef) end,
  277. TRef = make_ref(),
  278. erlang:send_after(Timeout, self(), {?MODULE, timeout, TRef}),
  279. State#state{loop_timeout_ref=TRef}.
  280. -spec handler_loop(any(), #http_req{}, #state{}) -> ok | none().
  281. handler_loop(HandlerState, Req, State=#state{loop_timeout_ref=TRef}) ->
  282. receive
  283. {?MODULE, timeout, TRef} ->
  284. terminate_request(HandlerState, Req, State);
  285. {?MODULE, timeout, OlderTRef} when is_reference(OlderTRef) ->
  286. handler_loop(HandlerState, Req, State);
  287. Message ->
  288. handler_call(HandlerState, Req, State, Message)
  289. end.
  290. -spec handler_call(any(), #http_req{}, #state{}, any()) -> ok | none().
  291. handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}},
  292. Message) ->
  293. try Handler:info(Message, Req, HandlerState) of
  294. {ok, Req2, HandlerState2} ->
  295. terminate_request(HandlerState2, Req2, State);
  296. {loop, Req2, HandlerState2} ->
  297. handler_before_loop(HandlerState2, Req2, State);
  298. {loop, Req2, HandlerState2, hibernate} ->
  299. handler_before_loop(HandlerState2, Req2,
  300. State#state{hibernate=true})
  301. catch Class:Reason ->
  302. error_logger:error_msg(
  303. "** Handler ~p terminating in info/3~n"
  304. " for the reason ~p:~p~n"
  305. "** Options were ~p~n** Handler state was ~p~n"
  306. "** Request was ~p~n** Stacktrace: ~p~n~n",
  307. [Handler, Class, Reason, Opts,
  308. HandlerState, Req, erlang:get_stacktrace()])
  309. end.
  310. -spec handler_terminate(any(), #http_req{}, #state{}) -> ok.
  311. handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) ->
  312. try
  313. Handler:terminate(Req#http_req{resp_state=locked}, HandlerState)
  314. catch Class:Reason ->
  315. error_logger:error_msg(
  316. "** Handler ~p terminating in terminate/2~n"
  317. " for the reason ~p:~p~n"
  318. "** Options were ~p~n** Handler state was ~p~n"
  319. "** Request was ~p~n** Stacktrace: ~p~n~n",
  320. [Handler, Class, Reason, Opts,
  321. HandlerState, Req, erlang:get_stacktrace()])
  322. end.
  323. -spec terminate_request(any(), #http_req{}, #state{}) -> ok | none().
  324. terminate_request(HandlerState, Req, State) ->
  325. HandlerRes = handler_terminate(HandlerState, Req, State),
  326. next_request(Req, State, HandlerRes).
  327. -spec next_request(#http_req{}, #state{}, any()) -> ok | none().
  328. next_request(Req=#http_req{connection=Conn, buffer=Buffer},
  329. State, HandlerRes) ->
  330. BodyRes = ensure_body_processed(Req),
  331. RespRes = ensure_response(Req),
  332. case {HandlerRes, BodyRes, RespRes, Conn} of
  333. {ok, ok, ok, keepalive} ->
  334. ?MODULE:parse_request(State#state{
  335. buffer=Buffer, req_empty_lines=0});
  336. _Closed ->
  337. terminate(State)
  338. end.
  339. -spec ensure_body_processed(#http_req{}) -> ok | close.
  340. ensure_body_processed(#http_req{body_state=done}) ->
  341. ok;
  342. ensure_body_processed(Req=#http_req{body_state=waiting}) ->
  343. case cowboy_http_req:body(Req) of
  344. {error, badarg} -> ok; %% No body.
  345. {error, _Reason} -> close;
  346. _Any -> ok
  347. end.
  348. -spec ensure_response(#http_req{}) -> ok.
  349. %% The handler has already fully replied to the client.
  350. ensure_response(#http_req{resp_state=done}) ->
  351. ok;
  352. %% No response has been sent but everything apparently went fine.
  353. %% Reply with 204 No Content to indicate this.
  354. ensure_response(Req=#http_req{resp_state=waiting}) ->
  355. _ = cowboy_http_req:reply(204, [], [], Req),
  356. ok;
  357. %% Close the chunked reply.
  358. ensure_response(#http_req{method='HEAD', resp_state=chunks}) ->
  359. close;
  360. ensure_response(#http_req{socket=Socket, transport=Transport,
  361. resp_state=chunks}) ->
  362. Transport:send(Socket, <<"0\r\n\r\n">>),
  363. close.
  364. -spec error_terminate(http_status(), #state{}) -> ok.
  365. error_terminate(Code, State=#state{socket=Socket, transport=Transport}) ->
  366. _ = cowboy_http_req:reply(Code, [], [], #http_req{
  367. socket=Socket, transport=Transport,
  368. connection=close, resp_state=waiting}),
  369. terminate(State).
  370. -spec terminate(#state{}) -> ok.
  371. terminate(#state{socket=Socket, transport=Transport}) ->
  372. Transport:close(Socket),
  373. ok.
  374. %% Internal.
  375. -spec version_to_connection(http_version()) -> keepalive | close.
  376. version_to_connection({1, 1}) -> keepalive;
  377. version_to_connection(_Any) -> close.
  378. -spec default_port(atom()) -> 80 | 443.
  379. default_port(ssl) -> 443;
  380. default_port(_) -> 80.
  381. %% @todo While 32 should be enough for everybody, we should probably make
  382. %% this configurable or something.
  383. -spec format_header(atom()) -> atom(); (binary()) -> binary().
  384. format_header(Field) when is_atom(Field) ->
  385. Field;
  386. format_header(Field) when byte_size(Field) =< 20; byte_size(Field) > 32 ->
  387. Field;
  388. format_header(Field) ->
  389. format_header(Field, true, <<>>).
  390. -spec format_header(binary(), boolean(), binary()) -> binary().
  391. format_header(<<>>, _Any, Acc) ->
  392. Acc;
  393. %% Replicate a bug in OTP for compatibility reasons when there's a - right
  394. %% after another. Proper use should always be 'true' instead of 'not Bool'.
  395. format_header(<< $-, Rest/bits >>, Bool, Acc) ->
  396. format_header(Rest, not Bool, << Acc/binary, $- >>);
  397. format_header(<< C, Rest/bits >>, true, Acc) ->
  398. format_header(Rest, false, << Acc/binary, (cowboy_bstr:char_to_upper(C)) >>);
  399. format_header(<< C, Rest/bits >>, false, Acc) ->
  400. format_header(Rest, false, << Acc/binary, (cowboy_bstr:char_to_lower(C)) >>).
  401. %% Tests.
  402. -ifdef(TEST).
  403. format_header_test_() ->
  404. %% {Header, Result}
  405. Tests = [
  406. {<<"Sec-Websocket-Version">>, <<"Sec-Websocket-Version">>},
  407. {<<"Sec-WebSocket-Version">>, <<"Sec-Websocket-Version">>},
  408. {<<"sec-websocket-version">>, <<"Sec-Websocket-Version">>},
  409. {<<"SEC-WEBSOCKET-VERSION">>, <<"Sec-Websocket-Version">>},
  410. %% These last tests ensures we're formatting headers exactly like OTP.
  411. %% Even though it's dumb, it's better for compatibility reasons.
  412. {<<"Sec-WebSocket--Version">>, <<"Sec-Websocket--version">>},
  413. {<<"Sec-WebSocket---Version">>, <<"Sec-Websocket---Version">>}
  414. ],
  415. [{H, fun() -> R = format_header(H) end} || {H, R} <- Tests].
  416. -endif.