Skip to content

Commit

Permalink
Fix header handling: Expires should be set elsewhere, and header
Browse files Browse the repository at this point in the history
values for files should not be discarded.
  • Loading branch information
davidw authored and choptastic committed Mar 29, 2013
1 parent e8f42d8 commit 4e1d981
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/mochiweb_bridge_modules/mochiweb_response_bridge.erl
Expand Up @@ -11,33 +11,33 @@
init({Req,DocRoot}) ->
{Req,DocRoot}.

build_response({Req, DocRoot}, Res) ->
build_response({Req, DocRoot}, Res) ->
% Some values...
Code = Res#response.statuscode,
case Res#response.data of
{data, Body} ->
Code = Res#response.statuscode,

% Assemble headers...
Headers = lists:flatten([
[{X#header.name, X#header.value} || X <- Res#response.headers],
[create_cookie_header(X) || X <- Res#response.cookies]
]),
%% Assemble headers...
Headers = lists:flatten([
[{X#header.name, X#header.value} || X <- Res#response.headers],
[create_cookie_header(X) || X <- Res#response.cookies]
]),

% Ensure content type...
F = fun(Key) -> lists:keymember(Key, 1, Headers) end,
HasContentType = lists:any(F, ["content-type", "Content-Type", "CONTENT-TYPE"]),
Headers2 = case HasContentType of
true -> Headers;
false -> [{"Content-Type", "text/html"}|Headers]
end,
%% Ensure content type...
F = fun(Key) ->
lists:keymember(Key, 1, Headers)
end,
HasContentType = lists:any(F, ["content-type", "Content-Type", "CONTENT-TYPE"]),
Headers2 = case HasContentType of
true -> Headers;
false -> [{"Content-Type", "text/html"}|Headers]
end,

case Res#response.data of
{data, Body} ->
% Send the mochiweb response...
Req:respond({Code, Headers2, Body});
{file, Path} ->
ExpireDate = simple_bridge_util:expires(years, 10),
%% Create the response telling Mochiweb to serve the file...
Headers = [{"Expires", ExpireDate}],
Req:serve_file(tl(Path), DocRoot, Headers)
Req:serve_file(tl(Path), DocRoot, Headers2)
end.

create_cookie_header(Cookie) ->
Expand Down

0 comments on commit 4e1d981

Please sign in to comment.