关于Erlang http代理

时间:2011-07-28 00:58:48

标签: http proxy erlang

是否有一些Erlang http代理? 哪个是最好的Erlang http客户端? httpc似乎湖的文件,例如,如何在httpc中发送cookie标头? 任何Erlang http代理的示例,或mochiweb中的代理?

4 个答案:

答案 0 :(得分:2)

我记不起在erlang中编写的任何相对着名的http代理的名称。快速谷歌搜索显示:http://www.duomark.com/erlang/tutorials/proxy.html

对于http客户端,我建议尝试lhttpc或ibrowse

如果http客户端没有内置方法在请求中指定cookie,通常可以构造一个http Cookie头并将其作为请求http头的一部分传递给http请求处理函数。 / p>

答案 1 :(得分:2)

我曾经使用upside-down-ternet和imagemagick的组合构建了一个类似webmachine的代理。它基于this资源示例。

我同意Alexander的观点,lhttpcibrowse是最好的http客户端。我已经在制作中使用了两个并取得了巨大成功。

答案 2 :(得分:1)

现在可以在GitHub上使用Erlang中实现的HTTP代理:https://github.com/Wavenet/http_proxy

答案 3 :(得分:0)

我知道这个问题已经得到了解答,但我会留下这个,以防万一有人正在寻找简单,直接和可读的东西。

-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.