WaterSweet / mochiweb

Automatically exported from code.google.com/p/mochiweb
Other
0 stars 0 forks source link

Patch adding 2 useful methods to mochiweb_headers.erl #48

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hello,

It's a bit awkward the fact that the trailer/footer of a chunked http 
message is supplied as a list of binaries and we have no function to 
convert such list into a mochiweb headers structure.

Hereby I send you a patch with 2 useful functions:

%% @spec from_binary(RawHttpHeader()) -> headers() 
%% @type RawHttpHeader() -> binary()
%% 
%% @doc Transforms a raw HTTP header into a mochiweb headers structure.
%%
%%      The given raw HTTP header is a binary representing a full HTTP
%%      header ending with double CRLF. 
%%      Example:
%%      <<"Content-Length: 47\r\nContent-Type: text/plain\r\n\r\n">>
%% 
from_binary(RawHttpHeader) ->
    to_mochiweb_headers(RawHttpHeader, []).

%% @spec from_binary_list(RawHttpHeaderList()) -> headers() 
%% @type RawHttpHeaderList() -> [ binary() ]
%% 
%% @doc Transforms a raw HTTP header into a mochiweb headers structure.
%%
%%      The given raw HTTP header is a list of binaries where each binary
%%      represents a raw HTTP header line ending with a single CRLF.
%%      Example:
%%      [ <<"Content-Length: 47\r\n">>, <<"Content-Type: text/plain\r
\n">> ]
%% 
from_binary_list(RawHttpHeaderList) ->
    from_binary(list_to_binary([RawHttpHeaderList, "\r\n"])).

to_mochiweb_headers(RawHttpHeader, Acc) ->
    { ok, {http_header, _, H, _, V}, Rest } =
        erlang:decode_packet(httph, RawHttpHeader, []),
    case Rest of
        <<"\r\n">> ->
            make([{H, V} | Acc]);
        R ->
            to_mochiweb_headers(R, [{H, V} | Acc])
    end.

I also added test statements to the test/0 function.

best regards,
Filipe Manana

Original issue reported on code.google.com by fdman...@gmail.com on 17 Nov 2009 at 9:32

GoogleCodeExporter commented 8 years ago
Hello, this second version of the patch fixes a bug that arises when an empty 
binary
(<<>>, <<"">>, <<"\r\n">>) or empty list is passed to one of those 2 new 
functions.

Btw, can I have some feedback about the patch?

best regards,
Filipe Manana

Original comment by fdman...@gmail.com on 21 Nov 2009 at 2:33

Attachments:

GoogleCodeExporter commented 8 years ago
This third patch adds:

+ from_binary_list/1 is now a special case of from_binary/1. 
to_mochiweb_headers/2
was renamed to from_binary/2.

+ updated the doc comment for from_binary

+ more tests (for empty lists/strings and binaries and lists containing mixes of
strings/binaries)

Original comment by fdman...@gmail.com on 22 Nov 2009 at 1:57

Attachments:

GoogleCodeExporter commented 8 years ago
r114

Original comment by bob.ippo...@gmail.com on 2 Jan 2010 at 6:54