1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include <android-base/logging.h>
24 #include <libwebsockets.h>
25 
26 #include <host/libs/websocket/websocket_handler.h>
27 
28 namespace cuttlefish {
29 class WebSocketServer {
30  public:
31   // Uses HTTP and WS
32   WebSocketServer(const char* protocol_name, const std::string& assets_dir,
33                   int port);
34   // Uses HTTPS and WSS when a certificates directory is provided
35   WebSocketServer(const char* protocol_name, const std::string& certs_dir,
36                   const std::string& assets_dir, int port);
37   ~WebSocketServer() = default;
38 
39   // Register a handler factory for websocket connections. A new handler will be
40   // created for each new websocket connection.
41   void RegisterHandlerFactory(
42       const std::string& path,
43       std::unique_ptr<WebSocketHandlerFactory> handler_factory_p);
44 
45   // Register a handler factory for dynamic HTTP requests. A new handler will be
46   // created for each HTTP request.
47   void RegisterDynHandlerFactory(const std::string& path,
48                                  DynHandlerFactory handler_factory);
49 
50   void Serve();
51 
52  private:
53   static int WebsocketCallback(struct lws* wsi,
54                                enum lws_callback_reasons reason, void* user,
55                                void* in, size_t len);
56 
57   static int DynHttpCallback(struct lws* wsi, enum lws_callback_reasons reason,
58                              void* user, void* in, size_t len);
59 
60   int ServerCallback(struct lws* wsi, enum lws_callback_reasons reason,
61                             void* user, void* in, size_t len);
62   int DynServerCallback(struct lws* wsi,
63                                enum lws_callback_reasons reason, void* user,
64                                void* in, size_t len);
65   std::shared_ptr<WebSocketHandler> InstantiateHandler(
66       const std::string& uri_path, struct lws* wsi);
67   std::unique_ptr<DynHandler> InstantiateDynHandler(
68       const std::string& uri_path, struct lws* wsi);
69 
70   void InitializeLwsObjects();
71 
72   std::unordered_map<struct lws*, std::shared_ptr<WebSocketHandler>> handlers_ =
73       {};
74   std::unordered_map<std::string, std::unique_ptr<WebSocketHandlerFactory>>
75       handler_factories_ = {};
76   std::unordered_map<struct lws*, std::unique_ptr<DynHandler>> dyn_handlers_ =
77       {};
78   std::unordered_map<std::string, DynHandlerFactory> dyn_handler_factories_ =
79       {};
80   std::string protocol_name_;
81   std::string assets_dir_;
82   std::string certs_dir_;
83   int server_port_;
84   struct lws_context* context_;
85   struct lws_http_mount static_mount_;
86   std::vector<struct lws_http_mount> dyn_mounts_ = {};
87   struct lws_protocol_vhost_options headers_;
88   lws_retry_bo_t retry_;
89 };
90 
91 }  // namespace cuttlefish
92