Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Examples updated

  • Loading branch information...
commit 3f62973bbc46342b8364efdedaccc15fd0de50f2 1 parent 1cd3518
Garrett Smith authored
8 examples/Makefile
View
@@ -1,8 +1,8 @@
compile:
- cd .. && make compile
+ erlc -I ../include -pa ../ebin *.erl
clean:
- cd .. && make clean
+ rm *.beam
-test:
- cd .. && make test
+shell:
+ erl -pa ../ebin
9 examples/echo_http.erl
View
@@ -1,17 +1,18 @@
-module(echo_http).
--include_lib("modlib/include/webapp.hrl").
+-include("webapp.hrl").
-export([start/1, request/3]).
-define(TITLE, "Echo").
start(Port) ->
+ application:start(inets),
modlib:start([{port, Port}, {modules, [?MODULE]}]).
request(Method, Path, Info) ->
- {ok,
- {html,
+ {ok,
+ {html,
["<html>",
"<head><title>", ?TITLE, "</title></head>",
"<body>",
@@ -47,5 +48,5 @@ post_params(Info) ->
headers(Info) ->
["<h4>Headers</h4>",
- [["<div><b>", Name, "</b>: ", Value, "</div>"]
+ [["<div><b>", Name, "</b>: ", Value, "</div>"]
|| {Name, Value} <- modlib:headers(Info)]].
2  examples/hello_http.erl
View
@@ -1,6 +1,6 @@
-module(hello_http).
--include_lib("modlib/include/webapp.hrl").
+-include("webapp.hrl").
-export([start/1, request/3]).
62 examples/proxy_http.erl
View
@@ -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  rebar.config
View
@@ -1,2 +1,2 @@
%%% -*-erlang-*-
-{erl_opts, [{src_dirs, ["examples"]}]}.
+%{erl_opts, [{src_dirs, ["examples"]}]}.
Please sign in to comment.
Something went wrong with that request. Please try again.