aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Dounin <mdounin@mdounin.ru>2018-03-17 23:04:26 +0300
committerMaxim Dounin <mdounin@mdounin.ru>2018-03-17 23:04:26 +0300
commit6559a420134b0f52ce2d4f147bdd92269ad5f677 (patch)
tree31489b3932e6aad60824b7a4cf306e66aa545e8a
parent6a0d9e5b2d9274e5ac5059a674763f19c2731b11 (diff)
downloadnginx-6559a420134b0f52ce2d4f147bdd92269ad5f677.tar.gz
nginx-6559a420134b0f52ce2d4f147bdd92269ad5f677.zip
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.
-rw-r--r--src/http/modules/ngx_http_grpc_module.c17
-rw-r--r--src/http/v2/ngx_http_v2_filter_module.c9
2 files changed, 16 insertions, 10 deletions
diff --git a/src/http/modules/ngx_http_grpc_module.c b/src/http/modules/ngx_http_grpc_module.c
index 774f8c79b..e39be537e 100644
--- a/src/http/modules/ngx_http_grpc_module.c
+++ b/src/http/modules/ngx_http_grpc_module.c
@@ -1743,13 +1743,16 @@ ngx_http_grpc_process_header(ngx_http_request_t *r)
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"grpc header done");
- if (ctx->end_stream
- && ctx->in == NULL
- && ctx->out == NULL
- && ctx->output_closed
- && b->last == b->pos)
- {
- u->keepalive = 1;
+ if (ctx->end_stream) {
+ u->headers_in.content_length_n = 0;
+
+ if (ctx->in == NULL
+ && ctx->out == NULL
+ && ctx->output_closed
+ && b->last == b->pos)
+ {
+ u->keepalive = 1;
+ }
}
return NGX_OK;
diff --git a/src/http/v2/ngx_http_v2_filter_module.c b/src/http/v2/ngx_http_v2_filter_module.c
index d2a5b46c5..aca80cf27 100644
--- a/src/http/v2/ngx_http_v2_filter_module.c
+++ b/src/http/v2/ngx_http_v2_filter_module.c
@@ -136,7 +136,7 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
u_char status, *pos, *start, *p, *tmp;
size_t len, tmp_len;
ngx_str_t host, location;
- ngx_uint_t i, port;
+ ngx_uint_t i, port, fin;
ngx_list_part_t *part;
ngx_table_elt_t *header;
ngx_connection_t *fc;
@@ -643,7 +643,10 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
header[i].value.len, tmp);
}
- frame = ngx_http_v2_create_headers_frame(r, start, pos, r->header_only);
+ fin = r->header_only
+ || (r->headers_out.content_length_n == 0 && !r->expect_trailers);
+
+ frame = ngx_http_v2_create_headers_frame(r, start, pos, fin);
if (frame == NULL) {
return NGX_ERROR;
}
@@ -1437,7 +1440,7 @@ ngx_http_v2_send_chain(ngx_connection_t *fc, ngx_chain_t *in, off_t limit)
in = in->next;
}
- if (in == NULL) {
+ if (in == NULL || stream->out_closed) {
if (stream->queued) {
fc->write->active = 1;