diff options
Diffstat (limited to 'src/shrpx_api_downstream_connection.cc')
-rw-r--r-- | src/shrpx_api_downstream_connection.cc | 86 |
1 files changed, 39 insertions, 47 deletions
diff --git a/src/shrpx_api_downstream_connection.cc b/src/shrpx_api_downstream_connection.cc index 254ab59..13ebac6 100644 --- a/src/shrpx_api_downstream_connection.cc +++ b/src/shrpx_api_downstream_connection.cc @@ -39,33 +39,27 @@ namespace shrpx { namespace { -// List of API endpoints -const std::array<APIEndpoint, 2> &apis() { - static const auto apis = new std::array<APIEndpoint, 2>{ - APIEndpoint{ - StringRef::from_lit("/api/v1beta1/backendconfig"), - true, - (1 << API_METHOD_POST) | (1 << API_METHOD_PUT), - &APIDownstreamConnection::handle_backendconfig, - }, - APIEndpoint{ - StringRef::from_lit("/api/v1beta1/configrevision"), - true, - (1 << API_METHOD_GET), - &APIDownstreamConnection::handle_configrevision, - }, - }; - - return *apis; -} +const auto backendconfig_endpoint = APIEndpoint{ + "/api/v1beta1/backendconfig"_sr, + true, + (1 << API_METHOD_POST) | (1 << API_METHOD_PUT), + &APIDownstreamConnection::handle_backendconfig, +}; + +const auto configrevision_endpoint = APIEndpoint{ + "/api/v1beta1/configrevision"_sr, + true, + (1 << API_METHOD_GET), + &APIDownstreamConnection::handle_configrevision, +}; } // namespace namespace { // The method string. This must be same order of APIMethod. constexpr StringRef API_METHOD_STRING[] = { - StringRef::from_lit("GET"), - StringRef::from_lit("POST"), - StringRef::from_lit("PUT"), + "GET"_sr, + "POST"_sr, + "PUT"_sr, }; } // namespace @@ -112,18 +106,18 @@ int APIDownstreamConnection::send_reply(unsigned int http_status, switch (api_status) { case APIStatusCode::SUCCESS: - api_status_str = StringRef::from_lit("Success"); + api_status_str = "Success"_sr; break; case APIStatusCode::FAILURE: - api_status_str = StringRef::from_lit("Failure"); + api_status_str = "Failure"_sr; break; default: assert(0); } - constexpr auto M1 = StringRef::from_lit("{\"status\":\""); - constexpr auto M2 = StringRef::from_lit("\",\"code\":"); - constexpr auto M3 = StringRef::from_lit("}"); + constexpr auto M1 = "{\"status\":\""_sr; + constexpr auto M2 = "\",\"code\":"_sr; + constexpr auto M3 = "}"_sr; // 3 is the number of digits in http_status, assuming it is 3 digits // number. @@ -131,7 +125,7 @@ int APIDownstreamConnection::send_reply(unsigned int http_status, api_status_str.size() + 3; auto buf = make_byte_ref(balloc, buflen); - auto p = buf.base; + auto p = std::begin(buf); p = std::copy(std::begin(M1), std::end(M1), p); p = std::copy(std::begin(api_status_str), std::end(api_status_str), p); @@ -140,24 +134,23 @@ int APIDownstreamConnection::send_reply(unsigned int http_status, p = std::copy(std::begin(data), std::end(data), p); p = std::copy(std::begin(M3), std::end(M3), p); - buf.len = p - buf.base; + buf = buf.subspan(0, p - std::begin(buf)); - auto content_length = util::make_string_ref_uint(balloc, buf.len); + auto content_length = util::make_string_ref_uint(balloc, buf.size()); - resp.fs.add_header_token(StringRef::from_lit("content-length"), - content_length, false, http2::HD_CONTENT_LENGTH); + resp.fs.add_header_token("content-length"_sr, content_length, false, + http2::HD_CONTENT_LENGTH); switch (http_status) { case 400: case 405: case 413: - resp.fs.add_header_token(StringRef::from_lit("connection"), - StringRef::from_lit("close"), false, + resp.fs.add_header_token("connection"_sr, "close"_sr, false, http2::HD_CONNECTION); break; } - if (upstream->send_reply(downstream_, buf.base, buf.len) != 0) { + if (upstream->send_reply(downstream_, buf.data(), buf.size()) != 0) { return -1; } @@ -170,8 +163,8 @@ const APIEndpoint *lookup_api(const StringRef &path) { case 26: switch (path[25]) { case 'g': - if (util::streq_l("/api/v1beta1/backendconfi", std::begin(path), 25)) { - return &apis()[0]; + if (util::streq("/api/v1beta1/backendconfi"_sr, path, 25)) { + return &backendconfig_endpoint; } break; } @@ -179,8 +172,8 @@ const APIEndpoint *lookup_api(const StringRef &path) { case 27: switch (path[26]) { case 'n': - if (util::streq_l("/api/v1beta1/configrevisio", std::begin(path), 26)) { - return &apis()[1]; + if (util::streq("/api/v1beta1/configrevisio"_sr, path, 26)) { + return &configrevision_endpoint; } break; } @@ -283,7 +276,7 @@ int APIDownstreamConnection::error_method_not_allowed() { auto &balloc = downstream_->get_block_allocator(); auto iov = make_byte_ref(balloc, len + 1); - auto p = iov.base; + auto p = std::begin(iov); for (uint8_t i = 0; i < API_METHOD_MAX; ++i) { if (api_->allowed_methods & (1 << i)) { auto &s = API_METHOD_STRING[i]; @@ -295,7 +288,7 @@ int APIDownstreamConnection::error_method_not_allowed() { p -= 2; *p = '\0'; - resp.fs.add_header_token(StringRef::from_lit("allow"), StringRef{iov.base, p}, + resp.fs.add_header_token("allow"_sr, StringRef{std::span{std::begin(iov), p}}, false, -1); return send_reply(405, APIStatusCode::FAILURE); } @@ -394,10 +387,10 @@ int APIDownstreamConnection::handle_backendconfig() { return 0; } - auto opt = StringRef{first, eq}; - auto optval = StringRef{eq + 1, eol}; + auto opt = StringRef{std::span{first, eq}}; + auto optval = StringRef{std::span{eq + 1, eol}}; - auto optid = option_lookup_token(opt.c_str(), opt.size()); + auto optid = option_lookup_token(opt); switch (optid) { case SHRPX_OPTID_BACKEND: @@ -442,9 +435,8 @@ int APIDownstreamConnection::handle_configrevision() { // "configRevision": N // } auto data = concat_string_ref( - balloc, StringRef::from_lit(R"(,"data":{"configRevision":)"), - util::make_string_ref_uint(balloc, config->config_revision), - StringRef::from_lit("}")); + balloc, R"(,"data":{"configRevision":)"_sr, + util::make_string_ref_uint(balloc, config->config_revision), "}"_sr); send_reply(200, APIStatusCode::SUCCESS, data); |