3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 static ngx_int_t ngx_http_chunked_filter_init(ngx_conf_t *cf);
15 static ngx_http_module_t ngx_http_chunked_filter_module_ctx = {
16 NULL, /* preconfiguration */
17 ngx_http_chunked_filter_init, /* postconfiguration */
19 NULL, /* create main configuration */
20 NULL, /* init main configuration */
22 NULL, /* create server configuration */
23 NULL, /* merge server configuration */
25 NULL, /* create location configuration */
26 NULL /* merge location configuration */
30 ngx_module_t ngx_http_chunked_filter_module = {
32 &ngx_http_chunked_filter_module_ctx, /* module context */
33 NULL, /* module directives */
34 NGX_HTTP_MODULE, /* module type */
35 NULL, /* init master */
36 NULL, /* init module */
37 NULL, /* init process */
38 NULL, /* init thread */
39 NULL, /* exit thread */
40 NULL, /* exit process */
41 NULL, /* exit master */
46 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
47 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
51 ngx_http_chunked_header_filter(ngx_http_request_t *r)
53 if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED
54 || r->headers_out.status == NGX_HTTP_NO_CONTENT
55 || r->headers_out.status == NGX_HTTP_CREATED
57 || (r->method & NGX_HTTP_HEAD))
59 return ngx_http_next_header_filter(r);
62 if (r->headers_out.content_length_n == -1) {
63 if (r->http_version < NGX_HTTP_VERSION_11) {
71 return ngx_http_next_header_filter(r);
76 ngx_http_chunked_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
81 ngx_chain_t out, tail, *cl, *tl, **ll;
83 if (in == NULL || !r->chunked || r->header_only) {
84 return ngx_http_next_body_filter(r, in);
94 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
95 "http chunk: %d", ngx_buf_size(cl->buf));
97 size += ngx_buf_size(cl->buf);
101 || ngx_buf_in_memory(cl->buf)
104 tl = ngx_alloc_chain_link(r->pool);
114 if (cl->next == NULL) {
122 b = ngx_calloc_buf(r->pool);
127 /* the "0000000000000000" is 64-bit hexadimal string */
129 chunk = ngx_palloc(r->pool, sizeof("0000000000000000" CRLF) - 1);
136 b->last = ngx_sprintf(chunk, "%xO" CRLF, size);
141 if (cl->buf->last_buf) {
142 b = ngx_calloc_buf(r->pool);
149 b->pos = (u_char *) CRLF "0" CRLF CRLF;
150 b->last = b->pos + 7;
152 cl->buf->last_buf = 0;
159 return ngx_http_next_body_filter(r, &out);
165 return ngx_http_next_body_filter(r, out.next);
168 b = ngx_calloc_buf(r->pool);
174 b->pos = (u_char *) CRLF;
175 b->last = b->pos + 2;
182 return ngx_http_next_body_filter(r, &out);
187 ngx_http_chunked_filter_init(ngx_conf_t *cf)
189 ngx_http_next_header_filter = ngx_http_top_header_filter;
190 ngx_http_top_header_filter = ngx_http_chunked_header_filter;
192 ngx_http_next_body_filter = ngx_http_top_body_filter;
193 ngx_http_top_body_filter = ngx_http_chunked_body_filter;