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