ninenines / gun

HTTP/1.1, HTTP/2, Websocket client (and more) for Erlang/OTP.
ISC License
891 stars 232 forks source link

How to use CONNECT #195

Closed xafizoff closed 5 years ago

xafizoff commented 5 years ago

Hi.

I'm trying to use gun with https proxy.

{ok,Pid} = gun:open(Host,443),
Ref = gun:connect(Pid,#{host => ProxyHost,
                        port => ProxyPort,
                        username => <<"me">>,
                        password => <<"secret">>,
                        transport => tls}),
{ok,Protococol} = gun:await_up(Pid),
Ref2 = gun:get(Pid,Path),
Response = gun:await(Pid,Ref2).

Is that usage correct? Also, is it possible to use CONNECT with websockets? Currently, I'm getting {error,timeout}.

essen commented 5 years ago

Not correct, see https://ninenines.eu/docs/en/gun/1.3/manual/gun.connect/#_examples

After the proxy is connected to you can use Gun normally including Websocket.

xafizoff commented 5 years ago

Thank you for the help. This seems to work:

    application:ensure_all_started(gun),
    {ok, ConnPid} = gun:open(ProxyHosg,ProxyPort),
    {ok, http} = gun:await_up(ConnPid),
    StreamRef = gun:connect(ConnPid, #{
        host => "websocket-server.example.org",
        port => 443,
        password => <<"me">>,          
        username => <<"secret">>,
        protocols => [http],
        transport => tls
    }),
    {response, fin, 200, _} = gun:await(ConnPid, StreamRef),
    Ref = gun:ws_upgrade(ConnPid,"/ws",[]),
    ...