Skip to content

Commit

Permalink
Merge changes from remote master.
Browse files Browse the repository at this point in the history
  • Loading branch information
choptastic committed May 31, 2012
2 parents 9a19a25 + c462679 commit 13ed2af
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/cowboy_bridge_modules/cowboy_request_bridge.erl
Expand Up @@ -42,7 +42,7 @@ path(ReqKey) ->
{Path, Req} = cowboy_http_req:path(Req),
case Path of
[] -> "/";
_ -> b2l(filename:join(Path))
_ -> "/" ++ b2l(filename:join(Path)) %Mochweb returns path as /path and Cowboy does not
end.

uri(ReqKey) ->
Expand All @@ -52,12 +52,16 @@ uri(ReqKey) ->

peer_ip(ReqKey) ->
?GET,
{{IP, _Port, Req}} = cowboy_http_req:peer(Req),
{{IP, _Port}, NewReq} = cowboy_http_req:peer(Req),
NewRequestCache = _RequestCache#request_cache{request=NewReq},
?PUT,
IP.

peer_port(ReqKey) ->
?GET,
{{_IP, Port, Req}} = cowboy_http_req:peer(Req),
{{_IP, Port}, NewReq} = cowboy_http_req:peer(Req),
NewRequestCache = _RequestCache#request_cache{request=NewReq},
?PUT,
Port.

headers(ReqKey) ->
Expand Down
23 changes: 20 additions & 3 deletions src/cowboy_bridge_modules/cowboy_response_bridge.erl
Expand Up @@ -47,8 +47,9 @@ build_response(ReqKey, Res) ->
},
cowboy_request_server:set(ReqKey,NewRequestCache),
{ok,FinReq};

{file, Path} ->


{file, P} ->
%% Note: that this entire {file, Path} section should be avoided
%% as much as possible, since this reads the entire file into
%% memory before sending.
Expand All @@ -60,6 +61,11 @@ build_response(ReqKey, Res) ->
%% See https://github.com/nitrogen/nitrogen/blob/master/rel/overlay/cowboy/etc/cowboy.config
%% and
%% https://github.com/choptastic/nitrogen/blob/master/rel/overlay/cowboy/site/src/nitrogen_sup.erl


% Cowboy path starts with / so we need to remove it
Path = strip_leading_slash(P),

ExpireDate = simple_bridge_util:expires(years, 10),

[$. | Ext] = filename:extension(Path),
Expand All @@ -70,7 +76,9 @@ build_response(ReqKey, Res) ->
{"Content-Type",Mimetype}
],

FullPath = filename:join(DocRoot,Path),
io:format("Serving static file ~p from docroot of ~p ~n",[Path, DocRoot]),

FullPath = filename:join(DocRoot, Path),
{ok, FinReq} = case file:read_file(FullPath) of
{error,enoent} ->
{ok, _R} = send(404,[],[],"Not Found",Req);
Expand All @@ -85,6 +93,15 @@ build_response(ReqKey, Res) ->
{ok, FinReq}
end.


%% Just to strip leading slash, as cowboy tends to do this.
%% If no leading slash, just return the path.
strip_leading_slash([$/ | Path]) ->
Path;
strip_leading_slash(Path) ->
Path.


send(Code,Headers,Cookies,Body,Req) ->
Req1 = prepare_cookies(Req,Cookies),
Req2 = prepare_headers(Req1,Headers),
Expand Down

0 comments on commit 13ed2af

Please sign in to comment.