3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
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);
17 static ngx_http_module_t ngx_http_header_filter_module_ctx = {
18 NULL, /* preconfiguration */
19 ngx_http_header_filter_init, /* postconfiguration */
21 NULL, /* create main configuration */
22 NULL, /* init main configuration */
24 NULL, /* create server configuration */
25 NULL, /* merge server configuration */
27 NULL, /* create location configuration */
28 NULL, /* merge location configuration */
32 ngx_module_t ngx_http_header_filter_module = {
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 */
48 static char ngx_http_server_string[] = "Server: nginx" CRLF;
49 static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
52 static ngx_str_t ngx_http_status_lines[] = {
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"),
62 /* ngx_null_string, */ /* "207 Multi-Status" */
64 #define NGX_HTTP_LEVEL_200 7
66 /* ngx_null_string, */ /* "300 Multiple Choices" */
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"),
73 /* ngx_null_string, */ /* "305 Use Proxy" */
74 /* ngx_null_string, */ /* "306 unused" */
75 /* ngx_null_string, */ /* "307 Temporary Redirect" */
77 #define NGX_HTTP_LEVEL_300 4
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
97 ngx_string("415 Unsupported Media Type"),
98 ngx_string("416 Requested Range Not Satisfiable"),
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" */
109 #define NGX_HTTP_LEVEL_400 17
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"),
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" */
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) },
130 { ngx_string("Content-Type"),
131 offsetof(ngx_http_headers_out_t, content_type) },
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) },
147 { ngx_null_string, 0 }
152 ngx_http_header_filter(ngx_http_request_t *r)
158 ngx_uint_t status, i;
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;
165 u_char addr[NGX_INET_ADDRSTRLEN];
173 if (r->http_version < NGX_HTTP_VERSION_10) {
177 if (r->method == NGX_HTTP_HEAD) {
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)
186 r->headers_out.last_modified_time = -1;
187 r->headers_out.last_modified = NULL;
191 len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1
192 /* the end of the header */
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;
205 if (r->headers_out.status < NGX_HTTP_MOVED_PERMANENTLY) {
207 status = r->headers_out.status - NGX_HTTP_OK;
209 if (r->headers_out.status == NGX_HTTP_NO_CONTENT) {
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;
219 } else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) {
221 status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY
222 + NGX_HTTP_LEVEL_200;
224 if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) {
228 } else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) {
230 status = r->headers_out.status - NGX_HTTP_BAD_REQUEST
232 + NGX_HTTP_LEVEL_300;
236 status = r->headers_out.status - NGX_HTTP_INTERNAL_SERVER_ERROR
239 + NGX_HTTP_LEVEL_400;
242 len += ngx_http_status_lines[status].len;
245 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
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;
252 if (r->headers_out.date == NULL) {
253 len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
256 if (r->headers_out.content_type.len) {
257 len += sizeof("Content-Type: ") - 1
258 + r->headers_out.content_type.len + 2;
260 if (r->headers_out.content_type_len == r->headers_out.content_type.len
261 && r->headers_out.charset.len)
263 len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
267 if (r->headers_out.content_length == NULL
268 && r->headers_out.content_length_n >= 0)
270 len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;
273 if (r->headers_out.last_modified == NULL
274 && r->headers_out.last_modified_time != -1)
276 len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
279 if (r->headers_out.location
280 && r->headers_out.location->value.len
281 && r->headers_out.location->value.data[0] == '/')
283 r->headers_out.location->hash = 0;
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;
289 } else if (r->headers_in.server.len) {
290 host = r->headers_in.server;
295 if (ngx_http_server_addr(r, &host) != NGX_OK) {
301 if (r->connection->ssl) {
302 len += sizeof("Location: https://") - 1
304 + r->headers_out.location->value.len + 2;
306 if (clcf->port_in_redirect && r->port != 443) {
307 len += r->port_text->len;
313 len += sizeof("Location: http://") - 1
315 + r->headers_out.location->value.len + 2;
317 if (clcf->port_in_redirect && r->port != 80) {
318 len += r->port_text->len;
328 len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
332 len += sizeof("Connection: keep-alive" CRLF) - 1;
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.
342 if (clcf->keepalive_header) {
343 len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2;
347 len += sizeof("Connection: closed" CRLF) - 1;
351 if (r->gzip && clcf->gzip_vary) {
352 len += sizeof("Vary: Accept-Encoding" CRLF) - 1;
356 part = &r->headers_out.headers.part;
359 for (i = 0; /* void */; i++) {
361 if (i >= part->nelts) {
362 if (part->next == NULL) {
371 if (header[i].hash == 0) {
375 len += header[i].key.len + sizeof(": ") - 1 + header[i].value.len
379 b = ngx_create_temp_buf(r->pool, len);
385 b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);
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);
393 b->last = ngx_copy(b->last, ngx_http_status_lines[status].data,
394 ngx_http_status_lines[status].len);
396 *b->last++ = CR; *b->last++ = LF;
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;
404 p = (u_char *) ngx_http_server_string;
405 len = sizeof(ngx_http_server_string) - 1;
408 b->last = ngx_cpymem(b->last, p, len);
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);
416 *b->last++ = CR; *b->last++ = LF;
419 if (r->headers_out.content_type.len) {
420 b->last = ngx_cpymem(b->last, "Content-Type: ",
421 sizeof("Content-Type: ") - 1);
423 b->last = ngx_copy(b->last, r->headers_out.content_type.data,
424 r->headers_out.content_type.len);
426 if (r->headers_out.content_type_len == r->headers_out.content_type.len
427 && r->headers_out.charset.len)
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);
434 /* update r->headers_out.content_type for possible logging */
436 r->headers_out.content_type.len = b->last - p;
437 r->headers_out.content_type.data = p;
440 *b->last++ = CR; *b->last++ = LF;
443 if (r->headers_out.content_length == NULL
444 && r->headers_out.content_length_n >= 0)
446 b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF,
447 r->headers_out.content_length_n);
450 if (r->headers_out.last_modified == NULL
451 && r->headers_out.last_modified_time != -1)
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);
457 *b->last++ = CR; *b->last++ = LF;
462 p = b->last + sizeof("Location: ") - 1;
464 b->last = ngx_cpymem(b->last, "Location: http",
465 sizeof("Location: http") - 1);
468 if (r->connection->ssl) {
473 *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/';
474 b->last = ngx_copy(b->last, host.data, host.len);
476 if (clcf->port_in_redirect) {
478 if (r->connection->ssl) {
479 if (r->port != 443) {
480 b->last = ngx_copy(b->last, r->port_text->data,
487 b->last = ngx_copy(b->last, r->port_text->data,
493 b->last = ngx_copy(b->last, r->headers_out.location->value.data,
494 r->headers_out.location->value.len);
496 /* update r->headers_out.location->value for possible logging */
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: ";
503 *b->last++ = CR; *b->last++ = LF;
507 b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF,
508 sizeof("Transfer-Encoding: chunked" CRLF) - 1);
512 b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF,
513 sizeof("Connection: keep-alive" CRLF) - 1);
515 if (clcf->keepalive_header) {
516 b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,
517 clcf->keepalive_header);
521 b->last = ngx_cpymem(b->last, "Connection: close" CRLF,
522 sizeof("Connection: close" CRLF) - 1);
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);
532 part = &r->headers_out.headers.part;
535 for (i = 0; /* void */; i++) {
537 if (i >= part->nelts) {
538 if (part->next == NULL) {
547 if (header[i].hash == 0) {
551 b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
552 *b->last++ = ':'; *b->last++ = ' ';
554 b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len);
555 *b->last++ = CR; *b->last++ = LF;
558 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
559 "%*s\n", (size_t) (b->last - b->pos), b->pos);
561 /* the end of HTTP header */
562 *b->last++ = CR; *b->last++ = LF;
564 r->header_size = b->last - b->pos;
566 if (r->header_only) {
573 return ngx_http_write_filter(r, &out);
578 ngx_http_header_filter_init(ngx_conf_t *cf)
580 ngx_http_top_header_filter = ngx_http_header_filter;