• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_CLIENT_WEBSOCKET_CLIENT_H
17 #define ARKCOMPILER_TOOLCHAIN_WEBSOCKET_CLIENT_WEBSOCKET_CLIENT_H
18 
19 #include "websocket_base.h"
20 #include "http.h"
21 #include <array>
22 #include <atomic>
23 #include <iostream>
24 #include <map>
25 #include <memory>
26 
27 namespace OHOS::ArkCompiler::Toolchain {
28 class WebSocketClient final : public WebSocketBase {
29 public:
30     ~WebSocketClient() noexcept override = default;
31 
32     void Close();
33 
34     bool InitToolchainWebSocketForPort(int port, uint32_t timeoutLimit = 5);
35     bool InitToolchainWebSocketForSockName(const std::string &sockName, uint32_t timeoutLimit = 5);
36     bool ClientSendWSUpgradeReq();
37     bool ClientRecvWSUpgradeRsp();
38 
39     std::string GetSocketStateString();
40 
41 private:
42     bool DecodeMessage(WebSocketFrame& wsFrame) const override;
43 
44     void CloseOnInitFailure();
45     bool ValidateIncomingFrame(const WebSocketFrame& wsFrame) const override;
46     std::string CreateFrame(bool isLast, FrameType frameType) const override;
47     std::string CreateFrame(bool isLast, FrameType frameType, const std::string& payload) const override;
48     std::string CreateFrame(bool isLast, FrameType frameType, std::string&& payload) const override;
49     bool ValidateServerHandShake(HttpResponse& response);
50 
51 private:
52     static constexpr std::array<std::string_view, 4> SOCKET_STATE_NAMES = {
53         "connecting",
54         "open",
55         "closing",
56         "closed"
57     };
58 
59     static constexpr char CLIENT_WS_UPGRADE_REQ_BEFORE_KEY[] = "GET / HTTP/1.1\r\n"
60                                                                "Connection: Upgrade\r\n"
61                                                                "Pragma: no-cache\r\n"
62                                                                "Cache-Control: no-cache\r\n"
63                                                                "Upgrade: websocket\r\n"
64                                                                "Sec-WebSocket-Version: 13\r\n"
65                                                                "Accept-Encoding: gzip, deflate, br\r\n"
66                                                                "Sec-WebSocket-Key: ";
67 
68     static constexpr char CLIENT_WS_UPGRADE_REQ_AFTER_KEY[] = "\r\nSec-WebSocket-Extensions: permessage-deflate\r\n"
69                                                               "\r\n";
70 
71     static constexpr int NET_SUCCESS = 1;
72     static constexpr uint8_t MASK_KEY[] = {0xa, 0xb, 0xc, 0xd};
73     std::string secWebSocketKey_;
74 };
75 } // namespace OHOS::ArkCompiler::Toolchain
76 
77 #endif // ARKCOMPILER_TOOLCHAIN_WEBSOCKET_CLIENT_WEBSOCKET_CLIENT_H
78