发新话题
打印

用Erlang实现一个简单的Web服务器

用Erlang实现一个简单的Web服务器

  用Erlang实现一个简单的Web服务器

新建一个httpd.erl文件,内容如下,

%% httpd.erl - MicroHttpd
-module(httpd).
-author("ninhenry@gmail.com").

-export([start/0,start/1,start/2,process/2]).
-import(regexp,[split/2]).

-define(defPort,6888).
-define(docRoot,"public").

start() -> start(?defPort,?docRoot).
start(Port) -> start(Port,?docRoot).  
start(Port,DocRoot) ->
   case gen_tcp:listen(Port, [binary,{packet, 0},{active, false}]) of
       {ok, LSock}        -> server_loop(LSock,DocRoot);
       {error, Reason}        -> exit({Port,Reason})
   end.

%% main server loop - wait for next connection, spawn child to process it
server_loop(LSock,DocRoot) ->
   case gen_tcp:accept(LSock) of
       {ok, Sock}        ->
           spawn(?MODULE,process,[Sock,DocRoot]),
           server_loop(LSock,DocRoot);
       {error, Reason}        ->
           exit({accept,Reason})
   end.

%% process current connection
process(Sock,DocRoot) ->
   Req = do_recv(Sock),
   {ok,[Cmd|[Name|[Vers|_]]]} = split(Req,"[ \r\n]"),
   FileName = DocRoot ++ Name,
   LogReq = Cmd ++ " " ++ Name ++ " " ++ Vers,
   Resp = case file:read_file(FileName) of
       {ok, Data}        ->
           io:format("~p ~p ok~n",[LogReq,FileName]),
           Data;
       {error, Reason}        ->
           io:format("~p ~p failed ~p~n",[LogReq,FileName,Reason]),
           error_response(LogReq,file:format_error(Reason))
   end,
   do_send(Sock,Resp),
   gen_tcp:close(Sock).

%% construct HTML for failure message
error_response(LogReq,Reason) ->
   "<html><head><title>Request Failed</title></head><body>\n" ++
   "<h1>Request Failed</h1>\n" ++ "Your request to " ++ LogReq ++
   " failed due to: " ++ Reason ++ "\n</body></html>\n".

%% send a line of text to the socket
do_send(Sock,Msg) ->
   case gen_tcp:send(Sock, Msg) of
       ok                -> ok;
       {error, Reason}        -> exit(Reason)
   end.

%% receive data from the socket
do_recv(Sock) ->
   case gen_tcp:recv(Sock, 0) of
       {ok, Bin}        -> binary_to_list(Bin);
       {error, closed}        -> exit(closed);
       {error, Reason}        -> exit(Reason)
   end.

在当前目录新建一个public目录,里面方个index.html,内容为
Hello world.

启动erl,运行"c(httpd)."
然后退出erl,重新进入erl,运行"httpd:start()."
访问http://localhost:6888/index.html

OK,一个简单的Web服务器完成了,这个功能还十分简单。。。

原帖:
http://cn.udclub.com/forum/viewt ... &extra=page%3D1
*
* WELCOME to the VeriSign Global Registry Service Whois Server.
*
* Sorry, the Whois database is currently down.
*
* Please wait a while and try again.  Thanks
*
源代码下载

附件

Fid_254/254_3784.gz (3 KB)

2006-10-3 08:58, 下载次数: 295

*
* WELCOME to the VeriSign Global Registry Service Whois Server.
*
* Sorry, the Whois database is currently down.
*
* Please wait a while and try again.  Thanks
*
呵呵,这里也有人玩erlang啊。不错不错。
E-mail: hhding!!gnu!gmail!!com
下载了,可用,很不错!
写得相当简洁
kangaroo-egg(袋鼠蛋)是国内第一个开源的动态web服务器,是完全采用java技术开发的功能强大拥有完全自主知识产权且开源的web服务器。其拥有自己的开发语言DQM及容器(类似于Servlet/JSP),可以很容易开发出满足各种业务要求的web应用。
同时具有:
动态网页扩展名自定义;
集成HTTP压缩功能;
自动生成和管理静态页面;
有条件的文件输出(下载);
隐藏动态网页代码;
----------------------
http://www.kangaroo-egg.com
ttttttttttttt
看到大熊猫了.这个东西是专门函数编程的动态脚本语言.wings3D,就是用它的.
zope/plone爱好者。
下个看看............
http://www.talons.cn
发新话题