Mercurial > hg > ngx_http_catch_body_filter_module
annotate ngx_http_catch_body_filter_module.c @ 2:04788ce8dae7
Close connection on errors.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 09 Feb 2015 16:31:47 +0300 |
parents | 5dcad7ad8eda |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
3 * Copyright (C) Maxim Dounin | |
4 */ | |
5 | |
6 #include <ngx_config.h> | |
7 #include <ngx_core.h> | |
8 #include <ngx_http.h> | |
9 | |
10 | |
11 typedef struct { | |
12 ngx_flag_t enable; | |
13 } ngx_http_catch_body_conf_t; | |
14 | |
15 | |
16 static void *ngx_http_catch_body_create_conf(ngx_conf_t *cf); | |
17 static char *ngx_http_catch_body_merge_conf(ngx_conf_t *cf, void *parent, | |
18 void *child); | |
19 static ngx_int_t ngx_http_catch_body_init(ngx_conf_t *cf); | |
20 | |
21 | |
22 static ngx_command_t ngx_http_catch_body_commands[] = { | |
23 | |
24 { ngx_string("catch_body"), | |
25 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
26 ngx_conf_set_flag_slot, | |
27 NGX_HTTP_LOC_CONF_OFFSET, | |
28 offsetof(ngx_http_catch_body_conf_t, enable), | |
29 NULL }, | |
30 | |
31 ngx_null_command | |
32 }; | |
33 | |
34 | |
35 static ngx_http_module_t ngx_http_catch_body_module_ctx = { | |
36 NULL, /* preconfiguration */ | |
37 ngx_http_catch_body_init, /* postconfiguration */ | |
38 | |
39 NULL, /* create main configuration */ | |
40 NULL, /* init main configuration */ | |
41 | |
42 NULL, /* create server configuration */ | |
43 NULL, /* merge server configuration */ | |
44 | |
45 ngx_http_catch_body_create_conf, /* create location configuration */ | |
46 ngx_http_catch_body_merge_conf /* merge location configuration */ | |
47 }; | |
48 | |
49 | |
50 ngx_module_t ngx_http_catch_body_filter_module = { | |
51 NGX_MODULE_V1, | |
52 &ngx_http_catch_body_module_ctx, /* module context */ | |
53 ngx_http_catch_body_commands, /* module directives */ | |
54 NGX_HTTP_MODULE, /* module type */ | |
55 NULL, /* init master */ | |
56 NULL, /* init module */ | |
57 NULL, /* init process */ | |
58 NULL, /* init thread */ | |
59 NULL, /* exit thread */ | |
60 NULL, /* exit process */ | |
61 NULL, /* exit master */ | |
62 NGX_MODULE_V1_PADDING | |
63 }; | |
64 | |
65 | |
66 static ngx_http_request_body_filter_pt ngx_http_next_request_body_filter; | |
67 | |
68 | |
69 static ngx_int_t | |
70 ngx_http_catch_body_filter(ngx_http_request_t *r, ngx_chain_t *in) | |
71 { | |
72 u_char *p; | |
73 ngx_chain_t *cl; | |
74 ngx_http_catch_body_conf_t *conf; | |
75 | |
76 conf = ngx_http_get_module_loc_conf(r, ngx_http_catch_body_filter_module); | |
77 | |
78 if (!conf->enable) { | |
79 return ngx_http_next_request_body_filter(r, in); | |
80 } | |
81 | |
82 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
83 "catch request body filter"); | |
84 | |
85 for (cl = in; cl; cl = cl->next) { | |
86 | |
87 p = cl->buf->pos; | |
88 | |
89 for (p = cl->buf->pos; p < cl->buf->last; p++) { | |
90 | |
91 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
92 "catch body in:%02Xd:%c", *p, *p); | |
93 | |
94 if (*p == 'X') { | |
95 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
96 "catch body: found"); | |
2
04788ce8dae7
Close connection on errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
97 |
04788ce8dae7
Close connection on errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
98 /* |
04788ce8dae7
Close connection on errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
99 * As we return NGX_HTTP_FORBIDDEN, the r->keepalive flag |
04788ce8dae7
Close connection on errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
100 * won't be reset by ngx_http_special_response_handler(). |
04788ce8dae7
Close connection on errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
101 * Make sure to reset it to prevent processing of unread |
04788ce8dae7
Close connection on errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
102 * parts of the request body. |
04788ce8dae7
Close connection on errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
103 */ |
04788ce8dae7
Close connection on errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
104 |
04788ce8dae7
Close connection on errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
105 r->keepalive = 0; |
04788ce8dae7
Close connection on errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
106 |
0 | 107 return NGX_HTTP_FORBIDDEN; |
108 } | |
109 } | |
110 } | |
111 | |
112 return ngx_http_next_request_body_filter(r, in); | |
113 } | |
114 | |
115 | |
116 static void * | |
117 ngx_http_catch_body_create_conf(ngx_conf_t *cf) | |
118 { | |
119 ngx_http_catch_body_conf_t *conf; | |
120 | |
121 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_catch_body_conf_t)); | |
122 if (conf == NULL) { | |
123 return NULL; | |
124 } | |
125 | |
126 conf->enable = NGX_CONF_UNSET; | |
127 | |
128 return conf; | |
129 } | |
130 | |
131 | |
132 static char * | |
133 ngx_http_catch_body_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
134 { | |
135 ngx_http_catch_body_conf_t *prev = parent; | |
136 ngx_http_catch_body_conf_t *conf = child; | |
137 | |
138 ngx_conf_merge_value(conf->enable, prev->enable, 0); | |
139 | |
140 return NGX_CONF_OK; | |
141 } | |
142 | |
143 | |
144 static ngx_int_t | |
145 ngx_http_catch_body_init(ngx_conf_t *cf) | |
146 { | |
147 ngx_http_next_request_body_filter = ngx_http_top_request_body_filter; | |
148 ngx_http_top_request_body_filter = ngx_http_catch_body_filter; | |
149 | |
150 return NGX_OK; | |
151 } |