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_WEBSERVD_PROTOCOL_HANDLER_H_ 16 #define WEBSERVER_WEBSERVD_PROTOCOL_HANDLER_H_ 17 18 #include <map> 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include <base/macros.h> 24 #include <base/memory/weak_ptr.h> 25 #include <base/strings/string_piece.h> 26 #include <brillo/secure_blob.h> 27 28 #include "webservd/config.h" 29 30 struct MHD_Daemon; 31 32 namespace webservd { 33 34 class Request; 35 class RequestHandlerInterface; 36 class ServerInterface; 37 38 // An instance of a handler for particular protocol (http/https) bound to a 39 // particular port to handle requests on. 40 class ProtocolHandler final { 41 public: 42 ProtocolHandler(const std::string& name, 43 ServerInterface* server_interface); 44 ~ProtocolHandler(); 45 46 // Registers a new request handler for the given URL and request method. 47 // Returns a handler ID (GUID). 48 std::string AddRequestHandler( 49 const std::string& url, 50 const std::string& method, 51 std::unique_ptr<RequestHandlerInterface> handler); 52 53 // Removes a previously registered handler. 54 bool RemoveRequestHandler(const std::string& handler_id); 55 56 // Finds a handler for given URL/Method. This is the method used to look up 57 // the handler for incoming HTTP requests. 58 // Returns the handler_id or empty string if not found. 59 std::string FindRequestHandler(const base::StringPiece& url, 60 const base::StringPiece& method) const; 61 // Binds the socket and listens to HTTP requests on it. 62 bool Start(Config::ProtocolHandler* config); 63 64 // Stops listening for requests. 65 bool Stop(); 66 67 // Returns the port this handler listens for requests on. GetPort()68 uint16_t GetPort() const { return port_; } 69 70 // Returns the protocol name for this handler ("http" or "https"). GetProtocol()71 const std::string& GetProtocol() const { return protocol_; } 72 73 // Returns the SHA-256 fingerprint of the TLS certificate used for https 74 // connection. Returns an empty byte array if this handler is serving http. GetCertificateFingerprint()75 const brillo::Blob& GetCertificateFingerprint() const { 76 return certificate_fingerprint_; 77 } 78 79 // Returns the unique protocol handler ID (GUID). GetID()80 const std::string& GetID() const { return id_; } 81 82 // Handler's name identifier (as provided in "name" setting of config file). 83 // Standard/default handler names are "http" and "https". GetName()84 std::string GetName() const { return name_; } 85 86 // Returns the pointer to the Server object. GetServer()87 ServerInterface* GetServer() const { return server_interface_; } 88 89 // Methods to store/remove/retrieve pending incoming requests for the duration 90 // of the request's processing. 91 void AddRequest(Request* request); 92 void RemoveRequest(Request* request); 93 Request* GetRequest(const std::string& request_id) const; 94 95 // Schedules an asynchronous call to DoWork(). 96 void ScheduleWork(); 97 98 private: 99 friend class Request; 100 friend class ServerHelper; 101 class Watcher; 102 struct HandlerMapEntry { 103 std::string url; 104 std::string method; 105 std::unique_ptr<RequestHandlerInterface> handler; 106 }; 107 108 // Called when new data is available on sockets for libmicrohttpd to process. 109 void DoWork(); 110 111 // libmicrohttpd daemon class. 112 MHD_Daemon* server_{nullptr}; 113 // A map that stores registered request handlers (the key is handler ID). 114 std::map<std::string, HandlerMapEntry> request_handlers_; 115 // A map that stores pending requests (the key is request ID). 116 std::map<std::string, Request*> requests_; 117 // Protocol Handler ID. 118 std::string id_; 119 // Protocol Handler name. 120 std::string name_; 121 // Reference back to the Server. 122 ServerInterface* server_interface_{nullptr}; 123 // The port we are listening to. 124 uint16_t port_{0}; 125 // The protocol name ("http" or "https"). 126 std::string protocol_; 127 // TLS certificate fingerprint (if any). 128 brillo::Blob certificate_fingerprint_; 129 // File descriptor watchers for current active sockets. 130 std::vector<std::unique_ptr<Watcher>> watchers_; 131 // Set to true when a timer request is scheduled. 132 bool work_scheduled_{false}; 133 134 base::WeakPtrFactory<ProtocolHandler> weak_ptr_factory_{this}; 135 DISALLOW_COPY_AND_ASSIGN(ProtocolHandler); 136 }; 137 138 } // namespace webservd 139 140 #endif // WEBSERVER_WEBSERVD_PROTOCOL_HANDLER_H_ 141