-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_rangelimit.c
256 lines (240 loc) · 8.55 KB
/
mod_rangelimit.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "mod_rangelimit.h"
// Module version: 1.0
#ifdef APACHE2
module AP_MODULE_DECLARE_DATA rangelimit_module;
#else
module MODULE_VAR_EXPORT rangelimit_module;
#endif
typedef struct {
int max_ranges;
int max_overlaps;
} ranges_server_conf;
#ifdef APACHE2
static void *create_rangelim_config(apr_pool_t *p, server_rec *s) {
ranges_server_conf *cfg = (ranges_server_conf *) apr_pcalloc(p, sizeof(ranges_server_conf));
#else
static void *create_rangelim_config(pool *p, server_rec *s) {
ranges_server_conf *cfg = (ranges_server_conf *) ap_pcalloc(p, sizeof(ranges_server_conf));
#endif
cfg->max_ranges = 20;
cfg->max_overlaps = 5;
return cfg;
}
static int check_range(int *arr, int *val) {
int i = 0;
for (i=0;i<=16;i++) {
if (*arr == 0)
return 0;
if (*arr == *val)
return i;
arr++;
}
return -1;
}
static int range_handler(request_rec *r) {
#ifdef APACHE2
const char *range_header = apr_table_get(r->headers_in, "Range");
#else
const char *range_header = ap_table_get(r->headers_in, "Range");
#endif
ranges_server_conf *cfg = ap_get_module_config(r->server->module_config, &rangelimit_module);
int range_num = 0; // counter of the number of checked ranges
int range_overlaps = 0; // counter of the number of overlapping ranges
char *ranges = NULL; // pointer to keep the ranges location
int start = -1, end = -1; // start and end of a single range
int n = 0; // number of characters collected by sscanf()
int start_count = 0; // counter for the new unique starts
int end_count = 0; // counter for the new unique ends
int begins[60];
int ends[60];
if (!range_header || strncasecmp(range_header, "bytes=", 6) || r->status != HTTP_OK ) {
if (r->server->loglevel == APLOG_DEBUG)
#ifdef APACHE2
ap_log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r, "mod_rangelimit: no range found" );
#else
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, r, "mod_rangelimit: no range found" );
#endif
return OK;
}
// if (r->server->loglevel == APLOG_DEBUG)
//#ifdef APACHE2
// ap_log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r, "mod_rangelimit: Range header: %s", range_header);
//#else
// ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, r, "mod_rangelimit: Range header: %s", range_header);
//#endif
#ifdef APACHE2
if (!ap_strchr_c(range_header, ',')) {
#else
if (!strchr(range_header, ',')) {
#endif
if (r->server->loglevel == APLOG_DEBUG)
#ifdef APACHE2
ap_log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r, "mod_rangelimit: single range, nothing to do here");
#else
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, r, "mod_rangelimit: single range, nothing to do here");
#endif
return OK;
}
// multiple ranges
#ifdef APACHE2
ranges = ap_strchr_c(range_header, '=');
#else
ranges = strchr(range_header, '=');
#endif
ranges++; // move the pointer to the begining of the first range
// start walking over the ranges
while ( sscanf(ranges, "%11d-%11d%n", &start, &end, &n) >= 1 ) {
if (start < 0) {
#ifdef APACHE2
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"mod_rangelimit: requested range(%d-%d) not satisfiable", start, end);
#else
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
"mod_rangelimit: requested range(%d-%d) not satisfiable", start, end);
#endif
return HTTP_RANGE_NOT_SATISFIABLE;
}
if (end >= 0 && end < start) {
#ifdef APACHE2
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"mod_rangelimit: requested range(%d-%d) not satisfiable", start, end);
#else
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
"mod_rangelimit: requested range(%d-%d) not satisfiable", start, end);
#endif
return HTTP_RANGE_NOT_SATISFIABLE;
}
// code for handling overlaps
if (check_range(begins, &start) > 0) {
range_overlaps++;
} else {
begins[start_count] = start;
start_count++;
}
if (check_range(ends, &end) > 0) {
range_overlaps++;
} else {
ends[end_count] = end;
end_count++;
}
// end of the overlaps code
range_num++; // increase the number of found ranges
if (r->server->loglevel == APLOG_DEBUG)
#ifdef APACHE2
ap_log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r,
"mod_rangelimit: range_num: %d overpalpping: %d start: %d end: %d n: %d",
range_num, range_overlaps, start, end, n);
#else
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, r,
"mod_rangelimit: range_num: %d overpalpping: %d start: %d end: %d n: %d",
range_num, range_overlaps, start, end, n);
#endif
if (range_num > cfg->max_ranges) {
#ifdef APACHE2
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"mod_rangelimit: too many ranges - %d", range_num);
#else
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
"mod_rangelimit: too many ranges - %d", range_num);
#endif
return HTTP_RANGE_NOT_SATISFIABLE;
}
if (range_overlaps > cfg->max_overlaps) {
#ifdef APACHE2
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"mod_rangelimit: too many overlapping ranges - %d", range_overlaps);
#else
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
"mod_rangelimit: too many overlapping ranges - %d", range_overlaps);
#endif
return HTTP_RANGE_NOT_SATISFIABLE;
}
if (end == -1) {
// since n is not set when end is not read
// find the end of this range using strchr
#ifdef APACHE2
ranges = ap_strchr_c(ranges, '-') + 1;
#else
ranges = strchr(ranges, '-') + 1;
#endif
} else {
ranges += n; // advance the pointer by the number of characters read
}
if ( *ranges != ',' ) {
break; // didn't find an expected delimiter, done?
}
++ranges; // skip the delimiter
start = end = -1; // clear the start and end points
n = 0; // clear the number of found chars
}
return OK;
}
static const char *set_max_ranges(cmd_parms *cmd, void *mconfig, const char *arg) {
ranges_server_conf *cfg = ap_get_module_config(cmd->server->module_config, &rangelimit_module);
cfg->max_ranges = atoi(arg);
return NULL;
}
static const char *set_max_overlaps(cmd_parms *cmd, void *mconfig, const char *arg) {
ranges_server_conf *cfg = ap_get_module_config(cmd->server->module_config, &rangelimit_module);
cfg->max_overlaps = atoi(arg);
return NULL;
}
static const command_rec rangelim_cmds[] = {
#ifdef APACHE2
AP_INIT_TAKE1("MaxRanges", set_max_ranges, NULL, RSRC_CONF,
"Define the maximum number of allowed range definitions. Default 20"),
AP_INIT_TAKE1("MaxOverlappingRanges", set_max_overlaps, NULL, RSRC_CONF,
"Define the maximum number of allowed ranges that overlap each other. Default 5"),
#else
{ "MaxRanges", set_max_ranges, NULL, RSRC_CONF, TAKE1,
"Define the maximum number of allowed range definitions. Default 20" },
{ "MaxOverlappingRanges", set_max_overlaps, NULL, RSRC_CONF, TAKE1,
"Define the maximum number of allowed ranges that overlap each other. Default 5" },
#endif
{NULL}
};
#ifdef APACHE2
static void register_hooks(apr_pool_t *p) {
static const char * const aszPost[] = { "mod_setenvif.c", NULL };
ap_hook_header_parser(range_handler, NULL, aszPost, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA rangelimit_module = {
STANDARD20_MODULE_STUFF,
NULL, /* per-directory config creator */
NULL, /* dir config merger */
create_rangelim_config, /* server config creator */
NULL, /* server config merger */
rangelim_cmds, /* command table */
register_hooks, /* set up other request processing hooks */
};
#else
module MODULE_VAR_EXPORT rangelimit_module = {
STANDARD_MODULE_STUFF,
NULL, /* module initializer */
NULL, /* per-directory config creator */
NULL, /* dir config merger */
create_rangelim_config, /* server config creator */
NULL, /* server config merger */
rangelim_cmds, /* command table */
NULL, /* [9] list of handlers */
NULL, /* [2] filename-to-URI translation */
NULL, /* [5] check/validate user_id */
NULL, /* [6] check user_id is valid *here* */
NULL, /* [4] check access by host address */
NULL, /* [7] MIME type checker/setter */
NULL, /* [8] fixups */
NULL, /* [10] logger */
#if MODULE_MAGIC_NUMBER >= 19970103
range_handler, /* [3] header parser */
#endif
#if MODULE_MAGIC_NUMBER >= 19970719
NULL, /* process initializer */
#endif
#if MODULE_MAGIC_NUMBER >= 19970728
NULL, /* process exit/cleanup */
#endif
#if MODULE_MAGIC_NUMBER >= 19970902
NULL /* [1] post read_request handling */
#endif
};
#endif // APACHE2