• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "net_websocket_send_context.h"
17 
18 #include "securec.h"
19 #include "netstack_log.h"
20 
21 namespace OHOS::NetStack::NetWebSocket {
WebSocketSendContext(CJWebsocketProxy * websocketProxy)22 WebSocketSendContext::WebSocketSendContext(CJWebsocketProxy *websocketProxy)
23     : WebSocketBaseContext(websocketProxy), data(nullptr), length(0), protocol(LWS_WRITE_TEXT)
24 {
25 }
26 
ParseParams(CArrUI8 params,bool stringType)27 void WebSocketSendContext::ParseParams(CArrUI8 params, bool stringType)
28 {
29     // set protocol
30     protocol = stringType ? LWS_WRITE_TEXT : LWS_WRITE_BINARY;
31     size_t len = static_cast<size_t>(params.size);
32     if (len == 0) {
33         NETSTACK_LOGE("no memory");
34         return;
35     };
36     // set data
37     // must have PRE and POST
38     size_t dataLen = static_cast<size_t>(LWS_SEND_BUFFER_PRE_PADDING + params.size + LWS_SEND_BUFFER_POST_PADDING);
39     if (dataLen == 0 || dataLen > MAX_LIMIT) {
40         NETSTACK_LOGE("WebSocketSendContext data is exceeded the limit");
41         return;
42     }
43     data = malloc(dataLen);
44     if (data == nullptr) {
45         NETSTACK_LOGE("no memory");
46         return;
47     }
48     if (memcpy_s(reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(data) + LWS_SEND_BUFFER_PRE_PADDING),
49                  len, params.head, len) < 0) {
50         NETSTACK_LOGE("copy failed");
51         free(data);
52         return;
53     }
54     // set length
55     length = len;
56     NETSTACK_LOGD("WebSocketSendContext SetParseOK");
57     SetParseOK(true);
58 }
59 
GetErrorCode() const60 int32_t WebSocketSendContext::GetErrorCode() const
61 {
62     if (WebSocketBaseContext::IsPermissionDenied()) {
63         return WEBSOCKET_PERMISSION_DENIED_CODE;
64     }
65     auto err = WebSocketBaseContext::GetErrorCode();
66     if (WEBSOCKET_ERR_MAP.find(err) != WEBSOCKET_ERR_MAP.end()) {
67         return err;
68     }
69     return WEBSOCKET_CONNECT_FAILED;
70 }
71 
GetErrorMessage() const72 std::string WebSocketSendContext::GetErrorMessage() const
73 {
74     auto err = WebSocketBaseContext::GetErrorCode();
75     auto it = WEBSOCKET_ERR_MAP.find(err);
76     if (it != WEBSOCKET_ERR_MAP.end()) {
77         return it->second;
78     }
79     it = WEBSOCKET_ERR_MAP.find(WEBSOCKET_UNKNOWN_OTHER_ERROR);
80     if (it != WEBSOCKET_ERR_MAP.end()) {
81         return it->second;
82     }
83     return {};
84 }
85 }