Reputation: 4761
Is there some Erlang http proxy? Which is the best Erlang http client? httpc seems lake of document, for example, How to send the cookie header in httpc? Any example of Erlang http proxy, or proxy in mochiweb?
Upvotes: 1
Views: 2597
Reputation:
I understand this question has been answered but i`m going to leave this here, just in case someone is looking for something simple, straight forward and readable.
-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.
Upvotes: 1
Reputation: 757
An HTTP proxy implemented in Erlang is now available on GitHub: https://github.com/Wavenet/http_proxy
Upvotes: 1
Reputation: 669
I can not recall name of any relatively famous http proxy written in erlang. Quick google search reveals this: http://www.duomark.com/erlang/tutorials/proxy.html
As for http client, I would recommend to try lhttpc or ibrowse
If an http client does not have a built-in method to specify a cookie in a request, usually you can construct an http Cookie header and pass it to the http requests handler function as a part of the request http headers.
Upvotes: 2
Reputation: 64312
I once built a proxy that did something like upside-down-ternet with a combination of webmachine and imagemagick. It was based on this resource example.
I concur with Alexander that lhttpc and ibrowse are the best http clients. I've used both in production with great success.
Upvotes: 2