wwbmmm commented on code in PR #3767:
URL: https://github.com/apache/brpc/pull/3767#discussion_r4704519568


##########
src/brpc/server.cpp:
##########
@@ -962,6 +1025,13 @@ int Server::StartInternal(const butil::EndPoint& endpoint,
                    << FLAGS_task_group_ntags << ")";
         return -1;
     }
+    // Validate `enabled_protocols' before copy_and_fill_server_options below
+    // adopts the pointers in the user-provided options, so that a rejected
+    // configuration does not transfer their ownership to this server.
+    if (ValidateEnabledProtocols(real_opt) != 0) {
+        return -1;
+    }

Review Comment:
   [replied by brpc-oncall robot] Fixed: the early check on the user-provided 
options is kept (it still runs before `copy_and_fill_server_options()` adopts 
the pointers), and `ValidateEnabledProtocols()` is called again after 
`bthread_init_fn` returns, before builtin services and listeners. Test: 
`restful_service_added_in_init_fn_requires_http`.



##########
src/brpc/server.h:
##########
@@ -284,10 +284,12 @@ struct ServerOptions {
 
     // Only enable these protocols, separated by spaces.
     // All names inside must be valid, check protocols name in global.cpp
-    // http/h2 and rdma_handshake are served whatever this field says:
-    // the builtin services are only reachable over http/h2, and
-    // rdma_handshake is a transport level handshake dispatching no request.
-    // Naming them here is allowed and changes nothing.
+    // When this field is non-empty, only the listed protocols are served.
+    // rdma_handshake is always served as it is a transport level handshake
+    // dispatching no request; naming it here is allowed and changes nothing.
+    // Builtin and restful services as well as http_master_service are served
+    // through http/h2, so if any of them is enabled, http or h2 must be listed
+    // as well, otherwise Server::Start() fails.

Review Comment:
   [replied by brpc-oncall robot] Documented at the top of `ServerOptions`: a 
`Start()` rejected by the validation adopts nothing and the caller still owns 
the pointers; once `Start()` gets past the validation, the `Server` owns them 
and frees them in its destructor.



##########
src/brpc/server.cpp:
##########
@@ -686,24 +686,102 @@ int Server::AddBuiltinServices() {
     return 0;
 }
 
-BUTIL_FORCE_INLINE bool is_http_protocol(const char* name) {
-    if (name[0] != 'h') {
-        return false;
+BUTIL_FORCE_INLINE bool is_rdma_handshake_protocol(const char* name) {
+    return strcmp(name, "rdma_handshake") == 0;
+}
+
+// Restful services are served through http/h2 requests. Empty maps may be
+// left by RemoveService(), so check the number of mappings instead of the
+// existence of the map.
+static bool HasRestfulServices(const Server::ServiceMap& services,
+                               const RestfulMap* global_restful_map) {
+    if (global_restful_map != nullptr && global_restful_map->size() != 0) {
+        return true;
+    }
+    for (Server::ServiceMap::const_iterator it = services.begin();
+         it != services.end(); ++it) {
+        if (it->second.restful_map != nullptr &&
+            it->second.restful_map->size() != 0) {
+            return true;
+        }
     }
-    return strcmp(name, "http") == 0 || strcmp(name, "h2") == 0;
+    return false;
 }
 
-BUTIL_FORCE_INLINE bool is_rdma_handshake_protocol(const char* name) {
-    return strcmp(name, "rdma_handshake") == 0;
+static std::string JoinWithAnd(const std::vector<std::string>& strs) {
+    std::string out;
+    for (size_t i = 0; i < strs.size(); ++i) {
+        if (i != 0) {
+            out.append(i + 1 == strs.size() ? " and " : ", ");
+        }
+        out.append(strs[i]);
+    }
+    return out;
 }
 
-Acceptor* Server::BuildAcceptor() {
+static void ParseEnabledProtocols(const std::string& enabled_protocols,
+                                  std::unordered_set<std::string>* whitelist) {
+    for (butil::StringSplitter sp(enabled_protocols.c_str(), ' '); sp; ++sp) {
+        whitelist->insert(std::string(sp.field(), sp.length()));
+    }
+}
+
+int Server::ValidateEnabledProtocols(const ServerOptions& options) const {
     std::unordered_set<std::string> whitelist;
-    for (butil::StringSplitter sp(_options.enabled_protocols.c_str(), ' ');
-         sp; ++sp) {
-        std::string protocol(sp.field(), sp.length());
-        whitelist.insert(protocol);
+    ParseEnabledProtocols(options.enabled_protocols, &whitelist);
+    if (whitelist.empty()) {
+        return 0;
+    }
+    const bool http_in_whitelist =
+        whitelist.count("http") != 0 || whitelist.count("h2") != 0;
+    std::vector<Protocol> protocols;
+    ListProtocols(&protocols);
+    for (size_t i = 0; i < protocols.size(); ++i) {
+        if (protocols[i].process_request != nullptr) {
+            whitelist.erase(protocols[i].name);
+        }
+    }
+    if (!whitelist.empty()) {
+        std::ostringstream err;
+        err << "ServerOptions.enabled_protocols has unknown protocols=`";
+        for (const auto& protocol : whitelist) {
+            err << protocol << ' ';
+        }
+        err << '\'';
+        LOG(ERROR) << err.str();
+        return -1;
+    }
+    if (http_in_whitelist) {
+        return 0;
+    }
+    std::vector<std::string> features;
+    std::vector<std::string> fixes;
+    if (options.has_builtin_services) {
+        features.push_back("ServerOptions.has_builtin_services is true");
+        fixes.push_back("set ServerOptions.has_builtin_services=false");

Review Comment:
   [replied by brpc-oncall robot] The message now checks the registration 
state: when `_builtin_service_count > 0` it reports that builtin services are 
already registered and suggests creating a new `Server` with 
`has_builtin_services=false` instead of an option change that cannot work. 
Test: `registered_builtin_services_hint_to_use_new_server`.



##########
src/brpc/server.cpp:
##########
@@ -962,6 +1025,13 @@ int Server::StartInternal(const butil::EndPoint& endpoint,
                    << FLAGS_task_group_ntags << ")";
         return -1;
     }
+    // Validate `enabled_protocols' before copy_and_fill_server_options below
+    // adopts the pointers in the user-provided options, so that a rejected
+    // configuration does not transfer their ownership to this server.
+    if (ValidateEnabledProtocols(real_opt) != 0) {
+        return -1;
+    }

Review Comment:
   [replied by brpc-oncall robot] Protocol configuration rejections (unknown 
names, missing http/h2, ALPN conflict, changed whitelist on restart) now set 
`errno = EINVAL`, and the new tests assert it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to