1 /*
2  * Copyright (c) 2022-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 #ifndef COMMUNICATIONNETSTACK_CONNECT_CONTEXT_H
17 #define COMMUNICATIONNETSTACK_CONNECT_CONTEXT_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "base_context.h"
23 #include "constant.h"
24 #include "nocopyable.h"
25 #include "libwebsockets.h"
26 #include "secure_char.h"
27 
28 namespace OHOS::NetStack::Websocket {
29 class ConnectContext final : public BaseContext {
30 public:
31     friend class WebSocketExec;
32     DISALLOW_COPY_AND_MOVE(ConnectContext);
33 
34     ConnectContext() = delete;
35 
36     ConnectContext(napi_env env, EventManager *manager);
37 
38     ConnectContext(napi_env env, const std::shared_ptr<EventManager> &sharedManager);
39 
40     ~ConnectContext() override;
41 
42     void ParseParams(napi_value *params, size_t paramsCount) override;
43 
44     void SetClientCert(std::string &cert, Secure::SecureChar &key, Secure::SecureChar &keyPassword);
45 
46     void GetClientCert(std::string &cert, Secure::SecureChar &key, Secure::SecureChar &keyPassword);
47 
48     void SetProtocol(std::string protocol);
49 
50     [[nodiscard]] std::string GetProtocol() const;
51 
52     void SetWebsocketProxyType(WebsocketProxyType type);
53 
54     [[nodiscard]] WebsocketProxyType GetUsingWebsocketProxyType() const;
55 
56     void SetSpecifiedWebsocketProxy(const std::string &host, int32_t port, const std::string &exclusionList);
57 
58     void GetSpecifiedWebsocketProxy(std::string &host, uint32_t &port, std::string &exclusionList) const;
59 
60     [[nodiscard]] int32_t GetErrorCode() const override;
61 
62     [[nodiscard]] std::string GetErrorMessage() const override;
63 
64     void SetAtomicService(bool isAtomicService);
65 
66     [[nodiscard]] bool IsAtomicService() const;
67 
68     void SetBundleName(const std::string &bundleName);
69 
70     [[nodiscard]] std::string GetBundleName() const;
71 
72     std::string url;
73 
74     std::map<std::string, std::string> header;
75 
76     std::string caPath_;
77 
78     std::string clientCert_;
79 
80     Secure::SecureChar clientKey_;
81 
82     Secure::SecureChar keyPassword_;
83 
84     std::string websocketProtocol_;
85 
86     WebsocketProxyType usingWebsocketProxyType_ = WebsocketProxyType::USE_SYSTEM;
87 
88     std::string websocketProxyHost_;
89 
90     int32_t websocketProxyPort_ = 0;
91 
92     std::string websocketProxyExclusions_;
93 
94     bool isAtomicService_ = false;
95 
96     std::string bundleName_;
97 
98 private:
99     std::string userCertPath_;
100 
101     void ParseHeader(napi_value optionsValue);
102 
103     void ParseCaPath(napi_value optionsValue);
104 
105     void ParseClientCert(napi_value optionsValue);
106 
107     bool ParseProxy(napi_value optionsValue);
108 
109     bool ParseProtocol(napi_value optionsValue);
110 
111     bool CheckParamsType(napi_value *params, size_t paramsCount);
112 
113     void ParseCallback(napi_value const *params, size_t paramsCount);
114 
115     void ParseParamsCountThree(napi_value const *params);
116 };
117 } // namespace OHOS::NetStack::Websocket
118 
119 #endif /* COMMUNICATIONNETSTACK_CONNECT_CONTEXT_H */
120