• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
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 
16 #ifndef ARKCOMPILER_TOOLCHAIN_WEBSOCKET_SERVER_WEBSOCKET_SERVER_H
17 #define ARKCOMPILER_TOOLCHAIN_WEBSOCKET_SERVER_WEBSOCKET_SERVER_H
18 
19 #include "websocket/http.h"
20 #include "websocket/websocket_base.h"
21 
22 #include <functional>
23 
24 namespace OHOS::ArkCompiler::Toolchain {
25 class WebSocketServer final : public WebSocketBase {
26 public:
27     using ValidateConnectionCallback = std::function<bool(const HttpRequest&)>;
28     using OpenConnectionCallback = std::function<void()>;
29 
30 public:
31     ~WebSocketServer() noexcept override = default;
32 
33     bool AcceptNewConnection();
34 
35 #if !defined(OHOS_PLATFORM)
36     // Initialize server socket, transition to `INITED` state.
37     bool InitTcpWebSocket(int port, uint32_t timeoutLimit = 0);
38 #else
39     // Initialize server socket, transition to `INITED` state.
40     bool InitUnixWebSocket(const std::string& sockName, uint32_t timeoutLimit = 0);
41     bool InitUnixWebSocket(int socketfd);
42     bool ConnectUnixWebSocketBySocketpair();
43 #endif
44 
45     void SetValidateConnectionCallback(ValidateConnectionCallback cb);
46     void SetOpenConnectionCallback(OpenConnectionCallback cb);
47 
48     void Close() override;
49 
50 private:
51     static bool ValidateHandShakeMessage(const HttpRequest& req);
52 
53     bool ValidateIncomingFrame(const WebSocketFrame& wsFrame) override;
54     std::string CreateFrame(bool isLast, FrameType frameType) const override;
55     std::string CreateFrame(bool isLast, FrameType frameType, const std::string& payload) const override;
56     std::string CreateFrame(bool isLast, FrameType frameType, std::string&& payload) const override;
57     bool DecodeMessage(WebSocketFrame& wsFrame) const override;
58 
59     bool HttpHandShake();
60     bool ProtocolUpgrade(const HttpRequest& req);
61     bool ResponseInvalidHandShake() const;
62     // Run `openCb_`, transition to `OnNewConnection` state.
63     void OnNewConnection();
64     // Close server socket, transition to `UNINITED` state.
65     void CloseServerSocket();
66 
67 private:
68     int32_t serverFd_ {-1};
69 
70     // Callbacks used during different stages of connection lifecycle.
71     // E.g. validation callback - it is executed during handshake
72     // and used to indicate whether the incoming connection should be accepted.
73     ValidateConnectionCallback validateCb_;
74     OpenConnectionCallback openCb_;
75 
76     static constexpr std::string_view BAD_REQUEST_RESPONSE = "HTTP/1.1 400 Bad Request\r\n\r\n";
77 };
78 } // namespace OHOS::ArkCompiler::Toolchain
79 
80 #endif // ARKCOMPILER_TOOLCHAIN_WEBSOCKET_SERVER_WEBSOCKET_SERVER_H
81