Tiny tiny Web Server (rserver)

Send Snippet To: Save this snippet to Code Collector Pro -- view all greg's snippets
language: Erlang
license: MIT

Code for Snippet:

                
% Copyright (c) 2008 Gregoire Lejeune
%  
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to
% deal in the Software without restriction, including without limitation the
% rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
% sell copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%   
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%    
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
% THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
% IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
% CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-module(rserver).
-author('[email protected]').
-compile(export_all).
 
-define(RSERVER_VERSION, "0.0.2").
 
start(Port, DocRoot) ->
  {ok, ListenSocket} = gen_tcp:listen(Port, [binary, {packet, 0}, {active, false}]),
  io:format("** RServer version ~s Started (port ~p | root ~p)~n", [?RSERVER_VERSION, Port, DocRoot]),
  loop(ListenSocket, DocRoot).
start() ->
  case file:consult("rserver.conf") of
    {ok, C} ->
      io:format("** Read config from ./rserver.conf~n"),
      [{port, P},{rootdir, D}] = C,
      start(P, D);
    {error, _} ->
      case file:consult(string:concat(os:getenv("HOME"), "/rserver.conf")) of
        {ok, C} ->
          io:format("** Read config from $HOME/rserver.conf~n"),
          [{port, P},{rootdir, D}] = C,
          start(P, D);
        {error, _} ->
          io:format("Error: no configuration file found!")
      end
  end.
 
loop(ListenSocket, DocRoot) ->
  case gen_tcp:accept(ListenSocket) of
    {ok, Socket} -> 
      spawn(fun() -> 
        handle_connection(Socket, DocRoot) 
      end),
      loop(ListenSocket, DocRoot);
    {error, Reason} ->
      io:format("Error: ~p~n", [Reason])
  end.
 
handle_connection(Socket, DocRoot) ->
  try communication(Socket, DocRoot)
  catch
    error:Reason ->
      {gen_tcp:send(Socket, io_lib:format("Error: ~p~n", [Reason]))}
  end,
  ok = gen_tcp:close(Socket).
 
communication(Socket, DocRoot) ->
  {ok, Binary} = gen_tcp:recv(Socket, 0),
  {ok, [R|Z]} = regexp:split( binary_to_list(Binary), "\r\n" ),
  {ok, [M,F,P]} = regexp:split( R, " " ),
  case F =:= "/" of 
    true -> File = string:concat(DocRoot, "/index.html");
    false -> File = string:concat(DocRoot, F)
  end,
  {WD, JJ, MD, AA, HH, MN, SS} = get_local_time(),
  case file:read_file(File) of
    {ok, Html} ->
      io:format("~p - - [~s, ~p ~s ~p ~p:~p:~p GMT] \"~s ~s ~s\" 200 ~p~n", ["IP", WD, JJ, MD, AA, HH, MN, SS, M, F, P, string:len(binary_to_list(Html))]),
      gen_tcp:send(Socket, io_lib:format("HTTP/1.1 200 OK~nDate: ~s, ~p ~s ~p ~p:~p:~p GMT~nServer: Rserver/1.0.0~n~n~s", [WD, JJ, MD, AA, HH, MN, SS, binary_to_list(Html)]));
    {error, _} ->
      io:format("[~s, ~p ~s ~p ~p:~p:~p GMT] 404 Error : ~s ~s not found~n", [WD, JJ, MD, AA, HH, MN, SS, M, F]),
      gen_tcp:send(Socket, io_lib:format("HTTP/1.1 404 Not Found~nDate: ~s, ~p ~s ~p ~p:~p:~p GMT~nServer: Rserver/1.0.0~n~n~s", [WD, JJ, MD, AA, HH, MN, SS, "<html><body>404 error</body></html>"]))
  end.  
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
day(0) -> "Mon"; 
day(1) -> "Tue";
day(2) -> "Wed";
day(3) -> "Thu";
day(4) -> "Fri";
day(5) -> "Sat";
day(6) -> "Sun".
 
month(1) -> "Jan";
month(2) -> "Feb";
month(3) -> "Mar";
month(4) -> "Apr";
month(5) -> "May";
month(6) -> "Jun";
month(7) -> "Jul";
month(8) -> "Aug";
month(9) -> "Sep";
month(10) -> "Oct";
month(11) -> "Nov";
month(12) -> "Dec".
 
get_local_time() ->
  D = calendar:local_time(),
  [{{AA,MM,JJ},{HH,MN,SS}}] = calendar:local_time_to_universal_time_dst(D),
  WD = day(calendar:day_of_the_week(AA, MM, JJ)),
  MD = month(MM),
  {WD, JJ, MD, AA, HH, MN, SS}.
comments powered by Disqus

Info

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 581
Date Added: 2008-10-16 21:56:16
Last Modified: 0000-00-00 00:00:00

Web Analytics