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
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
/* 
 * by Francesco Chemolli 
 * (C) 1999 Idonex AB 
 * 
 * Notice: this might look ugly, it's been designed to be split into 
 * a "library" program plus a tiny imlpementation module 
 */ 
 
constant cvs_version="$Id: throttling_bytype.pike,v 1.2 1999/09/05 16:01:08 grubba Exp $"; 
 
#include <module.h> 
inherit "module"; 
 
#ifdef THROTTLING_DEBUG 
#undef THROTTLING_DEBUG 
#define THROTTLING_DEBUG(X) perror("Throttling: "+X+"\n") 
#else 
#define THROTTLING_DEBUG(X) 
#endif 
 
#define THROW(X) throw( X+"\n" ) 
 
 
mapping rules; 
//format: ([pattern:({command_type,value,0|1(fix)})]) 
//command_type is a string (+-*/=!), value a float 
//if command_type==!, it means no throttling 
array(string) rulenames; //needed to keep the rules in order. 
 
//can throw if some rule could not be parsed. 
void update_rules() { 
  THROTTLING_DEBUG("by type: updating rules"); 
  rules=([]); 
  rulenames=({}); 
  string line, *words, cmd; 
  int lineno=0; 
  foreach ( (replace(QUERY(rules),"\t"," ")/"\n"),line) { 
    int fix=0; 
    float val=0; 
    string cmd; 
    lineno++; 
    if(!sizeof(line)) 
      continue; 
    if(line[0]=='#') 
      continue; 
    words=(line/" ")-({""}); 
     
    if (words[1]=="nothrottle") { 
      cmd="!"; 
      val=0; 
    } else if (sscanf(words[1],"%[+-*/=]%f",cmd,val) != 2) { 
      THROW("Could not parse rule at line "+lineno); 
    } 
    if (!((<"+","-","*","/","=","!">)[cmd])) 
      THROW("Unknown command at line "+lineno); 
    if (cmd=="!" || sizeof(words)>2 ) { 
      if (cmd=="!" || words[2]=="fix") //don't change order, or it bangs! 
        fix=1; 
      else 
        THROW("Unknown keyword at line "+lineno); 
    } 
    rules[words[0]]=({cmd,val,fix}); 
    rulenames+=({words[0]}); 
  } 
  THROTTLING_DEBUG(sprintf("by type: rules are:\n%O\n",rules)); 
} 
 
array register_module() { 
  return ({ 
    MODULE_FILTER, 
    "Throttling: throttle by type", 
    "This module will alter the throttling definitions by content type", 
    0,0}); 
} 
 
 
string check_variable(string name, mixed value) { 
  mixed err; 
  switch (name) { 
  case "rules": 
    set(name,lower_case(value)); 
    err=catch(update_rules()); 
    if (err) { 
      if (arrayp(err)) 
        return err[0]; 
      if (objectp(err)) 
        return err->describe(); 
    } 
    return 0; 
  } 
} 
 
void start() { 
  update_rules(); 
} 
 
array low_find_rule(string tomatch, mapping rules) { 
  THROTTLING_DEBUG("by type: got request for "+tomatch); 
  string s; 
  foreach(rulenames,s) { 
    THROTTLING_DEBUG("by type: examining "+s); 
    if (glob(s,tomatch)) { 
      THROTTLING_DEBUG("by type: **matched"); 
      return rules[s]; 
    } 
  } 
  THROTTLING_DEBUG("by type: **no match"); 
  return 0; 
} 
 
//override this for other similarly-working modules 
array find_rule (mapping res, object id, mapping rules) { 
  if (!res) return 0; 
  return low_find_rule(res->type,rules); 
} 
 
void apply_rule(array rule, object id) { 
  id->throttle=1; 
  switch(rule[0]) { 
  case "+": id->rate+=(int)rule[1];break; 
  case "-": id->rate-=(int)rule[1];break; 
  case "*": id->rate=(int)(id->rate*rule[1]);break; 
  case "/": id->rate=(int)(id->rate/rule[1]);break; 
  case "=": id->rate=(int)rule[1]; break; 
  case "!": id->throttle=0; break; 
  } 
  if (rule[2]) 
    id->misc->fixthrottle=1; 
} 
 
mixed filter (mapping res, object id) { 
  array rule; 
  if (id->misc->fixthrottle) 
    return 0; 
  rule=find_rule(res,id,rules); 
  if (!rule) 
    return 0; 
  apply_rule(rule,id); 
} 
 
 
void create() { 
  defvar("rules","","Rules",TYPE_TEXT_FIELD, 
#"Throttling rules. One rule per line, whose format is:<br> 
<tt>type-glob modifier [fix]</tt><br> 
<tt>type-glob</tt> is matched on the Content Type header. 
(i.e. <tt>image/gif</tt> or <tt>text/html</tt>).<p> 
<i>modifier</i> is the altering rule. There are six possible rule types:<br> 
<tt>+{number}</tt> adds <i>number</i> bytes/sec to the request<br> 
<tt>-{number}</tt> subtracts <i>number</i> bytes/sec to the request<br> 
<tt>*{number}</tt> multiplies the bandwidth assigned to the request 
  by <i>number</i> (a floating-point number)<br> 
<tt>/{number}</tt> divides the bandwidth assigned to the request 
  by <i>number</i> (a floating-point number)<br> 
<tt>={number}</tt> assigns the request <i>number</i> bytes/sec of  
  bandwidth<br> 
<tt>nothrottle</tt> asserts that the request is not to be throttled. 
  It implies using <tt>fix</tt>.<p> 
  The optional keyword <tt>fix</tt> will make the assigned bandwidth final. 
The entries are scanned in order, and processing is stopped as soon as  
a match is found.<p> 
Lines starting with <tt>#</tt> are considered comments."); 
}