• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-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 #include "server_start_context.h"
17 #include "constant.h"
18 #include "netstack_log.h"
19 #include "napi_utils.h"
20 
21 namespace OHOS::NetStack::Websocket {
ServerStartContext(napi_env env,const std::shared_ptr<EventManager> & sharedManager)22 ServerStartContext::ServerStartContext(napi_env env, const std::shared_ptr<EventManager> &sharedManager)
23     : BaseContext(env, sharedManager) {}
24 
25 ServerStartContext::~ServerStartContext() = default;
26 
ParseParams(napi_value * params,size_t paramsCount)27 void ServerStartContext::ParseParams(napi_value *params, size_t paramsCount)
28 {
29     if (!CheckParamsType(params, paramsCount)) {
30         ParseCallback(params, paramsCount);
31         return;
32     }
33 
34     if (paramsCount != FUNCTION_PARAM_ONE) {
35         SetParseOK(SetCallback(params[0]) == napi_ok);
36         return;
37     }
38 
39     napi_env env = GetEnv();
40     if (!ParseRequiredParams(env, params[0])) {
41         return;
42     }
43     ParseOptionalParams(env, params[0]);
44     SetParseOK(true);
45 }
46 
CheckParamsType(napi_value * params,size_t paramsCount)47 bool ServerStartContext::CheckParamsType(napi_value *params, size_t paramsCount)
48 {
49     if (paramsCount == FUNCTION_PARAM_ZERO) {
50         return true;
51     }
52 
53     if (paramsCount == FUNCTION_PARAM_ONE) {
54         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
55     }
56 
57     return false;
58 }
59 
ParseCallback(napi_value const * params,size_t paramsCount)60 void ServerStartContext::ParseCallback(napi_value const *params, size_t paramsCount)
61 {
62     if (paramsCount == FUNCTION_PARAM_ZERO) {
63         return;
64     }
65     if (paramsCount == FUNCTION_PARAM_ONE) {
66         if (NapiUtils::GetValueType(GetEnv(), params[FUNCTION_PARAM_ONE - 1]) == napi_object) {
67             SetCallback(params[FUNCTION_PARAM_ONE - 1]);
68         }
69         return;
70     }
71 }
72 
ParseRequiredParams(napi_env env,napi_value params)73 bool ServerStartContext::ParseRequiredParams(napi_env env, napi_value params)
74 {
75     if (NapiUtils::GetValueType(env, params) != napi_object) {
76         NETSTACK_LOGE("js type error");
77         return false;
78     }
79     uint32_t serverPort = NapiUtils::GetUint32Property(env, params, ContextKey::SERVER_PORT);
80     if (serverPort == 0) {
81         NETSTACK_LOGE("%{public}s not found", ContextKey::SERVER_PORT);
82     }
83     SetServerPort(serverPort);
84     uint32_t maxClientCnt = NapiUtils::GetUint32Property(env, params, ContextKey::MAX_CLIENT_NUMBER);
85     if (maxClientCnt == 0) {
86         NETSTACK_LOGE("max concurrent clients number is %{public}d", maxClientCnt);
87     }
88     SetMaxConcurrentClientsNumber(maxClientCnt);
89     uint32_t maxConn = NapiUtils::GetUint32Property(env, params, ContextKey::MAX_CONNECTIONS_FOR_ONE_CLIENT);
90     if (maxConn == 0) {
91         NETSTACK_LOGE("max connections for one clients:%{public}d", maxConn);
92     }
93     SetMaxConnectionsForOneClient(maxConn);
94     return true;
95 }
96 
ParseOptionalParams(napi_env env,napi_value params)97 void ServerStartContext::ParseOptionalParams(napi_env env, napi_value params)
98 {
99     if (NapiUtils::GetValueType(env, params) != napi_object) {
100         NETSTACK_LOGE("js type error");
101         return;
102     }
103     NETSTACK_LOGE("SERVER_IP:%{public}s", ContextKey::SERVER_IP);
104     std::string ip = NapiUtils::GetStringPropertyUtf8(env, params, ContextKey::SERVER_IP);
105     if (ip != "") {
106         SetServerIP(ip);
107     } else {
108         NETSTACK_LOGE("ip is null");
109         std::string ipTmp = "0.0.0.0";
110         SetServerIP(ipTmp);
111     }
112     std::string protocol = NapiUtils::GetStringPropertyUtf8(env, params, ContextKey::PROTOCOL);
113     if (protocol != "") {
114         SetServerProtocol(protocol);
115     } else {
116         NETSTACK_LOGE("protocol is null");
117         std::string ipTmp = "lws_server";
118         SetServerProtocol(ipTmp);
119     }
120     napi_value jsServerCert = NapiUtils::GetNamedProperty(env, params, ContextKey::SERVER_CERT);
121     if (NapiUtils::GetValueType(env, jsServerCert) != napi_object) {
122         NETSTACK_LOGE("jsServerCert type error");
123         return;
124     }
125     ParseServerCert(env, jsServerCert);
126 }
127 
ParseServerCert(napi_env env,napi_value params)128 void ServerStartContext::ParseServerCert(napi_env env, napi_value params)
129 {
130     if (NapiUtils::GetValueType(env, params) != napi_object) {
131         NETSTACK_LOGE("js type error");
132         return;
133     }
134     std::string certPath = NapiUtils::GetStringPropertyUtf8(env, params, ContextKey::CERT_PATH);
135     std::string keyPath = NapiUtils::GetStringPropertyUtf8(env, params, ContextKey::KEY_PATH);
136     SetServerCert(certPath, keyPath);
137 }
138 
SetServerIP(std::string & ip)139 void ServerStartContext::SetServerIP(std::string &ip)
140 {
141     serverIp_ = ip;
142 }
143 
SetServerPort(uint32_t & serverPort)144 void ServerStartContext::SetServerPort(uint32_t &serverPort)
145 {
146     serverPort_ = serverPort;
147 }
148 
SetServerCert(std::string & certPath,std::string & keyPath)149 void ServerStartContext::SetServerCert(std::string &certPath, std::string &keyPath)
150 {
151     certPath_ = certPath;
152     keyPath_ = keyPath;
153 }
154 
SetMaxConcurrentClientsNumber(uint32_t & clientsNumber)155 void ServerStartContext::SetMaxConcurrentClientsNumber(uint32_t &clientsNumber)
156 {
157     maxClientsNumber_ = clientsNumber;
158 }
159 
SetServerProtocol(std::string & protocol)160 void ServerStartContext::SetServerProtocol(std::string &protocol)
161 {
162     websocketServerProtocol_ = protocol;
163 }
164 
SetMaxConnectionsForOneClient(uint32_t & count)165 void ServerStartContext::SetMaxConnectionsForOneClient(uint32_t &count)
166 {
167     maxCountForOneClient_ = count;
168 }
169 
GetServerIP() const170 std::string ServerStartContext::GetServerIP() const
171 {
172     return serverIp_;
173 }
174 
GetServerPort() const175 uint32_t ServerStartContext::GetServerPort() const
176 {
177     return serverPort_;
178 }
179 
GetServerCert(std::string & certPath,std::string & keyPath) const180 void ServerStartContext::GetServerCert(std::string &certPath, std::string &keyPath) const
181 {
182     certPath = certPath_;
183     keyPath = keyPath_;
184 }
185 
GetMaxConcurrentClientsNumber() const186 uint32_t ServerStartContext::GetMaxConcurrentClientsNumber() const
187 {
188     return maxClientsNumber_;
189 }
190 
GetServerProtocol() const191 std::string ServerStartContext::GetServerProtocol() const
192 {
193     return websocketServerProtocol_;
194 }
195 
GetMaxConnectionsForOneClient() const196 uint32_t ServerStartContext::GetMaxConnectionsForOneClient() const
197 {
198     return maxCountForOneClient_;
199 }
200 
GetErrorCode() const201 int32_t ServerStartContext::GetErrorCode() const
202 {
203     if (BaseContext::IsPermissionDenied()) {
204         return PERMISSION_DENIED_CODE;
205     }
206     auto err = BaseContext::GetErrorCode();
207     if (err == PARSE_ERROR_CODE) {
208         return PARSE_ERROR_CODE;
209     }
210     if (WEBSOCKET_ERR_MAP.find(err) != WEBSOCKET_ERR_MAP.end()) {
211         return err;
212     }
213     return WEBSOCKET_UNKNOWN_OTHER_ERROR;
214 }
215 
GetErrorMessage() const216 std::string ServerStartContext::GetErrorMessage() const
217 {
218     if (BaseContext::IsPermissionDenied()) {
219         return PERMISSION_DENIED_MSG;
220     }
221     auto err = BaseContext::GetErrorCode();
222     if (err == PARSE_ERROR_CODE) {
223         return PARSE_ERROR_MSG;
224     }
225     auto it = WEBSOCKET_ERR_MAP.find(err);
226     if (it != WEBSOCKET_ERR_MAP.end()) {
227         return it->second;
228     }
229     it = WEBSOCKET_ERR_MAP.find(WEBSOCKET_UNKNOWN_OTHER_ERROR);
230     if (it != WEBSOCKET_ERR_MAP.end()) {
231         return it->second;
232     }
233     return {};
234 }
235 } // namespace OHOS::NetStack::Websocket