src: Use nghttp2_session_upgrade2

This commit is contained in:
Tatsuhiro Tsujikawa 2015-11-07 12:16:22 +09:00
parent 269a100081
commit 558934cee5
3 changed files with 20 additions and 5 deletions

View File

@ -815,7 +815,7 @@ int HttpClient::on_upgrade_connect() {
} else { } else {
auto meth = std::find_if( auto meth = std::find_if(
std::begin(config.headers), std::end(config.headers), 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)) { if (meth == std::end(config.headers)) {
req = "GET "; req = "GET ";
@ -988,8 +988,18 @@ int HttpClient::connection_made() {
if (!reqvec[0]->data_prd) { if (!reqvec[0]->data_prd) {
stream_user_data = reqvec[0].get(); stream_user_data = reqvec[0].get();
} }
rv = nghttp2_session_upgrade(session, settings_payload.data(), // If HEAD is used, that is only when user specified it with -H
settings_payloadlen, stream_user_data); // 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) { if (rv != 0) {
std::cerr << "[ERROR] nghttp2_session_upgrade() returned error: " std::cerr << "[ERROR] nghttp2_session_upgrade() returned error: "
<< nghttp2_strerror(rv) << std::endl; << nghttp2_strerror(rv) << std::endl;

View File

@ -108,9 +108,10 @@ int Http2Upstream::upgrade_upstream(HttpsUpstream *http) {
auto settings_payload = auto settings_payload =
base64::decode(std::begin(http2_settings), std::end(http2_settings)); base64::decode(std::begin(http2_settings), std::end(http2_settings));
rv = nghttp2_session_upgrade( rv = nghttp2_session_upgrade2(
session_, reinterpret_cast<const uint8_t *>(settings_payload.c_str()), session_, reinterpret_cast<const uint8_t *>(settings_payload.c_str()),
settings_payload.size(), nullptr); settings_payload.size(),
http->get_downstream()->get_request_method() == HTTP_HEAD, nullptr);
if (rv != 0) { if (rv != 0) {
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
ULOG(INFO, this) << "nghttp2_session_upgrade() returned error: " ULOG(INFO, this) << "nghttp2_session_upgrade() returned error: "

View File

@ -404,6 +404,10 @@ bool streq_l(const char (&a)[N], InputIt b, size_t blen) {
return streq(a, N - 1, b, blen); return streq(a, N - 1, b, blen);
} }
template <size_t N> 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); bool strifind(const char *a, const char *b);
template <typename InputIt> void inp_strlower(InputIt first, InputIt last) { template <typename InputIt> void inp_strlower(InputIt first, InputIt last) {