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