upstream 0.7.33
[nginx.git] / nginx / src / http / ngx_http_header_filter_module.c
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_conf_t *cf);
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,                                  /* preconfiguration */
19     ngx_http_header_filter_init,           /* postconfiguration */
20
21     NULL,                                  /* create main configuration */
22     NULL,                                  /* init main configuration */
23
24     NULL,                                  /* create server configuration */
25     NULL,                                  /* merge server configuration */
26
27     NULL,                                  /* create location configuration */
28     NULL,                                  /* merge location configuration */
29 };
30
31
32 ngx_module_t  ngx_http_header_filter_module = {
33     NGX_MODULE_V1,
34     &ngx_http_header_filter_module_ctx,    /* module context */
35     NULL,                                  /* module directives */
36     NGX_HTTP_MODULE,                       /* module type */
37     NULL,                                  /* init master */
38     NULL,                                  /* init module */
39     NULL,                                  /* init process */
40     NULL,                                  /* init thread */
41     NULL,                                  /* exit thread */
42     NULL,                                  /* exit process */
43     NULL,                                  /* exit master */
44     NGX_MODULE_V1_PADDING
45 };
46
47
48 static char ngx_http_server_string[] = "Server: nginx" CRLF;
49 static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
50
51
52 static ngx_str_t ngx_http_status_lines[] = {
53
54     ngx_string("200 OK"),
55     ngx_string("201 Created"),
56     ngx_null_string,  /* "202 Accepted" */
57     ngx_null_string,  /* "203 Non-Authoritative Information" */
58     ngx_string("204 No Content"),
59     ngx_null_string,  /* "205 Reset Content" */
60     ngx_string("206 Partial Content"),
61
62     /* ngx_null_string, */  /* "207 Multi-Status" */
63
64 #define NGX_HTTP_LEVEL_200  7
65
66     /* ngx_null_string, */  /* "300 Multiple Choices" */
67
68     ngx_string("301 Moved Permanently"),
69     ngx_string("302 Moved Temporarily"),
70     ngx_null_string,  /* "303 See Other" */
71     ngx_string("304 Not Modified"),
72
73     /* ngx_null_string, */  /* "305 Use Proxy" */
74     /* ngx_null_string, */  /* "306 unused" */
75     /* ngx_null_string, */  /* "307 Temporary Redirect" */
76
77 #define NGX_HTTP_LEVEL_300  4
78
79     ngx_string("400 Bad Request"),
80     ngx_string("401 Unauthorized"),
81     ngx_string("402 Payment Required"),
82     ngx_string("403 Forbidden"),
83     ngx_string("404 Not Found"),
84     ngx_string("405 Not Allowed"),
85     ngx_string("406 Not Acceptable"),
86     ngx_null_string,  /* "407 Proxy Authentication Required" */
87     ngx_string("408 Request Time-out"),
88     ngx_string("409 Conflict"),
89     ngx_string("410 Gone"),
90     ngx_string("411 Length Required"),
91     ngx_string("412 Precondition Failed"),
92     ngx_string("413 Request Entity Too Large"),
93     ngx_null_string,  /* "414 Request-URI Too Large", but we never send it
94                        * because we treat such requests as the HTTP/0.9
95                        * requests and send only a body without a header
96                        */
97     ngx_string("415 Unsupported Media Type"),
98     ngx_string("416 Requested Range Not Satisfiable"),
99
100     /* ngx_null_string, */  /* "417 Expectation Failed" */
101     /* ngx_null_string, */  /* "418 unused" */
102     /* ngx_null_string, */  /* "419 unused" */
103     /* ngx_null_string, */  /* "420 unused" */
104     /* ngx_null_string, */  /* "421 unused" */
105     /* ngx_null_string, */  /* "422 Unprocessable Entity" */
106     /* ngx_null_string, */  /* "423 Locked" */
107     /* ngx_null_string, */  /* "424 Failed Dependency" */
108
109 #define NGX_HTTP_LEVEL_400  17
110
111     ngx_string("500 Internal Server Error"),
112     ngx_string("501 Method Not Implemented"),
113     ngx_string("502 Bad Gateway"),
114     ngx_string("503 Service Temporarily Unavailable"),
115     ngx_string("504 Gateway Time-out"),
116
117     ngx_null_string,        /* "505 HTTP Version Not Supported" */
118     ngx_null_string,        /* "506 Variant Also Negotiates" */
119     ngx_string("507 Insufficient Storage"),
120     /* ngx_null_string, */  /* "508 unused" */
121     /* ngx_null_string, */  /* "509 unused" */
122     /* ngx_null_string, */  /* "510 Not Extended" */
123 };
124
125
126 ngx_http_header_out_t  ngx_http_headers_out[] = {
127     { ngx_string("Server"), offsetof(ngx_http_headers_out_t, server) },
128     { ngx_string("Date"), offsetof(ngx_http_headers_out_t, date) },
129 #if 0
130     { ngx_string("Content-Type"),
131                  offsetof(ngx_http_headers_out_t, content_type) },
132 #endif
133     { ngx_string("Content-Length"),
134                  offsetof(ngx_http_headers_out_t, content_length) },
135     { ngx_string("Content-Encoding"),
136                  offsetof(ngx_http_headers_out_t, content_encoding) },
137     { ngx_string("Location"), offsetof(ngx_http_headers_out_t, location) },
138     { ngx_string("Last-Modified"),
139                  offsetof(ngx_http_headers_out_t, last_modified) },
140     { ngx_string("Accept-Ranges"),
141                  offsetof(ngx_http_headers_out_t, accept_ranges) },
142     { ngx_string("Expires"), offsetof(ngx_http_headers_out_t, expires) },
143     { ngx_string("Cache-Control"),
144                  offsetof(ngx_http_headers_out_t, cache_control) },
145     { ngx_string("ETag"), offsetof(ngx_http_headers_out_t, etag) },
146
147     { ngx_null_string, 0 }
148 };
149
150
151 static ngx_int_t
152 ngx_http_header_filter(ngx_http_request_t *r)
153 {
154     u_char                    *p;
155     size_t                     len;
156     ngx_str_t                  host;
157     ngx_buf_t                 *b;
158     ngx_uint_t                 status, i;
159     ngx_chain_t                out;
160     ngx_list_part_t           *part;
161     ngx_table_elt_t           *header;
162     ngx_http_core_loc_conf_t  *clcf;
163     ngx_http_core_srv_conf_t  *cscf;
164     /* AF_INET only */
165     u_char                     addr[NGX_INET_ADDRSTRLEN];
166
167     r->header_sent = 1;
168
169     if (r != r->main) {
170         return NGX_OK;
171     }
172
173     if (r->http_version < NGX_HTTP_VERSION_10) {
174         return NGX_OK;
175     }
176
177     if (r->method == NGX_HTTP_HEAD) {
178         r->header_only = 1;
179     }
180
181     if (r->headers_out.last_modified_time != -1) {
182         if (r->headers_out.status != NGX_HTTP_OK
183             && r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT
184             && r->headers_out.status != NGX_HTTP_NOT_MODIFIED)
185         {
186             r->headers_out.last_modified_time = -1;
187             r->headers_out.last_modified = NULL;
188         }
189     }
190
191     len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1
192           /* the end of the header */
193           + sizeof(CRLF) - 1;
194
195     /* status line */
196
197     if (r->headers_out.status_line.len) {
198         len += r->headers_out.status_line.len;
199 #if (NGX_SUPPRESS_WARN)
200         status = NGX_INVALID_ARRAY_INDEX;
201 #endif
202
203     } else {
204
205         if (r->headers_out.status < NGX_HTTP_MOVED_PERMANENTLY) {
206             /* 2XX */
207             status = r->headers_out.status - NGX_HTTP_OK;
208
209             if (r->headers_out.status == NGX_HTTP_NO_CONTENT) {
210                 r->header_only = 1;
211                 r->headers_out.content_type.len = 0;
212                 r->headers_out.content_type.data = NULL;
213                 r->headers_out.last_modified_time = -1;
214                 r->headers_out.last_modified = NULL;
215                 r->headers_out.content_length = NULL;
216                 r->headers_out.content_length_n = -1;
217             }
218
219         } else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) {
220             /* 3XX */
221             status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY
222                                            + NGX_HTTP_LEVEL_200;
223
224             if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) {
225                 r->header_only = 1;
226             }
227
228         } else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) {
229             /* 4XX */
230             status = r->headers_out.status - NGX_HTTP_BAD_REQUEST
231                                            + NGX_HTTP_LEVEL_200
232                                            + NGX_HTTP_LEVEL_300;
233
234         } else {
235             /* 5XX */
236             status = r->headers_out.status - NGX_HTTP_INTERNAL_SERVER_ERROR
237                                            + NGX_HTTP_LEVEL_200
238                                            + NGX_HTTP_LEVEL_300
239                                            + NGX_HTTP_LEVEL_400;
240         }
241
242         len += ngx_http_status_lines[status].len;
243     }
244
245     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
246
247     if (r->headers_out.server == NULL) {
248         len += clcf->server_tokens ? sizeof(ngx_http_server_full_string) - 1:
249                                      sizeof(ngx_http_server_string) - 1;
250     }
251
252     if (r->headers_out.date == NULL) {
253         len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
254     }
255
256     if (r->headers_out.content_type.len) {
257         len += sizeof("Content-Type: ") - 1
258                + r->headers_out.content_type.len + 2;
259
260         if (r->headers_out.content_type_len == r->headers_out.content_type.len
261             && r->headers_out.charset.len)
262         {
263             len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
264         }
265     }
266
267     if (r->headers_out.content_length == NULL
268         && r->headers_out.content_length_n >= 0)
269     {
270         len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;
271     }
272
273     if (r->headers_out.last_modified == NULL
274         && r->headers_out.last_modified_time != -1)
275     {
276         len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
277     }
278
279     if (r->headers_out.location
280         && r->headers_out.location->value.len
281         && r->headers_out.location->value.data[0] == '/')
282     {
283         r->headers_out.location->hash = 0;
284
285         if (clcf->server_name_in_redirect) {
286             cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
287             host = cscf->server_name;
288
289         } else if (r->headers_in.server.len) {
290             host = r->headers_in.server;
291
292         } else {
293             host.data = addr;
294
295             if (ngx_http_server_addr(r, &host) != NGX_OK) {
296                 return NGX_ERROR;
297             }
298         }
299
300 #if (NGX_HTTP_SSL)
301         if (r->connection->ssl) {
302             len += sizeof("Location: https://") - 1
303                    + host.len
304                    + r->headers_out.location->value.len + 2;
305
306             if (clcf->port_in_redirect && r->port != 443) {
307                 len += r->port_text->len;
308             }
309
310         } else
311 #endif
312         {
313             len += sizeof("Location: http://") - 1
314                    + host.len
315                    + r->headers_out.location->value.len + 2;
316
317             if (clcf->port_in_redirect && r->port != 80) {
318                 len += r->port_text->len;
319             }
320         }
321
322     } else {
323         host.len = 0;
324         host.data = NULL;
325     }
326
327     if (r->chunked) {
328         len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
329     }
330
331     if (r->keepalive) {
332         len += sizeof("Connection: keep-alive" CRLF) - 1;
333
334         /*
335          * MSIE and Opera ignore the "Keep-Alive: timeout=<N>" header.
336          * MSIE keeps the connection alive for about 60-65 seconds.
337          * Opera keeps the connection alive very long.
338          * Mozilla keeps the connection alive for N plus about 1-10 seconds.
339          * Konqueror keeps the connection alive for about N seconds.
340          */
341
342         if (clcf->keepalive_header) {
343             len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2;
344         }
345
346     } else {
347         len += sizeof("Connection: closed" CRLF) - 1;
348     }
349
350 #if (NGX_HTTP_GZIP)
351     if (r->gzip && clcf->gzip_vary) {
352         len += sizeof("Vary: Accept-Encoding" CRLF) - 1;
353     }
354 #endif
355
356     part = &r->headers_out.headers.part;
357     header = part->elts;
358
359     for (i = 0; /* void */; i++) {
360
361         if (i >= part->nelts) {
362             if (part->next == NULL) {
363                 break;
364             }
365
366             part = part->next;
367             header = part->elts;
368             i = 0;
369         }
370
371         if (header[i].hash == 0) {
372             continue;
373         }
374
375         len += header[i].key.len + sizeof(": ") - 1 + header[i].value.len
376                + sizeof(CRLF) - 1;
377     }
378
379     b = ngx_create_temp_buf(r->pool, len);
380     if (b == NULL) {
381         return NGX_ERROR;
382     }
383
384     /* "HTTP/1.x " */
385     b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);
386
387     /* status line */
388     if (r->headers_out.status_line.len) {
389         b->last = ngx_copy(b->last, r->headers_out.status_line.data,
390                            r->headers_out.status_line.len);
391
392     } else {
393         b->last = ngx_copy(b->last, ngx_http_status_lines[status].data,
394                            ngx_http_status_lines[status].len);
395     }
396     *b->last++ = CR; *b->last++ = LF;
397
398     if (r->headers_out.server == NULL) {
399         if (clcf->server_tokens) {
400             p = (u_char *) ngx_http_server_full_string;
401             len = sizeof(ngx_http_server_full_string) - 1;
402
403         } else {
404             p = (u_char *) ngx_http_server_string;
405             len = sizeof(ngx_http_server_string) - 1;
406         }
407
408         b->last = ngx_cpymem(b->last, p, len);
409     }
410
411     if (r->headers_out.date == NULL) {
412         b->last = ngx_cpymem(b->last, "Date: ", sizeof("Date: ") - 1);
413         b->last = ngx_cpymem(b->last, ngx_cached_http_time.data,
414                              ngx_cached_http_time.len);
415
416         *b->last++ = CR; *b->last++ = LF;
417     }
418
419     if (r->headers_out.content_type.len) {
420         b->last = ngx_cpymem(b->last, "Content-Type: ",
421                              sizeof("Content-Type: ") - 1);
422         p = b->last;
423         b->last = ngx_copy(b->last, r->headers_out.content_type.data,
424                            r->headers_out.content_type.len);
425
426         if (r->headers_out.content_type_len == r->headers_out.content_type.len
427             && r->headers_out.charset.len)
428         {
429             b->last = ngx_cpymem(b->last, "; charset=",
430                                  sizeof("; charset=") - 1);
431             b->last = ngx_copy(b->last, r->headers_out.charset.data,
432                                r->headers_out.charset.len);
433
434             /* update r->headers_out.content_type for possible logging */
435
436             r->headers_out.content_type.len = b->last - p;
437             r->headers_out.content_type.data = p;
438         }
439
440         *b->last++ = CR; *b->last++ = LF;
441     }
442
443     if (r->headers_out.content_length == NULL
444         && r->headers_out.content_length_n >= 0)
445     {
446         b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF,
447                               r->headers_out.content_length_n);
448     }
449
450     if (r->headers_out.last_modified == NULL
451         && r->headers_out.last_modified_time != -1)
452     {
453         b->last = ngx_cpymem(b->last, "Last-Modified: ",
454                              sizeof("Last-Modified: ") - 1);
455         b->last = ngx_http_time(b->last, r->headers_out.last_modified_time);
456
457         *b->last++ = CR; *b->last++ = LF;
458     }
459
460     if (host.data) {
461
462         p = b->last + sizeof("Location: ") - 1;
463
464         b->last = ngx_cpymem(b->last, "Location: http",
465                              sizeof("Location: http") - 1);
466
467 #if (NGX_HTTP_SSL)
468         if (r->connection->ssl) {
469             *b->last++ ='s';
470         }
471 #endif
472
473         *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/';
474         b->last = ngx_copy(b->last, host.data, host.len);
475
476         if (clcf->port_in_redirect) {
477 #if (NGX_HTTP_SSL)
478             if (r->connection->ssl) {
479                 if (r->port != 443) {
480                     b->last = ngx_copy(b->last, r->port_text->data,
481                                        r->port_text->len);
482                 }
483             } else
484 #endif
485             {
486                 if (r->port != 80) {
487                     b->last = ngx_copy(b->last, r->port_text->data,
488                                        r->port_text->len);
489                 }
490             }
491         }
492
493         b->last = ngx_copy(b->last, r->headers_out.location->value.data,
494                            r->headers_out.location->value.len);
495
496         /* update r->headers_out.location->value for possible logging */
497
498         r->headers_out.location->value.len = b->last - p;
499         r->headers_out.location->value.data = p;
500         r->headers_out.location->key.len = sizeof("Location: ") - 1;
501         r->headers_out.location->key.data = (u_char *) "Location: ";
502
503         *b->last++ = CR; *b->last++ = LF;
504     }
505
506     if (r->chunked) {
507         b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF,
508                              sizeof("Transfer-Encoding: chunked" CRLF) - 1);
509     }
510
511     if (r->keepalive) {
512         b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF,
513                              sizeof("Connection: keep-alive" CRLF) - 1);
514
515         if (clcf->keepalive_header) {
516             b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,
517                                   clcf->keepalive_header);
518         }
519
520     } else {
521         b->last = ngx_cpymem(b->last, "Connection: close" CRLF,
522                              sizeof("Connection: close" CRLF) - 1);
523     }
524
525 #if (NGX_HTTP_GZIP)
526     if (r->gzip && clcf->gzip_vary) {
527         b->last = ngx_cpymem(b->last, "Vary: Accept-Encoding" CRLF,
528                              sizeof("Vary: Accept-Encoding" CRLF) - 1);
529     }
530 #endif
531
532     part = &r->headers_out.headers.part;
533     header = part->elts;
534
535     for (i = 0; /* void */; i++) {
536
537         if (i >= part->nelts) {
538             if (part->next == NULL) {
539                 break;
540             }
541
542             part = part->next;
543             header = part->elts;
544             i = 0;
545         }
546
547         if (header[i].hash == 0) {
548             continue;
549         }
550
551         b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
552         *b->last++ = ':'; *b->last++ = ' ';
553
554         b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len);
555         *b->last++ = CR; *b->last++ = LF;
556     }
557
558     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
559                    "%*s\n", (size_t) (b->last - b->pos), b->pos);
560
561     /* the end of HTTP header */
562     *b->last++ = CR; *b->last++ = LF;
563
564     r->header_size = b->last - b->pos;
565
566     if (r->header_only) {
567         b->last_buf = 1;
568     }
569
570     out.buf = b;
571     out.next = NULL;
572
573     return ngx_http_write_filter(r, &out);
574 }
575
576
577 static ngx_int_t
578 ngx_http_header_filter_init(ngx_conf_t *cf)
579 {
580     ngx_http_top_header_filter = ngx_http_header_filter;
581
582     return NGX_OK;
583 }