Я понимаю, что на этот вопрос ответили, но я оставлю это здесь, на тот случай, если кто-то ищет что-то простое, прямолинейное и удобочитаемое.
-module(proxy).
-define(HTTP_PORT, 80).
-define(DEFAULT_PROXY_PORT, 8000).
-export([start/0,start/1,worker/1]).
start()->
start (?DEFAULT_PROXY_PORT).
start(Port)->
{ok, Listen} = gen_tcp:listen (Port, [binary,
{packet, 0},
{reuseaddr, true},
{active, false},
{delay_send, false},
{nodelay, true}]),
server_loop (Listen).
server_loop (Listen)->
{ok, Socket} = gen_tcp:accept (Listen),
spawn (?MODULE, worker, [Socket]),
server_loop (Listen).
worker (Socket)->
{ok, Request} = gen_tcp:recv (Socket, 0),
ListRequest = binary_to_list (Request),
io:format ("~p ~n", [ListRequest]),
[Head, URL | RequestTail] = string:tokens (ListRequest, " "),
["http:", Host | URLTail] = string:tokens (URL, "/"),
Document = "/" ++ string:join (URLTail, "/"),
NewRequest = string:join ([Head, Document | RequestTail], " "),
io:format ("~p ~p ~p~n", [self(), Host, Document]),
{ok, SecondSocket} = gen_tcp:connect (Host, ?HTTP_PORT, [binary,
{packet, 0},
{active, false},
{delay_send, false},
{nodelay, true}]),
ok = gen_tcp:send (SecondSocket, NewRequest),
io:format ("~p waiting...~n", [self()]),
Response = read_tcp (SecondSocket),
io:format ("~p done!~n", [self()]),
ok = gen_tcp:send (Socket, Response),
gen_tcp:close (SecondSocket),
gen_tcp:close (Socket).
read_tcp (Socket)->
read_tcp (Socket, []).
read_tcp (Socket, SoFar)->
case gen_tcp:recv (Socket, 0, 500) of
{ok, Response} ->
read_tcp (Socket, [Response | SoFar]);
{error, timeout} ->
lists:reverse (SoFar);
{error, closed} ->
lists:reverse (SoFar)
end.