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
|
518
|
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
|
518
|
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
|
518
|
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" */
|
518
|
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) },
|
|
135 { ngx_string("Content-Length"),
|
|
136 offsetof(ngx_http_headers_out_t, content_length) },
|
|
137 { ngx_string("Content-Encoding"),
|
|
138 offsetof(ngx_http_headers_out_t, content_encoding) },
|
|
139 { ngx_string("Location"), offsetof(ngx_http_headers_out_t, location) },
|
|
140 { ngx_string("Last-Modified"),
|
|
141 offsetof(ngx_http_headers_out_t, last_modified) },
|
|
142 { ngx_string("Accept-Ranges"),
|
|
143 offsetof(ngx_http_headers_out_t, accept_ranges) },
|
|
144 { ngx_string("Expires"), offsetof(ngx_http_headers_out_t, expires) },
|
|
145 { ngx_string("Cache-Control"),
|
|
146 offsetof(ngx_http_headers_out_t, cache_control) },
|
|
147 { ngx_string("ETag"), offsetof(ngx_http_headers_out_t, etag) },
|
|
148
|
|
149 { ngx_null_string, 0 }
|
|
150 };
|
|
151
|
|
152
|
|
153 static ngx_int_t
|
|
154 ngx_http_header_filter(ngx_http_request_t *r)
|
|
155 {
|
|
156 u_char *p;
|
|
157 size_t len;
|
518
|
158 ngx_str_t host, *status_line;
|
230
|
159 ngx_buf_t *b;
|
493
|
160 ngx_uint_t status, i, port;
|
50
|
161 ngx_chain_t out;
|
|
162 ngx_list_part_t *part;
|
|
163 ngx_table_elt_t *header;
|
532
|
164 ngx_connection_t *c;
|
50
|
165 ngx_http_core_loc_conf_t *clcf;
|
362
|
166 ngx_http_core_srv_conf_t *cscf;
|
493
|
167 struct sockaddr_in *sin;
|
|
168 #if (NGX_HAVE_INET6)
|
|
169 struct sockaddr_in6 *sin6;
|
|
170 #endif
|
487
|
171 u_char addr[NGX_SOCKADDR_STRLEN];
|
50
|
172
|
216
|
173 r->header_sent = 1;
|
|
174
|
146
|
175 if (r != r->main) {
|
58
|
176 return NGX_OK;
|
|
177 }
|
|
178
|
50
|
179 if (r->http_version < NGX_HTTP_VERSION_10) {
|
|
180 return NGX_OK;
|
|
181 }
|
|
182
|
|
183 if (r->method == NGX_HTTP_HEAD) {
|
|
184 r->header_only = 1;
|
|
185 }
|
|
186
|
|
187 if (r->headers_out.last_modified_time != -1) {
|
|
188 if (r->headers_out.status != NGX_HTTP_OK
|
60
|
189 && r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT
|
|
190 && r->headers_out.status != NGX_HTTP_NOT_MODIFIED)
|
50
|
191 {
|
|
192 r->headers_out.last_modified_time = -1;
|
|
193 r->headers_out.last_modified = NULL;
|
|
194 }
|
|
195 }
|
|
196
|
|
197 len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1
|
|
198 /* the end of the header */
|
|
199 + sizeof(CRLF) - 1;
|
|
200
|
|
201 /* status line */
|
|
202
|
|
203 if (r->headers_out.status_line.len) {
|
|
204 len += r->headers_out.status_line.len;
|
518
|
205 status_line = &r->headers_out.status_line;
|
50
|
206 #if (NGX_SUPPRESS_WARN)
|
518
|
207 status = 0;
|
50
|
208 #endif
|
|
209
|
|
210 } else {
|
|
211
|
518
|
212 status = r->headers_out.status;
|
|
213
|
|
214 if (status >= NGX_HTTP_OK
|
|
215 && status < NGX_HTTP_LAST_LEVEL_200)
|
|
216 {
|
50
|
217 /* 2XX */
|
|
218
|
518
|
219 if (status == NGX_HTTP_NO_CONTENT) {
|
60
|
220 r->header_only = 1;
|
|
221 r->headers_out.content_type.len = 0;
|
|
222 r->headers_out.content_type.data = NULL;
|
|
223 r->headers_out.last_modified_time = -1;
|
|
224 r->headers_out.last_modified = NULL;
|
|
225 r->headers_out.content_length = NULL;
|
|
226 r->headers_out.content_length_n = -1;
|
|
227 }
|
|
228
|
518
|
229 status -= NGX_HTTP_OK;
|
|
230 status_line = &ngx_http_status_lines[status];
|
|
231 len += ngx_http_status_lines[status].len;
|
|
232
|
|
233 } else if (status >= NGX_HTTP_MOVED_PERMANENTLY
|
|
234 && status < NGX_HTTP_LAST_LEVEL_300)
|
|
235 {
|
50
|
236 /* 3XX */
|
|
237
|
518
|
238 if (status == NGX_HTTP_NOT_MODIFIED) {
|
50
|
239 r->header_only = 1;
|
|
240 }
|
|
241
|
518
|
242 status = status - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_LEVEL_200;
|
|
243 status_line = &ngx_http_status_lines[status];
|
|
244 len += ngx_http_status_lines[status].len;
|
|
245
|
|
246 } else if (status >= NGX_HTTP_BAD_REQUEST
|
520
|
247 && status < NGX_HTTP_LAST_LEVEL_400)
|
518
|
248 {
|
50
|
249 /* 4XX */
|
518
|
250 status = status - NGX_HTTP_BAD_REQUEST
|
|
251 + NGX_HTTP_LEVEL_200
|
|
252 + NGX_HTTP_LEVEL_300;
|
|
253
|
|
254 status_line = &ngx_http_status_lines[status];
|
|
255 len += ngx_http_status_lines[status].len;
|
|
256
|
|
257 } else if (status >= NGX_HTTP_INTERNAL_SERVER_ERROR
|
|
258 && status < NGX_HTTP_LAST_LEVEL_500)
|
|
259 {
|
|
260 /* 5XX */
|
|
261 status = status - NGX_HTTP_INTERNAL_SERVER_ERROR
|
|
262 + NGX_HTTP_LEVEL_200
|
|
263 + NGX_HTTP_LEVEL_300
|
|
264 + NGX_HTTP_LEVEL_400;
|
|
265
|
|
266 status_line = &ngx_http_status_lines[status];
|
|
267 len += ngx_http_status_lines[status].len;
|
50
|
268
|
|
269 } else {
|
518
|
270 len += NGX_INT_T_LEN;
|
|
271 status_line = NULL;
|
50
|
272 }
|
|
273 }
|
|
274
|
342
|
275 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
276
|
58
|
277 if (r->headers_out.server == NULL) {
|
342
|
278 len += clcf->server_tokens ? sizeof(ngx_http_server_full_string) - 1:
|
|
279 sizeof(ngx_http_server_string) - 1;
|
50
|
280 }
|
|
281
|
58
|
282 if (r->headers_out.date == NULL) {
|
50
|
283 len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
|
|
284 }
|
|
285
|
58
|
286 if (r->headers_out.content_type.len) {
|
50
|
287 len += sizeof("Content-Type: ") - 1
|
58
|
288 + r->headers_out.content_type.len + 2;
|
50
|
289
|
206
|
290 if (r->headers_out.content_type_len == r->headers_out.content_type.len
|
|
291 && r->headers_out.charset.len)
|
|
292 {
|
50
|
293 len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
|
|
294 }
|
|
295 }
|
|
296
|
58
|
297 if (r->headers_out.content_length == NULL
|
|
298 && r->headers_out.content_length_n >= 0)
|
|
299 {
|
|
300 len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;
|
|
301 }
|
|
302
|
|
303 if (r->headers_out.last_modified == NULL
|
|
304 && r->headers_out.last_modified_time != -1)
|
|
305 {
|
|
306 len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
|
|
307 }
|
|
308
|
532
|
309 c = r->connection;
|
|
310
|
50
|
311 if (r->headers_out.location
|
|
312 && r->headers_out.location->value.len
|
|
313 && r->headers_out.location->value.data[0] == '/')
|
|
314 {
|
58
|
315 r->headers_out.location->hash = 0;
|
|
316
|
362
|
317 if (clcf->server_name_in_redirect) {
|
|
318 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
319 host = cscf->server_name;
|
|
320
|
376
|
321 } else if (r->headers_in.server.len) {
|
|
322 host = r->headers_in.server;
|
362
|
323
|
|
324 } else {
|
487
|
325 host.len = NGX_SOCKADDR_STRLEN;
|
362
|
326 host.data = addr;
|
|
327
|
532
|
328 if (ngx_connection_local_sockaddr(c, &host, 0) != NGX_OK) {
|
362
|
329 return NGX_ERROR;
|
|
330 }
|
|
331 }
|
|
332
|
532
|
333 switch (c->local_sockaddr->sa_family) {
|
66
|
334
|
493
|
335 #if (NGX_HAVE_INET6)
|
|
336 case AF_INET6:
|
532
|
337 sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
|
493
|
338 port = ntohs(sin6->sin6_port);
|
|
339 break;
|
|
340 #endif
|
|
341 default: /* AF_INET */
|
532
|
342 sin = (struct sockaddr_in *) c->local_sockaddr;
|
493
|
343 port = ntohs(sin->sin_port);
|
|
344 break;
|
|
345 }
|
50
|
346
|
493
|
347 len += sizeof("Location: https://") - 1
|
|
348 + host.len
|
|
349 + r->headers_out.location->value.len + 2;
|
|
350
|
|
351 if (clcf->port_in_redirect) {
|
|
352
|
|
353 #if (NGX_HTTP_SSL)
|
532
|
354 if (c->ssl)
|
495
|
355 port = (port == 443) ? 0 : port;
|
|
356 else
|
66
|
357 #endif
|
495
|
358 port = (port == 80) ? 0 : port;
|
524
|
359
|
|
360 } else {
|
|
361 port = 0;
|
495
|
362 }
|
66
|
363
|
493
|
364 if (port) {
|
|
365 len += sizeof(":65535") - 1;
|
50
|
366 }
|
362
|
367
|
|
368 } else {
|
|
369 host.len = 0;
|
|
370 host.data = NULL;
|
493
|
371 port = 0;
|
50
|
372 }
|
|
373
|
|
374 if (r->chunked) {
|
|
375 len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
|
|
376 }
|
|
377
|
|
378 if (r->keepalive) {
|
|
379 len += sizeof("Connection: keep-alive" CRLF) - 1;
|
|
380
|
|
381 /*
|
|
382 * MSIE and Opera ignore the "Keep-Alive: timeout=<N>" header.
|
|
383 * MSIE keeps the connection alive for about 60-65 seconds.
|
|
384 * Opera keeps the connection alive very long.
|
|
385 * Mozilla keeps the connection alive for N plus about 1-10 seconds.
|
|
386 * Konqueror keeps the connection alive for about N seconds.
|
|
387 */
|
|
388
|
|
389 if (clcf->keepalive_header) {
|
|
390 len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2;
|
|
391 }
|
|
392
|
|
393 } else {
|
|
394 len += sizeof("Connection: closed" CRLF) - 1;
|
|
395 }
|
|
396
|
386
|
397 #if (NGX_HTTP_GZIP)
|
589
|
398 if (r->gzip_vary) {
|
|
399 if (clcf->gzip_vary) {
|
|
400 len += sizeof("Vary: Accept-Encoding" CRLF) - 1;
|
|
401
|
|
402 } else {
|
|
403 r->gzip_vary = 0;
|
|
404 }
|
382
|
405 }
|
386
|
406 #endif
|
382
|
407
|
50
|
408 part = &r->headers_out.headers.part;
|
|
409 header = part->elts;
|
|
410
|
|
411 for (i = 0; /* void */; i++) {
|
|
412
|
|
413 if (i >= part->nelts) {
|
|
414 if (part->next == NULL) {
|
|
415 break;
|
|
416 }
|
|
417
|
|
418 part = part->next;
|
|
419 header = part->elts;
|
|
420 i = 0;
|
|
421 }
|
|
422
|
58
|
423 if (header[i].hash == 0) {
|
50
|
424 continue;
|
|
425 }
|
|
426
|
|
427 len += header[i].key.len + sizeof(": ") - 1 + header[i].value.len
|
|
428 + sizeof(CRLF) - 1;
|
|
429 }
|
|
430
|
|
431 b = ngx_create_temp_buf(r->pool, len);
|
|
432 if (b == NULL) {
|
|
433 return NGX_ERROR;
|
|
434 }
|
|
435
|
|
436 /* "HTTP/1.x " */
|
|
437 b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);
|
|
438
|
|
439 /* status line */
|
518
|
440 if (status_line) {
|
|
441 b->last = ngx_copy(b->last, status_line->data, status_line->len);
|
50
|
442
|
|
443 } else {
|
518
|
444 b->last = ngx_sprintf(b->last, "%ui", status);
|
50
|
445 }
|
|
446 *b->last++ = CR; *b->last++ = LF;
|
|
447
|
58
|
448 if (r->headers_out.server == NULL) {
|
342
|
449 if (clcf->server_tokens) {
|
|
450 p = (u_char *) ngx_http_server_full_string;
|
|
451 len = sizeof(ngx_http_server_full_string) - 1;
|
|
452
|
|
453 } else {
|
|
454 p = (u_char *) ngx_http_server_string;
|
|
455 len = sizeof(ngx_http_server_string) - 1;
|
|
456 }
|
|
457
|
|
458 b->last = ngx_cpymem(b->last, p, len);
|
50
|
459 }
|
|
460
|
58
|
461 if (r->headers_out.date == NULL) {
|
50
|
462 b->last = ngx_cpymem(b->last, "Date: ", sizeof("Date: ") - 1);
|
|
463 b->last = ngx_cpymem(b->last, ngx_cached_http_time.data,
|
|
464 ngx_cached_http_time.len);
|
|
465
|
|
466 *b->last++ = CR; *b->last++ = LF;
|
|
467 }
|
|
468
|
58
|
469 if (r->headers_out.content_type.len) {
|
50
|
470 b->last = ngx_cpymem(b->last, "Content-Type: ",
|
|
471 sizeof("Content-Type: ") - 1);
|
|
472 p = b->last;
|
122
|
473 b->last = ngx_copy(b->last, r->headers_out.content_type.data,
|
|
474 r->headers_out.content_type.len);
|
50
|
475
|
206
|
476 if (r->headers_out.content_type_len == r->headers_out.content_type.len
|
|
477 && r->headers_out.charset.len)
|
|
478 {
|
50
|
479 b->last = ngx_cpymem(b->last, "; charset=",
|
|
480 sizeof("; charset=") - 1);
|
122
|
481 b->last = ngx_copy(b->last, r->headers_out.charset.data,
|
|
482 r->headers_out.charset.len);
|
50
|
483
|
58
|
484 /* update r->headers_out.content_type for possible logging */
|
|
485
|
|
486 r->headers_out.content_type.len = b->last - p;
|
|
487 r->headers_out.content_type.data = p;
|
50
|
488 }
|
|
489
|
|
490 *b->last++ = CR; *b->last++ = LF;
|
|
491 }
|
|
492
|
58
|
493 if (r->headers_out.content_length == NULL
|
|
494 && r->headers_out.content_length_n >= 0)
|
|
495 {
|
|
496 b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF,
|
|
497 r->headers_out.content_length_n);
|
|
498 }
|
|
499
|
|
500 if (r->headers_out.last_modified == NULL
|
|
501 && r->headers_out.last_modified_time != -1)
|
|
502 {
|
|
503 b->last = ngx_cpymem(b->last, "Last-Modified: ",
|
|
504 sizeof("Last-Modified: ") - 1);
|
|
505 b->last = ngx_http_time(b->last, r->headers_out.last_modified_time);
|
|
506
|
|
507 *b->last++ = CR; *b->last++ = LF;
|
|
508 }
|
|
509
|
362
|
510 if (host.data) {
|
|
511
|
50
|
512 p = b->last + sizeof("Location: ") - 1;
|
66
|
513
|
|
514 b->last = ngx_cpymem(b->last, "Location: http",
|
|
515 sizeof("Location: http") - 1);
|
|
516
|
|
517 #if (NGX_HTTP_SSL)
|
532
|
518 if (c->ssl) {
|
66
|
519 *b->last++ ='s';
|
|
520 }
|
|
521 #endif
|
|
522
|
|
523 *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/';
|
362
|
524 b->last = ngx_copy(b->last, host.data, host.len);
|
66
|
525
|
493
|
526 if (port) {
|
|
527 b->last = ngx_sprintf(b->last, ":%ui", port);
|
50
|
528 }
|
|
529
|
122
|
530 b->last = ngx_copy(b->last, r->headers_out.location->value.data,
|
|
531 r->headers_out.location->value.len);
|
50
|
532
|
58
|
533 /* update r->headers_out.location->value for possible logging */
|
|
534
|
50
|
535 r->headers_out.location->value.len = b->last - p;
|
|
536 r->headers_out.location->value.data = p;
|
607
|
537 r->headers_out.location->key.len = sizeof("Location") - 1;
|
|
538 r->headers_out.location->key.data = (u_char *) "Location";
|
50
|
539
|
|
540 *b->last++ = CR; *b->last++ = LF;
|
|
541 }
|
|
542
|
|
543 if (r->chunked) {
|
|
544 b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF,
|
|
545 sizeof("Transfer-Encoding: chunked" CRLF) - 1);
|
|
546 }
|
|
547
|
|
548 if (r->keepalive) {
|
|
549 b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF,
|
|
550 sizeof("Connection: keep-alive" CRLF) - 1);
|
|
551
|
|
552 if (clcf->keepalive_header) {
|
|
553 b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,
|
|
554 clcf->keepalive_header);
|
|
555 }
|
|
556
|
|
557 } else {
|
|
558 b->last = ngx_cpymem(b->last, "Connection: close" CRLF,
|
|
559 sizeof("Connection: close" CRLF) - 1);
|
|
560 }
|
|
561
|
386
|
562 #if (NGX_HTTP_GZIP)
|
589
|
563 if (r->gzip_vary) {
|
382
|
564 b->last = ngx_cpymem(b->last, "Vary: Accept-Encoding" CRLF,
|
|
565 sizeof("Vary: Accept-Encoding" CRLF) - 1);
|
|
566 }
|
386
|
567 #endif
|
382
|
568
|
50
|
569 part = &r->headers_out.headers.part;
|
|
570 header = part->elts;
|
|
571
|
|
572 for (i = 0; /* void */; i++) {
|
|
573
|
|
574 if (i >= part->nelts) {
|
|
575 if (part->next == NULL) {
|
|
576 break;
|
|
577 }
|
|
578
|
|
579 part = part->next;
|
|
580 header = part->elts;
|
|
581 i = 0;
|
|
582 }
|
|
583
|
58
|
584 if (header[i].hash == 0) {
|
50
|
585 continue;
|
|
586 }
|
|
587
|
122
|
588 b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
|
378
|
589 *b->last++ = ':'; *b->last++ = ' ';
|
50
|
590
|
122
|
591 b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len);
|
50
|
592 *b->last++ = CR; *b->last++ = LF;
|
|
593 }
|
|
594
|
532
|
595 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
520
|
596 "%*s", (size_t) (b->last - b->pos), b->pos);
|
50
|
597
|
|
598 /* the end of HTTP header */
|
|
599 *b->last++ = CR; *b->last++ = LF;
|
|
600
|
|
601 r->header_size = b->last - b->pos;
|
|
602
|
|
603 if (r->header_only) {
|
|
604 b->last_buf = 1;
|
|
605 }
|
|
606
|
|
607 out.buf = b;
|
|
608 out.next = NULL;
|
|
609
|
|
610 return ngx_http_write_filter(r, &out);
|
|
611 }
|
|
612
|
|
613
|
|
614 static ngx_int_t
|
230
|
615 ngx_http_header_filter_init(ngx_conf_t *cf)
|
50
|
616 {
|
|
617 ngx_http_top_header_filter = ngx_http_header_filter;
|
|
618
|
|
619 return NGX_OK;
|
|
620 }
|