anchor.diff:  make_version(_) ->
       <<"HTTP/1.1 ">>.
   
  -make_headers(Code, Length, RD) ->
  +make_headers(Code, Transfer, Length, RD) ->
       Hdrs0 = case Code of
           304 ->
               mochiweb_headers:make(wrq:resp_headers(RD));
           _ -> 
  -            case Length of
  -                chunked ->
  -                    mochiweb_headers:enter(
  -                      "Transfer-Encoding","chunked",
  -                      mochiweb_headers:make(wrq:resp_headers(RD)));
  -                _ ->
  +            LengthHeaders = case Length of
  +                undefined ->
  +                    mochiweb_headers:make(wrq:resp_headers(RD));
  +                _Length -> 
                       mochiweb_headers:enter(
                         "Content-Length",integer_to_list(Length),
                         mochiweb_headers:make(wrq:resp_headers(RD)))
  +            end,
  +            
  +            case is_chunked_transfer(wrq:version(RD), Transfer) of
  +                true ->
  +                    mochiweb_headers:enter(
  +                      "Transfer-Encoding","chunked",
  +                      LengthHeaders);
  +                false ->
  +                    LengthHeaders
               end
       end,
       ServerHeader = "MochiWeb/1.1 WebMachine/" ++ ?WMVSN ++ " Zotonic/" ++ ?ZOTONIC_VERSION,
  
positive.code: 	end,
      lists:foldl(F, [<<"\r\n">>], mochiweb_headers:to_list(Hdrs)).
  
  socket(ReqData) -> ReqData#wm_reqdata.socket.
  
  method(ReqData) -> wrq:method(ReqData).
  
negative.code: 	_ -> exit(normal)
      end.
  
  send_stream_body(Socket, IsChunked, X) -> 
      send_stream_body(Socket, IsChunked, X, 0).
  
  send_stream_body(Socket, IsChunked, {<<>>, done}, SoFar) ->
      send_chunk(Socket, IsChunked, <<>>),
      SoFar;
  send_stream_body(Socket, IsChunked, {Data, done}, SoFar) ->
      Size = send_chunk(Socket, IsChunked, Data),
      send_chunk(Socket, IsChunked, <<>>),
      Size + SoFar;
  send_stream_body(Socket, IsChunked, {<<>>, Next}, SoFar) ->
      send_stream_body(Socket, IsChunked, Next(), SoFar);
  send_stream_body(Socket, IsChunked, {[], Next}, SoFar) ->
      send_stream_body(Socket, IsChunked, Next(), SoFar);
  send_stream_body(Socket, IsChunked, {Data, Next}, SoFar) ->
      Size = send_chunk(Socket, IsChunked, Data),
      send_stream_body(Socket, IsChunked, Next(), Size + SoFar).
  
  
  %% @todo Remove usage of the put/get to get the number of bytes written
  %%       Use a separate process to accumulate these counts
  send_writer_body(Socket, IsChunked, {Encoder, Charsetter, BodyFun}) ->
      put(bytes_written, 0),
      Writer = fun(Data) ->
          Size = send_chunk(Socket, IsChunked, Encoder(Charsetter(Data))),
          put(bytes_written, get(bytes_written) + Size),
          Size
      end,
      BodyFun(Writer),
      send_chunk(Socket, IsChunked, <<>>),
      get(bytes_written).
  
  
  %% @todo Distinguish between HTTP/1.0 and 1.1
  %%       HTTP/1.0 should not add the size (and transfer-encoding: chunked)
  send_chunk(Socket, true, Data) ->
      Size = iolist_size(Data),
      send(Socket, mochihex:to_hex(Size)),
      send(Socket, <<"\r\n">>),
      send(Socket, Data),
      send(Socket, <<"\r\n">>),
      Size;
  send_chunk(_Socket, false, <<>>) ->
      0;
  send_chunk(Socket, false, Data) ->
      Size = iolist_size(Data),
      send(Socket, Data),
      Size.
  
  
  