upstream nginx-0.7.43
[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, port;
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     struct sockaddr_in        *sin;
165 #if (NGX_HAVE_INET6)
166     struct sockaddr_in6       *sin6;
167 #endif
168     u_char                     addr[NGX_SOCKADDR_STRLEN];
169
170     r->header_sent = 1;
171
172     if (r != r->main) {
173         return NGX_OK;
174     }
175
176     if (r->http_version < NGX_HTTP_VERSION_10) {
177         return NGX_OK;
178     }
179
180     if (r->method == NGX_HTTP_HEAD) {
181         r->header_only = 1;
182     }
183
184     if (r->headers_out.last_modified_time != -1) {
185         if (r->headers_out.status != NGX_HTTP_OK
186             && r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT
187             && r->headers_out.status != NGX_HTTP_NOT_MODIFIED)
188         {
189             r->headers_out.last_modified_time = -1;
190             r->headers_out.last_modified = NULL;
191         }
192     }
193
194     len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1
195           /* the end of the header */
196           + sizeof(CRLF) - 1;
197
198     /* status line */
199
200     if (r->headers_out.status_line.len) {
201         len += r->headers_out.status_line.len;
202 #if (NGX_SUPPRESS_WARN)
203         status = NGX_INVALID_ARRAY_INDEX;
204 #endif
205
206     } else {
207
208         if (r->headers_out.status < NGX_HTTP_MOVED_PERMANENTLY) {
209             /* 2XX */
210             status = r->headers_out.status - NGX_HTTP_OK;
211
212             if (r->headers_out.status == NGX_HTTP_NO_CONTENT) {
213                 r->header_only = 1;
214                 r->headers_out.content_type.len = 0;
215                 r->headers_out.content_type.data = NULL;
216                 r->headers_out.last_modified_time = -1;
217                 r->headers_out.last_modified = NULL;
218                 r->headers_out.content_length = NULL;
219                 r->headers_out.content_length_n = -1;
220             }
221
222         } else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) {
223             /* 3XX */
224             status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY
225                                            + NGX_HTTP_LEVEL_200;
226
227             if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) {
228                 r->header_only = 1;
229             }
230
231         } else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) {
232             /* 4XX */
233             status = r->headers_out.status - NGX_HTTP_BAD_REQUEST
234                                            + NGX_HTTP_LEVEL_200
235                                            + NGX_HTTP_LEVEL_300;
236
237         } else {
238             /* 5XX */
239             status = r->headers_out.status - NGX_HTTP_INTERNAL_SERVER_ERROR
240                                            + NGX_HTTP_LEVEL_200
241                                            + NGX_HTTP_LEVEL_300
242                                            + NGX_HTTP_LEVEL_400;
243         }
244
245         len += ngx_http_status_lines[status].len;
246     }
247
248     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
249
250     if (r->headers_out.server == NULL) {
251         len += clcf->server_tokens ? sizeof(ngx_http_server_full_string) - 1:
252                                      sizeof(ngx_http_server_string) - 1;
253     }
254
255     if (r->headers_out.date == NULL) {
256         len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
257     }
258
259     if (r->headers_out.content_type.len) {
260         len += sizeof("Content-Type: ") - 1
261                + r->headers_out.content_type.len + 2;
262
263         if (r->headers_out.content_type_len == r->headers_out.content_type.len
264             && r->headers_out.charset.len)
265         {
266             len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
267         }
268     }
269
270     if (r->headers_out.content_length == NULL
271         && r->headers_out.content_length_n >= 0)
272     {
273         len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;
274     }
275
276     if (r->headers_out.last_modified == NULL
277         && r->headers_out.last_modified_time != -1)
278     {
279         len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
280     }
281
282     if (r->headers_out.location
283         && r->headers_out.location->value.len
284         && r->headers_out.location->value.data[0] == '/')
285     {
286         r->headers_out.location->hash = 0;
287
288         if (clcf->server_name_in_redirect) {
289             cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
290             host = cscf->server_name;
291
292         } else if (r->headers_in.server.len) {
293             host = r->headers_in.server;
294
295         } else {
296             host.len = NGX_SOCKADDR_STRLEN;
297             host.data = addr;
298
299             if (ngx_http_server_addr(r, &host) != NGX_OK) {
300                 return NGX_ERROR;
301             }
302         }
303
304         switch (r->connection->local_sockaddr->sa_family) {
305
306 #if (NGX_HAVE_INET6)
307         case AF_INET6:
308             sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr;
309             port = ntohs(sin6->sin6_port);
310             break;
311 #endif
312         default: /* AF_INET */
313             sin = (struct sockaddr_in *) r->connection->local_sockaddr;
314             port = ntohs(sin->sin_port);
315             break;
316         }
317
318         len += sizeof("Location: https://") - 1
319                + host.len
320                + r->headers_out.location->value.len + 2;
321
322         if (clcf->port_in_redirect) {
323
324 #if (NGX_HTTP_SSL)
325             if (r->connection->ssl)
326                 port = (port == 443) ? 0 : port;
327             else
328 #endif
329                 port = (port == 80) ? 0 : port;
330         }
331
332         if (port) {
333             len += sizeof(":65535") - 1;
334         }
335
336     } else {
337         host.len = 0;
338         host.data = NULL;
339         port = 0;
340     }
341
342     if (r->chunked) {
343         len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
344     }
345
346     if (r->keepalive) {
347         len += sizeof("Connection: keep-alive" CRLF) - 1;
348
349         /*
350          * MSIE and Opera ignore the "Keep-Alive: timeout=<N>" header.
351          * MSIE keeps the connection alive for about 60-65 seconds.
352          * Opera keeps the connection alive very long.
353          * Mozilla keeps the connection alive for N plus about 1-10 seconds.
354          * Konqueror keeps the connection alive for about N seconds.
355          */
356
357         if (clcf->keepalive_header) {
358             len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2;
359         }
360
361     } else {
362         len += sizeof("Connection: closed" CRLF) - 1;
363     }
364
365 #if (NGX_HTTP_GZIP)
366     if (r->gzip && clcf->gzip_vary) {
367         len += sizeof("Vary: Accept-Encoding" CRLF) - 1;
368     }
369 #endif
370
371     part = &r->headers_out.headers.part;
372     header = part->elts;
373
374     for (i = 0; /* void */; i++) {
375
376         if (i >= part->nelts) {
377             if (part->next == NULL) {
378                 break;
379             }
380
381             part = part->next;
382             header = part->elts;
383             i = 0;
384         }
385
386         if (header[i].hash == 0) {
387             continue;
388         }
389
390         len += header[i].key.len + sizeof(": ") - 1 + header[i].value.len
391                + sizeof(CRLF) - 1;
392     }
393
394     b = ngx_create_temp_buf(r->pool, len);
395     if (b == NULL) {
396         return NGX_ERROR;
397     }
398
399     /* "HTTP/1.x " */
400     b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);
401
402     /* status line */
403     if (r->headers_out.status_line.len) {
404         b->last = ngx_copy(b->last, r->headers_out.status_line.data,
405                            r->headers_out.status_line.len);
406
407     } else {
408         b->last = ngx_copy(b->last, ngx_http_status_lines[status].data,
409                            ngx_http_status_lines[status].len);
410     }
411     *b->last++ = CR; *b->last++ = LF;
412
413     if (r->headers_out.server == NULL) {
414         if (clcf->server_tokens) {
415             p = (u_char *) ngx_http_server_full_string;
416             len = sizeof(ngx_http_server_full_string) - 1;
417
418         } else {
419             p = (u_char *) ngx_http_server_string;
420             len = sizeof(ngx_http_server_string) - 1;
421         }
422
423         b->last = ngx_cpymem(b->last, p, len);
424     }
425
426     if (r->headers_out.date == NULL) {
427         b->last = ngx_cpymem(b->last, "Date: ", sizeof("Date: ") - 1);
428         b->last = ngx_cpymem(b->last, ngx_cached_http_time.data,
429                              ngx_cached_http_time.len);
430
431         *b->last++ = CR; *b->last++ = LF;
432     }
433
434     if (r->headers_out.content_type.len) {
435         b->last = ngx_cpymem(b->last, "Content-Type: ",
436                              sizeof("Content-Type: ") - 1);
437         p = b->last;
438         b->last = ngx_copy(b->last, r->headers_out.content_type.data,
439                            r->headers_out.content_type.len);
440
441         if (r->headers_out.content_type_len == r->headers_out.content_type.len
442             && r->headers_out.charset.len)
443         {
444             b->last = ngx_cpymem(b->last, "; charset=",
445                                  sizeof("; charset=") - 1);
446             b->last = ngx_copy(b->last, r->headers_out.charset.data,
447                                r->headers_out.charset.len);
448
449             /* update r->headers_out.content_type for possible logging */
450
451             r->headers_out.content_type.len = b->last - p;
452             r->headers_out.content_type.data = p;
453         }
454
455         *b->last++ = CR; *b->last++ = LF;
456     }
457
458     if (r->headers_out.content_length == NULL
459         && r->headers_out.content_length_n >= 0)
460     {
461         b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF,
462                               r->headers_out.content_length_n);
463     }
464
465     if (r->headers_out.last_modified == NULL
466         && r->headers_out.last_modified_time != -1)
467     {
468         b->last = ngx_cpymem(b->last, "Last-Modified: ",
469                              sizeof("Last-Modified: ") - 1);
470         b->last = ngx_http_time(b->last, r->headers_out.last_modified_time);
471
472         *b->last++ = CR; *b->last++ = LF;
473     }
474
475     if (host.data) {
476
477         p = b->last + sizeof("Location: ") - 1;
478
479         b->last = ngx_cpymem(b->last, "Location: http",
480                              sizeof("Location: http") - 1);
481
482 #if (NGX_HTTP_SSL)
483         if (r->connection->ssl) {
484             *b->last++ ='s';
485         }
486 #endif
487
488         *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/';
489         b->last = ngx_copy(b->last, host.data, host.len);
490
491         if (port) {
492             b->last = ngx_sprintf(b->last, ":%ui", port);
493         }
494
495         b->last = ngx_copy(b->last, r->headers_out.location->value.data,
496                            r->headers_out.location->value.len);
497
498         /* update r->headers_out.location->value for possible logging */
499
500         r->headers_out.location->value.len = b->last - p;
501         r->headers_out.location->value.data = p;
502         r->headers_out.location->key.len = sizeof("Location: ") - 1;
503         r->headers_out.location->key.data = (u_char *) "Location: ";
504
505         *b->last++ = CR; *b->last++ = LF;
506     }
507
508     if (r->chunked) {
509         b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF,
510                              sizeof("Transfer-Encoding: chunked" CRLF) - 1);
511     }
512
513     if (r->keepalive) {
514         b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF,
515                              sizeof("Connection: keep-alive" CRLF) - 1);
516
517         if (clcf->keepalive_header) {
518             b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,
519                                   clcf->keepalive_header);
520         }
521
522     } else {
523         b->last = ngx_cpymem(b->last, "Connection: close" CRLF,
524                              sizeof("Connection: close" CRLF) - 1);
525     }
526
527 #if (NGX_HTTP_GZIP)
528     if (r->gzip && clcf->gzip_vary) {
529         b->last = ngx_cpymem(b->last, "Vary: Accept-Encoding" CRLF,
530                              sizeof("Vary: Accept-Encoding" CRLF) - 1);
531     }
532 #endif
533
534     part = &r->headers_out.headers.part;
535     header = part->elts;
536
537     for (i = 0; /* void */; i++) {
538
539         if (i >= part->nelts) {
540             if (part->next == NULL) {
541                 break;
542             }
543
544             part = part->next;
545             header = part->elts;
546             i = 0;
547         }
548
549         if (header[i].hash == 0) {
550             continue;
551         }
552
553         b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
554         *b->last++ = ':'; *b->last++ = ' ';
555
556         b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len);
557         *b->last++ = CR; *b->last++ = LF;
558     }
559
560     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
561                    "%*s\n", (size_t) (b->last - b->pos), b->pos);
562
563     /* the end of HTTP header */
564     *b->last++ = CR; *b->last++ = LF;
565
566     r->header_size = b->last - b->pos;
567
568     if (r->header_only) {
569         b->last_buf = 1;
570     }
571
572     out.buf = b;
573     out.next = NULL;
574
575     return ngx_http_write_filter(r, &out);
576 }
577
578
579 static ngx_int_t
580 ngx_http_header_filter_init(ngx_conf_t *cf)
581 {
582     ngx_http_top_header_filter = ngx_http_header_filter;
583
584     return NGX_OK;
585 }