webserver: Add support for multiple protocol handlers with the same name
Previously protocol handlers were designed with their ID to be unique.
If the device needed to server requests on multiple ports, the clients
were forced to register separate request handlers for each of the
unique protocol handler instance.
This change makes it possible to have multiple protocol handlers share
a common "name" (like "http" or "https") and all the request handlers
registered for a particular protocol handler with a given name will be
added to each protocol handler that has this name.
So, the implementation has changed a bit. Each protocol handler still
has a unique ID (GUID) that allows to identify each instance (for example
in order to send back the reply from the client to the exact instance
of the object that initiated the request), while leaving the rest of
client-side library (libwebserv) working with non-unique names when
registering request handlers.
This also changed the web server configuration file a bit, since it
used to use a JSON dictionary for each protocol handler config, now
its an array of objects, which allows to specify the same name for
more than one protocol handler.
BUG=brillo:412
TEST=`FEATURES=test emerge-link webserver privetd`
Change-Id: Ib67fb586dd249c834d2464bfa0ef0eaabb7d780f
Reviewed-on: https://chromium-review.googlesource.com/255262
Trybot-Ready: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Aaron Kemp <kemp@google.com>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/libwebserv/server.h b/libwebserv/server.h
index 5e8f88a..bb45204 100644
--- a/libwebserv/server.h
+++ b/libwebserv/server.h
@@ -63,16 +63,14 @@
// A helper method that returns the default handler for "https".
ProtocolHandler* GetDefaultHttpsHandler();
- // Returns an existing protocol handler by ID. If the handler with the
- // requested |id| does not exist, a new one will be created. See
- // documentation in ProtocolHandler about IDs and how they work with
- // webservd.
+ // Returns an existing protocol handler by name. If the handler with the
+ // requested |name| does not exist, a new one will be created.
//
// The created handler is purely client side, and depends on the server
- // being configured to open a corresponding handler with the given ID.
+ // being configured to open a corresponding handler with the given name.
// Because clients and the server come up asynchronously, we allow clients
// to register anticipated handlers before server starts up.
- ProtocolHandler* GetProtocolHandler(const std::string& id);
+ ProtocolHandler* GetProtocolHandler(const std::string& name);
// Returns true if the web server daemon is connected to DBus and our
// connection to it has been established.
@@ -109,6 +107,10 @@
LIBWEBSERV_PRIVATE void ProtocolHandlerRemoved(
const dbus::ObjectPath& object_path);
+ // Looks up a protocol handler by ID. If not found, returns nullptr.
+ LIBWEBSERV_PRIVATE ProtocolHandler* GetProtocolHandlerByID(
+ const std::string& id) const;
+
// Private implementation of D-Bus RequestHandlerInterface called by the web
// server daemon whenever a new request is available to be processed.
std::unique_ptr<RequestHandler> request_handler_;
@@ -118,9 +120,11 @@
// D-Bus object to handler registration of RequestHandlerInterface.
std::unique_ptr<chromeos::dbus_utils::DBusObject> dbus_object_;
+ // A mapping of protocol handler name to the associated object.
+ std::map<std::string, std::unique_ptr<ProtocolHandler>>
+ protocol_handlers_names_;
// A mapping of protocol handler IDs to the associated object.
- // Handler IDs are either GUIDs or the two well-known handler IDs.
- std::map<std::string, std::unique_ptr<ProtocolHandler>> protocol_handlers_;
+ std::map<std::string, ProtocolHandler*> protocol_handlers_ids_;
// A map between D-Bus object path of protocol handler and remote protocol
// handler ID.
std::map<dbus::ObjectPath, std::string> protocol_handler_id_map_;