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 <iostream>
17
18 #include "net_websocket.h"
19 #include "websocket_client_innerapi.h"
20
21 namespace OHOS::NetStack::WebSocketClient {
22
23 std::map<WebSocket *, WebSocketClient *> g_clientMap;
24 std::map<std::string, std::string> globalheaders;
25
GetInnerClientAdapter(WebSocket * key)26 WebSocketClient *GetInnerClientAdapter(WebSocket *key)
27 {
28 auto it = g_clientMap.find(key);
29 if (it != g_clientMap.end()) {
30 return it->second;
31 } else {
32 return nullptr;
33 }
34 }
35
GetNdkClientAdapter(const WebSocketClient * websocketClient)36 WebSocket *GetNdkClientAdapter(const WebSocketClient *websocketClient)
37 {
38 auto it = std::find_if(g_clientMap.begin(), g_clientMap.end(), [&websocketClient](const auto& pair) {
39 return pair.second == websocketClient;
40 });
41 if (it != g_clientMap.end()) {
42 return it->first;
43 }
44 return nullptr;
45 }
46
Conv2RequestOptions(struct OpenOptions * openOptions,struct WebSocket_RequestOptions requestOptions)47 int32_t Conv2RequestOptions(struct OpenOptions *openOptions,
48 struct WebSocket_RequestOptions requestOptions)
49 {
50 if (openOptions == nullptr) {
51 return -1;
52 }
53
54 struct WebSocket_Header *currentHeader = requestOptions.headers;
55
56 while (currentHeader != nullptr) {
57 std::string fieldName(currentHeader->fieldName);
58 std::string fieldValue(currentHeader->fieldValue);
59 openOptions->headers[fieldName] = fieldValue;
60 currentHeader = currentHeader->next;
61 }
62
63 return 0;
64 }
65
Conv2CloseOptions(struct CloseOption * closeOption,struct WebSocket_CloseOption requestOptions)66 int32_t Conv2CloseOptions(struct CloseOption *closeOption,
67 struct WebSocket_CloseOption requestOptions)
68 {
69 closeOption->code = requestOptions.code;
70 closeOption->reason = requestOptions.reason;
71 return 0;
72 }
73
Conv2CloseResult(struct CloseResult closeResult,struct WebSocket_CloseResult * webSocketCloseResult)74 int32_t Conv2CloseResult(struct CloseResult closeResult, struct WebSocket_CloseResult *webSocketCloseResult)
75 {
76 webSocketCloseResult->code = closeResult.code;
77 webSocketCloseResult->reason = closeResult.reason;
78 return 0;
79 }
80
Conv2ErrorResult(struct ErrorResult error,struct WebSocket_ErrorResult * webSocketErrorResult)81 int32_t Conv2ErrorResult(struct ErrorResult error, struct WebSocket_ErrorResult *webSocketErrorResult)
82 {
83 webSocketErrorResult->errorCode = error.errorCode;
84 webSocketErrorResult->errorMessage = error.errorMessage;
85 return 0;
86 }
87
Conv2OpenResult(struct OpenResult openResult,struct WebSocket_OpenResult * webSocketOpenResult)88 int32_t Conv2OpenResult(struct OpenResult openResult, struct WebSocket_OpenResult *webSocketOpenResult)
89 {
90 webSocketOpenResult->code = openResult.status;
91 webSocketOpenResult->reason = openResult.message;
92
93 return 0;
94 }
95
96 } // namespace OHOS::NetStack::WebSocketClient