comparison src/http/v2/ngx_http_v2_filter_module.c @ 7235:c2a0a838c40f

gRPC: special handling of "trailer only" responses. The gRPC protocol makes a distinction between HEADERS frame with the END_STREAM flag set, and a HEADERS frame followed by an empty DATA frame with the END_STREAM flag. The latter is not permitted, and results in errors not being propagated through nginx. Instead, gRPC clients complain that "server closed the stream without sending trailers" (seen in grpc-go) or "13: Received RST_STREAM with error code 2" (seen in grpc-c). To fix this, nginx now returns HEADERS with the END_STREAM flag if the response length is known to be 0, and we are not expecting any trailer headers to be added. And the response length is explicitly set to 0 in the gRPC proxy if we see initial HEADERS frame with the END_STREAM flag set.
author Maxim Dounin <mdounin@mdounin.ru>
date Sat, 17 Mar 2018 23:04:26 +0300
parents 87e9e4aabf1b
children 190591ab0d76
comparison
equal deleted inserted replaced
7234:c693daca57f7 7235:c2a0a838c40f
134 ngx_http_v2_header_filter(ngx_http_request_t *r) 134 ngx_http_v2_header_filter(ngx_http_request_t *r)
135 { 135 {
136 u_char status, *pos, *start, *p, *tmp; 136 u_char status, *pos, *start, *p, *tmp;
137 size_t len, tmp_len; 137 size_t len, tmp_len;
138 ngx_str_t host, location; 138 ngx_str_t host, location;
139 ngx_uint_t i, port; 139 ngx_uint_t i, port, fin;
140 ngx_list_part_t *part; 140 ngx_list_part_t *part;
141 ngx_table_elt_t *header; 141 ngx_table_elt_t *header;
142 ngx_connection_t *fc; 142 ngx_connection_t *fc;
143 ngx_http_cleanup_t *cln; 143 ngx_http_cleanup_t *cln;
144 ngx_http_v2_stream_t *stream; 144 ngx_http_v2_stream_t *stream;
641 641
642 pos = ngx_http_v2_write_value(pos, header[i].value.data, 642 pos = ngx_http_v2_write_value(pos, header[i].value.data,
643 header[i].value.len, tmp); 643 header[i].value.len, tmp);
644 } 644 }
645 645
646 frame = ngx_http_v2_create_headers_frame(r, start, pos, r->header_only); 646 fin = r->header_only
647 || (r->headers_out.content_length_n == 0 && !r->expect_trailers);
648
649 frame = ngx_http_v2_create_headers_frame(r, start, pos, fin);
647 if (frame == NULL) { 650 if (frame == NULL) {
648 return NGX_ERROR; 651 return NGX_ERROR;
649 } 652 }
650 653
651 ngx_http_v2_queue_blocked_frame(h2c, frame); 654 ngx_http_v2_queue_blocked_frame(h2c, frame);
1435 } 1438 }
1436 1439
1437 in = in->next; 1440 in = in->next;
1438 } 1441 }
1439 1442
1440 if (in == NULL) { 1443 if (in == NULL || stream->out_closed) {
1441 1444
1442 if (stream->queued) { 1445 if (stream->queued) {
1443 fc->write->active = 1; 1446 fc->write->active = 1;
1444 fc->write->ready = 0; 1447 fc->write->ready = 0;
1445 1448