Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_addition_filter_module.c @ 910:918e19c51a65
style fix
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 12 Dec 2006 20:58:41 +0000 |
parents | c2cae54f2045 |
children | 28a8e9b9e8f9 |
rev | line source |
---|---|
629 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
12 typedef struct { | |
13 ngx_str_t before_body; | |
14 ngx_str_t after_body; | |
15 } ngx_http_addition_conf_t; | |
16 | |
17 | |
18 typedef struct { | |
631 | 19 ngx_uint_t before_body_sent; |
629 | 20 } ngx_http_addition_ctx_t; |
21 | |
22 | |
23 static void *ngx_http_addition_create_conf(ngx_conf_t *cf); | |
24 static char *ngx_http_addition_merge_conf(ngx_conf_t *cf, void *parent, | |
25 void *child); | |
681 | 26 static ngx_int_t ngx_http_addition_filter_init(ngx_conf_t *cf); |
629 | 27 |
28 | |
29 static ngx_command_t ngx_http_addition_commands[] = { | |
30 | |
31 { ngx_string("add_before_body"), | |
32 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
33 ngx_conf_set_str_slot, | |
34 NGX_HTTP_LOC_CONF_OFFSET, | |
35 offsetof(ngx_http_addition_conf_t, before_body), | |
36 NULL }, | |
37 | |
38 { ngx_string("add_after_body"), | |
39 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
40 ngx_conf_set_str_slot, | |
41 NGX_HTTP_LOC_CONF_OFFSET, | |
42 offsetof(ngx_http_addition_conf_t, after_body), | |
43 NULL }, | |
44 | |
45 ngx_null_command | |
46 }; | |
47 | |
48 | |
49 static ngx_http_module_t ngx_http_addition_filter_module_ctx = { | |
50 NULL, /* preconfiguration */ | |
681 | 51 ngx_http_addition_filter_init, /* postconfiguration */ |
629 | 52 |
53 NULL, /* create main configuration */ | |
54 NULL, /* init main configuration */ | |
55 | |
56 NULL, /* create server configuration */ | |
57 NULL, /* merge server configuration */ | |
58 | |
59 ngx_http_addition_create_conf, /* create location configuration */ | |
60 ngx_http_addition_merge_conf /* merge location configuration */ | |
61 }; | |
62 | |
63 | |
64 ngx_module_t ngx_http_addition_filter_module = { | |
65 NGX_MODULE_V1, | |
66 &ngx_http_addition_filter_module_ctx, /* module context */ | |
67 ngx_http_addition_commands, /* module directives */ | |
68 NGX_HTTP_MODULE, /* module type */ | |
69 NULL, /* init master */ | |
681 | 70 NULL, /* init module */ |
629 | 71 NULL, /* init process */ |
72 NULL, /* init thread */ | |
73 NULL, /* exit thread */ | |
74 NULL, /* exit process */ | |
75 NULL, /* exit master */ | |
76 NGX_MODULE_V1_PADDING | |
77 }; | |
78 | |
79 | |
80 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; | |
81 static ngx_http_output_body_filter_pt ngx_http_next_body_filter; | |
82 | |
83 | |
84 static ngx_int_t | |
85 ngx_http_addition_header_filter(ngx_http_request_t *r) | |
86 { | |
87 ngx_http_addition_ctx_t *ctx; | |
88 ngx_http_addition_conf_t *conf; | |
89 | |
90 if (r->headers_out.status != NGX_HTTP_OK || r != r->main) { | |
91 return ngx_http_next_header_filter(r); | |
92 } | |
93 | |
631 | 94 conf = ngx_http_get_module_loc_conf(r, ngx_http_addition_filter_module); |
95 | |
96 if (conf->before_body.len == 0 && conf->after_body.len == 0) { | |
97 return ngx_http_next_header_filter(r); | |
98 } | |
99 | |
629 | 100 if (ngx_strncasecmp(r->headers_out.content_type.data, "text/html", |
101 sizeof("text/html") - 1) | |
102 != 0) | |
103 { | |
104 return ngx_http_next_header_filter(r); | |
105 } | |
106 | |
107 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_addition_ctx_t)); | |
108 if (ctx == NULL) { | |
109 return NGX_ERROR; | |
110 } | |
111 | |
112 ngx_http_set_ctx(r, ctx, ngx_http_addition_filter_module); | |
113 | |
114 ngx_http_clear_content_length(r); | |
115 ngx_http_clear_accept_ranges(r); | |
116 | |
117 return ngx_http_next_header_filter(r); | |
118 } | |
119 | |
120 | |
121 static ngx_int_t | |
122 ngx_http_addition_body_filter(ngx_http_request_t *r, ngx_chain_t *in) | |
123 { | |
124 ngx_int_t rc; | |
125 ngx_uint_t last; | |
126 ngx_chain_t *cl; | |
758
86bb73dc8d40
fix <!--#include virtual=... wait="yes" -->
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
127 ngx_http_request_t *sr; |
629 | 128 ngx_http_addition_ctx_t *ctx; |
129 ngx_http_addition_conf_t *conf; | |
130 | |
131 if (in == NULL || r->header_only) { | |
132 return ngx_http_next_body_filter(r, in); | |
133 } | |
134 | |
135 ctx = ngx_http_get_module_ctx(r, ngx_http_addition_filter_module); | |
136 | |
137 if (ctx == NULL) { | |
138 return ngx_http_next_body_filter(r, in); | |
139 } | |
140 | |
141 conf = ngx_http_get_module_loc_conf(r, ngx_http_addition_filter_module); | |
142 | |
143 if (!ctx->before_body_sent) { | |
144 ctx->before_body_sent = 1; | |
145 | |
146 if (conf->before_body.len) { | |
777
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
758
diff
changeset
|
147 rc = ngx_http_subrequest(r, &conf->before_body, NULL, &sr, NULL, 0); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
758
diff
changeset
|
148 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
758
diff
changeset
|
149 if (rc == NGX_ERROR || rc == NGX_DONE) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
758
diff
changeset
|
150 return rc; |
629 | 151 } |
152 } | |
153 } | |
154 | |
846
c2cae54f2045
fix add_before_body without add_after_body
Igor Sysoev <igor@sysoev.ru>
parents:
777
diff
changeset
|
155 if (conf->after_body.len == 0) { |
c2cae54f2045
fix add_before_body without add_after_body
Igor Sysoev <igor@sysoev.ru>
parents:
777
diff
changeset
|
156 ngx_http_set_ctx(r, NULL, ngx_http_addition_filter_module); |
c2cae54f2045
fix add_before_body without add_after_body
Igor Sysoev <igor@sysoev.ru>
parents:
777
diff
changeset
|
157 return ngx_http_next_body_filter(r, in); |
c2cae54f2045
fix add_before_body without add_after_body
Igor Sysoev <igor@sysoev.ru>
parents:
777
diff
changeset
|
158 } |
c2cae54f2045
fix add_before_body without add_after_body
Igor Sysoev <igor@sysoev.ru>
parents:
777
diff
changeset
|
159 |
629 | 160 last = 0; |
161 | |
162 for (cl = in; cl; cl = cl->next) { | |
163 if (cl->buf->last_buf) { | |
164 cl->buf->last_buf = 0; | |
631 | 165 cl->buf->sync = 1; |
629 | 166 last = 1; |
167 } | |
168 } | |
169 | |
170 rc = ngx_http_next_body_filter(r, in); | |
171 | |
631 | 172 if (rc == NGX_ERROR || !last || conf->after_body.len == 0) { |
629 | 173 return rc; |
174 } | |
175 | |
777
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
758
diff
changeset
|
176 rc = ngx_http_subrequest(r, &conf->after_body, NULL, &sr, NULL, 0); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
758
diff
changeset
|
177 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
758
diff
changeset
|
178 if (rc == NGX_ERROR || rc == NGX_DONE) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
758
diff
changeset
|
179 return rc; |
629 | 180 } |
181 | |
631 | 182 ngx_http_set_ctx(r, NULL, ngx_http_addition_filter_module); |
629 | 183 |
184 return ngx_http_send_special(r, NGX_HTTP_LAST); | |
185 } | |
186 | |
187 | |
188 static ngx_int_t | |
681 | 189 ngx_http_addition_filter_init(ngx_conf_t *cf) |
629 | 190 { |
191 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
192 ngx_http_top_header_filter = ngx_http_addition_header_filter; | |
193 | |
194 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
195 ngx_http_top_body_filter = ngx_http_addition_body_filter; | |
196 | |
197 return NGX_OK; | |
198 } | |
199 | |
200 | |
201 static void * | |
202 ngx_http_addition_create_conf(ngx_conf_t *cf) | |
203 { | |
204 ngx_http_addition_conf_t *conf; | |
205 | |
206 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_addition_conf_t)); | |
207 if (conf == NULL) { | |
208 return NGX_CONF_ERROR; | |
209 } | |
210 | |
211 /* | |
212 * set by ngx_pcalloc(): | |
213 * | |
214 * conf->before_body.len = 0; | |
215 * conf->before_body.date = NULL; | |
216 * conf->after_body.len = 0; | |
217 * conf->after_body.date = NULL; | |
218 */ | |
219 | |
220 return conf; | |
221 } | |
222 | |
223 | |
224 static char * | |
225 ngx_http_addition_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
226 { | |
227 ngx_http_addition_conf_t *prev = parent; | |
228 ngx_http_addition_conf_t *conf = child; | |
229 | |
230 ngx_conf_merge_str_value(conf->before_body, prev->before_body, ""); | |
231 ngx_conf_merge_str_value(conf->after_body, prev->after_body, ""); | |
232 | |
233 return NGX_CONF_OK; | |
234 } |