Permalink
Browse files

Serving files instead of returning not_found for static files.

  • Loading branch information...
1 parent 8d93f3f commit 1c5956cb807a317d703f69d9fac806d952ba9c5f roberto.aloi committed Nov 25, 2009
Showing with 54 additions and 2 deletions.
  1. +17 −1 lib/eptic-1.4/ebin/eptic.app
  2. +37 −1 lib/eptic-1.4/src/e_mod_ewgi.erl
@@ -13,7 +13,23 @@
{env, [
{upload_dir, "/tmp"},
{template_root, "templates"},
- {node_type, single_node}
+ {node_type, single_node},
+ {mime_types,[{"xml","text/xml"},
+ {"txt","text/plain"},
+ {"html","text/html"},
+ {"htm","text/html"},
+ {"css","text/css"},
+ {"png","image/png"},
+ {"jpeg","image/jpeg"},
+ {"jpg","image/jpeg"},
+ {"jpe","image/jpeg"},
+ {"gif","image/gif"},
+ {"bmp","image/bmp"},
+ {"zip","application/zip"},
+ {"tar","application/x-tar"},
+ {"js","application/x-javascript"},
+ {"pdf","application/pdf"},
+ {"ico","image/x-icon"}]}
]},
{mod, {eptic, []}}
]}.
@@ -26,6 +26,7 @@
-include_lib("eptic/include/e_mod_ewgi.hrl").
do(#ewgi_context{request = Request} = Context) ->
+ erlang:display({do_request, Request}),
e_logger:register_pid(self()),
case handle_args(Request) of
{ok, Args} ->
@@ -81,10 +82,36 @@ do(#ewgi_context{request = Request} = Context) ->
e_mod_inets:cleanup(),
e_logger:unregister_pid(self()),
- not_found
+ Response = serve_file(Request),
+ Context#ewgi_context{response = Response}
end
end.
+-spec serve_file(#ewgi_request{}) ->
+ #ewgi_response{}.
+serve_file(#ewgi_request{path_info = Path,
+ request_method = Method}) ->
+ case Method of
+ Method when Method =:= 'GET'; Method =:= 'HEAD' ->
+ Suffix = httpd_util:suffix(Path),
+ MimeType = lookup_mime_default(Suffix, "application/octet-stream"),
+ % XXX: do you know how much memory this could potentially use?
+ {ok, Data} = file:read_file("docroot"++Path),
+ #ewgi_response{
+ status = {200, "Ok"},
+ headers = [{"Content-type", MimeType}],
+ message_body = Data
+ };
+ 'POST' ->
+ not_found;
+ _Else ->
+ #ewgi_response{
+ status = {501, "Not Implemented"},
+ headers = [{"Content-type", "text/plain"}],
+ message_body = [<<"Bad method">>]
+ }
+ end.
+
parse_headers(Headers) ->
ParsedHeaders = [
{"accept", Headers#ewgi_http_headers.http_accept},
@@ -176,3 +203,12 @@ with_formatted_error(F) ->
format_response(Response)
end.
+lookup_mime_default(Suffix, Undefined) ->
+ {ok, MimeTypesDB} = application:get_env(eptic, mime_types),
+ MimeType = proplists:lookup(Suffix, MimeTypesDB),
+ case MimeType of
+ none ->
+ Undefined;
+ {Suffix, MT} ->
+ MT
+ end.

0 comments on commit 1c5956c

Please sign in to comment.