50
|
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
|
640
|
12 typedef struct {
|
|
13 ngx_chain_t *free;
|
|
14 ngx_chain_t *busy;
|
|
15 } ngx_http_chunked_filter_ctx_t;
|
|
16
|
|
17
|
230
|
18 static ngx_int_t ngx_http_chunked_filter_init(ngx_conf_t *cf);
|
50
|
19
|
|
20
|
|
21 static ngx_http_module_t ngx_http_chunked_filter_module_ctx = {
|
58
|
22 NULL, /* preconfiguration */
|
230
|
23 ngx_http_chunked_filter_init, /* postconfiguration */
|
50
|
24
|
|
25 NULL, /* create main configuration */
|
|
26 NULL, /* init main configuration */
|
|
27
|
|
28 NULL, /* create server configuration */
|
|
29 NULL, /* merge server configuration */
|
|
30
|
|
31 NULL, /* create location configuration */
|
178
|
32 NULL /* merge location configuration */
|
50
|
33 };
|
|
34
|
|
35
|
|
36 ngx_module_t ngx_http_chunked_filter_module = {
|
58
|
37 NGX_MODULE_V1,
|
50
|
38 &ngx_http_chunked_filter_module_ctx, /* module context */
|
|
39 NULL, /* module directives */
|
|
40 NGX_HTTP_MODULE, /* module type */
|
90
|
41 NULL, /* init master */
|
230
|
42 NULL, /* init module */
|
90
|
43 NULL, /* init process */
|
|
44 NULL, /* init thread */
|
|
45 NULL, /* exit thread */
|
|
46 NULL, /* exit process */
|
|
47 NULL, /* exit master */
|
|
48 NGX_MODULE_V1_PADDING
|
50
|
49 };
|
|
50
|
|
51
|
|
52 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
53 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
|
|
54
|
|
55
|
|
56 static ngx_int_t
|
|
57 ngx_http_chunked_header_filter(ngx_http_request_t *r)
|
|
58 {
|
640
|
59 ngx_http_core_loc_conf_t *clcf;
|
|
60 ngx_http_chunked_filter_ctx_t *ctx;
|
566
|
61
|
164
|
62 if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED
|
|
63 || r->headers_out.status == NGX_HTTP_NO_CONTENT
|
412
|
64 || r != r->main
|
|
65 || (r->method & NGX_HTTP_HEAD))
|
164
|
66 {
|
50
|
67 return ngx_http_next_header_filter(r);
|
|
68 }
|
|
69
|
|
70 if (r->headers_out.content_length_n == -1) {
|
|
71 if (r->http_version < NGX_HTTP_VERSION_11) {
|
|
72 r->keepalive = 0;
|
|
73
|
|
74 } else {
|
566
|
75 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
76
|
|
77 if (clcf->chunked_transfer_encoding) {
|
|
78 r->chunked = 1;
|
|
79
|
640
|
80 ctx = ngx_pcalloc(r->pool,
|
|
81 sizeof(ngx_http_chunked_filter_ctx_t));
|
|
82 if (ctx == NULL) {
|
|
83 return NGX_ERROR;
|
|
84 }
|
|
85
|
|
86 ngx_http_set_ctx(r, ctx, ngx_http_chunked_filter_module);
|
|
87
|
566
|
88 } else {
|
|
89 r->keepalive = 0;
|
|
90 }
|
50
|
91 }
|
|
92 }
|
|
93
|
|
94 return ngx_http_next_header_filter(r);
|
|
95 }
|
|
96
|
|
97
|
|
98 static ngx_int_t
|
|
99 ngx_http_chunked_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
100 {
|
640
|
101 u_char *chunk;
|
|
102 off_t size;
|
|
103 ngx_int_t rc;
|
|
104 ngx_buf_t *b;
|
|
105 ngx_chain_t *out, *cl, *tl, **ll;
|
|
106 ngx_http_chunked_filter_ctx_t *ctx;
|
50
|
107
|
|
108 if (in == NULL || !r->chunked || r->header_only) {
|
|
109 return ngx_http_next_body_filter(r, in);
|
|
110 }
|
|
111
|
640
|
112 ctx = ngx_http_get_module_ctx(r, ngx_http_chunked_filter_module);
|
|
113
|
|
114 out = NULL;
|
|
115 ll = &out;
|
50
|
116
|
|
117 size = 0;
|
|
118 cl = in;
|
|
119
|
|
120 for ( ;; ) {
|
|
121 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
122 "http chunk: %d", ngx_buf_size(cl->buf));
|
|
123
|
|
124 size += ngx_buf_size(cl->buf);
|
|
125
|
216
|
126 if (cl->buf->flush
|
|
127 || cl->buf->sync
|
|
128 || ngx_buf_in_memory(cl->buf)
|
|
129 || cl->buf->in_file)
|
|
130 {
|
50
|
131 tl = ngx_alloc_chain_link(r->pool);
|
|
132 if (tl == NULL) {
|
|
133 return NGX_ERROR;
|
|
134 }
|
|
135
|
|
136 tl->buf = cl->buf;
|
|
137 *ll = tl;
|
|
138 ll = &tl->next;
|
|
139 }
|
|
140
|
|
141 if (cl->next == NULL) {
|
|
142 break;
|
|
143 }
|
|
144
|
|
145 cl = cl->next;
|
|
146 }
|
|
147
|
|
148 if (size) {
|
640
|
149 tl = ngx_chain_get_free_buf(r->pool, &ctx->free);
|
|
150 if (tl == NULL) {
|
50
|
151 return NGX_ERROR;
|
|
152 }
|
|
153
|
640
|
154 b = tl->buf;
|
|
155 chunk = b->start;
|
|
156
|
|
157 if (chunk == NULL) {
|
|
158 /* the "0000000000000000" is 64-bit hexadecimal string */
|
50
|
159
|
640
|
160 chunk = ngx_palloc(r->pool, sizeof("0000000000000000" CRLF) - 1);
|
|
161 if (chunk == NULL) {
|
|
162 return NGX_ERROR;
|
|
163 }
|
|
164
|
|
165 b->start = chunk;
|
|
166 b->end = chunk + sizeof("0000000000000000" CRLF) - 1;
|
50
|
167 }
|
|
168
|
640
|
169 b->tag = (ngx_buf_tag_t) &ngx_http_chunked_filter_module;
|
|
170 b->memory = 0;
|
50
|
171 b->temporary = 1;
|
|
172 b->pos = chunk;
|
|
173 b->last = ngx_sprintf(chunk, "%xO" CRLF, size);
|
|
174
|
640
|
175 tl->next = out;
|
|
176 out = tl;
|
50
|
177 }
|
|
178
|
|
179 if (cl->buf->last_buf) {
|
640
|
180 tl = ngx_chain_get_free_buf(r->pool, &ctx->free);
|
|
181 if (tl == NULL) {
|
50
|
182 return NGX_ERROR;
|
|
183 }
|
|
184
|
640
|
185 b = tl->buf;
|
|
186
|
|
187 b->tag = (ngx_buf_tag_t) &ngx_http_chunked_filter_module;
|
|
188 b->temporary = 0;
|
50
|
189 b->memory = 1;
|
|
190 b->last_buf = 1;
|
|
191 b->pos = (u_char *) CRLF "0" CRLF CRLF;
|
|
192 b->last = b->pos + 7;
|
|
193
|
|
194 cl->buf->last_buf = 0;
|
|
195
|
640
|
196 *ll = tl;
|
|
197
|
50
|
198 if (size == 0) {
|
|
199 b->pos += 2;
|
|
200 }
|
|
201
|
640
|
202 } else if (size > 0) {
|
|
203 tl = ngx_chain_get_free_buf(r->pool, &ctx->free);
|
|
204 if (tl == NULL) {
|
50
|
205 return NGX_ERROR;
|
|
206 }
|
|
207
|
640
|
208 b = tl->buf;
|
|
209
|
|
210 b->tag = (ngx_buf_tag_t) &ngx_http_chunked_filter_module;
|
|
211 b->temporary = 0;
|
50
|
212 b->memory = 1;
|
|
213 b->pos = (u_char *) CRLF;
|
|
214 b->last = b->pos + 2;
|
640
|
215
|
|
216 *ll = tl;
|
|
217
|
|
218 } else {
|
|
219 *ll = NULL;
|
50
|
220 }
|
|
221
|
640
|
222 rc = ngx_http_next_body_filter(r, out);
|
50
|
223
|
640
|
224 ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &out,
|
|
225 (ngx_buf_tag_t) &ngx_http_chunked_filter_module);
|
|
226
|
|
227 return rc;
|
50
|
228 }
|
|
229
|
|
230
|
|
231 static ngx_int_t
|
230
|
232 ngx_http_chunked_filter_init(ngx_conf_t *cf)
|
50
|
233 {
|
|
234 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
235 ngx_http_top_header_filter = ngx_http_chunked_header_filter;
|
|
236
|
|
237 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
238 ngx_http_top_body_filter = ngx_http_chunked_body_filter;
|
|
239
|
|
240 return NGX_OK;
|
|
241 }
|