Potential bug in {packet, http}

Colm Dougan colm.dougan@REDACTED
Sat Sep 19 22:51:34 CEST 2009


HI,

When I used erlang:decode_packet to parse a HTTP request with an
absolute URI I get the expected outcome :

1> erlang:decode_packet(http, <<"GET http://www.foo.com/bar
HTTP/1.0\r\n">>, []).
{ok,{http_request,'GET',
                  {absoluteURI,http,"www.foo.com",undefined,"/bar"},
                  {1,0}},
    <<>>}

However, when I use {packet, http} for the same thing the outcome is
not what I expect :

1> my_httpd:start().
Listening on port: 8082
ok
2> my_httpd:do_client(<<"GET http://www.foo.com/bar HTTP/1.0\r\n">>).
Connecting to localhost:8082 ...
Connected
ok
Method: 'GET', Uri: {absoluteURI,http,"www.foo.com",undefined,"/bar
HTTP/1"}, Version: {1, 0}

The code of my_httpd is below.  Is this a bug or am I missing something?

Thanks,
Colm


-module(my_httpd).
-export([
    start/0,
    do_client/0,
    do_client/1
]).

-define(LPORT, 8082).
-define(LOPTS,
    [binary, {packet, 0},
             {active, false},
             {packet, http}]
).

start() ->
    spawn(fun init_server/0),
    ok.

do_client() ->
    do_client(<<"GET http://www.foo.com/bar HTTP/1.0\r\n">>).

do_client(Bin) ->
    io:format("Connecting to localhost:~p ...~n", [?LPORT]),
    {ok, CSock} = gen_tcp:connect("localhost", ?LPORT, [binary]),
    io:format("Connected~n"),
    ok = gen_tcp:send(CSock, Bin),
    gen_tcp:close(CSock),
    ok.

init_server() ->
    io:format("Listening on port: ~p~n", [?LPORT]),
    {ok, LSock} = gen_tcp:listen(?LPORT, ?LOPTS),
    server_loop(LSock).

server_loop(LSock) ->
    {ok, Sock} = gen_tcp:accept(LSock),
    catch(handle_client(Sock)),
    catch(gen_tcp:close(Sock)),
    server_loop(LSock).

handle_client(Sock) ->
    case gen_tcp:recv(Sock, 0) of
        {ok, {http_request, Method, Uri, Version}} ->
            io:format("Method: ~p, Uri: ~p, Version: ~p~n", [Method,
Uri, Version]);
        {ok, Other} ->
            io:format("Unexpected: ~p~n", [Other]);
        {error, closed} ->
            io:format("Closed~n")
    end.


More information about the erlang-bugs mailing list