Mercurial > hg > nginx
comparison src/http/ngx_http_header_filter.c @ 41:59e7c7f30d49
nginx-0.0.1-2002-12-26-19:26:23 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 26 Dec 2002 16:26:23 +0000 |
parents | 53cb81681040 |
children | cd035a94e0b6 |
comparison
equal
deleted
inserted
replaced
40:d5d4f3bba6f0 | 41:59e7c7f30d49 |
---|---|
4 #include <ngx_config.h> | 4 #include <ngx_config.h> |
5 #include <ngx_core.h> | 5 #include <ngx_core.h> |
6 #include <ngx_string.h> | 6 #include <ngx_string.h> |
7 #include <ngx_table.h> | 7 #include <ngx_table.h> |
8 #include <ngx_hunk.h> | 8 #include <ngx_hunk.h> |
9 #include <ngx_config_file.h> | |
9 #include <ngx_http.h> | 10 #include <ngx_http.h> |
11 #include <ngx_http_write_filter.h> | |
10 | 12 |
11 | 13 |
12 static int ngx_http_header_filter(ngx_http_request_t *r); | 14 static int ngx_http_header_filter(ngx_http_request_t *r); |
13 | 15 |
14 ngx_http_module_t ngx_http_header_filter_module = { | 16 |
17 ngx_http_module_t ngx_http_header_filter_module_ctx = { | |
15 NGX_HTTP_MODULE, | 18 NGX_HTTP_MODULE, |
16 | 19 |
17 NULL, /* create server config */ | 20 NULL, /* create server config */ |
18 NULL, /* create location config */ | 21 NULL, /* create location config */ |
19 NULL, /* module directives */ | 22 |
20 | |
21 NULL, /* init module */ | |
22 NULL, /* translate handler */ | 23 NULL, /* translate handler */ |
23 | 24 |
24 ngx_http_header_filter, /* output header filter */ | 25 ngx_http_header_filter, /* output header filter */ |
25 NULL, /* next output header filter */ | 26 NULL, /* next output header filter */ |
26 NULL, /* output body filter */ | 27 NULL, /* output body filter */ |
27 NULL /* next output body filter */ | 28 NULL /* next output body filter */ |
28 }; | 29 }; |
29 | 30 |
30 | 31 |
32 ngx_module_t ngx_http_header_filter_module = { | |
33 &ngx_http_header_filter_module_ctx, /* module context */ | |
34 NULL, /* module directives */ | |
35 NGX_HTTP_MODULE_TYPE, /* module type */ | |
36 NULL /* init module */ | |
37 }; | |
38 | |
39 | |
31 static char server_string[] = "Server: " NGINX_VER CRLF; | 40 static char server_string[] = "Server: " NGINX_VER CRLF; |
32 | 41 |
33 | 42 |
34 static ngx_str_t http_codes[] = { | 43 static ngx_str_t http_codes[] = { |
35 | 44 |
45 ngx_string("200 OK"), | |
46 #if 0 | |
36 { 6, "200 OK" }, | 47 { 6, "200 OK" }, |
48 #endif | |
37 | 49 |
38 { 21, "301 Moved Permanently" }, | 50 { 21, "301 Moved Permanently" }, |
39 { 21, "302 Moved Temporarily" }, | 51 { 21, "302 Moved Temporarily" }, |
40 { 0, NULL }, | 52 { 0, NULL }, |
41 { 16, "304 Not Modified" }, | 53 { 16, "304 Not Modified" }, |
57 time_t ims; | 69 time_t ims; |
58 ngx_hunk_t *h; | 70 ngx_hunk_t *h; |
59 ngx_chain_t *ch; | 71 ngx_chain_t *ch; |
60 ngx_table_elt_t *header; | 72 ngx_table_elt_t *header; |
61 | 73 |
62 if (r->http_version < NGX_HTTP_VERSION_10) | 74 if (r->http_version < NGX_HTTP_VERSION_10) { |
63 return NGX_OK; | 75 return NGX_OK; |
76 } | |
64 | 77 |
65 /* 9 is for "HTTP/1.x ", 2 is for trailing "\r\n" | 78 /* 9 is for "HTTP/1.x ", 2 is for trailing "\r\n" |
66 and 2 is for end of header */ | 79 and 2 is for end of header */ |
67 len = 9 + 2 + 2; | 80 len = 9 + 2 + 2; |
68 | 81 |
87 } | 100 } |
88 | 101 |
89 /* status line */ | 102 /* status line */ |
90 if (r->headers_out.status_line.len) { | 103 if (r->headers_out.status_line.len) { |
91 len += r->headers_out.status_line.len; | 104 len += r->headers_out.status_line.len; |
92 } else { | 105 |
93 if (r->headers_out.status < NGX_HTTP_MOVED_PERMANENTLY) | 106 } else { |
107 if (r->headers_out.status < NGX_HTTP_MOVED_PERMANENTLY) { | |
94 status = r->headers_out.status - NGX_HTTP_OK; | 108 status = r->headers_out.status - NGX_HTTP_OK; |
95 | 109 |
96 else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) | 110 } else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) { |
97 status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY + 1; | 111 status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY + 1; |
98 | 112 |
99 else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) | 113 } else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) { |
100 status = r->headers_out.status - NGX_HTTP_BAD_REQUEST + 1 + 4; | 114 status = r->headers_out.status - NGX_HTTP_BAD_REQUEST + 1 + 4; |
101 | 115 |
102 else | 116 } else { |
103 status = r->headers_out.status | 117 status = r->headers_out.status |
104 - NGX_HTTP_INTERNAL_SERVER_ERROR + 1 + 4 + 5; | 118 - NGX_HTTP_INTERNAL_SERVER_ERROR + 1 + 4 + 5; |
119 } | |
105 | 120 |
106 len += http_codes[status].len; | 121 len += http_codes[status].len; |
107 } | 122 } |
108 | 123 |
109 if (r->headers_out.server && r->headers_out.server->key.len) { | 124 if (r->headers_out.server && r->headers_out.server->key.len) { |
120 /* "Date: ... \r\n"; */ | 135 /* "Date: ... \r\n"; */ |
121 len += 37; | 136 len += 37; |
122 } | 137 } |
123 | 138 |
124 /* 2^64 is 20 characters */ | 139 /* 2^64 is 20 characters */ |
125 if (r->headers_out.content_length >= 0) | 140 if (r->headers_out.content_length >= 0) { |
126 len += 48; | 141 len += 48; |
142 } | |
127 | 143 |
128 #if 0 | 144 #if 0 |
129 if (r->headers_out.content_type.len) | 145 if (r->headers_out.content_type.len) |
130 len += r->headers_out.content_type.len + 16; | 146 len += r->headers_out.content_type.len + 16; |
131 #endif | 147 #endif |
136 } else if (r->headers_out.last_modified_time != -1) { | 152 } else if (r->headers_out.last_modified_time != -1) { |
137 /* "Last-Modified: ... \r\n"; */ | 153 /* "Last-Modified: ... \r\n"; */ |
138 len += 46; | 154 len += 46; |
139 } | 155 } |
140 | 156 |
141 if (r->keepalive) | 157 if (r->keepalive) { |
142 len += 24; | 158 len += 24; |
143 else | 159 } else { |
144 len += 19; | 160 len += 19; |
161 } | |
145 | 162 |
146 header = (ngx_table_elt_t *) r->headers_out.headers->elts; | 163 header = (ngx_table_elt_t *) r->headers_out.headers->elts; |
147 for (i = 0; i < r->headers_out.headers->nelts; i++) { | 164 for (i = 0; i < r->headers_out.headers->nelts; i++) { |
148 if (header[i].key.len == 0) | 165 if (header[i].key.len == 0) { |
149 continue; | 166 continue; |
167 } | |
150 | 168 |
151 len += header[i].key.len + 2 + header[i].value.len + 2; | 169 len += header[i].key.len + 2 + header[i].value.len + 2; |
152 } | 170 } |
153 | 171 |
154 ngx_test_null(h, ngx_create_temp_hunk(r->pool, len, 0, 64), NGX_ERROR); | 172 ngx_test_null(h, ngx_create_temp_hunk(r->pool, len, 0, 64), NGX_ERROR); |
181 h->last.mem += ngx_http_get_time(h->last.mem, time(NULL)); | 199 h->last.mem += ngx_http_get_time(h->last.mem, time(NULL)); |
182 *(h->last.mem++) = CR; *(h->last.mem++) = LF; | 200 *(h->last.mem++) = CR; *(h->last.mem++) = LF; |
183 } | 201 } |
184 | 202 |
185 /* 2^64 is 20 characters */ | 203 /* 2^64 is 20 characters */ |
186 if (r->headers_out.content_length >= 0) | 204 if (r->headers_out.content_length >= 0) { |
187 h->last.mem += ngx_snprintf(h->last.mem, 49, "Content-Length: %u" CRLF, | 205 h->last.mem += ngx_snprintf(h->last.mem, 49, "Content-Length: %u" CRLF, |
188 r->headers_out.content_length); | 206 r->headers_out.content_length); |
207 } | |
189 | 208 |
190 #if 0 | 209 #if 0 |
191 if (r->headers_out.content_type.len) { | 210 if (r->headers_out.content_type.len) { |
192 ngx_memcpy(h->last.mem, "Content-Type: ", 14); | 211 ngx_memcpy(h->last.mem, "Content-Type: ", 14); |
193 h->last.mem += 14; | 212 h->last.mem += 14; |
217 ngx_memcpy(h->last.mem, "Connection: close" CRLF, 19); | 236 ngx_memcpy(h->last.mem, "Connection: close" CRLF, 19); |
218 h->last.mem += 19; | 237 h->last.mem += 19; |
219 } | 238 } |
220 | 239 |
221 for (i = 0; i < r->headers_out.headers->nelts; i++) { | 240 for (i = 0; i < r->headers_out.headers->nelts; i++) { |
222 if (header[i].key.len == 0) | 241 if (header[i].key.len == 0) { |
223 continue; | 242 continue; |
243 } | |
224 | 244 |
225 ngx_memcpy(h->last.mem, header[i].key.data, header[i].key.len); | 245 ngx_memcpy(h->last.mem, header[i].key.data, header[i].key.len); |
226 h->last.mem += header[i].key.len; | 246 h->last.mem += header[i].key.len; |
227 *(h->last.mem++) = ':' ; *(h->last.mem++) = ' ' ; | 247 *(h->last.mem++) = ':' ; *(h->last.mem++) = ' ' ; |
228 | 248 |
237 /**/ | 257 /**/ |
238 | 258 |
239 /* end of HTTP header */ | 259 /* end of HTTP header */ |
240 *(h->last.mem++) = CR; *(h->last.mem++) = LF; | 260 *(h->last.mem++) = CR; *(h->last.mem++) = LF; |
241 | 261 |
242 if (r->header_only) | 262 if (r->header_only) { |
243 h->type |= NGX_HUNK_LAST; | 263 h->type |= NGX_HUNK_LAST; |
264 } | |
244 | 265 |
245 ngx_test_null(ch, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR); | 266 ngx_test_null(ch, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR); |
246 | 267 |
247 ch->hunk = h; | 268 ch->hunk = h; |
248 ch->next = NULL; | 269 ch->next = NULL; |