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