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 22 #include <android-base/logging.h> 23 #include <libwebsockets.h> 24 25 #include <host/libs/websocket/websocket_handler.h> 26 27 namespace cuttlefish { 28 class WebSocketServer { 29 public: 30 WebSocketServer( 31 const char* protocol_name, 32 const std::string &certs_dir, 33 const std::string &assets_dir, 34 int port); 35 ~WebSocketServer() = default; 36 37 void RegisterHandlerFactory( 38 const std::string &path, 39 std::unique_ptr<WebSocketHandlerFactory> handler_factory_p); 40 void Serve(); 41 42 43 private: 44 static std::unordered_map<struct lws*, std::shared_ptr<WebSocketHandler>> handlers_; 45 static std::unordered_map<std::string, std::unique_ptr<WebSocketHandlerFactory>> 46 handler_factories_; 47 48 static std::string GetPath(struct lws* wsi); 49 static int ServerCallback(struct lws* wsi, enum lws_callback_reasons reason, 50 void* user, void* in, size_t len); 51 static std::shared_ptr<WebSocketHandler> InstantiateHandler( 52 const std::string& uri_path, struct lws* wsi); 53 54 struct lws_context* context_; 55 struct lws_http_mount mount_; 56 struct lws_protocol_vhost_options headers_; 57 lws_retry_bo_t retry_; 58 }; 59 60 } // namespace cuttlefish 61