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