Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

experimental: add a configurable dispatch module

Allow an application to supply its own dispatch module by setting the new
dispatch_mod variable in the server config. The module is expected to have
an out/1 function that returns one of the following atoms:

* done: this indicates the dispatch module has handled the request and Yaws
  should go look for a new request on this connection

* close: this tells Yaws to close the socket

* continue: this tells Yaws to continue with the normal dispatch path
  • Loading branch information...
commit f0ee7bf0ff018cf4b175d2fbc7841674da2880ab 1 parent faef983
@vinoski vinoski authored
Showing with 54 additions and 29 deletions.
  1. +2 −1  include/yaws.hrl
  2. +2 −0  src/yaws.erl
  3. +50 −28 src/yaws_server.erl
View
3  include/yaws.hrl
@@ -242,7 +242,8 @@
php_handler = {cgi, "/usr/bin/php-cgi"},
shaper,
deflate_options,
- mime_types_info % if undefined, global config is used
+ mime_types_info, % if undefined, global config is used
+ dispatch_mod % custom dispatch module
}).
View
2  src/yaws.erl
@@ -286,6 +286,8 @@ setup_sconf(SL, SC) ->
deflate_options = lkup(deflate_options, SL,
SC#sconf.deflate_options),
mime_types_info = lkup(mime_types_info, SL,
+ SC#sconf.mime_types_info),
+ dispatch_mod = lkup(dispatch_mod, SL,
SC#sconf.mime_types_info)
}.
View
78 src/yaws_server.erl
@@ -1105,7 +1105,7 @@ acceptor0(GS, Top) ->
%%% Internal functions
%%%----------------------------------------------------------------------
-aloop(CliSock, {IP,Port}, GS, Num) ->
+aloop(CliSock, {IP,Port}=IPPort, GS, Num) ->
case yaws_trace:get_type(GS#gs.gconf) of
undefined ->
ok;
@@ -1135,24 +1135,44 @@ aloop(CliSock, {IP,Port}, GS, Num) ->
{Req, H} = fix_abs_uri(Req0, H0),
?Debug("{Req, H} = ~p~n", [{Req, H}]),
SC = pick_sconf(GS#gs.gconf, H, GS#gs.group),
- ?Debug("SC: ~s", [?format_record(SC, sconf)]),
- ?TC([{record, SC, sconf}]),
- ?Debug("Headers = ~s~n", [?format_record(H, headers)]),
- ?Debug("Request = ~s~n", [?format_record(Req, http_request)]),
- run_trace_filter(GS, IP, Req, H),
- put(outh, #outh{}),
- put(sc, SC),
- yaws_stats:hit(),
- check_keepalive_maxuses(GS, Num),
- Call = case yaws_shaper:check(SC, IP) of
- allow ->
- call_method(Req#http_request.method,CliSock,
- {IP,Port},Req,H);
- {deny, Status, Msg} ->
- deliver_xxx(CliSock, Req, Status, Msg)
- end,
- Call2 = fix_keepalive_maxuses(Call),
- handle_method_result(Call2, CliSock, {IP,Port}, GS, Req, H, Num);
+ DispatchResult = case SC#sconf.dispatch_mod of
+ undefined ->
+ continue;
+ DispatchMod ->
+ Arg = make_arg(SC, CliSock, H, Req, undefined),
+ DispatchMod:dispatch(Arg)
+ end,
+ case DispatchResult of
+ done ->
+ erase_transients(),
+ case exceed_keepalive_maxuses(GS, Num) of
+ true -> {ok, Num+1};
+ false -> aloop(CliSock, IPPort, GS, Num+1)
+ end;
+ closed ->
+ %% Dispatcher closed the socket
+ erase_transients(),
+ {ok, Num+1};
+ continue ->
+ ?Debug("SC: ~s", [?format_record(SC, sconf)]),
+ ?TC([{record, SC, sconf}]),
+ ?Debug("Headers = ~s~n", [?format_record(H, headers)]),
+ ?Debug("Request = ~s~n", [?format_record(Req, http_request)]),
+ run_trace_filter(GS, IP, Req, H),
+ put(outh, #outh{}),
+ put(sc, SC),
+ yaws_stats:hit(),
+ check_keepalive_maxuses(GS, Num),
+ Call = case yaws_shaper:check(SC, IP) of
+ allow ->
+ call_method(Req#http_request.method,CliSock,
+ IPPort,Req,H);
+ {deny, Status, Msg} ->
+ deliver_xxx(CliSock, Req, Status, Msg)
+ end,
+ Call2 = fix_keepalive_maxuses(Call),
+ handle_method_result(Call2, CliSock, IPPort, GS, Req, H, Num)
+ end;
closed ->
case yaws_trace:get_type(GS#gs.gconf) of
undefined -> ok;
@@ -1190,15 +1210,15 @@ run_trace_filter(GS, IP, Req, H) ->
%% process dictionary outh variable if required to say that the
%% connection has exceeded its maxuses.
check_keepalive_maxuses(GS, Num) ->
+ Flag = exceed_keepalive_maxuses(GS, Num),
+ put(outh, (get(outh))#outh{exceedmaxuses=Flag}).
+
+exceed_keepalive_maxuses(GS, Num) ->
case (GS#gs.gconf)#gconf.keepalive_maxuses of
- nolimit ->
- ok;
- 0 ->
- ok;
- N when Num+1 < N ->
- ok;
- _N ->
- put(outh, (get(outh))#outh{exceedmaxuses=true})
+ nolimit -> false;
+ 0 -> false;
+ N when Num+1 < N -> false;
+ _N -> true
end.
%% Change to Res to 'done' if we've exceeded our maxuses.
@@ -1207,7 +1227,7 @@ fix_keepalive_maxuses(Res) ->
continue ->
case (get(outh))#outh.exceedmaxuses of
true ->
- done; %% no keepalive this time!
+ done; % no keepalive this time!
_ ->
Res
end;
@@ -1569,6 +1589,8 @@ no_body_method(CliSock, IPPort, Req, Head) ->
make_arg(CliSock0, IPPort, Head, Req, Bin) ->
SC = get(sc),
+ make_arg(SC, CliSock0, IPPort, Head, Req, Bin).
+make_arg(SC, CliSock0, IPPort, Head, Req, Bin) ->
CliSock = case yaws:is_ssl(SC) of
nossl ->
CliSock0;

0 comments on commit f0ee7bf

Please sign in to comment.
Something went wrong with that request. Please try again.