Skip to content

Commit

Permalink
make cowboy_revproxy working with latest version of cowboy.
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitc committed Mar 26, 2012
1 parent a976a96 commit c552bed
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
24 changes: 11 additions & 13 deletions README.md
Expand Up @@ -58,6 +58,17 @@ Here is a simple example of function proxying the port 8080 to google.com:
cowboy_tcp_transport, [{port, 8080}],
cowboy_revproxy, [{proxy, {?MODULE, proxy}}]).

To test it do, in the source folder:

$ rebar get-deps compile
$ erl -pa ebin -pa deps/cowboy/ebin
Then in the erlang shell:

1> cowboy_revproxy_example:start().

and go on http://127.0.0.1:8080/ , it should display the google page.


This simple proxy function allows you to handle an HTTP request locally

Expand Down Expand Up @@ -85,16 +96,3 @@ A simple "Hello World" HTTP handler:

terminate(Req, State) ->
ok.



To test it do, in the source folder:

$ rebar get-deps compile
$ erl -pa ebin -pa deps/cowboy/ebin
Then in the erlang shell:

1> cowboy_revproxy_example:start().

and go on http://127.0.0.1:8080/ , it should display the google page.
17 changes: 11 additions & 6 deletions src/cowboy_revproxy.erl
@@ -1,7 +1,7 @@
%M% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%%% ex: ft=erlang ts=4 sw=4 et
%%%
%%% This file is part of cowboy_revproxy released under the MIT license.
%%% This file is part of cowboy_revproxy released under the MIT license.
%%% See the NOTICE for more information.


Expand All @@ -13,6 +13,7 @@
-export([init/4]).

-include_lib("eunit/include/eunit.hrl").
-include_lib("cowboy/include/http.hrl").

%% proxy state
-record(stproxy, {
Expand All @@ -34,10 +35,14 @@
transport :: module(),
dispatch :: cowboy_dispatcher:dispatch_rules(),
handler :: {module(), any()},
onrequest :: undefined | fun((#http_req{}) -> #http_req{}),
urldecode :: {fun((binary(), T) -> binary()), T},
req_empty_lines = 0 :: integer(),
max_empty_lines = 5:: integer(),
max_line_length = 4096:: integer(),
timeout = 5000 :: timeout(),
max_empty_lines :: integer(),
req_keepalive = 1 :: integer(),
max_keepalive :: integer(),
max_line_length :: integer(),
timeout :: timeout(),
buffer = <<>> :: binary(),
hibernate = false :: boolean(),
loop_timeout = infinity :: timeout(),
Expand All @@ -55,7 +60,7 @@ start_link(ListenerPid, Socket, Transport, Opts) ->
init(ListenerPid, Socket, Transport, Opts) ->
Handler = proplists:get_value(proxy, Opts),
Timeout = proplists:get_value(timeout, Opts, 5000),
receive shoot -> ok end,
ok = cowboy:accept_ack(ListenerPid),
wait_request(#stproxy{listener=ListenerPid, socket=Socket,
transport=Transport, handler=Handler, timeout=Timeout}).

Expand Down Expand Up @@ -135,7 +140,7 @@ terminate(#stproxy{socket=Socket, transport=Transport}) ->


remote_connect({Ip, Port}) ->
{cowboy_tcp_transport, gen_tcp:connect(Ip, Port, [binary,
{cowboy_tcp_transport, gen_tcp:connect(Ip, Port, [binary,
{packet, 0}, {delay_send, true}])};
remote_connect({ssl, Ip, Port, Opts}) ->
Opts1 = parse_ssl_options(Opts),
Expand Down
7 changes: 4 additions & 3 deletions src/cowboy_revproxy_example.erl
Expand Up @@ -7,6 +7,7 @@ proxy(_Data) ->
start() ->
application:start(cowboy),
application:start(cowboy_revproxy),
cowboy:start_listener(http, 100,
cowboy_tcp_transport, [{port, 8080}],
cowboy_revproxy, [{proxy, {?MODULE, proxy}}]).

cowboy:start_listener(http, 100,
cowboy_tcp_transport, [{port, 8080}],
cowboy_revproxy, [{proxy, {?MODULE, proxy}}]).

0 comments on commit c552bed

Please sign in to comment.