Changeset 7234:c693daca57f7 in nginx


Ignore:
Timestamp:
03/17/18 20:04:25 (2 years ago)
Author:
Maxim Dounin <mdounin@…>
Branch:
default
Phase:
public
Message:

gRPC: special handling of the TE request header.

According to the gRPC protocol specification, the "TE" header is used
to detect incompatible proxies, and at least grpc-c server rejects
requests without "TE: trailers".

To preserve the logic, we have to pass "TE: trailers" to the backend if
and only if the original request contains "trailers" in the "TE" header.
Note that no other TE values are allowed in HTTP/2, so we have to remove
anything else.

Location:
src/http
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/http/modules/ngx_http_grpc_module.c

    r7233 r7234  
    177177    ngx_int_t rc);
    178178
     179static ngx_int_t ngx_http_grpc_internal_trailers_variable(
     180    ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
     181
     182static ngx_int_t ngx_http_grpc_add_variables(ngx_conf_t *cf);
    179183static void *ngx_http_grpc_create_loc_conf(ngx_conf_t *cf);
    180184static char *ngx_http_grpc_merge_loc_conf(ngx_conf_t *cf,
     
    420424
    421425static ngx_http_module_t  ngx_http_grpc_module_ctx = {
    422     NULL,                                  /* preconfiguration */
     426    ngx_http_grpc_add_variables,           /* preconfiguration */
    423427    NULL,                                  /* postconfiguration */
    424428
     
    464468static ngx_keyval_t  ngx_http_grpc_headers[] = {
    465469    { ngx_string("Content-Length"), ngx_string("$content_length") },
     470    { ngx_string("TE"), ngx_string("$grpc_internal_trailers") },
    466471    { ngx_string("Host"), ngx_string("") },
    467472    { ngx_string("Connection"), ngx_string("") },
    468473    { ngx_string("Transfer-Encoding"), ngx_string("") },
    469     { ngx_string("TE"), ngx_string("") },
    470474    { ngx_string("Keep-Alive"), ngx_string("") },
    471475    { ngx_string("Expect"), ngx_string("") },
     
    484488    ngx_string("X-Accel-Charset"),
    485489    ngx_null_string
     490};
     491
     492
     493static ngx_http_variable_t  ngx_http_grpc_vars[] = {
     494
     495    { ngx_string("grpc_internal_trailers"), NULL,
     496      ngx_http_grpc_internal_trailers_variable, 0,
     497      NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
     498
     499      ngx_http_null_variable
    486500};
    487501
     
    39974011
    39984012
     4013static ngx_int_t
     4014ngx_http_grpc_internal_trailers_variable(ngx_http_request_t *r,
     4015    ngx_http_variable_value_t *v, uintptr_t data)
     4016{
     4017    ngx_table_elt_t  *te;
     4018
     4019    te = r->headers_in.te;
     4020
     4021    if (te == NULL) {
     4022        v->not_found = 1;
     4023        return NGX_OK;
     4024    }
     4025
     4026    if (ngx_strlcasestrn(te->value.data, te->value.data + te->value.len,
     4027                         (u_char *) "trailers", 8 - 1)
     4028        == NULL)
     4029    {
     4030        v->not_found = 1;
     4031        return NGX_OK;
     4032    }
     4033
     4034    v->valid = 1;
     4035    v->no_cacheable = 0;
     4036    v->not_found = 0;
     4037
     4038    v->data = (u_char *) "trailers";
     4039    v->len = sizeof("trailers") - 1;
     4040
     4041    return NGX_OK;
     4042}
     4043
     4044
     4045static ngx_int_t
     4046ngx_http_grpc_add_variables(ngx_conf_t *cf)
     4047{
     4048    ngx_http_variable_t  *var, *v;
     4049
     4050    for (v = ngx_http_grpc_vars; v->name.len; v++) {
     4051        var = ngx_http_add_variable(cf, &v->name, v->flags);
     4052        if (var == NULL) {
     4053            return NGX_ERROR;
     4054        }
     4055
     4056        var->get_handler = v->get_handler;
     4057        var->data = v->data;
     4058    }
     4059
     4060    return NGX_OK;
     4061}
     4062
     4063
    39994064static void *
    40004065ngx_http_grpc_create_loc_conf(ngx_conf_t *cf)
  • src/http/ngx_http_request.c

    r7206 r7234  
    133133                 ngx_http_process_header_line },
    134134
     135    { ngx_string("TE"),
     136                 offsetof(ngx_http_headers_in_t, te),
     137                 ngx_http_process_header_line },
     138
    135139    { ngx_string("Expect"),
    136140                 offsetof(ngx_http_headers_in_t, expect),
  • src/http/ngx_http_request.h

    r7206 r7234  
    198198
    199199    ngx_table_elt_t                  *transfer_encoding;
     200    ngx_table_elt_t                  *te;
    200201    ngx_table_elt_t                  *expect;
    201202    ngx_table_elt_t                  *upgrade;
Note: See TracChangeset for help on using the changeset viewer.