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_WEBSOCKET_H 17 #define ARKCOMPILER_TOOLCHAIN_WEBSOCKET_WEBSOCKET_H 18 19 #include <iostream> 20 #include <memory> 21 #include <atomic> 22 23 namespace panda::test { 24 class WebSocketTest; 25 } 26 namespace OHOS::ArkCompiler::Toolchain { 27 struct WebSocketFrame { 28 uint8_t fin = 0; 29 uint8_t opcode = 0; 30 uint8_t mask = 0; 31 uint64_t payloadLen = 0; 32 char maskingkey[5] = {0}; 33 std::unique_ptr<char []> payload = nullptr; 34 }; 35 36 struct HttpProtocol { 37 std::string connection; 38 std::string upgrade; 39 std::string version; 40 std::string secWebSocketKey; 41 }; 42 43 class WebSocket { 44 public: 45 enum SocketState : uint8_t { 46 UNINITED, 47 INITED, 48 CONNECTED, 49 }; 50 WebSocket() = default; 51 ~WebSocket() = default; 52 std::string Decode(); 53 void Close(); 54 void SendReply(const std::string& message) const; 55 #if !defined(OHOS_PLATFORM) 56 bool InitTcpWebSocket(int port, uint32_t timeoutLimit = 0); 57 bool ConnectTcpWebSocket(); 58 #else 59 bool InitUnixWebSocket(const std::string& sockName, uint32_t timeoutLimit = 0); 60 bool ConnectUnixWebSocket(); 61 #endif 62 bool IsConnected(); 63 64 private: 65 friend class panda::test::WebSocketTest; 66 67 bool DecodeMessage(WebSocketFrame& wsFrame); 68 bool HttpHandShake(); 69 bool HttpProtocolDecode(const std::string& request, HttpProtocol& req); 70 bool HandleFrame(WebSocketFrame& wsFrame); 71 bool ProtocolUpgrade(const HttpProtocol& req); 72 uint64_t NetToHostLongLong(char* buf, uint32_t len); 73 bool SetWebSocketTimeOut(int32_t fd, uint32_t timeoutLimit); 74 bool Recv(int32_t client, char* buf, size_t totalLen, int32_t flags) const; 75 bool Send(int32_t client, const char* buf, size_t totalLen, int32_t flags) const; 76 77 int32_t client_ {-1}; 78 int32_t fd_ {-1}; 79 std::atomic<SocketState> socketState_ {SocketState::UNINITED}; 80 static constexpr int32_t ENCODED_KEY_LEN = 128; 81 static constexpr char EOL[] = "\r\n"; 82 static constexpr int32_t SOCKET_HANDSHAKE_LEN = 1024; 83 static constexpr int32_t SOCKET_HEADER_LEN = 2; 84 static constexpr int32_t SOCKET_MASK_LEN = 4; 85 static constexpr int32_t SOCKET_SUCCESS = 0; 86 static constexpr int32_t PAYLOAD_LEN = 2; 87 static constexpr int32_t EXTEND_PAYLOAD_LEN = 8; 88 static constexpr char WEB_SOCKET_GUID[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; 89 }; 90 } // namespace OHOS::ArkCompiler::Toolchain 91 92 #endif // ARKCOMPILER_TOOLCHAIN_WEBSOCKET_WEBSOCKET_H