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, port;
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;
166 struct sockaddr_in6 *sin6;
168 u_char addr[NGX_SOCKADDR_STRLEN];
176 if (r->http_version < NGX_HTTP_VERSION_10) {
180 if (r->method == NGX_HTTP_HEAD) {
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)
189 r->headers_out.last_modified_time = -1;
190 r->headers_out.last_modified = NULL;
194 len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1
195 /* the end of the header */
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;
208 if (r->headers_out.status < NGX_HTTP_MOVED_PERMANENTLY) {
210 status = r->headers_out.status - NGX_HTTP_OK;
212 if (r->headers_out.status == NGX_HTTP_NO_CONTENT) {
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;
222 } else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) {
224 status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY
225 + NGX_HTTP_LEVEL_200;
227 if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) {
231 } else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) {
233 status = r->headers_out.status - NGX_HTTP_BAD_REQUEST
235 + NGX_HTTP_LEVEL_300;
239 status = r->headers_out.status - NGX_HTTP_INTERNAL_SERVER_ERROR
242 + NGX_HTTP_LEVEL_400;
245 len += ngx_http_status_lines[status].len;
248 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
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;
255 if (r->headers_out.date == NULL) {
256 len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
259 if (r->headers_out.content_type.len) {
260 len += sizeof("Content-Type: ") - 1
261 + r->headers_out.content_type.len + 2;
263 if (r->headers_out.content_type_len == r->headers_out.content_type.len
264 && r->headers_out.charset.len)
266 len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
270 if (r->headers_out.content_length == NULL
271 && r->headers_out.content_length_n >= 0)
273 len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;
276 if (r->headers_out.last_modified == NULL
277 && r->headers_out.last_modified_time != -1)
279 len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
282 if (r->headers_out.location
283 && r->headers_out.location->value.len
284 && r->headers_out.location->value.data[0] == '/')
286 r->headers_out.location->hash = 0;
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;
292 } else if (r->headers_in.server.len) {
293 host = r->headers_in.server;
296 host.len = NGX_SOCKADDR_STRLEN;
299 if (ngx_http_server_addr(r, &host) != NGX_OK) {
304 switch (r->connection->local_sockaddr->sa_family) {
308 sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr;
309 port = ntohs(sin6->sin6_port);
312 default: /* AF_INET */
313 sin = (struct sockaddr_in *) r->connection->local_sockaddr;
314 port = ntohs(sin->sin_port);
318 len += sizeof("Location: https://") - 1
320 + r->headers_out.location->value.len + 2;
322 if (clcf->port_in_redirect) {
325 if (r->connection->ssl)
326 port = (port == 443) ? 0 : port;
329 port = (port == 80) ? 0 : port;
333 len += sizeof(":65535") - 1;
343 len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
347 len += sizeof("Connection: keep-alive" CRLF) - 1;
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.
357 if (clcf->keepalive_header) {
358 len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2;
362 len += sizeof("Connection: closed" CRLF) - 1;
366 if (r->gzip && clcf->gzip_vary) {
367 len += sizeof("Vary: Accept-Encoding" CRLF) - 1;
371 part = &r->headers_out.headers.part;
374 for (i = 0; /* void */; i++) {
376 if (i >= part->nelts) {
377 if (part->next == NULL) {
386 if (header[i].hash == 0) {
390 len += header[i].key.len + sizeof(": ") - 1 + header[i].value.len
394 b = ngx_create_temp_buf(r->pool, len);
400 b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);
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);
408 b->last = ngx_copy(b->last, ngx_http_status_lines[status].data,
409 ngx_http_status_lines[status].len);
411 *b->last++ = CR; *b->last++ = LF;
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;
419 p = (u_char *) ngx_http_server_string;
420 len = sizeof(ngx_http_server_string) - 1;
423 b->last = ngx_cpymem(b->last, p, len);
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);
431 *b->last++ = CR; *b->last++ = LF;
434 if (r->headers_out.content_type.len) {
435 b->last = ngx_cpymem(b->last, "Content-Type: ",
436 sizeof("Content-Type: ") - 1);
438 b->last = ngx_copy(b->last, r->headers_out.content_type.data,
439 r->headers_out.content_type.len);
441 if (r->headers_out.content_type_len == r->headers_out.content_type.len
442 && r->headers_out.charset.len)
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);
449 /* update r->headers_out.content_type for possible logging */
451 r->headers_out.content_type.len = b->last - p;
452 r->headers_out.content_type.data = p;
455 *b->last++ = CR; *b->last++ = LF;
458 if (r->headers_out.content_length == NULL
459 && r->headers_out.content_length_n >= 0)
461 b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF,
462 r->headers_out.content_length_n);
465 if (r->headers_out.last_modified == NULL
466 && r->headers_out.last_modified_time != -1)
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);
472 *b->last++ = CR; *b->last++ = LF;
477 p = b->last + sizeof("Location: ") - 1;
479 b->last = ngx_cpymem(b->last, "Location: http",
480 sizeof("Location: http") - 1);
483 if (r->connection->ssl) {
488 *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/';
489 b->last = ngx_copy(b->last, host.data, host.len);
492 b->last = ngx_sprintf(b->last, ":%ui", port);
495 b->last = ngx_copy(b->last, r->headers_out.location->value.data,
496 r->headers_out.location->value.len);
498 /* update r->headers_out.location->value for possible logging */
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: ";
505 *b->last++ = CR; *b->last++ = LF;
509 b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF,
510 sizeof("Transfer-Encoding: chunked" CRLF) - 1);
514 b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF,
515 sizeof("Connection: keep-alive" CRLF) - 1);
517 if (clcf->keepalive_header) {
518 b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,
519 clcf->keepalive_header);
523 b->last = ngx_cpymem(b->last, "Connection: close" CRLF,
524 sizeof("Connection: close" CRLF) - 1);
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);
534 part = &r->headers_out.headers.part;
537 for (i = 0; /* void */; i++) {
539 if (i >= part->nelts) {
540 if (part->next == NULL) {
549 if (header[i].hash == 0) {
553 b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
554 *b->last++ = ':'; *b->last++ = ' ';
556 b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len);
557 *b->last++ = CR; *b->last++ = LF;
560 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
561 "%*s\n", (size_t) (b->last - b->pos), b->pos);
563 /* the end of HTTP header */
564 *b->last++ = CR; *b->last++ = LF;
566 r->header_size = b->last - b->pos;
568 if (r->header_only) {
575 return ngx_http_write_filter(r, &out);
580 ngx_http_header_filter_init(ngx_conf_t *cf)
582 ngx_http_top_header_filter = ngx_http_header_filter;