1 /* 2 * Copyright (c) 2025 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 NET_WEBSOCKET_EXEC_H 17 #define NET_WEBSOCKET_EXEC_H 18 19 #include "net_websocket_connect_context.h" 20 #include "net_websocket_send_context.h" 21 #include "net_websocket_close_context.h" 22 #include "libwebsockets.h" 23 24 namespace OHOS::NetStack::NetWebSocket { 25 class NetWebSocketExec final { 26 public: 27 static bool CreatConnectInfo(WebSocketConnectContext *context, lws_context *lwsContext, 28 CJWebsocketProxy *websocketProxy); 29 /* async work execute */ 30 static bool ExecConnect(WebSocketConnectContext *context); 31 32 static bool ExecSend(WebSocketSendContext *context); 33 34 static bool ExecClose(WebSocketCloseContext *context); 35 36 static int LwsCallback(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 37 38 private: 39 static bool ParseUrl(WebSocketConnectContext *context, char *prefix, 40 size_t prefixLen, char *address, size_t addressLen, 41 char *path, size_t pathLen, int *port); 42 43 static int RaiseError(CJWebsocketProxy *websocketProxy, uint32_t httpResponse); 44 45 static int HttpDummy(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 46 47 static int LwsCallbackClientAppendHandshakeHeader(lws *wsi, lws_callback_reasons reason, void *user, void *in, 48 size_t len); 49 50 static int LwsCallbackWsPeerInitiatedClose(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 51 52 static int LwsCallbackClientWritable(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 53 54 static int LwsCallbackClientConnectionError(lws *wsi, lws_callback_reasons reason, void *user, void *in, 55 size_t len); 56 57 static int LwsCallbackClientReceive(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 58 59 static int LwsCallbackClientFilterPreEstablish(lws *wsi, lws_callback_reasons reason, void *user, void *in, 60 size_t len); 61 62 static int LwsCallbackClientEstablished(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 63 64 static int LwsCallbackClientClosed(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 65 66 static int LwsCallbackWsiDestroy(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 67 68 static int LwsCallbackProtocolDestroy(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 69 70 static int LwsCallbackVhostCertAging(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 71 72 static void OnOpen(CJWebsocketProxy *websocketProxy, uint32_t status, const std::string &message); 73 74 static void OnError(CJWebsocketProxy *websocketProxy, int32_t code, uint32_t httpResponse); 75 76 static uint32_t GetHttpResponseFromWsi(lws *wsi); 77 78 static void OnMessage(CJWebsocketProxy *websocketProxy, void *data, size_t length, bool isBinary, bool isFinal); 79 80 static void OnClose(CJWebsocketProxy *websocketProxy, lws_close_status closeStatus, const std::string &closeReason); 81 82 static void OnDataEnd(CJWebsocketProxy *websocketProxy); 83 84 static void OnHeaderReceive(CJWebsocketProxy *websocketProxy, const std::map<std::string, std::string> &headers); 85 86 static void FillContextInfo(WebSocketConnectContext *context, lws_context_creation_info &info, char *proxyAds); 87 88 static bool FillCaPath(WebSocketConnectContext *context, lws_context_creation_info &info); 89 90 static void GetWebsocketProxyInfo(WebSocketConnectContext *context, std::string &host, 91 uint32_t &port, std::string &exclusions); 92 static void HandleRcvMessage(CJWebsocketProxy *websocketProxy, void *data, 93 size_t length, bool isBinary, bool isFinal); 94 }; 95 } // namespace OHOS::NetStack::NetWebSocket 96 #endif /* NET_WEBSOCKET_EXEC_H */ 97