comparison src/http/v3/ngx_http_v3_request.c @ 8215:38c0898b6df7 quic

HTTP/3.
author Roman Arutyunyan <arut@nginx.com>
date Fri, 13 Mar 2020 19:36:33 +0300
parents
children 1307308c3cf1
comparison
equal deleted inserted replaced
8214:6fd2cce50fe2 8215:38c0898b6df7
1
2 /*
3 * Copyright (C) Roman Arutyunyan
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 #include <ngx_http.h>
11
12
13 #define NGX_HTTP_V3_FRAME_DATA 0x00
14 #define NGX_HTTP_V3_FRAME_HEADERS 0x01
15 #define NGX_HTTP_V3_FRAME_CANCEL_PUSH 0x03
16 #define NGX_HTTP_V3_FRAME_SETTINGS 0x04
17 #define NGX_HTTP_V3_FRAME_PUSH_PROMISE 0x05
18 #define NGX_HTTP_V3_FRAME_GOAWAY 0x07
19 #define NGX_HTTP_V3_FRAME_MAX_PUSH_ID 0x0d
20
21
22 static ngx_int_t ngx_http_v3_process_pseudo_header(ngx_http_request_t *r,
23 ngx_str_t *name, ngx_str_t *value);
24
25
26 struct {
27 ngx_str_t name;
28 ngx_uint_t method;
29 } ngx_http_v3_methods[] = {
30
31 { ngx_string("GET"), NGX_HTTP_GET },
32 { ngx_string("POST"), NGX_HTTP_POST },
33 { ngx_string("HEAD"), NGX_HTTP_HEAD },
34 { ngx_string("OPTIONS"), NGX_HTTP_OPTIONS },
35 { ngx_string("PROPFIND"), NGX_HTTP_PROPFIND },
36 { ngx_string("PUT"), NGX_HTTP_PUT },
37 { ngx_string("MKCOL"), NGX_HTTP_MKCOL },
38 { ngx_string("DELETE"), NGX_HTTP_DELETE },
39 { ngx_string("COPY"), NGX_HTTP_COPY },
40 { ngx_string("MOVE"), NGX_HTTP_MOVE },
41 { ngx_string("PROPPATCH"), NGX_HTTP_PROPPATCH },
42 { ngx_string("LOCK"), NGX_HTTP_LOCK },
43 { ngx_string("UNLOCK"), NGX_HTTP_UNLOCK },
44 { ngx_string("PATCH"), NGX_HTTP_PATCH },
45 { ngx_string("TRACE"), NGX_HTTP_TRACE }
46 };
47
48
49 ngx_int_t
50 ngx_http_v3_parse_header(ngx_http_request_t *r, ngx_buf_t *b, ngx_uint_t pseudo)
51 {
52 u_char *p, ch;
53 ngx_str_t name, value;
54 ngx_int_t rc;
55 ngx_uint_t length, index, insert_count, sign, base, delta_base,
56 huffman, dynamic, offset;
57 ngx_connection_t *c;
58 ngx_http_v3_header_t *h;
59 enum {
60 sw_start = 0,
61 sw_length,
62 sw_length_1,
63 sw_length_2,
64 sw_length_3,
65 sw_header_block,
66 sw_req_insert_count,
67 sw_delta_base,
68 sw_read_delta_base,
69 sw_header,
70 sw_old_header,
71 sw_header_ri,
72 sw_header_pbi,
73 sw_header_lri,
74 sw_header_lpbi,
75 sw_header_l_name_len,
76 sw_header_l_name,
77 sw_header_value_len,
78 sw_header_read_value_len,
79 sw_header_value
80 } state;
81
82 c = r->connection;
83
84 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
85 "http3 parse header, pseudo:%ui", pseudo);
86
87 if (r->state == sw_old_header) {
88 r->state = sw_header;
89 return NGX_OK;
90 }
91
92 length = r->h3_length;
93 index = r->h3_index;
94 insert_count = r->h3_insert_count;
95 sign = r->h3_sign;
96 delta_base = r->h3_delta_base;
97 huffman = r->h3_huffman;
98 dynamic = r->h3_dynamic;
99 offset = r->h3_offset;
100
101 name.data = r->header_name_start;
102 name.len = r->header_name_end - r->header_name_start;
103 value.data = r->header_start;
104 value.len = r->header_end - r->header_start;
105
106 if (r->state == sw_start) {
107 length = 1;
108 }
109
110 again:
111
112 state = r->state;
113
114 if (state == sw_header && length == 0) {
115 r->state = sw_start;
116 return NGX_HTTP_PARSE_HEADER_DONE;
117 }
118
119 for (p = b->pos; p < b->last; p++) {
120
121 if (state >= sw_header_block && length-- == 0) {
122 goto failed;
123 }
124
125 ch = *p;
126
127 switch (state) {
128
129 case sw_start:
130
131 if (ch != NGX_HTTP_V3_FRAME_HEADERS) {
132 goto failed;
133 }
134
135 r->request_start = p;
136 state = sw_length;
137 break;
138
139 case sw_length:
140
141 length = ch;
142 if (length & 0xc0) {
143 state = sw_length_1;
144 break;
145 }
146
147 state = sw_header_block;
148 break;
149
150 case sw_length_1:
151
152 length = (length << 8) + ch;
153 if ((length & 0xc000) != 0x4000) {
154 state = sw_length_2;
155 break;
156 }
157
158 length &= 0x3fff;
159 state = sw_header_block;
160 break;
161
162 case sw_length_2:
163
164 length = (length << 8) + ch;
165 if ((length & 0xc00000) != 0x800000) {
166 state = sw_length_3;
167 break;
168 }
169
170 /* fall through */
171
172 case sw_length_3:
173
174 length &= 0x3fffff;
175 state = sw_header_block;
176 break;
177
178 case sw_header_block:
179
180 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
181 "http3 header block length:%ui", length);
182
183 if (ch != 0xff) {
184 insert_count = ch;
185 state = sw_delta_base;
186 break;
187 }
188
189 insert_count = 0;
190 state = sw_req_insert_count;
191 break;
192
193 case sw_req_insert_count:
194
195 insert_count = (insert_count << 7) + (ch & 0x7f);
196 if (ch & 0x80) {
197 break;
198 }
199
200 insert_count += 0xff;
201 state = sw_delta_base;
202 break;
203
204 case sw_delta_base:
205
206 sign = (ch & 0x80) ? 1 : 0;
207 delta_base = ch & 0x7f;
208
209 if (delta_base != 0x7f) {
210 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
211 "http3 header block "
212 "insert_count:%ui, sign:%ui, delta_base:%ui",
213 insert_count, sign, delta_base);
214 goto done;
215 }
216
217 delta_base = 0;
218 state = sw_read_delta_base;
219 break;
220
221 case sw_read_delta_base:
222
223 delta_base = (delta_base << 7) + (ch & 0x7f);
224 if (ch & 0x80) {
225 break;
226 }
227
228 delta_base += 0x7f;
229
230 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
231 "http3 header block "
232 "insert_count:%ui, sign:%ui, delta_base:%ui",
233 insert_count, sign, delta_base);
234 goto done;
235
236 case sw_header:
237
238 index = 0;
239 huffman = 0;
240 ngx_str_null(&name);
241 ngx_str_null(&value);
242
243 if (ch & 0x80) {
244 /* Indexed Header Field */
245
246 dynamic = (ch & 0x40) ? 0 : 1;
247 index = ch & 0x3f;
248
249 if (index != 0x3f) {
250 goto done;
251 }
252
253 index = 0;
254 state = sw_header_ri;
255 break;
256 }
257
258 if (ch & 0x40) {
259 /* Literal Header Field With Name Reference */
260
261 dynamic = (ch & 0x10) ? 0 : 1;
262 index = ch & 0x0f;
263
264 if (index != 0x0f) {
265 state = sw_header_value_len;
266 break;
267 }
268
269 index = 0;
270 state = sw_header_lri;
271 break;
272 }
273
274 if (ch & 0x20) {
275 /* Literal Header Field Without Name Reference */
276
277 huffman = (ch & 0x08) ? 1 : 0;
278 name.len = ch & 0x07;
279
280 if (name.len == 0) {
281 goto failed;
282 }
283
284 if (name.len != 0x07) {
285 offset = 0;
286 state = sw_header_l_name;
287 break;
288 }
289
290 name.len = 0;
291 state = sw_header_l_name_len;
292 break;
293 }
294
295 if (ch & 10) {
296 /* Indexed Header Field With Post-Base Index */
297
298 dynamic = 2;
299 index = ch & 0x0f;
300
301 if (index != 0x0f) {
302 goto done;
303 }
304
305 index = 0;
306 state = sw_header_pbi;
307 break;
308 }
309
310 /* Literal Header Field With Post-Base Name Reference */
311
312 dynamic = 2;
313 index = ch & 0x07;
314
315 if (index != 0x07) {
316 state = sw_header_value_len;
317 break;
318 }
319
320 index = 0;
321 state = sw_header_lpbi;
322 break;
323
324 case sw_header_ri:
325
326 index = (index << 7) + (ch & 0x7f);
327 if (ch & 0x80) {
328 break;
329 }
330
331 index += 0x3f;
332 goto done;
333
334 case sw_header_pbi:
335
336 index = (index << 7) + (ch & 0x7f);
337 if (ch & 0x80) {
338 break;
339 }
340
341 index += 0x0f;
342 goto done;
343
344 case sw_header_lri:
345
346 index = (index << 7) + (ch & 0x7f);
347 if (ch & 0x80) {
348 break;
349 }
350
351 index += 0x0f;
352 state = sw_header_value_len;
353 break;
354
355 case sw_header_lpbi:
356
357 index = (index << 7) + (ch & 0x7f);
358 if (ch & 0x80) {
359 break;
360 }
361
362 index += 0x07;
363 state = sw_header_value_len;
364 break;
365
366
367 case sw_header_l_name_len:
368
369 name.len = (name.len << 7) + (ch & 0x7f);
370 if (ch & 0x80) {
371 break;
372 }
373
374 name.len += 0x07;
375 offset = 0;
376 state = sw_header_l_name;
377 break;
378
379 case sw_header_l_name:
380 if (offset++ == 0) {
381 name.data = p;
382 }
383
384 if (offset != name.len) {
385 break;
386 }
387
388 if (huffman) {
389 if (ngx_http_v3_decode_huffman(c, &name) != NGX_OK) {
390 goto failed;
391 }
392 }
393
394 state = sw_header_value_len;
395 break;
396
397 case sw_header_value_len:
398
399 huffman = (ch & 0x80) ? 1 : 0;
400 value.len = ch & 0x7f;
401
402 if (value.len == 0) {
403 value.data = p;
404 goto done;
405 }
406
407 if (value.len != 0x7f) {
408 offset = 0;
409 state = sw_header_value;
410 break;
411 }
412
413 value.len = 0;
414 state = sw_header_read_value_len;
415 break;
416
417 case sw_header_read_value_len:
418
419 value.len = (value.len << 7) + (ch & 0x7f);
420 if (ch & 0x80) {
421 break;
422 }
423
424 value.len += 0x7f;
425 offset = 0;
426 state = sw_header_value;
427 break;
428
429 case sw_header_value:
430
431 if (offset++ == 0) {
432 value.data = p;
433 }
434
435 if (offset != value.len) {
436 break;
437 }
438
439 if (huffman) {
440 if (ngx_http_v3_decode_huffman(c, &value) != NGX_OK) {
441 goto failed;
442 }
443 }
444
445 goto done;
446
447 case sw_old_header:
448
449 break;
450 }
451 }
452
453 b->pos = p;
454 r->state = state;
455 r->h3_length = length;
456 r->h3_index = index;
457 r->h3_insert_count = insert_count;
458 r->h3_sign = sign;
459 r->h3_delta_base = delta_base;
460 r->h3_huffman = huffman;
461 r->h3_dynamic = dynamic;
462 r->h3_offset = offset;
463
464 /* XXX fix large reallocations */
465 r->header_name_start = name.data;
466 r->header_name_end = name.data + name.len;
467 r->header_start = value.data;
468 r->header_end = value.data + value.len;
469
470 /* XXX r->lowcase_index = i; */
471
472 return NGX_AGAIN;
473
474 done:
475
476 b->pos = p + 1;
477 r->state = sw_header;
478 r->h3_length = length;
479 r->h3_insert_count = insert_count;
480 r->h3_sign = sign;
481 r->h3_delta_base = delta_base;
482
483 if (state < sw_header) {
484 if (ngx_http_v3_check_insert_count(c, insert_count) != NGX_OK) {
485 return NGX_DONE;
486 }
487
488 goto again;
489 }
490
491 if (sign == 0) {
492 base = insert_count + delta_base;
493 } else {
494 base = insert_count - delta_base - 1;
495 }
496
497 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,
498 "http3 header %s[%ui], base:%ui, \"%V\":\"%V\"",
499 dynamic ? "dynamic" : "static", index, base, &name, &value);
500
501 if (name.data == NULL) {
502
503 if (dynamic == 2) {
504 index = base - index - 1;
505 } else if (dynamic == 1) {
506 index += base;
507 }
508
509 h = ngx_http_v3_lookup_table(c, dynamic, index);
510 if (h == NULL) {
511 goto failed;
512 }
513
514 name = h->name;
515
516 if (value.data == NULL) {
517 value = h->value;
518 }
519 }
520
521 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
522 "http3 header \"%V\":\"%V\"", &name, &value);
523
524 if (pseudo) {
525 rc = ngx_http_v3_process_pseudo_header(r, &name, &value);
526
527 if (rc == NGX_ERROR) {
528 goto failed;
529 }
530
531 if (rc == NGX_OK) {
532 r->request_end = p + 1;
533 goto again;
534 }
535
536 /* rc == NGX_DONE */
537
538 r->state = sw_old_header;
539 }
540
541 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
542 "http3 header left:%ui", length);
543
544 r->header_name_start = name.data;
545 r->header_name_end = name.data + name.len;
546 r->header_start = value.data;
547 r->header_end = value.data + value.len;
548 r->header_hash = ngx_hash_key(name.data, name.len); /* XXX */
549
550 /* XXX r->lowcase_index = i; */
551
552 return NGX_OK;
553
554 failed:
555
556 return NGX_HTTP_PARSE_INVALID_REQUEST;
557 }
558
559
560 static ngx_int_t
561 ngx_http_v3_process_pseudo_header(ngx_http_request_t *r, ngx_str_t *name,
562 ngx_str_t *value)
563 {
564 ngx_uint_t i;
565 ngx_connection_t *c;
566
567 c = r->connection;
568
569 if (name->len == 7 && ngx_strncmp(name->data, ":method", 7) == 0) {
570 r->method_start = value->data;
571 r->method_end = value->data + value->len;
572
573 for (i = 0; i < sizeof(ngx_http_v3_methods)
574 / sizeof(ngx_http_v3_methods[0]); i++)
575 {
576 if (value->len == ngx_http_v3_methods[i].name.len
577 && ngx_strncmp(value->data, ngx_http_v3_methods[i].name.data,
578 value->len) == 0)
579 {
580 r->method = ngx_http_v3_methods[i].method;
581 break;
582 }
583 }
584
585 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
586 "http3 method \"%V\" %ui", value, r->method);
587 return NGX_OK;
588 }
589
590 if (name->len == 5 && ngx_strncmp(name->data, ":path", 5) == 0) {
591 r->uri_start = value->data;
592 r->uri_end = value->data + value->len;
593
594 if (ngx_http_parse_uri(r) != NGX_OK) {
595 ngx_log_error(NGX_LOG_INFO, c->log, 0,
596 "client sent invalid :path header: \"%V\"", value);
597 return NGX_ERROR;
598 }
599
600 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
601 "http3 path \"%V\"", value);
602
603 return NGX_OK;
604 }
605
606 if (name->len == 7 && ngx_strncmp(name->data, ":scheme", 7) == 0) {
607 r->schema_start = value->data;
608 r->schema_end = value->data + value->len;
609
610 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
611 "http3 schema \"%V\"", value);
612
613 return NGX_OK;
614 }
615
616 if (name->len == 10 && ngx_strncmp(name->data, ":authority", 10) == 0) {
617 r->host_start = value->data;
618 r->host_end = value->data + value->len;
619
620 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
621 "http3 authority \"%V\"", value);
622
623 return NGX_OK;
624 }
625
626 if (name->len && name->data[0] == ':') {
627 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
628 "http3 unknown pseudo header \"%V\" \"%V\"",
629 name, value);
630 return NGX_OK;
631 }
632
633 return NGX_DONE;
634 }
635
636
637 ngx_chain_t *
638 ngx_http_v3_create_header(ngx_http_request_t *r)
639 {
640 u_char *p;
641 size_t len, hlen, n;
642 ngx_buf_t *b;
643 ngx_uint_t i, j;
644 ngx_chain_t *hl, *cl, *bl;
645 ngx_list_part_t *part;
646 ngx_table_elt_t *header;
647 ngx_connection_t *c;
648 ngx_http_core_loc_conf_t *clcf;
649
650 c = r->connection;
651
652 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 create header");
653
654 /* XXX support chunked body in the chunked filter */
655 if (r->headers_out.content_length_n == -1) {
656 return NULL;
657 }
658
659 len = 0;
660
661 if (r->headers_out.status == NGX_HTTP_OK) {
662 len += ngx_http_v3_encode_prefix_int(NULL, 25, 6);
663
664 } else {
665 len += 3 + ngx_http_v3_encode_prefix_int(NULL, 25, 4)
666 + ngx_http_v3_encode_prefix_int(NULL, 3, 7);
667 }
668
669 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
670
671 if (r->headers_out.server == NULL) {
672 if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
673 n = sizeof(NGINX_VER) - 1;
674
675 } else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
676 n = sizeof(NGINX_VER_BUILD) - 1;
677
678 } else {
679 n = sizeof("nginx") - 1;
680 }
681
682 len += ngx_http_v3_encode_prefix_int(NULL, 92, 4)
683 + ngx_http_v3_encode_prefix_int(NULL, n, 7) + n;
684 }
685
686 if (r->headers_out.date == NULL) {
687 len += ngx_http_v3_encode_prefix_int(NULL, 6, 4)
688 + ngx_http_v3_encode_prefix_int(NULL, ngx_cached_http_time.len,
689 7)
690 + ngx_cached_http_time.len;
691 }
692
693 if (r->headers_out.content_type.len) {
694 n = r->headers_out.content_type.len;
695
696 if (r->headers_out.content_type_len == r->headers_out.content_type.len
697 && r->headers_out.charset.len)
698 {
699 n += sizeof("; charset=") - 1 + r->headers_out.charset.len;
700 }
701
702 len += ngx_http_v3_encode_prefix_int(NULL, 53, 4)
703 + ngx_http_v3_encode_prefix_int(NULL, n, 7) + n;
704 }
705
706 if (r->headers_out.content_length_n == 0) {
707 len += ngx_http_v3_encode_prefix_int(NULL, 4, 6);
708
709 } else {
710 len += ngx_http_v3_encode_prefix_int(NULL, 4, 4) + 1 + NGX_OFF_T_LEN;
711 }
712
713 if (r->headers_out.last_modified == NULL
714 && r->headers_out.last_modified_time != -1)
715 {
716 len += ngx_http_v3_encode_prefix_int(NULL, 10, 4) + 1
717 + sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT");
718 }
719
720 /* XXX location */
721
722 #if (NGX_HTTP_GZIP)
723 if (r->gzip_vary) {
724 if (clcf->gzip_vary) {
725 /* Vary: Accept-Encoding */
726 len += ngx_http_v3_encode_prefix_int(NULL, 59, 6);
727
728 } else {
729 r->gzip_vary = 0;
730 }
731 }
732 #endif
733
734 part = &r->headers_out.headers.part;
735 header = part->elts;
736
737 for (i = 0; /* void */; i++) {
738
739 if (i >= part->nelts) {
740 if (part->next == NULL) {
741 break;
742 }
743
744 part = part->next;
745 header = part->elts;
746 i = 0;
747 }
748
749 if (header[i].hash == 0) {
750 continue;
751 }
752
753 len += ngx_http_v3_encode_prefix_int(NULL, header[i].key.len, 3)
754 + header[i].key.len
755 + ngx_http_v3_encode_prefix_int(NULL, header[i].value.len, 7 )
756 + header[i].value.len;
757 }
758
759 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 header len:%uz", len);
760
761 b = ngx_create_temp_buf(r->pool, len);
762 if (b == NULL) {
763 return NULL;
764 }
765
766 *b->last++ = 0;
767 *b->last++ = 0;
768
769 if (r->headers_out.status == NGX_HTTP_OK) {
770 /* :status: 200 */
771 *b->last = 0xc0;
772 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 25, 6);
773
774 } else {
775 /* :status: 200 */
776 *b->last = 0x70;
777 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 25, 4);
778 *b->last = 0;
779 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 3, 7);
780 b->last = ngx_sprintf(b->last, "%03ui ", r->headers_out.status);
781 }
782
783 if (r->headers_out.server == NULL) {
784 if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
785 p = (u_char *) NGINX_VER;
786 n = sizeof(NGINX_VER) - 1;
787
788 } else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
789 p = (u_char *) NGINX_VER_BUILD;
790 n = sizeof(NGINX_VER_BUILD) - 1;
791
792 } else {
793 p = (u_char *) "nginx";
794 n = sizeof("nginx") - 1;
795 }
796
797 /* server */
798 *b->last = 0x70;
799 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 92, 4);
800 *b->last = 0;
801 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, n, 7);
802 b->last = ngx_cpymem(b->last, p, n);
803 }
804
805 if (r->headers_out.date == NULL) {
806 /* date */
807 *b->last = 0x70;
808 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 6, 4);
809 *b->last = 0;
810 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last,
811 ngx_cached_http_time.len, 7);
812 b->last = ngx_cpymem(b->last, ngx_cached_http_time.data,
813 ngx_cached_http_time.len);
814 }
815
816 if (r->headers_out.content_type.len) {
817 n = r->headers_out.content_type.len;
818
819 if (r->headers_out.content_type_len == r->headers_out.content_type.len
820 && r->headers_out.charset.len)
821 {
822 n += sizeof("; charset=") - 1 + r->headers_out.charset.len;
823 }
824
825 /* content-type: text/plain */
826 *b->last = 0x70;
827 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 53, 4);
828 *b->last = 0;
829 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, n, 7);
830
831 p = b->last;
832 b->last = ngx_copy(b->last, r->headers_out.content_type.data,
833 r->headers_out.content_type.len);
834
835 if (r->headers_out.content_type_len == r->headers_out.content_type.len
836 && r->headers_out.charset.len)
837 {
838 b->last = ngx_cpymem(b->last, "; charset=",
839 sizeof("; charset=") - 1);
840 b->last = ngx_copy(b->last, r->headers_out.charset.data,
841 r->headers_out.charset.len);
842
843 /* update r->headers_out.content_type for possible logging */
844
845 r->headers_out.content_type.len = b->last - p;
846 r->headers_out.content_type.data = p;
847 }
848 }
849
850 if (r->headers_out.content_length_n == 0) {
851 /* content-length: 0 */
852 *b->last = 0xc0;
853 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 4, 6);
854
855 } else if (r->headers_out.content_length_n > 0) {
856 /* content-length: 0 */
857 *b->last = 0x70;
858 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 4, 4);
859 p = b->last++;
860 b->last = ngx_sprintf(b->last, "%O", r->headers_out.content_length_n);
861 *p = b->last - p - 1;
862 }
863
864 if (r->headers_out.last_modified == NULL
865 && r->headers_out.last_modified_time != -1)
866 {
867 /* last-modified */
868 *b->last = 0x70;
869 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 10, 4);
870 p = b->last++;
871 b->last = ngx_http_time(b->last, r->headers_out.last_modified_time);
872 *p = b->last - p - 1;
873 }
874
875 #if (NGX_HTTP_GZIP)
876 if (r->gzip_vary) {
877 /* vary: accept-encoding */
878 *b->last = 0xc0;
879 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 59, 6);
880 }
881 #endif
882
883 part = &r->headers_out.headers.part;
884 header = part->elts;
885
886 for (i = 0; /* void */; i++) {
887
888 if (i >= part->nelts) {
889 if (part->next == NULL) {
890 break;
891 }
892
893 part = part->next;
894 header = part->elts;
895 i = 0;
896 }
897
898 if (header[i].hash == 0) {
899 continue;
900 }
901
902 *b->last = 0x30;
903 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last,
904 header[i].key.len,
905 3);
906 for (j = 0; j < header[i].key.len; j++) {
907 *b->last++ = ngx_tolower(header[i].key.data[j]);
908 }
909
910 *b->last = 0;
911 b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last,
912 header[i].value.len,
913 7);
914 b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len);
915 }
916
917 cl = ngx_alloc_chain_link(c->pool);
918 if (cl == NULL) {
919 return NULL;
920 }
921
922 cl->buf = b;
923 cl->next = NULL;
924
925 n = b->last - b->pos;
926
927 len = 1 + ngx_http_v3_encode_varlen_int(NULL, n);
928
929 b = ngx_create_temp_buf(c->pool, len);
930 if (b == NULL) {
931 return NULL;
932 }
933
934 *b->last++ = NGX_HTTP_V3_FRAME_HEADERS;
935 b->last = (u_char *) ngx_http_v3_encode_varlen_int(b->last, n);
936
937 hl = ngx_alloc_chain_link(c->pool);
938 if (hl == NULL) {
939 return NULL;
940 }
941
942 hl->buf = b;
943 hl->next = cl;
944
945 hlen = 1 + ngx_http_v3_encode_varlen_int(NULL, len);
946
947 if (r->headers_out.content_length_n >= 0) {
948 len = 1 + ngx_http_v3_encode_varlen_int(NULL,
949 r->headers_out.content_length_n);
950
951 b = ngx_create_temp_buf(c->pool, len);
952 if (b == NULL) {
953 NULL;
954 }
955
956 *b->last++ = NGX_HTTP_V3_FRAME_DATA;
957 b->last = (u_char *) ngx_http_v3_encode_varlen_int(b->last,
958 r->headers_out.content_length_n);
959
960 bl = ngx_alloc_chain_link(c->pool);
961 if (bl == NULL) {
962 return NULL;
963 }
964
965 bl->buf = b;
966 bl->next = NULL;
967 cl->next = bl;
968 }
969
970 return hl;
971 }