| Daniel Erat | 35f6587 | 2015-08-17 20:59:29 -0600 | [diff] [blame] | 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. |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 14 | |
| 15 | #ifndef WEBSERVER_LIBWEBSERV_PROTOCOL_HANDLER_H_ |
| 16 | #define WEBSERVER_LIBWEBSERV_PROTOCOL_HANDLER_H_ |
| 17 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 18 | #include <memory> |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame] | 19 | #include <set> |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 20 | #include <string> |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 21 | |
| 22 | #include <base/callback_forward.h> |
| 23 | #include <base/macros.h> |
| Alex Vakulenko | 75d6da2 | 2015-10-13 10:01:34 -0700 | [diff] [blame] | 24 | #include <brillo/secure_blob.h> |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 25 | |
| 26 | #include <libwebserv/export.h> |
| 27 | #include <libwebserv/request_handler_interface.h> |
| 28 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 29 | namespace libwebserv { |
| 30 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 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. |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 35 | class LIBWEBSERV_EXPORT ProtocolHandler { |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 36 | public: |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 37 | ProtocolHandler() = default; |
| 38 | virtual ~ProtocolHandler() = default; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 39 | |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 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; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 43 | |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame] | 44 | // Handler's name identifier (as provided in "name" setting of config file). |
| 45 | // Standard/default handler names are "http" and "https". |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 46 | virtual std::string GetName() const = 0; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 47 | |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame] | 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. |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 51 | virtual std::set<uint16_t> GetPorts() const = 0; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 52 | |
| 53 | // Returns the transport protocol that is served by this handler. |
| 54 | // Can be either "http" or "https". |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame] | 55 | // If the handler is not connected to the server, this will return an empty |
| 56 | // set. |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 57 | virtual std::set<std::string> GetProtocols() const = 0; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 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. |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame] | 61 | // If the handler is not connected to the server, this will return an empty |
| 62 | // array. |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 63 | virtual brillo::Blob GetCertificateFingerprint() const = 0; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 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. |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 90 | virtual int AddHandler(const std::string& url, |
| 91 | const std::string& method, |
| 92 | std::unique_ptr<RequestHandlerInterface> handler) = 0; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 93 | |
| 94 | // Similar to AddHandler() above but the handler is just a callback function. |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 95 | virtual int AddHandlerCallback( |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 96 | const std::string& url, |
| 97 | const std::string& method, |
| 98 | const base::Callback<RequestHandlerInterface::HandlerSignature>& |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 99 | handler_callback) = 0; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 100 | |
| 101 | // Removes the handler with the specified |handler_id|. |
| 102 | // Returns false if the handler with the given ID is not found. |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 103 | virtual bool RemoveHandler(int handler_id) = 0; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 104 | |
| 105 | static const char kHttp[]; |
| 106 | static const char kHttps[]; |
| 107 | |
| 108 | private: |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 109 | DISALLOW_COPY_AND_ASSIGN(ProtocolHandler); |
| 110 | }; |
| 111 | |
| 112 | } // namespace libwebserv |
| 113 | |
| 114 | #endif // WEBSERVER_LIBWEBSERV_PROTOCOL_HANDLER_H_ |