• 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_NONE) {
52         NETSTACK_LOGD("param is none");
53         return true;
54     }
55 
56     if (paramsCount == TlsSocket::PARAM_JUST_CALLBACK) {
57         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_0]) != napi_function) {
58             NETSTACK_LOGE("first param is not string");
59             SetNeedThrowException(true);
60             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
61             return false;
62         }
63         return true;
64     }
65 
66     if (paramsCount == TlsSocket::PARAM_OPTIONS_AND_CALLBACK) {
67         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_0]) != napi_number) {
68             NETSTACK_LOGE("first param is not int");
69             SetNeedThrowException(true);
70             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
71             return false;
72         }
73         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_1]) != napi_function) {
74             NETSTACK_LOGE("second param is not function");
75             return false;
76         }
77         return true;
78     }
79     return false;
80 }
81 } // namespace TlsSocketServer
82 } // namespace NetStack
83 } // namespace OHOS
84