简体   繁体   English

关于Erlang HTTP代理

[英]About Erlang http proxy

Is there some Erlang http proxy? 是否有一些Erlang http代理? Which is the best Erlang http client? 哪个是最好的Erlang http客户端? httpc seems lake of document, for example, How to send the cookie header in httpc? httpc似乎是文档之湖,例如,如何在httpc中发送cookie标头? Any example of Erlang http proxy, or proxy in mochiweb? Erlang http代理或mochiweb中的代理的任何示例?

I can not recall name of any relatively famous http proxy written in erlang. 我不记得任何用erlang编写的相对著名的http代理的名称。 Quick google search reveals this: http://www.duomark.com/erlang/tutorials/proxy.html 谷歌快速搜索显示了这一点: http : //www.duomark.com/erlang/tutorials/proxy.html

As for http client, I would recommend to try lhttpc or ibrowse 至于http客户端,我建议尝试使用lhttpc或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. 如果http客户端没有用于在请求中指定cookie的内置方法,通常您可以构造一个http Cookie头,并将其作为请求http头的一部分传递给http请求处理函数。

I once built a proxy that did something like upside-down-ternet with a combination of webmachine and imagemagick. 我曾经构建了一个代理,该代理结合了webmachine和imagemagick的功能,实现了上下颠倒以太网 It was based on this resource example. 它基于资源示例。

I concur with Alexander that lhttpc and ibrowse are the best http clients. 我同意Alexander的观点,认为lhttpcibrowse是最好的http客户端。 I've used both in production with great success. 我在生产中都使用了这两种方法,并取得了巨大的成功。

现已在GitHub上提供以Erlang实现的HTTP代理: https : //github.com/Wavenet/http_proxy

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM