Anonymous avatar Anonymous committed cbea43e

Polished the code a bit.

Comments (0)

Files changed (1)

 %%%-------------------------------------------------------------------
 %%% @author Torbjorn Tornkvist <tobbe@tornkvist.org>
 %%% @copyright (C) 2010, Torbjorn Tornkvist
-%%% @doc Bla bla
+%%% @doc Repository browser.
 %%%
 %%% @end
 %%%-------------------------------------------------------------------
 
 -define(DEFAULT_PORT, 8081).
 
+%-define(dbg(Fs,As), true).
+-define(dbg(Fs,As), io:format("~p(~p): "++Fs, [?MODULE,?LINE|As])).
+
 start() ->
     inets_start().
 
 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, "localhost"},
+                     {server_name, hostname()},
                      {server_root, CWD},
                      {document_root, CWD},
-%                     {script, {"GET", "hgwebdir.cgi"}},
+                     {alias, {"/icons", inets_icons_dir()}},
                      {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]]},
+                     {modules, [?MODULE, mod_cgi, mod_alias, 
+                                mod_dir, mod_get, mod_head]}
 %                     {mime_types, [{"css", "text/css"}, 
 %                                   {"js", "text/javascript"}, 
 %                                   {"html", "text/html"}]}
     link(Pid),
     {ok, Pid}.
 
+hostname() ->
+    {ok,Host} = inet:gethostname(),
+    Host.
+
+inets_icons_dir() ->
+     [$/|filename:join(
+           lists:reverse(
+             ["icons","server_root","examples"
+              |tl(tl(lists:reverse(
+                       string:tokens(
+                         code:which(httpd),"/"))))]))].
+
 inets_stop(Pid) ->
     inets:stop(httpd, Pid).
 
 do(Info) ->
     try dodo(Info)
-    catch _:Err -> io:format("Err(~p): ~p~n", [Err, erlang:get_stacktrace()]) end.
+    catch _:Err -> io:format("Error(~p): ~p~n", 
+                             [Err, erlang:get_stacktrace()]) 
+    end.
 
 dodo(Info) ->
     case proplists:get_value(status, Info#mod.data) of
                 undefined ->
                     Path = Info#mod.request_uri,
                     case re:run(Path, "hgweb.cgi", []) of
-                        {match,_} -> 
-                            io:format("Path=~p~n",[Path]),
-                            case do_rewrite(Path) of
-                                Path ->
-                                    io:format("No new Path=~p~n",[Path]),
-                                    {proceed, Info#mod.data};
-                                NewPath ->
-                                    io:format("NewPath=~p~n",[NewPath]),
-                                    ServerName = httpd_util:lookup(Info#mod.config_db, server_name),
-                                    io:format("ServerName=~p~n",[ServerName]),
-                                    URL = "http://" ++ ServerName ++ ":" ++ i2l(?DEFAULT_PORT) ++ NewPath,
-                                    io:format("URL=~p~n",[URL]),
-                                    ReasonPhrase = httpd_util:reason_phrase(301),
-                                    Message = httpd_util:message(301, URL, Info#mod.config_db),
-                                    {proceed,
-                                     [{response, {301, 
-                                                  ["Location: ", URL, "\r\n",
-                                                   "Content-Type: text/html\r\n",
-                                                   "\r\n",
-                                                   "<HTML>\n<HEAD>\n<TITLE>",ReasonPhrase,
-                                                   "</TITLE>\n</HEAD>\n"
-                                                   "<BODY>\n<H1>",ReasonPhrase,
-                                                   "</H1>\n", Message, 
-                                                   "\n</BODY>\n</HTML>\n"]}}
-                                      | Info#mod.data]}
-                            end;
-                        _ ->
-                            {proceed, Info#mod.data} 
+                        {match,_} -> maybe_hgweb_rewrite(Info, Path);
+                        _         -> {proceed, Info#mod.data} 
                     end
             end;
 	%% A response has been generated or sent!
 	_Response ->
             {proceed, Info#mod.data}
     end.
+
+maybe_hgweb_rewrite(Info, Path) ->
+    case hgweb_rewrite(Path) of
+        Path ->
+            {proceed, Info#mod.data};
+        NewPath ->
+            ServerName = httpd_util:lookup(Info#mod.config_db, server_name),
+            URL = "http://" ++ ServerName ++ ":" ++ i2l(?DEFAULT_PORT) ++ NewPath,
+            ReasonPhrase = httpd_util:reason_phrase(301),
+            Message = httpd_util:message(301, URL, Info#mod.config_db),
+            {proceed,
+             [{response, {301, 
+                          ["Location: ", URL, "\r\n",
+                           "Content-Type: text/html\r\n",
+                           "\r\n",
+                           "<HTML>\n<HEAD>\n<TITLE>",ReasonPhrase,
+                           "</TITLE>\n</HEAD>\n"
+                           "<BODY>\n<H1>",ReasonPhrase,
+                           "</H1>\n", Message, 
+                           "\n</BODY>\n</HTML>\n"]}}
+              | Info#mod.data]}
+    end.
 	
+hgweb_rewrite(Path) ->
+    hgweb_rewrite(Path,
+                  [fun  hg_static_rewrite/1
+                   ,fun hg_branches_rewrite/1
+                   ,fun hg_graph_rewrite/1
+                   ,fun hg_tags_rewrite/1
+                   ,fun hg_shortlog_rewrite/1
+                   ,fun hg_rev_rewrite/1
+                   ,fun hg_file_rewrite/1
+                  ]).
 
-do_rewrite(Path) ->
-    do_rewrite(Path,
-               [fun static_rewrite/1
-                ,fun branches_rewrite/1
-                ,fun graph_rewrite/1
-                ,fun tags_rewrite/1
-                ,fun shortlog_rewrite/1
-                ]).
-
-do_rewrite(Path, [])     -> Path;
-do_rewrite(Path, [F|Fs]) ->
+hgweb_rewrite(Path, [])     -> Path;
+hgweb_rewrite(Path, [F|Fs]) ->
     case F(Path) of
-        Path    -> do_rewrite(Path, Fs);
+        Path    -> hgweb_rewrite(Path, Fs);
         NewPath -> NewPath
     end.
 
-static_rewrite(Path) ->
-    re:replace(Path, 
-               "(.*hgweb.cgi)(.*)(/static.*)", 
-               "\\1\\3", 
-               [{return, list}, global]).
+hg_static_rewrite(Path) ->
+    r(Path, "(.*hgweb.cgi)(.*)(/static.*)", "\\1\\3").
 
-branches_rewrite(Path) ->
-    re:replace(Path, 
-               "(.*hgweb.cgi)(.*)(/branches.*)", 
-               "\\1\\3", 
-               [{return, list}, global]).
+hg_branches_rewrite(Path) ->
+    r(Path, "(.*hgweb.cgi)(.*)(/branches.*)", "\\1\\3").
 
-graph_rewrite(Path) ->
-    re:replace(Path, 
-               "(.*hgweb.cgi)(.*)(/graph.*)", 
-               "\\1\\3", 
-               [{return, list}, global]).
+hg_graph_rewrite(Path) ->
+    r(Path, "(.*hgweb.cgi)(.*)(/graph.*)", "\\1\\3").
 
-tags_rewrite(Path) ->
-    re:replace(Path, 
-               "(.*hgweb.cgi)(.*)(/tags.*)", 
-               "\\1\\3", 
-               [{return, list}, global]).
+hg_tags_rewrite(Path) ->
+    r(Path, "(.*hgweb.cgi)(.*)(/tags.*)", "\\1\\3").
 
-shortlog_rewrite(Path) ->
-    re:replace(Path, 
-               "(.*hgweb.cgi)(.*)(/shortlog)(.*)", 
-               "\\1\\4", 
-               [{return, list}, global]).
+hg_shortlog_rewrite(Path) ->
+    r(Path, "(.*hgweb.cgi)(.*)(/shortlog)(.*)", "\\1\\4").
 
+hg_rev_rewrite(Path) ->
+    r(Path, "(.*hgweb.cgi)(.*)(/rev.*)", "\\1\\3").
+
+hg_file_rewrite(Path) ->
+    r(Path, "(.*hgweb.cgi)(.*)(/file.*)", "\\1\\3").
+
+r(Path, RegExp, Return) ->
+    re:replace(Path, RegExp, Return, [{return, list}, global]).
 
 i2l(I) when is_integer(I) -> integer_to_list(I).
 
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.