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