forked from erlyaws/yaws
/
yaws_shaper.erl
62 lines (51 loc) · 1.63 KB
/
yaws_shaper.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
%%%----------------------------------------------------------------------
%%% File : yaws_shaper.erl
%%% Author : Christopher Faulet <christopher@yakaz.com>
%%% Purpose :
%%% Created : 14 Dec 2010 by Christopher Faulet <christopher@yakaz.com>
%%%----------------------------------------------------------------------
-module(yaws_shaper).
-author('christopher@yakaz.com').
-export([behaviour_info/1]).
%% API
-export([
check/2,
update/3
]).
-include("../include/yaws.hrl").
-include("../include/yaws_api.hrl").
-include("yaws_debug.hrl").
%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
behaviour_info(callbacks) ->
[{check,1}, {update,3}];
behaviour_info(_Other) ->
undefined.
check(#sconf{shaper=undefined}, _) ->
allow;
check(#sconf{shaper=Mod}, IP) ->
case catch Mod:check(IP) of
allow ->
allow;
{deny, Status, Msg} ->
{deny, Status, Msg};
_ ->
allow
end.
update(#sconf{shaper=undefined}, _, _) ->
ok;
update(#sconf{shaper=Mod}, IP, Req) ->
Bytes = case Req#http_request.method of
'HEAD' -> 0;
_ ->
case yaws:outh_get_contlen() of
undefined ->
case yaws:outh_get_act_contlen() of
undefined -> 0;
Actlen -> Actlen
end;
I2 -> I2
end
end,
catch Mod:update(IP, 1, Bytes).