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/http.h" 20 #include "websocket/websocket_base.h" 21 22 #include <array> 23 #include <atomic> 24 #include <iostream> 25 #include <map> 26 #include <memory> 27 28 namespace OHOS::ArkCompiler::Toolchain { 29 class WebSocketClient final : public WebSocketBase { 30 public: 31 ~WebSocketClient() noexcept override = default; 32 33 bool DecodeMessage(WebSocketFrame& wsFrame) const override; 34 void Close() override; 35 36 bool InitToolchainWebSocketForPort(int port, uint32_t timeoutLimit = 5); 37 bool InitToolchainWebSocketForSockName(const std::string &sockName, uint32_t timeoutLimit = 5); 38 bool ClientSendWSUpgradeReq(); 39 bool ClientRecvWSUpgradeRsp(); 40 41 std::string GetSocketStateString(); 42 43 private: 44 static bool ValidateServerHandShake(HttpResponse& response); 45 46 void CloseConnectionSocketOnFail(); 47 bool ValidateIncomingFrame(const WebSocketFrame& wsFrame) override; 48 std::string CreateFrame(bool isLast, FrameType frameType) const override; 49 std::string CreateFrame(bool isLast, FrameType frameType, const std::string& payload) const override; 50 std::string CreateFrame(bool isLast, FrameType frameType, std::string&& payload) const override; 51 52 private: 53 static constexpr std::array<std::string_view, 3> SOCKET_STATE_NAMES = { 54 "uninited", 55 "inited", 56 "connected" 57 }; 58 59 static constexpr std::string_view HTTP_SWITCHING_PROTOCOLS_STATUS_CODE = "101"; 60 static constexpr std::string_view HTTP_RESPONSE_REQUIRED_UPGRADE = "websocket"; 61 static constexpr std::string_view HTTP_RESPONSE_REQUIRED_CONNECTION = "upgrade"; 62 63 // may replace default websocket-key with randomly generated, as required by spec 64 static constexpr unsigned char DEFAULT_WEB_SOCKET_KEY[] = "64b4B+s5JDlgkdg7NekJ+g=="; 65 static constexpr char CLIENT_WEBSOCKET_UPGRADE_REQ[] = "GET / HTTP/1.1\r\n" 66 "Connection: Upgrade\r\n" 67 "Pragma: no-cache\r\n" 68 "Cache-Control: no-cache\r\n" 69 "Upgrade: websocket\r\n" 70 "Sec-WebSocket-Version: 13\r\n" 71 "Accept-Encoding: gzip, deflate, br\r\n" 72 "Sec-WebSocket-Key: 64b4B+s5JDlgkdg7NekJ+g==\r\n" 73 "Sec-WebSocket-Extensions: permessage-deflate\r\n" 74 "\r\n"; 75 76 static constexpr int NET_SUCCESS = 1; 77 static constexpr uint8_t MASK_KEY[] = {0xa, 0xb, 0xc, 0xd}; 78 }; 79 } // namespace OHOS::ArkCompiler::Toolchain 80 81 #endif // ARKCOMPILER_TOOLCHAIN_WEBSOCKET_CLIENT_WEBSOCKET_CLIENT_H 82