Permalink
Browse files

Add webmachine support to SimpleBridge.

  • Loading branch information...
1 parent 02ac81b commit ae873db629181430fd33c0342fdffee35aaf15de @rustyio rustyio committed with Torbjorn Tornkvist Dec 20, 2010
View
@@ -1,15 +1,22 @@
% Compile Nitrogen Files.
-{ './src/*', [ debug_info,
+{
+ [
+ "./src/*",
+ "./src/*/*"
+ ], [
+ debug_info,
{ i, "./include" },
{ outdir, "./ebin" }
-]}.
+ ]
+}.
-{ './src/*/*', [ debug_info,
- { i, "./include" },
- { outdir, "./ebin" }
-]}.
-
-{ './test/*', [ debug_info,
+{
+ [
+ "./test/*"
+ ],
+ [
+ debug_info,
{ i, "./include" },
{ outdir, "./test_ebin" }
-]}.
+ ]
+}.
@@ -0,0 +1,96 @@
+% Simple Bridge
+% Copyright (c) 2008-2010 Rusty Klophaus
+% See MIT-LICENSE for licensing information.
+
+-module (webmachine_request_bridge).
+-behaviour (simple_bridge_request).
+-include ("simple_bridge.hrl").
+
+-export ([
+ init/1,
+ request_method/1,
+ path/1,
+ uri/1,
+ peer_ip/1,
+ peer_port/1,
+ headers/1,
+ cookies/1,
+ query_params/1,
+ post_params/1,
+ request_body/1,
+ socket/1,
+ recv_from_socket/3
+]).
+
+init(Req) ->
+ Req.
+
+request_method(Req) ->
+ wrq:method(Req).
+
+path(Req) ->
+ wrq:path(Req).
+
+uri(Req) ->
+ RawPath = wrq:raw_path(Req),
+ {_, QueryString, _} = mochiweb_util:urlsplit_path(RawPath),
+ QueryString.
+
+peer_ip(_Req) ->
+ throw(unsupported).
+
+peer_port(_Req) ->
+ throw(unsupported).
+
+headers(Req) ->
+ F = fun(Header) -> wrq:get_req_header(Header, Req) end,
+ Headers1 = [
+ {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")},
+ {x_forwarded_for, F("x-forwarded-for")},
+ {transfer_encoding, F("transfer-encoding")}
+ ],
+ [{K, V} || {K, V} <- Headers1, V /= undefined].
+
+cookies(Req) ->
+ wrq:req_cookie(Req).
+
+query_params(Req) ->
+ Value = wrq:req_qs(Req),
+ Value.
+
+post_params(Req) ->
+ Body = wrq:req_body(Req),
+ Value = mochiweb_util:parse_qs(Body),
+ Value.
+
+request_body(Req) ->
+ wrq:req_body(Req).
+
+socket(Req) ->
+ {Socket, _} = Req:socket(),
+ Socket.
+
+recv_from_socket(Length, Timeout, Req) ->
+ Socket = socket(Req),
+ case gen_tcp:recv(Socket, Length, Timeout) of
+ {ok, Data} -> Data;
+ _ -> exit(normal)
+ end.
+
@@ -0,0 +1,45 @@
+%% Simple Bridge
+%% Copyright (c) 2008-2010 Rusty Klophaus
+%% See MIT-LICENSE for licensing information.
+
+-module (webmachine_response_bridge).
+-behaviour (simple_bridge_response).
+-include_lib ("simple_bridge.hrl").
+-export ([build_response/2]).
+
+build_response(Req, Res) ->
+ Code = Res#response.statuscode,
+ case Res#response.data of
+ {data, Body} ->
+ Size = integer_to_list(httpd_util:flatlength(Body)),
+
+ %% Assemble headers...
+ Headers = lists:flatten([
+ {content_length, Size},
+ [{X#header.name, X#header.value} || X <- Res#response.headers],
+ [create_cookie_header(X) || X <- Res#response.cookies]
+ ]),
+
+ Req1 = wrq:set_response_code(Code, Req),
+ Req2 = wrq:set_resp_headers(Headers, Req1),
+ {ok, Body, Req2};
+ {file, Path} ->
+ %% We should really never get here, because Webmachine
+ %% works via a dispatch table mapping routes to
+ %% modules. For this case to happen, it means the dispatch
+ %% table mapped a route to a static file.
+ throw({not_yet_implemented, {file, Path}})
+ end.
+
+create_cookie_header(Cookie) ->
+ SecondsToLive = Cookie#cookie.minutes_to_live * 60,
+ Expire = to_cookie_expire(SecondsToLive),
+ Name = Cookie#cookie.name,
+ Value = Cookie#cookie.value,
+ Path = Cookie#cookie.path,
+ {"Set-Cookie", io_lib:format("~s=~s; Path=~s; Expires=~s", [Name, 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).

0 comments on commit ae873db

Please sign in to comment.