Permalink
Browse files

Merge branch 'master' into mainline

  • Loading branch information...
2 parents 0254be4 + ea7d7e9 commit 6db59ccb55385745834fc8eba58573e5d84c0fe0 @choptastic choptastic committed May 31, 2012
@@ -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) ->
@@ -47,26 +47,38 @@ build_response(ReqKey, Res) ->
},
cowboy_request_server:set(ReqKey,NewRequestCache),
{ok,FinReq};
-
- {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),
+
+
+ {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.
+ %%
+ %% You want to make sure that cowboy.config is properly set
+ %% up with paths so that the requests for static files are
+ %% properly handled by cowboy directly.
+ %%
+ %% 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),
Mimetype = mimetypes:extension(Ext),
- %% Create the response telling Mochiweb to serve the file...
Headers = [
{"Expires", ExpireDate},
{"Content-Type",Mimetype}
],
- io:format("Serving static file ~p~n",[Path]),
+ io:format("Serving static file ~p from docroot of ~p ~n",[Path, DocRoot]),
- FullPath = filename:join(DocRoot,Path),
+ FullPath = filename:join(DocRoot, Path),
{ok, FinReq} = case file:read_file(FullPath) of
{error,enoent} ->
{ok, _R} = send(404,[],[],"Not Found",Req);
@@ -81,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),
@@ -5,15 +5,11 @@
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
+ headers/1, header/2, cookies/1,
+ query_params/1, post_params/1, request_body/1,
+ socket/1, recv_from_socket/3
]).
-
-%% @todo could not figure out how to get the socket from misultin
-%% so could not implement socket/1, recv_from_socket/3 that are
-%% present in other request modules
-
init(Req) ->
Req.
@@ -28,36 +24,70 @@ uri(Req) ->
Req:get(uri).
peer_ip(Req) ->
- Req:get(peer_addr).
+ case Req:get(peer_addr) of
+ {ok, IP} -> IP;
+ IP -> IP
+ end.
peer_port(Req) ->
Req:get(peer_port).
+header(connection, Req) ->
+ misultin_utility:get_key_value('Connection', Req:get(headers));
+header(accept, Req) ->
+ misultin_utility:get_key_value('Accept', Req:get(headers));
+header(host, Req) ->
+ misultin_utility:get_key_value('Host', Req:get(headers));
+header(if_modified_since, Req) ->
+ misultin_utility:get_key_value('If-Modified-Since', Req:get(headers));
+header(if_match, Req) ->
+ misultin_utility:get_key_value('If-Match', Req:get(headers));
+header(if_none_match, Req) ->
+ misultin_utility:get_key_value('If-None-Match', Req:get(headers));
+header(if_range, Req) ->
+ misultin_utility:get_key_value('If-Range', Req:get(headers));
+header(if_unmodified_since, Req) ->
+ misultin_utility:get_key_value('If-Unmodified-Since', Req:get(headers));
+header(range, Req) ->
+ misultin_utility:get_key_value('Range', Req:get(headers));
+header(referer, Req) ->
+ misultin_utility:get_key_value('Referer', Req:get(headers));
+header(user_agent, Req) ->
+ misultin_utility:get_key_value('User-Agent', Req:get(headers));
+header(accept_ranges, Req) ->
+ misultin_utility:get_key_value('Accept-Ranges', Req:get(headers));
+header(cookie, Req) ->
+ misultin_utility:get_key_value('Cookie', Req:get(headers));
+header(keep_alive, Req) ->
+ misultin_utility:get_key_value('Keep-Alive', Req:get(headers));
+header(location, Req) ->
+ misultin_utility:get_key_value('Location', Req:get(headers));
+header(content_length, Req) ->
+ misultin_utility:get_key_value('Content-Length', Req:get(headers));
+header(content_type, Req) ->
+ misultin_utility:get_key_value('Content-Type', Req:get(headers));
+header(content_encoding, Req) ->
+ misultin_utility:get_key_value('Content-Encoding', Req:get(headers));
+header(authorization, Req) ->
+ misultin_utility:get_key_value('Authorization', Req:get(headers));
+header(x_forwarded_for, Req) ->
+ misultin_utility:get_key_value('X-Forwarded-For', Req:get(headers));
+header(transfer_encoding, Req) ->
+ misultin_utility:get_key_value('Transfer-Encoding', Req:get(headers));
+header(accept_language, Req) ->
+ misultin_utility:get_key_value('Accept-Language', Req:get(headers));
+header(Header, Req) ->
+ misultin_utility:get_key_value(Header, Req:get(headers)).
+
headers(Req) ->
- Headers = Req:get(headers),
- F = fun(Header) -> proplists:get_value(Header, Headers) 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')},
- {transfer_encoding, F('Transfer-Encoding')}
+ Headers1 = [ connection, accept, host, if_modified_since,
+ if_match, if_none_match, if_range, if_unmodified_since,
+ range, referer, user_agent, accept_language, accept_ranges, cookie,
+ keep_alive, location, content_length, content_type,
+ content_encoding, authorization, x_forwarded_for, transfer_encoding
],
- [{K, V} || {K, V} <- Headers1, V /= undefined].
+ Headers2 = lists:map(fun(H) -> {H, header(H, Req)} end, Headers1),
+ [{K, V} || {K, V} <- Headers2, V /= undefined].
cookies(Req) ->
Headers = headers(Req),
@@ -81,3 +111,15 @@ post_params(Req) ->
request_body(Req) ->
Req:get(body).
+
+socket(Req) ->
+ Req:get(socket).
+
+recv_from_socket(Length, Timeout, Req) ->
+ Socket = socket(Req),
+ case gen_tcp:recv(Socket, Length, Timeout) of
+ {ok, Data} ->
+ Data;
+ _Other ->
+ exit(normal)
+ end.
@@ -1,9 +1,12 @@
-module (misultin_response_bridge).
-behaviour (simple_bridge_response).
-include_lib ("simple_bridge.hrl").
--export ([build_response/2]).
+-export ([build_response/2,init/1]).
-build_response(Req, Res) ->
+init({Req,DocRoot}) ->
+ {Req,DocRoot}.
+
+build_response({Req, DocRoot}, Res) ->
% Some values...
Code = Res#response.statuscode,
case Res#response.data of
@@ -18,7 +21,7 @@ build_response(Req, Res) ->
% Send the misultin response...
Req:respond(Code, Headers, Body);
{file, Path} ->
- Req:file([Path])
+ Req:file([DocRoot, Path])
end.
create_cookie_header(Cookie) ->
Oops, something went wrong.

0 comments on commit 6db59cc

Please sign in to comment.