鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > erlang > >

Cowboy 源码分析(二十二)

来源:互联网 作者:佚名 时间:2012-11-15 13:44
今天又是早早下班,买了排骨,莲藕,现在正压着排骨莲藕汤,从小就学做饭,发现其实做饭也蛮多乐趣的。等这篇文章发布以后,我应该已经吃完很久了,哈哈。回到主题,上一篇,我们 函数 : -spec handler_init(#http_req{}, #state{}) - ok.handler_init(Req,

  今天又是早早下班,买了排骨,莲藕,现在正压着排骨莲藕汤,从小就学做饭,发现其实做饭也蛮多乐趣的。等这篇文章发布以后,香港服务器租用,我应该已经吃完很久了,哈哈。回到主题,上一篇,我们 函数

-spec handler_init(#http_req{}, #state{}) -> ok. handler_init(Req, State=#state{transport=Transport, handler={Handler, Opts}}) -> try Handler:init({Transport:name(), http}, Req, Opts) of {ok, Req2, HandlerState} -> handler_handle(HandlerState, Req2, State); {loop, Req2, HandlerState} -> handler_before_loop(HandlerState, Req2, State); {loop, Req2, HandlerState, hibernate} -> handler_before_loop(HandlerState, Req2, State#state{hibernate=true}); {loop, Req2, HandlerState, Timeout} -> handler_before_loop(HandlerState, Req2, State#state{loop_timeout=Timeout}); {loop, Req2, HandlerState, Timeout, hibernate} -> handler_before_loop(HandlerState, Req2, State#state{hibernate=true, loop_timeout=Timeout}); {shutdown, Req2, HandlerState} -> handler_terminate(HandlerState, Req2, State); %% @todo {upgrade, transport, Module} {upgrade, protocol, Module} -> upgrade_protocol(Req, State, Module) catch Class:Reason -> error_terminate(500, State), PLReq = lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))), error_logger:error_msg( "** Handler ~p terminating in init/3~n" " for the reason ~p:~p~n" "** Options were ~p~n" "** Request was ~p~n** Stacktrace: ~p~n~n", [Handler, Class, Reason, Opts, PLReq, erlang:get_stacktrace()]) end.

  查看的参数值如下:

  < Handler = default_handler
  < Transport = cowboy_tcp_transport
  < Req = {http_req,#Port<0.2731>,cowboy_tcp_transport,keepalive,<0.481.0>,
                  'GET',
                  {1,1},
                  undefined,
                  [<<"localhost">>],
                  undefined,<<"localhost">>,8080,[],undefined,<<"/">>,
                  undefined,<<>>,[],
                  [{'Connection',<<"keep-alive">>},
                   {'Accept-Encoding',<<"gzip, deflate">>},
                   {'Accept-Language',<<"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3">>},
                   {'Accept',<<"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8">>},
                   {'User-Agent',<<"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1">>},
                   {'Host',<<"localhost">>}],
                  [{'Connection',[<<"keep-alive">>]}],
                  undefined,[],waiting,<<>>,waiting,[],<<>>,undefined,
                  {#Fun<cowboy_http.urldecode.2>,crash}}
  < Opts = []

  看下具体逻辑:

  try Handler:init({Transport:name(), http}, Req, Opts) of

  = try default_handler:init({cowboy_tcp_transport:name(), http}, Req, Opts) of

  首先,看下 cowboy_tcp_transport:name/0 函数:

%% @doc Name of this transport API, <em>tcp</em>. -spec name() -> tcp. name() -> tcp.

  一个简单的不能再简单的方法,返回原子tcp。

  接着看 default_handler:init/3 函数:

init({_Any, http}, Req, []) -> {ok, Req, undefined}.

  这个函数也很简单,只是简单的把参数组成元组返回。

  回到cowboy_http_protocol:handler_init/2 函数

{ok, Req2, HandlerState} -> handler_handle(HandlerState, Req2, State);

网友评论
<