forked from cfsego/limit_upload_rate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_limit_upload_module.c
377 lines (276 loc) · 11.1 KB
/
ngx_http_limit_upload_module.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Author: cfsego
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
size_t limit_rate;
size_t limit_rate_after;
ngx_uint_t log_level;
} ngx_http_limit_upload_conf_t;
typedef struct {
off_t received;
size_t limit_rate;
ngx_http_event_handler_pt read_event_handler;
ngx_http_event_handler_pt write_event_handler;
} ngx_http_limit_upload_ctx_t;
static ngx_int_t ngx_http_limit_upload_add_variable(ngx_conf_t *cf);
static ngx_int_t ngx_http_limit_upload_init(ngx_conf_t *cf);
static void *ngx_http_limit_upload_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_limit_upload_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child);
static ngx_int_t ngx_http_variable_limit_upload_get_size(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static void ngx_http_variable_limit_upload_set_size(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_limit_upload_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_limit_upload_input_body_filter(ngx_http_request_t *r,
ngx_buf_t *buf);
static void ngx_http_limit_upload_delay(ngx_http_request_t *r);
static ngx_conf_enum_t ngx_http_limit_upload_log_levels[] = {
{ ngx_string("info"), NGX_LOG_INFO },
{ ngx_string("notice"), NGX_LOG_NOTICE },
{ ngx_string("warn"), NGX_LOG_WARN },
{ ngx_string("error"), NGX_LOG_ERR },
{ ngx_null_string, 0 }
};
static ngx_command_t ngx_http_limit_upload_commands[] = {
{ ngx_string("limit_upload_rate"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_limit_upload_conf_t, limit_rate),
NULL },
{ ngx_string("limit_upload_rate_after"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_limit_upload_conf_t, limit_rate_after),
NULL },
{ ngx_string("limit_upload_rate_log_level"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_enum_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_limit_upload_conf_t, log_level),
&ngx_http_limit_upload_log_levels },
ngx_null_command
};
static ngx_http_module_t ngx_http_limit_upload_module_ctx = {
ngx_http_limit_upload_add_variable, /* preconfiguration */
ngx_http_limit_upload_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_limit_upload_create_loc_conf, /* create location configration */
ngx_http_limit_upload_merge_loc_conf /* merge location configration */
};
ngx_module_t ngx_http_limit_upload_module = {
NGX_MODULE_V1,
&ngx_http_limit_upload_module_ctx, /* module context */
ngx_http_limit_upload_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_str_t ngx_http_limit_upload_var_name
= ngx_string("limit_upload_rate");
static ngx_http_input_body_filter_pt ngx_http_next_input_body_filter;
static ngx_int_t
ngx_http_limit_upload_input_body_filter(ngx_http_request_t *r, ngx_buf_t *buf)
{
off_t excess;
ngx_int_t rc;
ngx_msec_t delay;
ngx_http_core_loc_conf_t *clcf;
ngx_http_limit_upload_ctx_t *ctx;
ngx_http_limit_upload_conf_t *llcf;
rc = ngx_http_next_input_body_filter(r, buf);
if (rc != NGX_OK) {
return rc;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_limit_upload_module);
llcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_upload_module);
if (ctx->limit_rate == 0) {
ctx->limit_rate = llcf->limit_rate;
}
if (ctx->limit_rate) {
ctx->received += ngx_buf_size(buf);
excess = ctx->received - llcf->limit_rate_after
- ctx->limit_rate * (ngx_time() - r->start_sec + 1);
ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"limit upload: ctx=%p, received=%O, "
"excess=%O, limit_rate=%z, period=%M",
ctx, ctx->received, excess, ctx->limit_rate,
ngx_time() - r->start_sec + 1);
if (excess > 0) {
if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
delay = excess * 1000 / ctx->limit_rate;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"limit upload: delay=%M", delay);
ctx->read_event_handler = r->read_event_handler;
ctx->write_event_handler = r->write_event_handler;
r->read_event_handler = ngx_http_test_reading;
r->write_event_handler = ngx_http_limit_upload_delay;
ngx_add_timer(r->connection->write, delay);
if (!r->connection->read->ready) {
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
ngx_add_timer(r->connection->read, clcf->client_body_timeout);
if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
} else if (r->connection->read->timer_set) {
ngx_del_timer(r->connection->read);
}
return NGX_AGAIN;
}
}
return NGX_OK;
}
static void *
ngx_http_limit_upload_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_limit_upload_conf_t *conf;
conf = ngx_palloc(cf->pool, sizeof(ngx_http_limit_upload_conf_t));
if (conf == NULL) {
return NULL;
}
conf->limit_rate = NGX_CONF_UNSET_SIZE;
conf->limit_rate_after = NGX_CONF_UNSET_SIZE;
conf->log_level = NGX_CONF_UNSET_UINT;
return conf;
}
static char *
ngx_http_limit_upload_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_limit_upload_conf_t *conf = child;
ngx_http_limit_upload_conf_t *prev = parent;
ngx_conf_merge_size_value(conf->limit_rate, prev->limit_rate, 0);
ngx_conf_merge_size_value(conf->limit_rate_after, prev->limit_rate_after,
0);
ngx_conf_merge_uint_value(conf->log_level,
prev->log_level, NGX_LOG_ERR);
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_limit_upload_add_variable(ngx_conf_t *cf)
{
ngx_http_variable_t *var;
var = ngx_http_add_variable(cf, &ngx_http_limit_upload_var_name,
NGX_HTTP_VAR_CHANGEABLE
|NGX_HTTP_VAR_NOCACHEABLE
|NGX_HTTP_VAR_INDEXED);
if (var == NULL) {
return NGX_ERROR;
}
var->set_handler = ngx_http_variable_limit_upload_set_size;
var->get_handler = ngx_http_variable_limit_upload_get_size;
var->data = offsetof(ngx_http_limit_upload_ctx_t, limit_rate);
if (ngx_http_get_variable_index(cf, &ngx_http_limit_upload_var_name)
== NGX_ERROR)
{
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_limit_upload_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_limit_upload_handler;
ngx_http_next_input_body_filter = ngx_http_top_input_body_filter;
ngx_http_top_input_body_filter = ngx_http_limit_upload_input_body_filter;
return NGX_OK;
}
static ngx_int_t
ngx_http_limit_upload_handler(ngx_http_request_t *r)
{
ngx_http_limit_upload_ctx_t *ctx;
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_limit_upload_ctx_t));
if (ctx == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"limit upload: ctx=%p", ctx);
ngx_http_set_ctx(r, ctx, ngx_http_limit_upload_module);
return NGX_OK;
}
static ngx_int_t
ngx_http_variable_limit_upload_get_size(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
size_t *sp;
ngx_http_limit_upload_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r, ngx_http_limit_upload_module);
sp = (size_t *) ((char *) ctx + data);
v->data = ngx_pnalloc(r->pool, NGX_SIZE_T_LEN);
if (v->data == NULL) {
return NGX_ERROR;
}
v->len = ngx_sprintf(v->data, "%uz", *sp) - v->data;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
}
static void
ngx_http_variable_limit_upload_set_size(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
ssize_t s, *sp;
ngx_str_t val;
ngx_http_limit_upload_ctx_t *ctx;
val.len = v->len;
val.data = v->data;
s = ngx_parse_size(&val);
if (s == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"invalid size \"%V\"", &val);
return;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_limit_upload_module);
sp = (ssize_t *) ((char *) ctx + data);
*sp = s;
return;
}
static void
ngx_http_limit_upload_delay(ngx_http_request_t *r)
{
ngx_event_t *wev;
ngx_http_limit_upload_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r, ngx_http_limit_upload_module);
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"limit upload delay");
wev = r->connection->write;
if (!wev->timedout) {
if (ngx_handle_write_event(wev, 0) != NGX_OK) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
}
return;
}
wev->timedout = 0;
if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
r->read_event_handler = ctx->read_event_handler;
r->write_event_handler = ctx->write_event_handler;
r->read_event_handler(r);
}