3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 ngx_str_t before_body;
17 ngx_array_t *types_keys;
18 } ngx_http_addition_conf_t;
22 ngx_uint_t before_body_sent;
23 } ngx_http_addition_ctx_t;
26 static void *ngx_http_addition_create_conf(ngx_conf_t *cf);
27 static char *ngx_http_addition_merge_conf(ngx_conf_t *cf, void *parent,
29 static ngx_int_t ngx_http_addition_filter_init(ngx_conf_t *cf);
32 static ngx_command_t ngx_http_addition_commands[] = {
34 { ngx_string("add_before_body"),
35 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
36 ngx_conf_set_str_slot,
37 NGX_HTTP_LOC_CONF_OFFSET,
38 offsetof(ngx_http_addition_conf_t, before_body),
41 { ngx_string("add_after_body"),
42 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
43 ngx_conf_set_str_slot,
44 NGX_HTTP_LOC_CONF_OFFSET,
45 offsetof(ngx_http_addition_conf_t, after_body),
48 { ngx_string("addtion_types"),
49 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
51 NGX_HTTP_LOC_CONF_OFFSET,
52 offsetof(ngx_http_addition_conf_t, types_keys),
53 &ngx_http_html_default_types[0] },
59 static ngx_http_module_t ngx_http_addition_filter_module_ctx = {
60 NULL, /* preconfiguration */
61 ngx_http_addition_filter_init, /* postconfiguration */
63 NULL, /* create main configuration */
64 NULL, /* init main configuration */
66 NULL, /* create server configuration */
67 NULL, /* merge server configuration */
69 ngx_http_addition_create_conf, /* create location configuration */
70 ngx_http_addition_merge_conf /* merge location configuration */
74 ngx_module_t ngx_http_addition_filter_module = {
76 &ngx_http_addition_filter_module_ctx, /* module context */
77 ngx_http_addition_commands, /* module directives */
78 NGX_HTTP_MODULE, /* module type */
79 NULL, /* init master */
80 NULL, /* init module */
81 NULL, /* init process */
82 NULL, /* init thread */
83 NULL, /* exit thread */
84 NULL, /* exit process */
85 NULL, /* exit master */
90 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
91 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
95 ngx_http_addition_header_filter(ngx_http_request_t *r)
97 ngx_http_addition_ctx_t *ctx;
98 ngx_http_addition_conf_t *conf;
100 if (r->headers_out.status != NGX_HTTP_OK || r != r->main) {
101 return ngx_http_next_header_filter(r);
104 conf = ngx_http_get_module_loc_conf(r, ngx_http_addition_filter_module);
106 if (conf->before_body.len == 0 && conf->after_body.len == 0) {
107 return ngx_http_next_header_filter(r);
110 if (ngx_http_test_content_type(r, &conf->types) == NULL) {
111 return ngx_http_next_header_filter(r);
114 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_addition_ctx_t));
119 ngx_http_set_ctx(r, ctx, ngx_http_addition_filter_module);
121 ngx_http_clear_content_length(r);
122 ngx_http_clear_accept_ranges(r);
124 return ngx_http_next_header_filter(r);
129 ngx_http_addition_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
134 ngx_http_request_t *sr;
135 ngx_http_addition_ctx_t *ctx;
136 ngx_http_addition_conf_t *conf;
138 if (in == NULL || r->header_only) {
139 return ngx_http_next_body_filter(r, in);
142 ctx = ngx_http_get_module_ctx(r, ngx_http_addition_filter_module);
145 return ngx_http_next_body_filter(r, in);
148 conf = ngx_http_get_module_loc_conf(r, ngx_http_addition_filter_module);
150 if (!ctx->before_body_sent) {
151 ctx->before_body_sent = 1;
153 if (conf->before_body.len) {
154 if (ngx_http_subrequest(r, &conf->before_body, NULL, &sr, NULL, 0)
162 if (conf->after_body.len == 0) {
163 ngx_http_set_ctx(r, NULL, ngx_http_addition_filter_module);
164 return ngx_http_next_body_filter(r, in);
169 for (cl = in; cl; cl = cl->next) {
170 if (cl->buf->last_buf) {
171 cl->buf->last_buf = 0;
177 rc = ngx_http_next_body_filter(r, in);
179 if (rc == NGX_ERROR || !last || conf->after_body.len == 0) {
183 if (ngx_http_subrequest(r, &conf->after_body, NULL, &sr, NULL, 0)
189 ngx_http_set_ctx(r, NULL, ngx_http_addition_filter_module);
191 return ngx_http_send_special(r, NGX_HTTP_LAST);
196 ngx_http_addition_filter_init(ngx_conf_t *cf)
198 ngx_http_next_header_filter = ngx_http_top_header_filter;
199 ngx_http_top_header_filter = ngx_http_addition_header_filter;
201 ngx_http_next_body_filter = ngx_http_top_body_filter;
202 ngx_http_top_body_filter = ngx_http_addition_body_filter;
209 ngx_http_addition_create_conf(ngx_conf_t *cf)
211 ngx_http_addition_conf_t *conf;
213 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_addition_conf_t));
215 return NGX_CONF_ERROR;
219 * set by ngx_pcalloc():
221 * conf->before_body = { 0, NULL };
222 * conf->after_body = { 0, NULL };
223 * conf->types = { NULL };
224 * conf->types_keys = NULL;
232 ngx_http_addition_merge_conf(ngx_conf_t *cf, void *parent, void *child)
234 ngx_http_addition_conf_t *prev = parent;
235 ngx_http_addition_conf_t *conf = child;
237 ngx_conf_merge_str_value(conf->before_body, prev->before_body, "");
238 ngx_conf_merge_str_value(conf->after_body, prev->after_body, "");
240 if (ngx_http_merge_types(cf, conf->types_keys, &conf->types,
241 prev->types_keys, &prev->types,
242 ngx_http_html_default_types)
245 return NGX_CONF_ERROR;