Permalink
Browse files

Merge branch 'master' of git://github.com/etnt/SimpleBridge

Conflicts:
	src/yaws_bridge_modules/yaws_request_bridge.erl
  • Loading branch information...
2 parents 97f03f0 + 1d10978 commit 954ba819ddd78434c456ae1cb7aed0cae6c57c8e @choptastic committed Aug 11, 2011
@@ -68,6 +68,7 @@
content_encoding,
authorization,
transfer_encoding,
+ x_forwarded_for,
other = [] %% misc other headers
}).
@@ -3,7 +3,6 @@
% See MIT-LICENSE for licensing information.
-module(yaws_request_bridge).
--include_lib ("yaws_api.hrl").
-include_lib ("simple_bridge.hrl").
-export ([
init/1,
@@ -18,14 +17,13 @@ init(Req) ->
Req.
request_method(Arg) ->
- (Arg#arg.req)#http_request.method.
+ yaws_api:http_request_method(yaws_api:arg_req(Arg)).
path(Arg) ->
- Arg#arg.server_path.
+ yaws_api:arg_server_path(Arg).
uri(Arg) ->
- Req = Arg#arg.req,
- {abs_path, Path} = Req#http_request.path,
+ {abs_path, Path} = yaws_api:http_request_path(yaws_api:arg_req(Arg)),
Path.
peer_ip(Arg) ->
@@ -58,40 +56,48 @@ atomize_header(Header) when is_list(Header) ->
list_to_atom(lists:map(LowerUnderscore,Header)).
headers(Arg) ->
- Headers = Arg#arg.headers,
- Others = [{atomize_header(Header),Value} || {http_header,_Num,Header,_,Value} <- Headers#headers.other],
-
- Others ++ [
- {connection, Headers#headers.connection},
- {accept, Headers#headers.accept},
- {host, Headers#headers.host},
- {if_modified_since, Headers#headers.if_modified_since},
- {if_match, Headers#headers.if_match},
- {if_none_match, Headers#headers.if_none_match},
- {if_range, Headers#headers.if_range},
- {if_unmodified_since, Headers#headers.if_unmodified_since},
- {range, Headers#headers.range},
- {referer, Headers#headers.referer},
- {user_agent, Headers#headers.user_agent},
- {accept_ranges, Headers#headers.accept_ranges},
- {cookie, Headers#headers.cookie},
- {keep_alive, Headers#headers.keep_alive},
- {location, Headers#headers.location},
- {content_length, Headers#headers.content_length},
- {content_type, Headers#headers.content_type},
- {content_encoding, Headers#headers.content_encoding},
- {authorization, Headers#headers.authorization},
- {transfer_encoding, Headers#headers.transfer_encoding}
- ].
+ Headers = yaws_api:arg_headers(Arg),
+
+ ReparedHeaders = [
+ {connection, yaws_api:headers_connection(Headers)},
+ {accept, yaws_api:headers_accept(Headers)},
+ {host, yaws_api:headers_host(Headers)},
+ {if_modified_since, yaws_api:headers_if_modified_since(Headers)},
+ {if_match, yaws_api:headers_if_match(Headers)},
+ {if_none_match, yaws_api:headers_if_none_match(Headers)},
+ {if_range, yaws_api:headers_if_range(Headers)},
+ {if_unmodified_since, yaws_api:headers_if_unmodified_since(Headers)},
+ {range, yaws_api:headers_range(Headers)},
+ {referer, yaws_api:headers_referer(Headers)},
+ {user_agent, yaws_api:headers_user_agent(Headers)},
+ {accept_ranges, yaws_api:headers_accept_ranges(Headers)},
+ {cookie, yaws_api:headers_cookie(Headers)},
+ {keep_alive, yaws_api:headers_keep_alive(Headers)},
+ {location, yaws_api:headers_location(Headers)},
+ {content_length, yaws_api:headers_content_length(Headers)},
+ {content_type, yaws_api:headers_content_type(Headers)},
+ {content_encoding, yaws_api:headers_content_encoding(Headers)},
+ {authorization, yaws_api:headers_authorization(Headers)},
+ {transfer_encoding, yaws_api:headers_transfer_encoding(Headers)},
+ {x_forwarded_for, yaws_api:headers_x_forwarded_for(Headers)}
+ ],
+
+
+ %% Get the other headers and format them to fit the paradigm we're using above
+ Others = yaws_api:headers_other(Headers),
+ Others2 = [{atomize_header(Header),Value} || {http_header,_Num,Header,_,Value} <- Others],
+
+ %% Prepend the "other headers" since, it's probably shorter than the normal headers
+ Others2 ++ PreparedHeaders.
cookie(Key, Req) ->
Key1 = wf:to_list(Key),
- Headers = Req#arg.headers,
- yaws_api:find_cookie_val(Key1, Headers#headers.cookie).
+ Headers = yaws_api:arg_headers(Req),
+ yaws_api:find_cookie_val(Key1, yaws_api:headers_cookie(Headers)).
cookies(Req) ->
- Headers = Req#arg.headers,
- CookieList = Headers#headers.cookie,
+ Headers = yaws_api:arg_headers(Req),
+ CookieList = yaws_api:headers_cookie(Headers),
F = fun(Cookie) ->
Key = hd(string:tokens(Cookie, "=")),
Val = yaws_api:find_cookie_val(Key, [Cookie]),
@@ -106,13 +112,13 @@ post_params(Arg) ->
yaws_api:parse_post(Arg).
request_body(Arg) ->
- case Arg#arg.clidata of
+ case yaws_api:arg_clidata(Arg) of
{partial, Data} -> Data;
Data -> Data
end.
socket(Arg) ->
- Arg#arg.clisock.
+ yaws_api:arg_clisock(Arg).
recv_from_socket(Length, Timeout, Arg) ->
Socket = socket(Arg),
@@ -4,50 +4,63 @@
-module(yaws_response_bridge).
-behaviour (simple_bridge_response).
--include_lib ("yaws_api.hrl").
-include_lib ("simple_bridge.hrl").
-export ([build_response/2]).
build_response(_Arg, Res) ->
% Get vars...
Code = Res#response.statuscode,
+
+ %% Assemble headers...
+ Headers = assemble_headers(Res),
+
+ %% Get the content type...
+ ContentType = get_content_type(Res),
+
case Res#response.data of
{data, Body} ->
- % Assemble headers...
- Headers = lists:flatten([
- [{header, {X#header.name, X#header.value}} || X <- Res#response.headers],
- [create_cookie(X) || X <- Res#response.cookies]
- ]),
-
- % Get the content type...
- ContentType = coalesce([
- kvl3(content_type, Res#response.headers),
- kvl3("content-type", Res#response.headers),
- kvl3("Content-Type", Res#response.headers),
- kvl3("CONTENT-TYPE", Res#response.headers),
- "text/html"
- ]),
% Send the yaws response...
lists:flatten([
- {status, Code},
- Headers,
- {content, ContentType, Body}
- ]);
+ {status, Code},
+ Headers,
+ {content, ContentType, Body}
+ ]);
{file, Path} ->
+
%% Calculate expire date far into future...
Seconds = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
TenYears = 10 * 365 * 24 * 60 * 60,
Seconds1 = calendar:gregorian_seconds_to_datetime(Seconds + TenYears),
ExpireDate = httpd_util:rfc1123_date(Seconds1),
- % Create the response telling Yaws to server file...
- Options = [{header, {"Expires", ExpireDate}}],
- Path = filename:join(".", Path),
- {page, {Options, Path}}
+ %% Get the file content
+ {ok,Bin} = file:read_file(Path),
+
+ %% Send the yaws response...
+ lists:flatten([
+ {status, Code},
+ [{header, {"Expires", ExpireDate}} | Headers],
+ {content, ContentType, Bin}
+ ])
end.
+assemble_headers(Res) ->
+ lists:flatten([
+ [{header, {X#header.name, X#header.value}} || X <- Res#response.headers],
+ [create_cookie(X) || X <- Res#response.cookies]
+ ]).
+
+get_content_type(Res) ->
+ coalesce([
+ kvl3(content_type, Res#response.headers),
+ kvl3("content-type", Res#response.headers),
+ kvl3("Content-Type", Res#response.headers),
+ kvl3("CONTENT-TYPE", Res#response.headers),
+ "text/html"
+ ]).
+
kvl3(Key,L) ->
case lists:keysearch(Key,2,L) of
{value, {_,_,Val}} -> Val;

0 comments on commit 954ba81

Please sign in to comment.