From 558934cee5e8b0557e089c8da08f9e973af6d033 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 7 Nov 2015 12:16:22 +0900 Subject: [PATCH] src: Use nghttp2_session_upgrade2 --- src/nghttp.cc | 16 +++++++++++++--- src/shrpx_http2_upstream.cc | 5 +++-- src/util.h | 4 ++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/nghttp.cc b/src/nghttp.cc index cf0bef2b..5d58471b 100644 --- a/src/nghttp.cc +++ b/src/nghttp.cc @@ -815,7 +815,7 @@ int HttpClient::on_upgrade_connect() { } else { auto meth = std::find_if( std::begin(config.headers), std::end(config.headers), - [](const Header &kv) { return util::strieq_l(":method", kv.name); }); + [](const Header &kv) { return util::streq_l(":method", kv.name); }); if (meth == std::end(config.headers)) { req = "GET "; @@ -988,8 +988,18 @@ int HttpClient::connection_made() { if (!reqvec[0]->data_prd) { stream_user_data = reqvec[0].get(); } - rv = nghttp2_session_upgrade(session, settings_payload.data(), - settings_payloadlen, stream_user_data); + // If HEAD is used, that is only when user specified it with -H + // option. + auto head_request = + stream_user_data && + std::find_if(std::begin(config.headers), std::end(config.headers), + [](const Header &kv) { + return util::streq_l(":method", kv.name) && + util::streq_l("HEAD", kv.value); + }) != std::end(config.headers); + rv = nghttp2_session_upgrade2(session, settings_payload.data(), + settings_payloadlen, head_request, + stream_user_data); if (rv != 0) { std::cerr << "[ERROR] nghttp2_session_upgrade() returned error: " << nghttp2_strerror(rv) << std::endl; diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index 97d297c3..05d0f70a 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -108,9 +108,10 @@ int Http2Upstream::upgrade_upstream(HttpsUpstream *http) { auto settings_payload = base64::decode(std::begin(http2_settings), std::end(http2_settings)); - rv = nghttp2_session_upgrade( + rv = nghttp2_session_upgrade2( session_, reinterpret_cast(settings_payload.c_str()), - settings_payload.size(), nullptr); + settings_payload.size(), + http->get_downstream()->get_request_method() == HTTP_HEAD, nullptr); if (rv != 0) { if (LOG_ENABLED(INFO)) { ULOG(INFO, this) << "nghttp2_session_upgrade() returned error: " diff --git a/src/util.h b/src/util.h index 60662926..3a835e75 100644 --- a/src/util.h +++ b/src/util.h @@ -404,6 +404,10 @@ bool streq_l(const char (&a)[N], InputIt b, size_t blen) { return streq(a, N - 1, b, blen); } +template bool streq_l(const char (&a)[N], const std::string &b) { + return streq(a, N - 1, std::begin(b), b.size()); +} + bool strifind(const char *a, const char *b); template void inp_strlower(InputIt first, InputIt last) {