diff --git a/src/ngx_pagespeed.cc b/src/ngx_pagespeed.cc index a4490d433..965f9ed4a 100644 --- a/src/ngx_pagespeed.cc +++ b/src/ngx_pagespeed.cc @@ -1086,6 +1086,17 @@ char* ps_merge_srv_conf(ngx_conf_t* cf, void* parent, void* child) { cfg_m->driver_factory->set_main_conf(parent_cfg_s->options); cfg_s->server_context = cfg_m->driver_factory->MakeNgxServerContext( "dummy_hostname", dummy_port); + +#if (NGX_HTTP_V2) + // Save the variable index of the "http2" variable, so we can use it + // at request time to lookup whether that's on. We do this conditionally + // since NGINX will complain to the user (at [emerg] level!) if it doesn't + // know about it. + ngx_str_t name = ngx_string("http2"); + cfg_s->server_context->set_ngx_http2_variable_index( + ngx_http_get_variable_index(cf, &name)); +#endif + // The server context sets some options when we call global_options(). So // let it do that, then merge in options we got from the config file. // Once we do that we're done with cfg_s->options. diff --git a/src/ngx_server_context.cc b/src/ngx_server_context.cc index 3e18c0488..5677f9e77 100644 --- a/src/ngx_server_context.cc +++ b/src/ngx_server_context.cc @@ -35,7 +35,8 @@ namespace net_instaweb { NgxServerContext::NgxServerContext( NgxRewriteDriverFactory* factory, StringPiece hostname, int port) - : SystemServerContext(factory, hostname, port) { + : SystemServerContext(factory, hostname, port), + ngx_http2_variable_index_(NGX_ERROR) { } NgxServerContext::~NgxServerContext() { } @@ -70,11 +71,23 @@ SystemRequestContext* NgxServerContext::NewRequestContext( local_ip.len = 0; } - return new SystemRequestContext(thread_system()->NewMutex(), - timer(), - ps_determine_host(r), - local_port, - str_to_string_piece(local_ip)); + SystemRequestContext* ctx = new SystemRequestContext( + thread_system()->NewMutex(), timer(), + ps_determine_host(r), local_port, str_to_string_piece(local_ip)); + + // See if http2 is in use. + if (ngx_http2_variable_index_ >= 0) { + ngx_http_variable_value_t* val = + ngx_http_get_indexed_variable(r, ngx_http2_variable_index_); + if (val != NULL && val->valid) { + StringPiece str_val(reinterpret_cast(val->data), val->len); + if (str_val == "h2" || str_val == "h2c") { + ctx->set_using_http2(true); + } + } + } + + return ctx; } GoogleString NgxServerContext::FormatOption(StringPiece option_name, diff --git a/src/ngx_server_context.h b/src/ngx_server_context.h index 180faa1b0..389650ccf 100644 --- a/src/ngx_server_context.h +++ b/src/ngx_server_context.h @@ -58,8 +58,18 @@ class NgxServerContext : public SystemServerContext { virtual GoogleString FormatOption(StringPiece option_name, StringPiece args); + void set_ngx_http2_variable_index(ngx_int_t idx) { + ngx_http2_variable_index_ = idx; + } + + ngx_int_t ngx_http2_variable_index() const { + return ngx_http2_variable_index_; + } + private: NgxRewriteDriverFactory* ngx_factory_; + // what index the "http2" var is, or NGX_ERROR. + ngx_int_t ngx_http2_variable_index_; DISALLOW_COPY_AND_ASSIGN(NgxServerContext); };