anchor.diff:  -include_lib("eunit/include/eunit.hrl").
   -endif.
   
  +-type resp_body_fun() :: fun(() -> {sent, non_neg_integer()}).
  +
   -record(http_req, {
   	%% Transport.
   	socket = undefined :: undefined | inet:socket(),
  
positive.code: 	reply(Status, Headers, Body, Req).
  
  %% @doc Send a reply to the client.
  -spec reply(cowboy_http:status(), cowboy_http:headers(), iodata(), Req)
  	-> {ok, Req} when Req::req().
  reply(Status, Headers, Body, Req=#http_req{socket=Socket, transport=Transport,
  		version=Version, connection=Connection,
  		method=Method, resp_state=waiting, resp_headers=RespHeaders}) ->
  	RespConn = response_connection(Headers, Connection),
  	ContentLen = case Body of {CL, _} -> CL; _ -> iolist_size(Body) end,
  	HTTP11Headers = case Version of
  		{1, 1} -> [{<<"connection">>, atom_to_connection(Connection)}];
  		_ -> []
  	end,
  	{ReplyType, Req2} = response(Status, Headers, RespHeaders,  [
  		{<<"content-length">>, integer_to_list(ContentLen)},
  		{<<"date">>, cowboy_clock:rfc1123()},
  		{<<"server">>, <<"Cowboy">>}
  	|HTTP11Headers], Req),
  	if	Method =:= <<"HEAD">> -> ok;
  		ReplyType =:= hook -> ok; %% Hook replied for us, stop there.
  		true ->
  			case Body of
  				{_, StreamFun} -> StreamFun();
  				_ -> Transport:send(Socket, Body)
  			end
  	end,
  	{ok, Req2#http_req{connection=RespConn, resp_state=done,
  		resp_headers=[], resp_body= <<>>}}.
  
negative.code: %% `reply/3'.
  %%
  %% @see cowboy_req:transport/1.
  -spec set_resp_body_fun(non_neg_integer(), resp_body_fun(), Req)
  	-> Req when Req::req().
  set_resp_body_fun(StreamLen, StreamFun, Req) ->
  	Req#http_req{resp_body={StreamLen, StreamFun}}.
  
  