blob: c4a9bef27411328e1e66c5669cee2123b4bcacf8 [file] [log] [blame]
Daniel Erat35f65872015-08-17 20:59:29 -06001// 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 Vakulenko31a63792015-02-03 12:44:57 -080014
15#ifndef WEBSERVER_LIBWEBSERV_PROTOCOL_HANDLER_H_
16#define WEBSERVER_LIBWEBSERV_PROTOCOL_HANDLER_H_
17
Alex Vakulenko31a63792015-02-03 12:44:57 -080018#include <memory>
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080019#include <set>
Alex Vakulenko31a63792015-02-03 12:44:57 -080020#include <string>
Alex Vakulenko31a63792015-02-03 12:44:57 -080021
22#include <base/callback_forward.h>
23#include <base/macros.h>
Alex Vakulenko75d6da22015-10-13 10:01:34 -070024#include <brillo/secure_blob.h>
Alex Vakulenko31a63792015-02-03 12:44:57 -080025
26#include <libwebserv/export.h>
27#include <libwebserv/request_handler_interface.h>
28
Alex Vakulenko31a63792015-02-03 12:44:57 -080029namespace libwebserv {
30
Alex Vakulenko31a63792015-02-03 12:44:57 -080031// 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 Wiley33207262015-12-15 18:33:50 -080035class LIBWEBSERV_EXPORT ProtocolHandler {
Alex Vakulenko31a63792015-02-03 12:44:57 -080036 public:
Christopher Wiley33207262015-12-15 18:33:50 -080037 ProtocolHandler() = default;
38 virtual ~ProtocolHandler() = default;
Alex Vakulenko31a63792015-02-03 12:44:57 -080039
Christopher Wiley33207262015-12-15 18:33:50 -080040 // 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 Vakulenko31a63792015-02-03 12:44:57 -080043
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080044 // Handler's name identifier (as provided in "name" setting of config file).
45 // Standard/default handler names are "http" and "https".
Christopher Wiley33207262015-12-15 18:33:50 -080046 virtual std::string GetName() const = 0;
Alex Vakulenko31a63792015-02-03 12:44:57 -080047
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080048 // 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 Wiley33207262015-12-15 18:33:50 -080051 virtual std::set<uint16_t> GetPorts() const = 0;
Alex Vakulenko31a63792015-02-03 12:44:57 -080052
53 // Returns the transport protocol that is served by this handler.
54 // Can be either "http" or "https".
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080055 // If the handler is not connected to the server, this will return an empty
56 // set.
Christopher Wiley33207262015-12-15 18:33:50 -080057 virtual std::set<std::string> GetProtocols() const = 0;
Alex Vakulenko31a63792015-02-03 12:44:57 -080058
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 Vakulenko3d2d5632015-03-02 15:58:32 -080061 // If the handler is not connected to the server, this will return an empty
62 // array.
Christopher Wiley33207262015-12-15 18:33:50 -080063 virtual brillo::Blob GetCertificateFingerprint() const = 0;
Alex Vakulenko31a63792015-02-03 12:44:57 -080064
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 Wiley33207262015-12-15 18:33:50 -080090 virtual int AddHandler(const std::string& url,
91 const std::string& method,
92 std::unique_ptr<RequestHandlerInterface> handler) = 0;
Alex Vakulenko31a63792015-02-03 12:44:57 -080093
94 // Similar to AddHandler() above but the handler is just a callback function.
Christopher Wiley33207262015-12-15 18:33:50 -080095 virtual int AddHandlerCallback(
Alex Vakulenko31a63792015-02-03 12:44:57 -080096 const std::string& url,
97 const std::string& method,
98 const base::Callback<RequestHandlerInterface::HandlerSignature>&
Christopher Wiley33207262015-12-15 18:33:50 -080099 handler_callback) = 0;
Alex Vakulenko31a63792015-02-03 12:44:57 -0800100
101 // Removes the handler with the specified |handler_id|.
102 // Returns false if the handler with the given ID is not found.
Christopher Wiley33207262015-12-15 18:33:50 -0800103 virtual bool RemoveHandler(int handler_id) = 0;
Alex Vakulenko31a63792015-02-03 12:44:57 -0800104
105 static const char kHttp[];
106 static const char kHttps[];
107
108 private:
Alex Vakulenko31a63792015-02-03 12:44:57 -0800109 DISALLOW_COPY_AND_ASSIGN(ProtocolHandler);
110};
111
112} // namespace libwebserv
113
114#endif // WEBSERVER_LIBWEBSERV_PROTOCOL_HANDLER_H_