Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft committed May 15, 2013
1 parent ff3dfdc commit d31e08f
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
deps
*.o
*.beam
*.plt
*.plt
ebin
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: erlang
otp_release:
- R16B
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
REBAR = ./rebar

default: compile

all: deps compile

compile:
$(REBAR) compile

deps:
$(REBAR) get-deps

clean:
$(REBAR) clean

distclean: clean
$(REBAR) delete-deps

test:
$(REBAR) skip_deps=true eunit

docs: deps
$(REBAR) skip_deps=true doc

dialyzer: compile
@dialyzer -Wno_return -c ebin

.PHONY: all deps test
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ simple_http_proxy
=================

Simple http proxy for cowboy/erlang

Status
------

This is a work-in-progress. It's mainly waiting on cowboy's client to be finished to be ready for production.
Binary file added rebar
Binary file not shown.
12 changes: 12 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{cover_enabled, true}.

{erl_opts, [
debug_info,
warnings_as_errors,
{parse_transform, lager_transform}
]}.

{deps, [
{lager, ".*", {git, "https://github.com/basho/lager.git", "2.0.0"}},
{cowboy, ".*", {git, "https://github.com/extend/cowboy.git", "0.8.4"}}
]}.
17 changes: 17 additions & 0 deletions src/simple_http_proxy.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{application, simple_http_proxy, [
{id, "simple_http_proxy"},
{description, "Simple http proxy"},
{vsn, "0.1.0"},
{registered, []},
{applications, [
kernel,
stdlib,
crypto,
ssl,
lager,
ranch,
cowboy
]},
{mod, { simple_http_proxy, []}},
{env, []}
]}.
78 changes: 78 additions & 0 deletions src/simple_http_proxy.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
%%
%% simple_http_proxy.erl
%%
-module (simple_http_proxy).

-export([init/3]).
-export([handle/2]).
-export([terminate/3]).

-record (state, {
client,
url
}).

init(_Transport, Req, Url) when is_binary(Url) ->
{ok, Client} = cowboy_client:init([]),
{ok, Req, #state{client=Client, url=Url}}.

handle(Req, #state{client=Client, url=Url}=State) ->
{Method, Req} = cowboy_req:method(Req),
{Headers, Req} = cowboy_req:headers(Req),
{PathInfo, Req} = cowboy_req:path_info(Req),
{FullPath, Req} = cowboy_req:path(Req),
{Query, Req} = cowboy_req:qs(Req),
{Port, Req} = cowboy_req:port(Req),
{Host, Req} = cowboy_req:host(Req),
{HostUrl, Req} = cowboy_req:host_url(Req),

Proto = case HostUrl of
<<"https",_>> -> <<"https">>;
_ -> <<"http">>
end,

%% Setup the request info
ReqPath = binary_join([<<>>|PathInfo], <<"/">>),
[MountPath, TrailingSlash] = binary:split(FullPath, ReqPath),

ReqUrl = <<Url/binary,ReqPath/binary,TrailingSlash/binary,"?",Query/binary>>,

ReqHeaders = [
{<<"x-forwarded-proto">>, Proto},
{<<"x-forwarded-host">>, Host},
{<<"x-forwarded-path">>, MountPath},
{<<"x-forwarded-port">>, list_to_binary(integer_to_list(Port))}
|proplists:delete(<<"connection">>, Headers)],

{ok, Client2} = cowboy_client:request(Method, ReqUrl, ReqHeaders, Client),
{ok, Status, ResHeaders, Client3} = cowboy_client:response(Client2),

{ok, Req2} = cowboy_req:chunked_reply(Status, ResHeaders, Req),

stream_body(Req2, Client3),

{ok, Req2, State}.


stream_body(Req, Client) ->
case cowboy_client:stream_body(Client) of
{ok, Data} ->
lager:debug("proxy sending data ~p", [Data]),
ok = cowboy_req:chunk(Data, Req),
cowboy_client:stream_body(Client);
{done, Client} ->
lager:debug("proxy finished streaming"),
ok;
_ ->
ok
end.

terminate(_Reason, _Req, _State) ->
ok.

binary_join([], _Sep) ->
<<>>;
binary_join([H], _Sep) ->
<< H/binary >>;
binary_join([H | T], Sep) ->
<< H/binary, Sep/binary, (binary_join(T, Sep))/binary >>.
21 changes: 21 additions & 0 deletions test/app.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
%% @private
-module (app).

%% API.
-export([start/0]).

%% API.

start() ->
ok = application:start(crypto),
ok = application:start(ranch),
ok = application:start(cowboy),

Dispatch = cowboy_router:compile([
{'_', [
{"/ui", simple_http_proxy, <<"http://flokk-ui-test.herokuapp.com">>}
]}
]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
{env, [{dispatch, Dispatch}]}
]).

0 comments on commit d31e08f

Please sign in to comment.