diff --git a/frameworks/js/napi/websocket/async_context/include/connect_context.h b/frameworks/js/napi/websocket/async_context/include/connect_context.h index f6f77da9eac663d3cf0988614d0c50c13f73037f..4614fd5b231fc6bfe0f15f46fca1ce0878796d6a 100755 --- a/frameworks/js/napi/websocket/async_context/include/connect_context.h +++ b/frameworks/js/napi/websocket/async_context/include/connect_context.h @@ -75,7 +75,7 @@ public: std::string clientCert_; - bool skipServerCertVerification_; + bool skipServerCertVerification_ = false; Secure::SecureChar clientKey_; diff --git a/frameworks/js/napi/websocket/websocket_exec/src/websocket_exec.cpp b/frameworks/js/napi/websocket/websocket_exec/src/websocket_exec.cpp index 959ee7b21a6ee4adf703aafd2fc15d422367b5ba..d8707298ba7580dc83b772223f9c37163d57f51d 100644 --- a/frameworks/js/napi/websocket/websocket_exec/src/websocket_exec.cpp +++ b/frameworks/js/napi/websocket/websocket_exec/src/websocket_exec.cpp @@ -526,6 +526,18 @@ void WebSocketExec::FillContextInfo(ConnectContext *context, lws_context_creatio } } +static bool WebSocketConnect(lws_client_connect_info &connectInfo, const std::shared_ptr &manager, + ConnectContext *context) +{ + if (lws_client_connect_via_info(&connectInfo) == nullptr) { + NETSTACK_LOGI("ExecConnect websocket connect failed"); + context->SetErrorCode(-1); + OnConnectError(manager.get(), COMMON_ERROR_CODE, 0); + return false; + } + return true; +} + bool WebSocketExec::CreatConnectInfo(ConnectContext *context, lws_context *lwsContext, const std::shared_ptr &manager) { @@ -549,30 +561,31 @@ bool WebSocketExec::CreatConnectInfo(ConnectContext *context, lws_context *lwsCo if (strcpy_s(customizedProtocol, context->GetProtocol().length() + 1, context->GetProtocol().c_str()) != EOK) { NETSTACK_LOGE("memory copy failed"); } - + connectInfo.context = lwsContext; connectInfo.port = port; connectInfo.address = address.c_str(); + if (std::strlen(path.c_str()) != path.length()) { + NETSTACK_LOGE("c_str() length does not match path length."); + return false; + } connectInfo.path = path.c_str(); connectInfo.host = tempHost.c_str(); connectInfo.origin = address.c_str(); connectInfo.protocol = customizedProtocol; - + if (protocol == PREFIX_HTTPS || protocol == PREFIX_WSS) { connectInfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK | LCCSCF_ALLOW_SELFSIGNED; } if (context->skipServerCertVerification_) { NETSTACK_LOGI("ExecConnect skip server cert verify"); - connectInfo.ssl_connection |= LCCSCF_ALLOW_INSECURE; + connectInfo.ssl_connection = ((unsigned int)connectInfo.ssl_connection) | LCCSCF_ALLOW_INSECURE; } lws *wsi = nullptr; connectInfo.pwsi = &wsi; connectInfo.retry_and_idle_policy = &RETRY; connectInfo.userdata = manager.get(); - if (lws_client_connect_via_info(&connectInfo) == nullptr) { - NETSTACK_LOGI("ExecConnect websocket connect failed"); - context->SetErrorCode(-1); - OnConnectError(manager.get(), COMMON_ERROR_CODE, 0); + if (!WebSocketConnect(connectInfo, manager, context)) { return false; } return true; diff --git a/frameworks/js/napi/websocket/websocket_module/src/websocket_client.cpp b/frameworks/js/napi/websocket/websocket_module/src/websocket_client.cpp index 53276d5f130d02858be0b6967a761acf1a17c3f4..91eee44cbb73fcd48592b4c260649d65b2a2791d 100644 --- a/frameworks/js/napi/websocket/websocket_module/src/websocket_client.cpp +++ b/frameworks/js/napi/websocket/websocket_module/src/websocket_client.cpp @@ -405,6 +405,10 @@ int CreatConnectInfo(const std::string url, lws_context *lwsContext, WebSocketCl connectInfo.context = lwsContext; connectInfo.address = address; connectInfo.port = port; + if (std::strlen(path.c_str()) != path.length()) { + NETSTACK_LOGE("c_str() length does not match path length."); + return -1; + } connectInfo.path = path.c_str(); connectInfo.host = tempHost.c_str(); connectInfo.origin = address;