1 /*
2  * Copyright (c) 2023-2024 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_send_context.h"
17 
18 #include <cstdint>
19 #include <string>
20 #include <string_view>
21 #include <vector>
22 
23 #include "constant.h"
24 #include "napi_utils.h"
25 #include "netstack_log.h"
26 
27 namespace OHOS {
28 namespace NetStack {
29 namespace TlsSocketServer {
30 namespace {
31 constexpr std::string_view PARSE_ERROR = "options is not type of TLSServerSendOptions";
32 } // namespace
33 
TLSServerSendContext(napi_env env,EventManager * manager)34 TLSServerSendContext::TLSServerSendContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
35 
ParseParams(napi_value * params,size_t paramsCount)36 void TLSServerSendContext::ParseParams(napi_value *params, size_t paramsCount)
37 {
38     if (!CheckParamsType(params, paramsCount)) {
39         return;
40     }
41     if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_0]) == napi_string) {
42         m_sendData = NapiUtils::GetStringFromValueUtf8(GetEnv(), params[TlsSocket::ARG_INDEX_0]);
43         if (m_sendData.empty()) {
44             NETSTACK_LOGE("string data is empty");
45             if (paramsCount == TlsSocket::PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
46                 NETSTACK_LOGE("failed to set callback");
47             }
48             return;
49         }
50     }
51 
52     if (NapiUtils::ValueIsArrayBuffer(GetEnv(), params[TlsSocket::ARG_INDEX_0])) {
53         size_t length = 0;
54         void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), params[TlsSocket::ARG_INDEX_0], &length);
55         if (data == nullptr || length == 0) {
56             NETSTACK_LOGE("arraybuffer data is empty");
57             if (paramsCount == TlsSocket::PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
58                 NETSTACK_LOGE("failed to set callback");
59             }
60             return;
61         }
62         m_sendData.append(reinterpret_cast<char *>(data), length);
63     }
64 
65     if (paramsCount == TlsSocket::PARAM_OPTIONS_AND_CALLBACK) {
66         SetParseOK(SetCallback(params[TlsSocket::ARG_INDEX_1]) == napi_ok);
67         return;
68     }
69     SetParseOK(true);
70 }
71 
CheckParamsType(napi_value * params,size_t paramsCount)72 bool TLSServerSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
73 {
74     if (paramsCount == TlsSocket::PARAM_JUST_OPTIONS) {
75         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_0]) != napi_string &&
76             !NapiUtils::ValueIsArrayBuffer(GetEnv(), params[TlsSocket::ARG_INDEX_0])) {
77             NETSTACK_LOGE("first param is not string or arraybuffer");
78             SetNeedThrowException(true);
79             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
80             return false;
81         }
82         return true;
83     }
84 
85     if (paramsCount == TlsSocket::PARAM_OPTIONS_AND_CALLBACK) {
86         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_0]) != napi_string &&
87             !NapiUtils::ValueIsArrayBuffer(GetEnv(), params[TlsSocket::ARG_INDEX_0])) {
88             NETSTACK_LOGE("first param is not string or arraybuffer");
89             SetNeedThrowException(true);
90             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
91             return false;
92         }
93         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_1]) != napi_function) {
94             NETSTACK_LOGE("second param is not function");
95             return false;
96         }
97         return true;
98     }
99     return false;
100 }
101 } // namespace TlsSocketServer
102 } // namespace NetStack
103 } // namespace OHOS
104