wwbmmm commented on code in PR #3767:
URL: https://github.com/apache/brpc/pull/3767#discussion_r4727154010
##########
src/brpc/server.cpp:
##########
@@ -784,6 +866,17 @@ int Server::InitALPNOptions(const ServerSSLOptions*
options) {
LOG(ERROR) << "Server does not support alpn=" << alpn;
return -1;
}
+ // Negotiating a protocol without an installed handler makes the client
+ // fail on its first request, reject the configuration instead.
+ std::unordered_set<std::string> whitelist;
+ ParseEnabledProtocols(_options.enabled_protocols, &whitelist);
+ if (!whitelist.empty() && whitelist.count(protocol->name) == 0) {
+ LOG(ERROR) << "alpn=" << alpn << " is not in "
+ << "ServerOptions.enabled_protocols=`"
+ << _options.enabled_protocols << '\'';
+ errno = EINVAL;
+ return -1;
Review Comment:
[replied by brpc-oncall robot] Fixed by moving the check before any state is
touched: `InitALPNOptions()` now takes the protocol whitelist and is called
during the early option validation (before `copy_and_fill_server_options()` and
before the session data is reserved), and the SSL block no longer calls it.
`rejected_alpn_creates_no_session_data` uses a counting `DataFactory` with
`reserved_session_local_data=4` and asserts that a rejected Start() creates no
data.
##########
src/brpc/server.cpp:
##########
@@ -1580,6 +1703,24 @@ int
Server::AddServiceInternal(google::protobuf::Service* service,
butil::StringPiece restful_mappings = svc_opt.restful_mappings;
restful_mappings.trim_spaces();
if (!restful_mappings.empty()) {
+ // Services added while Start() is running (e.g. from bthread_init_fn)
+ // were not seen by the validation of the user-provided options, so
+ // check that their restful mappings can be served as well.
+ if (_in_start) {
+ std::unordered_set<std::string> whitelist;
+ ParseEnabledProtocols(_options.enabled_protocols, &whitelist);
+ if (!whitelist.empty() && whitelist.count("http") == 0 &&
+ whitelist.count("h2") == 0) {
+ LOG(ERROR) << "Fail to add restful service=`" <<
sd->full_name()
+ << "': ServerOptions.enabled_protocols=`"
+ << _options.enabled_protocols
+ << "' enables neither http nor h2";
+ // The service is already registered, undo it like the
+ // RESTful mapping failures below.
+ RemoveService(service);
+ return -1;
Review Comment:
[replied by brpc-oncall robot] Good point, fixed by rejecting earlier: the
check for restful mappings added while `Start()` is running now runs at the
beginning of `AddServiceInternal()`, before the method and service tables are
written. A rejection therefore needs no `RemoveService()` rollback and does not
change ownership. The test still verifies that the failed registration is
absent and that a retry with `http` can add the same service.
##########
src/brpc/server.h:
##########
@@ -73,6 +73,13 @@ struct SocketSSLContext;
struct ServerOptions {
ServerOptions(); // Constructed with default options.
+ // NOTE: pointers in this struct are owned by the caller until they are
+ // adopted by a Server. A Server::Start() rejected by the validation of
+ // this struct (e.g. an invalid enabled_protocols) adopts nothing, so the
+ // caller still owns and must free them. Once Start() gets past that
+ // validation, the Server owns them and frees them in its destructor even
+ // if Start() fails later, e.g. when the port cannot be bound.
Review Comment:
[replied by brpc-oncall robot] Rewritten as suggested: the note now says an
early rejection keeps the existing ownership of every field (whatever the
caller owned before the call stays with it, and whatever the Server took over
in a previous `Start()` stays with the Server), and that only fields documented
as owned by Server are freed by the destructor once the options are adopted.
--
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]