Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rustyio committed May 9, 2009
0 parents commit c2c445a
Show file tree
Hide file tree
Showing 17 changed files with 512 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "lib/etap"]
path = lib/etap
url = git://github.com/ngerakines/etap.git
10 changes: 10 additions & 0 deletions Emakefile
@@ -0,0 +1,10 @@
% Compile Nitrogen Files.
{ './src/*', [ debug_info,
{ i, "./include" },
{ outdir, "./ebin" }
]}.

{ './src/*/*', [ debug_info,
{ i, "./include" },
{ outdir, "./ebin" }
]}.
14 changes: 14 additions & 0 deletions Makefile
@@ -0,0 +1,14 @@
all: compile

compile:
mkdir -p ebin
erl \
-pa ./ebin \
-make

clean:
rm -rf ./ebin/*.*

test:
git submodule update --init lib/etap
make -C lib/etap
1 change: 1 addition & 0 deletions ebin/.gitignore
@@ -0,0 +1 @@

Binary file added ebin/mochiweb_request_bridge.beam
Binary file not shown.
Binary file added ebin/request_bridge.beam
Binary file not shown.
Binary file added ebin/request_bridge_wrapper.beam
Binary file not shown.
Binary file added ebin/response_bridge.beam
Binary file not shown.
Binary file added ebin/response_bridge_wrapper.beam
Binary file not shown.
3 changes: 3 additions & 0 deletions include/simplebridge.inc
@@ -0,0 +1,3 @@
-record(response, { statuscode=200, headers=[], cookies=[], data=[] }).
-record(cookie, { name, value, path="/", minutes_to_live=20 }).
-record(header, { name, value }).
171 changes: 171 additions & 0 deletions src/platforms/inets_request_bridge.erl.txt
@@ -0,0 +1,171 @@
% Erlang Web Bridge
% Copyright (c) 2008-2009 Rusty Klophaus
% See MIT-LICENSE for licensing information.

-module (wf_platform_inets).
-include ("httpd.hrl").
-export ([
get_platform/0,

get_raw_path/0,
get_querystring/0,
get_request_method/0,
get_request_body/0,

get_headers/0,
get_header/1,

parse_get_args/0,
parse_post_args/0,

get_cookie/1,
create_cookie/4,

create_header/2,

build_response/0,

get_socket/0,
recv_from_socket/2
]).

get_platform() -> inets.

%%% PATH, METHOD, AND ARGS %%%

get_raw_path() ->
Info = wf_platform:get_request(),
Info#mod.request_uri.

get_querystring() ->
Info = wf_platform:get_request(),
{_Path, QueryString} = httpd_util:split_path(Info#mod.request_uri),
case QueryString of
[] -> [];
_ -> tl(QueryString)
end.

get_request_method() ->
Info = wf_platform:get_request(),
wf:to_atom(Info#mod.method).

get_request_body() ->
Req = wf_platform:get_request(),
Req#mod.entity_body.

parse_get_args() ->
QueryString = get_querystring(),
Query = httpd:parse_query(QueryString),
[{Key, Value} || {Key, Value} <- Query, Key /= []].

parse_post_args() ->
Info = wf_platform:get_request(),
Query = httpd:parse_query(Info#mod.entity_body),
[{Key, Value} || {Key, Value} <- Query, Key /= []].


%%% HEADERS

get_headers() ->
Info = wf_platform:get_request(),
Headers = Info#mod.parsed_header,
F = fun(Header) -> proplists:get_value(Header, Headers) end,
[
{connection, F("connection")},
{accept, F("accept")},
{host, F("host")},
{if_modified_since, F("if-modified-since")},
{if_match, F("if-match")},
{if_none_match, F("if-range")},
{if_unmodified_since, F("if-unmodified-since")},
{range, F("range")},
{referer, F("referer")},
{user_agent, F("user-agent")},
{accept_ranges, F("accept-ranges")},
{cookie, F("cookie")},
{keep_alive, F("keep-alive")},
{location, F("location")},
{content_length, F("content-length")},
{content_type, F("content-type")},
{content_encoding, F("content-encoding")},
{authorization, F("authorization")},
{transfer_encoding, F("transfer-encoding")}
].

get_header(Header) ->
Headers = get_headers(),
proplists:get_value(Header, Headers).

%%% COOKIES %%%

get_cookie(Key) ->
Key1 = wf:to_list(Key),
Info = wf_platform:get_request(),
Headers = Info#mod.parsed_header,
CookieData = proplists:get_value("cookie", Headers, ""),
F = fun(Cookie) ->
case string:tokens(Cookie, "=") of
[] -> [];
L ->
X = string:strip(hd(L)),
Y = string:join(tl(L), "="),
{X, Y}
end
end,
Cookies = [F(X) || X <- string:tokens(CookieData, ";")],
proplists:get_value(Key1, Cookies).

create_cookie(Key, Value, Path, MinutesToLive) ->
SecondsToLive = MinutesToLive * 60,
Expire = to_cookie_expire(SecondsToLive),
{"Set-Cookie", wf:f("~s=~s; Path=~s; Expires=~s", [Key, Value, Path, Expire])}.

to_cookie_expire(SecondsToLive) ->
Seconds = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
DateTime = calendar:gregorian_seconds_to_datetime(Seconds + SecondsToLive),
httpd_util:rfc1123_date(DateTime).



%%% HEADERS %%%
create_header(Key, Value) ->
{Key, Value}.


%%% RESPONSE %%%

build_response() ->
% Get vars...
Info = wf_platform:get_request(),
ResponseCode = get(wf_response_code),
ContentType = get(wf_content_type),
Body = get(wf_response_body),
Size = integer_to_list(httpd_util:flatlength(Body)),

% Assemble headers...
Headers = lists:flatten([
{code, ResponseCode},
{content_type, ContentType},
{content_length, Size},
get(wf_headers)
]),

% Send the inets response...
{proceed,[
{response, {response, Headers, Body}},
{mime_type, ContentType} | Info#mod.data
]}.


%%% SOCKETS %%%

get_socket() ->
Info = wf_platform:get_request(),
Info#mod.socket.

recv_from_socket(Length, Timeout) ->
Socket = get_socket(),
case gen_tcp:recv(Socket, Length, Timeout) of
{ok, Data} -> Data;
_ -> exit(normal)
end.
45 changes: 45 additions & 0 deletions src/platforms/mochiweb_request_bridge.erl
@@ -0,0 +1,45 @@
% Simple Erlang Web Bridge
% Copyright (c) 2008-2009 Rusty Klophaus
% See MIT-LICENSE for licensing information.

-module (mochiweb_request_bridge).
-behaviour (request_bridge).
-export ([
init/1,
request_method/1, url/1, protocol/1, hostname/1, port/1, path/1, querystring/1,
peer_ip/1, peer_port/1
]).


init(Req) ->
Req.

request_method(Req) ->
Req:get(method).

url(_Req) ->
ok.

protocol(_Req) ->
ok.

hostname(_Req) ->
ok.

port(_Req) ->
ok.

path(_Req) ->
ok.

querystring(Req) ->
Req = wf_platform:get_request(),
RawPath = Req:get(raw_path),
{_, QueryString, _} = mochiweb_util:urlsplit_path(RawPath),
QueryString.

peer_ip(_Req) ->
ok.

peer_port(_Req) ->
ok.

0 comments on commit c2c445a

Please sign in to comment.