Skip to content

Commit

Permalink
Examples updated
Browse files Browse the repository at this point in the history
  • Loading branch information
garrett committed Feb 1, 2012
1 parent 1cd3518 commit 3f62973
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 10 deletions.
8 changes: 4 additions & 4 deletions examples/Makefile
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,8 @@
compile: compile:
cd .. && make compile erlc -I ../include -pa ../ebin *.erl


clean: clean:
cd .. && make clean rm *.beam


test: shell:
cd .. && make test erl -pa ../ebin
9 changes: 5 additions & 4 deletions examples/echo_http.erl
Original file line number Original file line Diff line number Diff line change
@@ -1,17 +1,18 @@
-module(echo_http). -module(echo_http).


-include_lib("modlib/include/webapp.hrl"). -include("webapp.hrl").


-export([start/1, request/3]). -export([start/1, request/3]).


-define(TITLE, "Echo"). -define(TITLE, "Echo").


start(Port) -> start(Port) ->
application:start(inets),
modlib:start([{port, Port}, {modules, [?MODULE]}]). modlib:start([{port, Port}, {modules, [?MODULE]}]).


request(Method, Path, Info) -> request(Method, Path, Info) ->
{ok, {ok,
{html, {html,
["<html>", ["<html>",
"<head><title>", ?TITLE, "</title></head>", "<head><title>", ?TITLE, "</title></head>",
"<body>", "<body>",
Expand Down Expand Up @@ -47,5 +48,5 @@ post_params(Info) ->


headers(Info) -> headers(Info) ->
["<h4>Headers</h4>", ["<h4>Headers</h4>",
[["<div><b>", Name, "</b>: ", Value, "</div>"] [["<div><b>", Name, "</b>: ", Value, "</div>"]
|| {Name, Value} <- modlib:headers(Info)]]. || {Name, Value} <- modlib:headers(Info)]].
2 changes: 1 addition & 1 deletion examples/hello_http.erl
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(hello_http). -module(hello_http).


-include_lib("modlib/include/webapp.hrl"). -include("webapp.hrl").


-export([start/1, request/3]). -export([start/1, request/3]).


Expand Down
62 changes: 62 additions & 0 deletions examples/proxy_http.erl
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,62 @@
-module(proxy_http).

-include("webapp.hrl").
-include("httpd.hrl").

-export([start/1, request/3]).

start(Port) ->
application:start(inets),
modlib:start([{port, Port}, {modules, [?MODULE]}]).

request(_, "/favicon.ico", _) ->
{not_found, "Not Found"};
request(_Method, _Path, Mod) ->
handle_request(Mod).

handle_request(Mod) ->
Method = httpc_method(Mod),
URL = proxy_url(httpc_uri(Mod)),
{Headers, ContentType} = httpc_headers(Mod),
Body = httpc_body(Mod),
handle_response(httpc_request(Method, URL, Headers, ContentType, Body)).

httpc_request(Method, URL, Headers, undefined, _) ->
httpc_request(Method, {URL, Headers});
httpc_request(Method, URL, Headers, ContentType, Body) ->
httpc_request(Method, {URL, Headers, ContentType, Body}).

httpc_request(Method, Request) ->
error_logger:info_report({proxy_request, {Method, Request}}),
httpc:request(Method, Request, [], []).

handle_response({ok, {{_, Code, _}, Headers, Body}}) ->
{Code, Headers, Body};
handle_response({error, Err}) ->
error_logger:error_report({proxy_error, Err}),
{500, "Internal Error"}.

proxy_url(URI) ->
"http://localhost:8888" ++ URI.

httpc_method(#mod{method="POST"}) -> post;
httpc_method(#mod{method="GET"}) -> get;
httpc_method(#mod{method="DELETE"}) -> delete;
httpc_method(#mod{method="PUT"}) -> put;
httpc_method(#mod{method="HEAD"}) -> head;
httpc_method(_) -> bad_request().

httpc_uri(#mod{request_uri=URI}) -> URI.

httpc_headers(#mod{parsed_header=Headers}) ->
case lists:keytake("content-type", 1, Headers) of
{value, {_, ContentType}, NewHeaders} ->
{NewHeaders, ContentType};
false ->
{Headers, undefined}
end.

httpc_body(#mod{entity_body=Body}) -> Body.

bad_request() ->
throw({badreq, "Bad Request"}).
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,2 @@
%%% -*-erlang-*- %%% -*-erlang-*-
{erl_opts, [{src_dirs, ["examples"]}]}. %{erl_opts, [{src_dirs, ["examples"]}]}.

0 comments on commit 3f62973

Please sign in to comment.