ehttp / src / ehttp.erl

%%%-------------------------------------------------------------------
%%% @author Torbjorn Tornkvist <tobbe@tornkvist.org>
%%% @copyright (C) 2010, Torbjorn Tornkvist
%%% @doc Bla bla
%%%
%%% @end
%%%-------------------------------------------------------------------
-module(ehttp).
-export([start/0, stop/0, do/1]).
-include("httpd.hrl").

-define(DEFAULT_PORT, 8081).

start() ->
    inets_start().

stop() ->
    RegName = list_to_atom("httpd_instance_sup_"++
                           integer_to_list(?DEFAULT_PORT)),
    inets_stop(whereis(RegName)).
   
inets_start() ->
    inets:start(),
    {ok, CWD} = file:get_cwd(),
    io:format("CWD=~p~n",[CWD]),
    {ok, Pid} = 
        inets:start(httpd, 
                    [{port, ?DEFAULT_PORT},
                     {server_name, "ehttp"},
                     {server_root, CWD},
                     {document_root, CWD},
%                     {script, {"GET", "hgwebdir.cgi"}},
                     {script_alias, {"/hgweb.cgi", CWD++"/hgweb.cgi"}},
                     {modules, [?MODULE, mod_cgi, mod_dir, mod_get, mod_head]}
%                     {modules, [?MODULE,mod_get,mod_head]}
%                     {modules, [mod_get, mod_head, mod_log]]},
%                     {mime_types, [{"css", "text/css"}, 
%                                   {"js", "text/javascript"}, 
%                                   {"html", "text/html"}]}
                    ]),
    link(Pid),
    {ok, Pid}.

inets_stop(Pid) ->
    inets:stop(httpd, Pid).

do(Info) ->
    case proplists:get_value(status, Info#mod.data) of
        %% A status code has been generated!
        {_StatusCode, _PhraseArgs, _Reason} ->
            {proceed,Info#mod.data};
        %% No status code has been generated!
        undefined ->
            case proplists:get_value(response, Info#mod.data) of
                %% No response has been generated!
                undefined ->
                    Path = Info#mod.request_uri,
                    case re:run(Path, "hgweb.cgi", []) of
                        {match,_} -> 
                            NewPath = re:replace(Path, 
                                                 "(.*hgweb.cgi)(.*)(/static.*)", 
                                                 "\\1\\3", 
                                                 [{return, list}, global]),
                            {proceed, [{new_request_uri, NewPath}
                                       | Info#mod.data]};
                        _ ->
                            {proceed, Info#mod.data} 
                    end
            end;
	%% A response has been generated or sent!
	_Response ->
            {proceed, Info#mod.data}
    end.
	
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.