0
|
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 #include <nginx.h>
|
|
11
|
|
12
|
|
13 static ngx_int_t ngx_http_header_filter_init(ngx_cycle_t *cycle);
|
|
14 static ngx_int_t ngx_http_header_filter(ngx_http_request_t *r);
|
|
15
|
|
16
|
|
17 static ngx_http_module_t ngx_http_header_filter_module_ctx = {
|
|
18 NULL, /* pre conf */
|
|
19
|
|
20 NULL, /* create main configuration */
|
|
21 NULL, /* init main configuration */
|
|
22
|
|
23 NULL, /* create server configuration */
|
|
24 NULL, /* merge server configuration */
|
|
25
|
|
26 NULL, /* create location configuration */
|
|
27 NULL, /* merge location configuration */
|
|
28 };
|
|
29
|
|
30
|
|
31 ngx_module_t ngx_http_header_filter_module = {
|
|
32 NGX_MODULE,
|
|
33 &ngx_http_header_filter_module_ctx, /* module context */
|
|
34 NULL, /* module directives */
|
|
35 NGX_HTTP_MODULE, /* module type */
|
|
36 ngx_http_header_filter_init, /* init module */
|
|
37 NULL /* init child */
|
|
38 };
|
|
39
|
|
40
|
|
41 static char server_string[] = "Server: " NGINX_VER CRLF;
|
|
42
|
|
43
|
|
44 static ngx_str_t http_codes[] = {
|
|
45
|
|
46 ngx_string("200 OK"),
|
|
47 ngx_null_string, /* "201 Created" */
|
|
48 ngx_null_string, /* "202 Accepted" */
|
|
49 ngx_null_string, /* "203 Non-Authoritative Information" */
|
|
50 ngx_null_string, /* "204 No Content" */
|
|
51 ngx_null_string, /* "205 Reset Content" */
|
|
52 ngx_string("206 Partial Content"),
|
|
53 ngx_null_string, /* "207 Multi-Status" */
|
|
54
|
|
55 #if 0
|
|
56 ngx_null_string, /* "300 Multiple Choices" */
|
|
57 #endif
|
|
58
|
|
59 ngx_string("301 Moved Permanently"),
|
|
60 #if 0
|
|
61 ngx_string("302 Moved Temporarily"),
|
|
62 #else
|
|
63 ngx_string("302 Found"),
|
|
64 #endif
|
|
65 ngx_null_string, /* "303 See Other" */
|
|
66 ngx_string("304 Not Modified"),
|
|
67
|
|
68 ngx_string("400 Bad Request"),
|
|
69 ngx_string("401 Unauthorized"),
|
|
70 ngx_null_string, /* "402 Payment Required" */
|
|
71 ngx_string("403 Forbidden"),
|
|
72 ngx_string("404 Not Found"),
|
|
73 ngx_string("405 Not Allowed"),
|
|
74 ngx_null_string, /* "406 Not Acceptable" */
|
|
75 ngx_null_string, /* "407 Proxy Authentication Required" */
|
|
76 ngx_string("408 Request Time-out"),
|
|
77 ngx_null_string, /* "409 Conflict" */
|
|
78 ngx_null_string, /* "410 Gone" */
|
|
79 ngx_string("411 Length Required"),
|
|
80 ngx_null_string, /* "412 Precondition Failed" */
|
|
81 ngx_string("413 Request Entity Too Large"),
|
|
82 ngx_null_string, /* "414 Request-URI Too Large" but we never send it
|
|
83 * because we treat such requests as the HTTP/0.9
|
|
84 * requests and send only a body without a header
|
|
85 */
|
|
86 ngx_null_string, /* "415 Unsupported Media Type" */
|
|
87 ngx_string("416 Requested Range Not Satisfiable"),
|
|
88
|
|
89 ngx_string("500 Internal Server Error"),
|
|
90 ngx_string("501 Method Not Implemented"),
|
|
91 ngx_string("502 Bad Gateway"),
|
|
92 ngx_string("503 Service Temporarily Unavailable"),
|
|
93 ngx_string("504 Gateway Time-out")
|
|
94 };
|
|
95
|
|
96
|
|
97 ngx_http_header_t ngx_http_headers_out[] = {
|
|
98 { ngx_string("Server"), offsetof(ngx_http_headers_out_t, server) },
|
|
99 { ngx_string("Date"), offsetof(ngx_http_headers_out_t, date) },
|
|
100 { ngx_string("Content-Type"),
|
|
101 offsetof(ngx_http_headers_out_t, content_type) },
|
|
102 { ngx_string("Content-Length"),
|
|
103 offsetof(ngx_http_headers_out_t, content_length) },
|
|
104 { ngx_string("Content-Encoding"),
|
|
105 offsetof(ngx_http_headers_out_t, content_encoding) },
|
|
106 { ngx_string("Location"), offsetof(ngx_http_headers_out_t, location) },
|
|
107 { ngx_string("Last-Modified"),
|
|
108 offsetof(ngx_http_headers_out_t, last_modified) },
|
|
109 { ngx_string("Accept-Ranges"),
|
|
110 offsetof(ngx_http_headers_out_t, accept_ranges) },
|
|
111 { ngx_string("Expires"), offsetof(ngx_http_headers_out_t, expires) },
|
|
112 { ngx_string("Cache-Control"),
|
|
113 offsetof(ngx_http_headers_out_t, cache_control) },
|
|
114 { ngx_string("ETag"), offsetof(ngx_http_headers_out_t, etag) },
|
|
115
|
|
116 { ngx_null_string, 0 }
|
|
117 };
|
|
118
|
|
119
|
|
120 static ngx_int_t ngx_http_header_filter(ngx_http_request_t *r)
|
|
121 {
|
|
122 u_char *p;
|
|
123 size_t len;
|
|
124 ngx_uint_t status, i;
|
|
125 ngx_buf_t *b;
|
|
126 ngx_chain_t *ln;
|
|
127 ngx_list_part_t *part;
|
|
128 ngx_table_elt_t *header;
|
|
129 ngx_http_core_loc_conf_t *clcf;
|
|
130
|
|
131 if (r->http_version < NGX_HTTP_VERSION_10) {
|
|
132 return NGX_OK;
|
|
133 }
|
|
134
|
|
135 if (r->method == NGX_HTTP_HEAD) {
|
|
136 r->header_only = 1;
|
|
137 }
|
|
138
|
|
139 if (r->headers_out.last_modified_time != -1) {
|
|
140 if (r->headers_out.status != NGX_HTTP_OK
|
|
141 && r->headers_out.status != NGX_HTTP_NOT_MODIFIED
|
|
142 && r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT)
|
|
143 {
|
|
144 r->headers_out.last_modified_time = -1;
|
|
145 r->headers_out.last_modified = NULL;
|
|
146 }
|
|
147 }
|
|
148
|
|
149 /* 2 is for trailing "\r\n" and 2 is for "\r\n" in the end of header */
|
|
150 len = sizeof("HTTP/1.x ") - 1 + 2 + 2;
|
|
151
|
|
152 /* status line */
|
2
|
153
|
0
|
154 if (r->headers_out.status_line.len) {
|
|
155 len += r->headers_out.status_line.len;
|
|
156 #if (NGX_SUPPRESS_WARN)
|
|
157 status = NGX_INVALID_ARRAY_INDEX;
|
|
158 #endif
|
|
159
|
|
160 } else {
|
|
161
|
|
162 if (r->headers_out.status < NGX_HTTP_MOVED_PERMANENTLY) {
|
|
163 /* 2XX */
|
|
164 status = r->headers_out.status - NGX_HTTP_OK;
|
|
165
|
|
166 } else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) {
|
|
167 /* 3XX */
|
|
168 status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY + 8;
|
|
169
|
|
170 if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) {
|
|
171 r->header_only = 1;
|
|
172 }
|
|
173
|
|
174 } else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) {
|
|
175 /* 4XX */
|
|
176 status = r->headers_out.status - NGX_HTTP_BAD_REQUEST + 8 + 4;
|
|
177
|
|
178 } else {
|
|
179 /* 5XX */
|
|
180 status = r->headers_out.status
|
|
181 - NGX_HTTP_INTERNAL_SERVER_ERROR + 8 + 4 + 17;
|
|
182 }
|
|
183
|
|
184 len += http_codes[status].len;
|
|
185 }
|
|
186
|
|
187 if (r->headers_out.server && r->headers_out.server->key.len) {
|
|
188 len += r->headers_out.server->key.len
|
|
189 + r->headers_out.server->value.len + 2;
|
|
190 } else {
|
|
191 len += sizeof(server_string) - 1;
|
|
192 }
|
|
193
|
|
194 if (r->headers_out.date && r->headers_out.date->key.len) {
|
|
195 len += r->headers_out.date->key.len
|
|
196 + r->headers_out.date->value.len + 2;
|
|
197 } else {
|
|
198 len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
|
|
199 }
|
|
200
|
|
201 if (r->headers_out.content_length == NULL) {
|
|
202 if (r->headers_out.content_length_n >= 0) {
|
|
203 len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;
|
|
204 }
|
|
205 }
|
|
206
|
|
207 if (r->headers_out.content_type && r->headers_out.content_type->value.len) {
|
|
208 r->headers_out.content_type->key.len = 0;
|
|
209 len += sizeof("Content-Type: ") - 1
|
|
210 + r->headers_out.content_type->value.len + 2;
|
|
211
|
|
212 if (r->headers_out.charset.len) {
|
|
213 len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
|
|
214 }
|
|
215 }
|
|
216
|
|
217 if (r->headers_out.location
|
|
218 && r->headers_out.location->value.len
|
|
219 && r->headers_out.location->value.data[0] == '/')
|
|
220 {
|
|
221 r->headers_out.location->key.len = 0;
|
|
222 len += sizeof("Location: http://") - 1
|
6
|
223 + r->server_name.len + r->headers_out.location->value.len + 2;
|
0
|
224
|
|
225 if (r->port != 80) {
|
|
226 len += r->port_text->len;
|
|
227 }
|
|
228 }
|
|
229
|
|
230 if (r->headers_out.last_modified && r->headers_out.last_modified->key.len) {
|
|
231 len += r->headers_out.last_modified->key.len
|
|
232 + r->headers_out.last_modified->value.len + 2;
|
|
233
|
|
234 } else if (r->headers_out.last_modified_time != -1) {
|
|
235 len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
|
|
236 }
|
|
237
|
|
238 if (r->chunked) {
|
|
239 len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
|
|
240 }
|
|
241
|
|
242 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
243
|
|
244 if (r->keepalive) {
|
|
245 len += sizeof("Connection: keep-alive" CRLF) - 1;
|
|
246
|
|
247 /*
|
|
248 * MSIE and Opera ignore the "Keep-Alive: timeout=<N>" header.
|
|
249 * MSIE keeps the connection alive for about 60-65 seconds.
|
|
250 * Opera keeps the connection alive very long.
|
|
251 * Mozilla keeps the connection alive for N plus about 1-10 seconds.
|
|
252 * Konqueror keeps the connection alive for about N seconds.
|
|
253 */
|
|
254
|
22
|
255 if (clcf->keepalive_header) {
|
26
|
256 len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2;
|
0
|
257 }
|
|
258
|
|
259 } else {
|
|
260 len += sizeof("Connection: closed" CRLF) - 1;
|
|
261 }
|
|
262
|
|
263 part = &r->headers_out.headers.part;
|
|
264 header = part->elts;
|
|
265
|
|
266 for (i = 0; /* void */; i++) {
|
|
267
|
|
268 if (i >= part->nelts) {
|
|
269 if (part->next == NULL) {
|
|
270 break;
|
|
271 }
|
|
272
|
|
273 part = part->next;
|
|
274 header = part->elts;
|
|
275 i = 0;
|
|
276 }
|
|
277
|
|
278 if (header[i].key.len == 0) {
|
|
279 continue;
|
|
280 }
|
|
281
|
|
282 /* 2 is for ": " and 2 is for "\r\n" */
|
|
283 len += header[i].key.len + 2 + header[i].value.len + 2;
|
|
284 }
|
|
285
|
|
286 if (!(b = ngx_create_temp_buf(r->pool, len))) {
|
|
287 return NGX_ERROR;
|
|
288 }
|
|
289
|
|
290 /* "HTTP/1.x " */
|
|
291 b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);
|
|
292
|
|
293 /* status line */
|
|
294 if (r->headers_out.status_line.len) {
|
|
295 b->last = ngx_cpymem(b->last, r->headers_out.status_line.data,
|
|
296 r->headers_out.status_line.len);
|
|
297
|
|
298 } else {
|
|
299 b->last = ngx_cpymem(b->last, http_codes[status].data,
|
|
300 http_codes[status].len);
|
|
301 }
|
|
302 *(b->last++) = CR; *(b->last++) = LF;
|
|
303
|
|
304 if (!(r->headers_out.server && r->headers_out.server->key.len)) {
|
|
305 b->last = ngx_cpymem(b->last, server_string, sizeof(server_string) - 1);
|
|
306 }
|
|
307
|
|
308 if (!(r->headers_out.date && r->headers_out.date->key.len)) {
|
|
309 b->last = ngx_cpymem(b->last, "Date: ", sizeof("Date: ") - 1);
|
|
310 b->last = ngx_cpymem(b->last, ngx_cached_http_time.data,
|
|
311 ngx_cached_http_time.len);
|
|
312
|
|
313 *(b->last++) = CR; *(b->last++) = LF;
|
|
314 }
|
|
315
|
|
316 if (r->headers_out.content_length == NULL) {
|
|
317 if (r->headers_out.content_length_n >= 0) {
|
4
|
318 b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF,
|
|
319 r->headers_out.content_length_n);
|
0
|
320 }
|
|
321 }
|
|
322
|
|
323 if (r->headers_out.content_type && r->headers_out.content_type->value.len) {
|
|
324 b->last = ngx_cpymem(b->last, "Content-Type: ",
|
|
325 sizeof("Content-Type: ") - 1);
|
|
326 p = b->last;
|
|
327 b->last = ngx_cpymem(b->last, r->headers_out.content_type->value.data,
|
|
328 r->headers_out.content_type->value.len);
|
|
329
|
|
330 if (r->headers_out.charset.len) {
|
|
331 b->last = ngx_cpymem(b->last, "; charset=",
|
|
332 sizeof("; charset=") - 1);
|
|
333 b->last = ngx_cpymem(b->last, r->headers_out.charset.data,
|
|
334 r->headers_out.charset.len);
|
|
335
|
|
336 r->headers_out.content_type->value.len = b->last - p;
|
|
337 r->headers_out.content_type->value.data = p;
|
|
338 }
|
|
339
|
|
340 *(b->last++) = CR; *(b->last++) = LF;
|
|
341 }
|
|
342
|
|
343 if (r->headers_out.location
|
|
344 && r->headers_out.location->value.len
|
|
345 && r->headers_out.location->value.data[0] == '/')
|
|
346 {
|
|
347 p = b->last + sizeof("Location: ") - 1;
|
|
348 b->last = ngx_cpymem(b->last, "Location: http://",
|
|
349 sizeof("Location: http://") - 1);
|
6
|
350 b->last = ngx_cpymem(b->last, r->server_name.data,
|
|
351 r->server_name.len);
|
0
|
352 if (r->port != 80) {
|
|
353 b->last = ngx_cpymem(b->last, r->port_text->data,
|
|
354 r->port_text->len);
|
|
355 }
|
|
356
|
|
357 b->last = ngx_cpymem(b->last, r->headers_out.location->value.data,
|
|
358 r->headers_out.location->value.len);
|
|
359
|
|
360 r->headers_out.location->value.len = b->last - p;
|
|
361 r->headers_out.location->value.data = p;
|
|
362
|
|
363 *(b->last++) = CR; *(b->last++) = LF;
|
|
364 }
|
|
365
|
|
366 if (!(r->headers_out.last_modified && r->headers_out.last_modified->key.len)
|
|
367 && r->headers_out.last_modified_time != -1)
|
|
368 {
|
|
369 b->last = ngx_cpymem(b->last, "Last-Modified: ",
|
|
370 sizeof("Last-Modified: ") - 1);
|
4
|
371 b->last = ngx_http_time(b->last, r->headers_out.last_modified_time);
|
0
|
372
|
|
373 *(b->last++) = CR; *(b->last++) = LF;
|
|
374 }
|
|
375
|
|
376 if (r->chunked) {
|
|
377 b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF,
|
|
378 sizeof("Transfer-Encoding: chunked" CRLF) - 1);
|
|
379 }
|
|
380
|
|
381 if (r->keepalive) {
|
|
382 b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF,
|
|
383 sizeof("Connection: keep-alive" CRLF) - 1);
|
|
384
|
22
|
385 if (clcf->keepalive_header) {
|
4
|
386 b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,
|
|
387 clcf->keepalive_header);
|
0
|
388 }
|
|
389
|
|
390 } else {
|
|
391 b->last = ngx_cpymem(b->last, "Connection: close" CRLF,
|
|
392 sizeof("Connection: close" CRLF) - 1);
|
|
393 }
|
|
394
|
|
395 part = &r->headers_out.headers.part;
|
|
396 header = part->elts;
|
|
397
|
|
398 for (i = 0; /* void */; i++) {
|
|
399
|
|
400 if (i >= part->nelts) {
|
|
401 if (part->next == NULL) {
|
|
402 break;
|
|
403 }
|
|
404
|
|
405 part = part->next;
|
|
406 header = part->elts;
|
|
407 i = 0;
|
|
408 }
|
|
409
|
|
410 if (header[i].key.len == 0) {
|
|
411 continue;
|
|
412 }
|
|
413
|
|
414 b->last = ngx_cpymem(b->last, header[i].key.data, header[i].key.len);
|
|
415 *(b->last++) = ':' ; *(b->last++) = ' ' ;
|
|
416
|
|
417 b->last = ngx_cpymem(b->last, header[i].value.data,
|
|
418 header[i].value.len);
|
|
419 *(b->last++) = CR; *(b->last++) = LF;
|
|
420 }
|
|
421
|
|
422 #if (NGX_DEBUG)
|
|
423 *(b->last) = '\0';
|
|
424 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "%s\n", b->pos);
|
|
425 #endif
|
|
426
|
|
427 /* the end of HTTP header */
|
|
428 *(b->last++) = CR; *(b->last++) = LF;
|
|
429
|
|
430 r->header_size = b->last - b->pos;
|
|
431
|
|
432 if (r->header_only) {
|
|
433 b->last_buf = 1;
|
|
434 }
|
|
435
|
|
436 if (!(ln = ngx_alloc_chain_link(r->pool))) {
|
|
437 return NGX_ERROR;
|
|
438 }
|
|
439
|
|
440 ln->buf = b;
|
|
441 ln->next = NULL;
|
|
442
|
|
443 return ngx_http_write_filter(r, ln);
|
|
444 }
|
|
445
|
|
446
|
|
447 static ngx_int_t ngx_http_header_filter_init(ngx_cycle_t *cycle)
|
|
448 {
|
|
449 ngx_http_top_header_filter = ngx_http_header_filter;
|
|
450
|
|
451 return NGX_OK;
|
|
452 }
|