2

Below is part of one example explaining how to use gen_tcp. However, almost all of the tutorials will just show you an echo server, but none doing parsing of a real world HTTP request.

    handle_connect(Socket, BinaryList, Count) ->
    io:format("handle_connect ~p~n", [self()]),
    case gen_tcp:recv(Socket, 0) of
    {ok, Binary} ->
        io:format("request is :~s~n", Binary),
        case gen_tcp:send(Socket, Binary) of
            ok ->
                handle_connect(Socket, BinaryList, Count);
            {error, Reason} ->
                io:format("send failed~n"),
                gen_tcp:close(Socket)
        end;

I am curious about how one can parse the data in Erlang that is received by gen_tcp:recv/\[2,3\]. I skimmed through the code at lib/inets-5.7.1/src/http_server but there are too many modules, and I still can't figure out what the basics of parsing an HTTP request would be.

Can anyone show me how to parse an HTTP request, and what approach web frameworks take? Thanks~

toraritte
  • 6,300
  • 3
  • 46
  • 67
Allan Ruin
  • 5,229
  • 7
  • 37
  • 42
  • See if this helps: https://stackoverflow.com/questions/43957164/erlang-gen-tcp-not-receiving-anything – 7stud Jun 24 '18 at 18:29

1 Answers1

4

There are a couple of lightweight web server libraries for Erlang including:

There is also Yaws which is more comparable to Apache. I would recommend checking out the code of Cowboy since it is very modular.

A shortcut to answer your question might be found here:

http://www.erlang.org/doc/man/erlang.html#decode_packet-3 and https://github.com/extend/cowboy/blob/master/src/cowboy_http_protocol.erl#L99

Shamis Shukoor
  • 2,515
  • 5
  • 29
  • 33
Tilman
  • 2,015
  • 14
  • 16