comparison src/http/modules/ngx_http_chunked_filter.c @ 0:f0b350454894 NGINX_0_1_0

nginx 0.1.0 *) The first public version.
author Igor Sysoev <http://sysoev.ru>
date Mon, 04 Oct 2004 00:00:00 +0400
parents
children cc9f381affaa
comparison
equal deleted inserted replaced
-1:000000000000 0:f0b350454894
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 static ngx_int_t ngx_http_chunked_filter_init(ngx_cycle_t *cycle);
13
14
15 static ngx_http_module_t ngx_http_chunked_filter_module_ctx = {
16 NULL, /* pre conf */
17
18 NULL, /* create main configuration */
19 NULL, /* init main configuration */
20
21 NULL, /* create server configuration */
22 NULL, /* merge server configuration */
23
24 NULL, /* create location configuration */
25 NULL, /* merge location configuration */
26 };
27
28
29 ngx_module_t ngx_http_chunked_filter_module = {
30 NGX_MODULE,
31 &ngx_http_chunked_filter_module_ctx, /* module context */
32 NULL, /* module directives */
33 NGX_HTTP_MODULE, /* module type */
34 ngx_http_chunked_filter_init, /* init module */
35 NULL /* init child */
36 };
37
38
39 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
40 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
41
42
43 static ngx_int_t ngx_http_chunked_header_filter(ngx_http_request_t *r)
44 {
45 if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) {
46 return ngx_http_next_header_filter(r);
47 }
48
49 if (r->headers_out.content_length_n == -1) {
50 if (r->http_version < NGX_HTTP_VERSION_11) {
51 r->keepalive = 0;
52
53 } else {
54 r->chunked = 1;
55 }
56 }
57
58 return ngx_http_next_header_filter(r);
59 }
60
61
62 static ngx_int_t ngx_http_chunked_body_filter(ngx_http_request_t *r,
63 ngx_chain_t *in)
64 {
65 u_char *chunk;
66 size_t size, len;
67 ngx_buf_t *b;
68 ngx_chain_t out, tail, *cl, *tl, **ll;
69
70 if (in == NULL || !r->chunked) {
71 return ngx_http_next_body_filter(r, in);
72 }
73
74 out.buf = NULL;
75 ll = &out.next;
76
77 size = 0;
78 cl = in;
79
80 for ( ;; ) {
81 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
82 "http chunk: %d", ngx_buf_size(cl->buf));
83
84 size += ngx_buf_size(cl->buf);
85
86 ngx_test_null(tl, ngx_alloc_chain_link(r->pool), NGX_ERROR);
87 tl->buf = cl->buf;
88 *ll = tl;
89 ll = &tl->next;
90
91 if (cl->next == NULL) {
92 break;
93 }
94
95 cl = cl->next;
96 }
97
98 if (size) {
99 ngx_test_null(chunk, ngx_palloc(r->pool, 11), NGX_ERROR);
100 len = ngx_snprintf((char *) chunk, 11, SIZE_T_X_FMT CRLF, size);
101
102 ngx_test_null(b, ngx_calloc_buf(r->pool), NGX_ERROR);
103 b->temporary = 1;
104 b->pos = chunk;
105 b->last = chunk + len;
106
107 out.buf = b;
108 }
109
110 if (cl->buf->last_buf) {
111 ngx_test_null(b, ngx_calloc_buf(r->pool), NGX_ERROR);
112 b->memory = 1;
113 b->last_buf = 1;
114 b->pos = (u_char *) CRLF "0" CRLF CRLF;
115 b->last = b->pos + 7;
116
117 cl->buf->last_buf = 0;
118
119 if (size == 0) {
120 b->pos += 2;
121 out.buf = b;
122 out.next = NULL;
123
124 return ngx_http_next_body_filter(r, &out);
125 }
126
127 } else {
128 if (size == 0) {
129 *ll = NULL;
130 return ngx_http_next_body_filter(r, out.next);
131 }
132
133 ngx_test_null(b, ngx_calloc_buf(r->pool), NGX_ERROR);
134 b->memory = 1;
135 b->pos = (u_char *) CRLF;
136 b->last = b->pos + 2;
137 }
138
139 tail.buf = b;
140 tail.next = NULL;
141 *ll = &tail;
142
143 return ngx_http_next_body_filter(r, &out);
144 }
145
146
147 static ngx_int_t ngx_http_chunked_filter_init(ngx_cycle_t *cycle)
148 {
149 ngx_http_next_header_filter = ngx_http_top_header_filter;
150 ngx_http_top_header_filter = ngx_http_chunked_header_filter;
151
152 ngx_http_next_body_filter = ngx_http_top_body_filter;
153 ngx_http_top_body_filter = ngx_http_chunked_body_filter;
154
155 return NGX_OK;
156 }