1 // Copyright 2015 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef WEBSERVER_LIBWEBSERV_PROTOCOL_HANDLER_H_ 16 #define WEBSERVER_LIBWEBSERV_PROTOCOL_HANDLER_H_ 17 18 #include <memory> 19 #include <set> 20 #include <string> 21 22 #include <base/callback_forward.h> 23 #include <base/macros.h> 24 #include <brillo/secure_blob.h> 25 26 #include <libwebserv/export.h> 27 #include <libwebserv/request_handler_interface.h> 28 29 namespace libwebserv { 30 31 // Wrapper around a protocol handler (e.g. HTTP or HTTPs). 32 // ProtocolHandler allows consumers to add request handlers on a given protocol. 33 // When the ProtocolHandler is connected, allows users to read port and protocol 34 // information. 35 class LIBWEBSERV_EXPORT ProtocolHandler { 36 public: 37 ProtocolHandler() = default; 38 virtual ~ProtocolHandler() = default; 39 40 // Returns true if the protocol handler object is backed by a ProtocolHandler 41 // on the remote web server and is capable of processing incoming requests. 42 virtual bool IsConnected() const = 0; 43 44 // Handler's name identifier (as provided in "name" setting of config file). 45 // Standard/default handler names are "http" and "https". 46 virtual std::string GetName() const = 0; 47 48 // Returns the ports the handler is bound to. There could be multiple. 49 // If the handler is not connected to the server, this will return an empty 50 // set. 51 virtual std::set<uint16_t> GetPorts() const = 0; 52 53 // Returns the transport protocol that is served by this handler. 54 // Can be either "http" or "https". 55 // If the handler is not connected to the server, this will return an empty 56 // set. 57 virtual std::set<std::string> GetProtocols() const = 0; 58 59 // Returns a SHA-256 fingerprint of HTTPS certificate used. Returns an empty 60 // byte buffer if this handler does not serve the HTTPS protocol. 61 // If the handler is not connected to the server, this will return an empty 62 // array. 63 virtual brillo::Blob GetCertificateFingerprint() const = 0; 64 65 // Adds a request handler for given |url|. If the |url| ends with a '/', this 66 // makes the handler respond to any URL beneath this path. 67 // Note that it is not possible to add a specific handler just for the root 68 // path "/". Doing so means "respond to any URL". 69 // |method| is optional request method verb, such as "GET" or "POST". 70 // If |method| is empty, the handler responds to any request verb. 71 // If there are more than one handler for a given request, the most specific 72 // match is chosen. For example, if there are the following handlers provided: 73 // - A["/foo/", ""] 74 // - B["/foo/bar", "GET"] 75 // - C["/foo/bar", ""] 76 // Here is what handlers are called when making certain requests: 77 // - GET("/foo/bar") => B[] 78 // - POST("/foo/bar") => C[] 79 // - PUT("/foo/bar") => C[] 80 // - GET("/foo/baz") => A[] 81 // - GET("/foo") => 404 Not Found 82 // This functions returns a handler ID which can be used later to remove 83 // the handler. 84 // 85 // The handler registration information is stored inside ProtocolHandler and 86 // is used to register the handlers with the web server daemon when it becomes 87 // available. This also happens when the web server goes away and then comes 88 // back (e.g. restarted). So, there is no need to re-register the handlers 89 // once the web server process is restarted. 90 virtual int AddHandler(const std::string& url, 91 const std::string& method, 92 std::unique_ptr<RequestHandlerInterface> handler) = 0; 93 94 // Similar to AddHandler() above but the handler is just a callback function. 95 virtual int AddHandlerCallback( 96 const std::string& url, 97 const std::string& method, 98 const base::Callback<RequestHandlerInterface::HandlerSignature>& 99 handler_callback) = 0; 100 101 // Removes the handler with the specified |handler_id|. 102 // Returns false if the handler with the given ID is not found. 103 virtual bool RemoveHandler(int handler_id) = 0; 104 105 static const char kHttp[]; 106 static const char kHttps[]; 107 108 private: 109 DISALLOW_COPY_AND_ASSIGN(ProtocolHandler); 110 }; 111 112 } // namespace libwebserv 113 114 #endif // WEBSERVER_LIBWEBSERV_PROTOCOL_HANDLER_H_ 115