From f2159bc2c1eda8870cf41eb19f94e4996b1aa266 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 17 Oct 2018 08:38:55 +0900 Subject: [PATCH] nghttpx: Convert UpstreamAltMode to enum class --- src/shrpx_client_handler.cc | 6 ++++-- src/shrpx_config.cc | 14 ++++++++------ src/shrpx_config.h | 10 +++++----- src/shrpx_connection_handler.cc | 2 +- src/shrpx_downstream.cc | 2 +- src/shrpx_http2_upstream.cc | 24 +++++++++++++----------- src/shrpx_https_upstream.cc | 5 +++-- 7 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/shrpx_client_handler.cc b/src/shrpx_client_handler.cc index 53a5a5b1..cdb20146 100644 --- a/src/shrpx_client_handler.cc +++ b/src/shrpx_client_handler.cc @@ -952,10 +952,12 @@ ClientHandler::get_downstream_connection(int &err, Downstream *downstream, err = 0; switch (faddr_->alt_mode) { - case ALTMODE_API: + case UpstreamAltMode::API: return std::make_unique(worker_); - case ALTMODE_HEALTHMON: + case UpstreamAltMode::HEALTHMON: return std::make_unique(); + default: + break; } auto &balloc = downstream->get_block_allocator(); diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index e7a632c4..5e0642d9 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -756,7 +756,7 @@ int parse_memcached_connection_params(MemcachedConnectionParams &out, } // namespace struct UpstreamParams { - int alt_mode; + UpstreamAltMode alt_mode; bool tls; bool sni_fwd; bool proxyproto; @@ -779,17 +779,19 @@ int parse_upstream_params(UpstreamParams &out, const StringRef &src_params) { } else if (util::strieq_l("no-tls", param)) { out.tls = false; } else if (util::strieq_l("api", param)) { - if (out.alt_mode && out.alt_mode != ALTMODE_API) { + if (out.alt_mode != UpstreamAltMode::NONE && + out.alt_mode != UpstreamAltMode::API) { LOG(ERROR) << "frontend: api and healthmon are mutually exclusive"; return -1; } - out.alt_mode = ALTMODE_API; + out.alt_mode = UpstreamAltMode::API; } else if (util::strieq_l("healthmon", param)) { - if (out.alt_mode && out.alt_mode != ALTMODE_HEALTHMON) { + if (out.alt_mode != UpstreamAltMode::NONE && + out.alt_mode != UpstreamAltMode::HEALTHMON) { LOG(ERROR) << "frontend: api and healthmon are mutually exclusive"; return -1; } - out.alt_mode = ALTMODE_HEALTHMON; + out.alt_mode = UpstreamAltMode::HEALTHMON; } else if (util::strieq_l("proxyproto", param)) { out.proxyproto = true; } else if (!param.empty()) { @@ -2572,7 +2574,7 @@ int parse_config(Config *config, int optid, const StringRef &opt, addr.alt_mode = params.alt_mode; addr.accept_proxy_protocol = params.proxyproto; - if (addr.alt_mode == ALTMODE_API) { + if (addr.alt_mode == UpstreamAltMode::API) { apiconf.enabled = true; } diff --git a/src/shrpx_config.h b/src/shrpx_config.h index bb6fc262..8ea9c55d 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -420,13 +420,13 @@ struct AltSvc { uint16_t port; }; -enum UpstreamAltMode { +enum class UpstreamAltMode { // No alternative mode - ALTMODE_NONE, + NONE, // API processing mode - ALTMODE_API, + API, // Health monitor mode - ALTMODE_HEALTHMON, + HEALTHMON, }; struct UpstreamAddr { @@ -444,7 +444,7 @@ struct UpstreamAddr { // domain socket, this is 0. int family; // Alternate mode - int alt_mode; + UpstreamAltMode alt_mode; // true if |host| contains UNIX domain socket path. bool host_unix; // true if TLS is enabled. diff --git a/src/shrpx_connection_handler.cc b/src/shrpx_connection_handler.cc index fa32f7fc..14c11ce4 100644 --- a/src/shrpx_connection_handler.cc +++ b/src/shrpx_connection_handler.cc @@ -407,7 +407,7 @@ int ConnectionHandler::handle_connection(int fd, sockaddr *addr, int addrlen, Worker *worker; - if (faddr->alt_mode == ALTMODE_API) { + if (faddr->alt_mode == UpstreamAltMode::API) { worker = workers_[0].get(); if (LOG_ENABLED(INFO)) { diff --git a/src/shrpx_downstream.cc b/src/shrpx_downstream.cc index 2538aa95..0b20d173 100644 --- a/src/shrpx_downstream.cc +++ b/src/shrpx_downstream.cc @@ -610,7 +610,7 @@ bool Downstream::request_buf_full() { auto worker = handler->get_worker(); // We don't check buffer size here for API endpoint. - if (faddr->alt_mode == ALTMODE_API) { + if (faddr->alt_mode == UpstreamAltMode::API) { return false; } diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index 591a6686..62999efd 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -358,8 +358,8 @@ int Http2Upstream::on_request_headers(Downstream *downstream, auto faddr = handler_->get_upstream_addr(); // For HTTP/2 proxy, we require :authority. - if (method_token != HTTP_CONNECT && config->http2_proxy && !faddr->alt_mode && - !authority) { + if (method_token != HTTP_CONNECT && config->http2_proxy && + faddr->alt_mode == UpstreamAltMode::NONE && !authority) { rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR); return 0; } @@ -383,7 +383,8 @@ int Http2Upstream::on_request_headers(Downstream *downstream, if (method_token == HTTP_OPTIONS && path->value == StringRef::from_lit("*")) { // Server-wide OPTIONS request. Path is empty. - } else if (config->http2_proxy && !faddr->alt_mode) { + } else if (config->http2_proxy && + faddr->alt_mode == UpstreamAltMode::NONE) { req.path = path->value; } else { req.path = http2::rewrite_clean_path(downstream->get_block_allocator(), @@ -1026,10 +1027,11 @@ Http2Upstream::Http2Upstream(ClientHandler *handler) auto faddr = handler_->get_upstream_addr(); - rv = nghttp2_session_server_new2( - &session_, http2conf.upstream.callbacks, this, - faddr->alt_mode ? http2conf.upstream.alt_mode_option - : http2conf.upstream.option); + rv = + nghttp2_session_server_new2(&session_, http2conf.upstream.callbacks, this, + faddr->alt_mode != UpstreamAltMode::NONE + ? http2conf.upstream.alt_mode_option + : http2conf.upstream.option); assert(rv == 0); @@ -1043,7 +1045,7 @@ Http2Upstream::Http2Upstream(ClientHandler *handler) entry[0].value = http2conf.upstream.max_concurrent_streams; entry[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; - if (faddr->alt_mode) { + if (faddr->alt_mode != UpstreamAltMode::NONE) { entry[1].value = (1u << 31) - 1; } else { entry[1].value = http2conf.upstream.window_size; @@ -1070,7 +1072,7 @@ Http2Upstream::Http2Upstream(ClientHandler *handler) } auto window_size = - faddr->alt_mode + faddr->alt_mode != UpstreamAltMode::NONE ? std::numeric_limits::max() : http2conf.upstream.optimize_window_size ? std::min(http2conf.upstream.connection_window_size, @@ -1186,7 +1188,7 @@ int Http2Upstream::on_write() { if (http2conf.upstream.optimize_window_size) { auto faddr = handler_->get_upstream_addr(); - if (!faddr->alt_mode) { + if (faddr->alt_mode == UpstreamAltMode::NONE) { auto window_size = std::min(http2conf.upstream.connection_window_size, static_cast(hint.rwin * 2)); @@ -1980,7 +1982,7 @@ int Http2Upstream::consume(int32_t stream_id, size_t len) { auto faddr = handler_->get_upstream_addr(); - if (faddr->alt_mode) { + if (faddr->alt_mode != UpstreamAltMode::NONE) { return 0; } diff --git a/src/shrpx_https_upstream.cc b/src/shrpx_https_upstream.cc index 0e34e197..3e5a5d94 100644 --- a/src/shrpx_https_upstream.cc +++ b/src/shrpx_https_upstream.cc @@ -413,7 +413,8 @@ int htp_hdrs_completecb(http_parser *htp) { // mruby hook may change method value - if (req.no_authority && config->http2_proxy && !faddr->alt_mode) { + if (req.no_authority && config->http2_proxy && + faddr->alt_mode == UpstreamAltMode::NONE) { // Request URI should be absolute-form for client proxy mode return -1; } @@ -464,7 +465,7 @@ int htp_hdrs_completecb(http_parser *htp) { return -1; } - if (faddr->alt_mode) { + if (faddr->alt_mode != UpstreamAltMode::NONE) { // Normally, we forward expect: 100-continue to backend server, // and let them decide whether responds with 100 Continue or not. // For alternative mode, we have no backend, so just send 100