• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "tls_server_napi_context.h"
17 
18 #include <cstdint>
19 #include <string_view>
20 
21 #include "constant.h"
22 #include "napi_utils.h"
23 #include "netstack_log.h"
24 
25 namespace OHOS {
26 namespace NetStack {
27 namespace TlsSocketServer {
28 static constexpr std::string_view PARSE_ERROR = "data is not int";
29 
TLSServerNapiContext(napi_env env,EventManager * manager)30 TLSServerNapiContext::TLSServerNapiContext(napi_env env, EventManager *manager) : BaseContext(env, manager)
31 {
32     remoteCert_.encodingFormat = TlsSocket::EncodingFormat::DER;
33 }
34 
ParseParams(napi_value * params,size_t paramsCount)35 void TLSServerNapiContext::ParseParams(napi_value *params, size_t paramsCount)
36 {
37     if (!CheckParamsType(params, paramsCount)) {
38         return;
39     }
40     if (paramsCount == TlsSocket::PARAM_JUST_CALLBACK) {
41         SetParseOK(SetCallback(params[TlsSocket::ARG_INDEX_0]) == napi_ok);
42         return;
43     }
44     SetParseOK(true);
45 
46     return;
47 }
48 
CheckParamsType(napi_value * params,size_t paramsCount)49 bool TLSServerNapiContext::CheckParamsType(napi_value *params, size_t paramsCount)
50 {
51     if (paramsCount == TlsSocket::PARAM_JUST_CALLBACK) {
52         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_0]) != napi_function) {
53             NETSTACK_LOGE("first param is not string");
54             SetNeedThrowException(true);
55             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
56             return false;
57         }
58         return true;
59     }
60 
61     if (paramsCount == TlsSocket::PARAM_OPTIONS_AND_CALLBACK) {
62         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_0]) != napi_number) {
63             NETSTACK_LOGE("first param is not int");
64             SetNeedThrowException(true);
65             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
66             return false;
67         }
68         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_1]) != napi_function) {
69             NETSTACK_LOGE("second param is not function");
70             return false;
71         }
72         return true;
73     }
74     return false;
75 }
76 } // namespace TlsSocketServer
77 } // namespace NetStack
78 } // namespace OHOS
79