comparison ngx_http_catch_body_filter_module.c @ 0:5dcad7ad8eda

Initial import.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 19 Jan 2015 15:37:04 +0300
parents
children 04788ce8dae7
comparison
equal deleted inserted replaced
-1:000000000000 0:5dcad7ad8eda
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");
97 return NGX_HTTP_FORBIDDEN;
98 }
99 }
100 }
101
102 return ngx_http_next_request_body_filter(r, in);
103 }
104
105
106 static void *
107 ngx_http_catch_body_create_conf(ngx_conf_t *cf)
108 {
109 ngx_http_catch_body_conf_t *conf;
110
111 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_catch_body_conf_t));
112 if (conf == NULL) {
113 return NULL;
114 }
115
116 conf->enable = NGX_CONF_UNSET;
117
118 return conf;
119 }
120
121
122 static char *
123 ngx_http_catch_body_merge_conf(ngx_conf_t *cf, void *parent, void *child)
124 {
125 ngx_http_catch_body_conf_t *prev = parent;
126 ngx_http_catch_body_conf_t *conf = child;
127
128 ngx_conf_merge_value(conf->enable, prev->enable, 0);
129
130 return NGX_CONF_OK;
131 }
132
133
134 static ngx_int_t
135 ngx_http_catch_body_init(ngx_conf_t *cf)
136 {
137 ngx_http_next_request_body_filter = ngx_http_top_request_body_filter;
138 ngx_http_top_request_body_filter = ngx_http_catch_body_filter;
139
140 return NGX_OK;
141 }