-
Notifications
You must be signed in to change notification settings - Fork 3
/
ngx_hashdos_module.c
246 lines (204 loc) · 7.75 KB
/
ngx_hashdos_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
/*
* Copyright (C) 2012 54chen<[email protected]>
* http://54chen.com
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static void * ngx_http_hashdos_create_loc_conf(ngx_conf_t *cf);
static char * ngx_http_hashdos_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_http_hashdos_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_hashdos_handler(ngx_http_request_t *r);
static void ngx_hashdos_request_body_handler(ngx_http_request_t *r);
typedef struct {
ngx_flag_t enable;
ngx_int_t body_max_count;
} ngx_http_hashdos_loc_conf_t;
typedef struct {
ngx_flag_t done:1;
ngx_flag_t waiting_more_body:1;
} ngx_http_post_read_ctx_t;
static ngx_command_t ngx_http_hashdos_commands[] = {
{ ngx_string("hashdos"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_hashdos_loc_conf_t, enable),
NULL },
{ ngx_string("body_max_count"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_hashdos_loc_conf_t, body_max_count),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_hashdos_module_ctx = {
NULL, /* preconfiguration */
ngx_http_hashdos_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_hashdos_create_loc_conf, /* create location configuration */
ngx_http_hashdos_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_hashdos_module = {
NGX_MODULE_V1,
&ngx_http_hashdos_module_ctx, /* module context */
ngx_http_hashdos_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 void *
ngx_http_hashdos_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_hashdos_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hashdos_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->enable = NGX_CONF_UNSET;
conf->body_max_count = NGX_CONF_UNSET;
return conf;
}
static char *
ngx_http_hashdos_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_hashdos_loc_conf_t *prev = parent;
ngx_http_hashdos_loc_conf_t *conf = child;
ngx_conf_merge_value(conf->enable, prev->enable, 1);
ngx_conf_merge_value(conf->body_max_count,prev->body_max_count,1000);
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_hashdos_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_ACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_hashdos_handler;
return NGX_OK;
}
static ngx_int_t
ngx_http_hashdos_handler(ngx_http_request_t *r)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,"[hashdos] start handler....");
ngx_int_t rc;
ngx_http_hashdos_loc_conf_t *alcf;
ngx_http_post_read_ctx_t *ctx;
alcf = ngx_http_get_module_loc_conf(r, ngx_http_hashdos_module);
if (!alcf->enable) {
return NGX_OK;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_hashdos_module);
if (ctx != NULL) {
if (ctx->done) {
return NGX_DECLINED;
}
return NGX_DONE;
}
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_post_read_ctx_t));
if (ctx == NULL) {
ngx_http_finalize_request(r, NGX_ERROR);
return NGX_ERROR;
}
ngx_http_set_ctx(r, ctx, ngx_http_hashdos_module);
rc = ngx_http_read_client_request_body(r, ngx_hashdos_request_body_handler);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,"[hashdos] get body request...., rc is : %O " , rc);
return rc;
}
if (rc == NGX_AGAIN) {
ctx->waiting_more_body = 1;
return NGX_DONE;
}
return NGX_DECLINED;
}
static void
ngx_hashdos_request_body_handler(ngx_http_request_t *r)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,"[hashdos] body trasfering is over....");
ngx_http_hashdos_loc_conf_t *alcf;
ngx_int_t count, limit;
u_char ch, *p;
ngx_chain_t *cl;
ngx_buf_t *buf, *next;
ngx_http_post_read_ctx_t *ctx;
r->read_event_handler = ngx_http_request_empty_handler;
ctx = ngx_http_get_module_ctx(r, ngx_http_hashdos_module);
ctx->done = 1;
r->main->count--;
if (r->request_body == NULL || r->request_body->bufs == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,"[hashdos] body bufs is null....");
return ;
}
alcf = ngx_http_get_module_loc_conf(r, ngx_http_hashdos_module);
if (alcf->body_max_count <= 0) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,"[hashdos] configure body_max_count <= 0, set limit to 1000");
limit = 1000;
} else {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,"[hashdos] configure body_max_count is %O", alcf->body_max_count);
limit = alcf->body_max_count;
}
count = 0;
cl = r->request_body->bufs;
buf = cl->buf;
next = '\0';
if (cl->next == NULL) {
for (p = buf->pos; p < buf->last; p++){
ch = *p;
if(ch == '&'){
count++;
}
}
}
if(cl->next != NULL){
for (;cl;cl = cl->next) {
next = cl->buf;
if (next->in_file) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"[hashdos] in-file buffer found. aborted. "
"consider increasing your client_body_buffer_size "
"setting....");
ctx->waiting_more_body = 0;
ctx->done = 1;
r->main->count--;
return ;
}
for (p = next->pos; p < next->last; p++){
ch = *p;
if(ch == '&'){
count++;
}
}
}
}
++count;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,"[hashdos] parse request body params count is .... %O, limit is %O", count, limit);
if(count >= limit){
(void) ngx_http_discard_request_body(r);
ngx_http_finalize_request(r, NGX_HTTP_REQUEST_ENTITY_TOO_LARGE);
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"[hashdos] in rb->bfs -> client intended to send too large body: %O bytes, body size: %O, limit is: %O",
r->headers_in.content_length_n,count,limit);
ctx->waiting_more_body = 0;
ctx->done = 1;
r->main->count--;
}
if (ctx->waiting_more_body) {
ctx->waiting_more_body = 0;
ngx_http_core_run_phases(r);
}
}