1 /*
2  * Copyright (c) 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 #ifndef HTTP_ENTITY_NET_HTTP_REQUEST_H
17 #define HTTP_ENTITY_NET_HTTP_REQUEST_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 #include "constant.h"
24 #include "net_http_utils.h"
25 
26 namespace OHOS::NetStack::Http {
27 
28 enum class HttpProtocol {
29     HTTP1_1,
30     HTTP2,
31     HTTP_NONE, // default choose by curl
32 };
33 
34 enum class UsingHttpProxyType {
35     NOT_USE,
36     USE_DEFAULT,
37     USE_SPECIFIED,
38 };
39 
40 struct MultiFormData {
41     MultiFormData() = default;
42     ~MultiFormData() = default;
43     std::string name;
44     std::string contentType;
45     std::string remoteFileName;
46     std::string data;
47     std::string filePath;
48 };
49 
50 class HttpRequest final {
51 public:
52     HttpRequest();
53 
54     void SetUrl(const std::string &url);
55 
56     void SetMethod(const std::string &method);
57 
58     void SetBody(const void *data, size_t length);
59 
60     void SetHeader(const std::string &key, const std::string &val);
61 
62     void SetReadTimeout(uint32_t readTimeout);
63 
64     void SetMaxLimit(uint32_t maxLimit);
65 
66     void SetConnectTimeout(uint32_t connectTimeout);
67 
68     void SetUsingProtocol(HttpProtocol httpProtocol);
69 
70     void SetHttpDataType(HttpDataType dataType);
71 
72     void SetUsingHttpProxyType(UsingHttpProxyType type);
73 
74     void SetSpecifiedHttpProxy(const std::string &host, int32_t port, const std::string &exclusionList);
75 
76     void SetCaPath(const std::string &path);
77 
78     void SetDnsServers(const std::vector<std::string> &dnsServers);
79 
80     void SetDohUrl(const std::string &dohUrl);
81 
82     void SetRangeNumber(int64_t resumeFromNumber, int64_t resumeToNumber);
83 
84     void SetClientCert(std::string &cert, std::string &certType, std::string &key, SecureChar &keyPasswd);
85 
86     void AddMultiFormData(const MultiFormData &multiFormData);
87 
88     [[nodiscard]] const std::string &GetUrl() const;
89 
90     [[nodiscard]] const std::string &GetMethod() const;
91 
92     [[nodiscard]] const std::string &GetBody() const;
93 
94     [[nodiscard]] const std::map<std::string, std::string> &GetHeader() const;
95 
96     [[nodiscard]] uint32_t GetReadTimeout() const;
97 
98     [[nodiscard]] uint32_t GetMaxLimit() const;
99 
100     [[nodiscard]] uint32_t GetConnectTimeout() const;
101 
102     [[nodiscard]] uint32_t GetHttpVersion() const;
103 
104     void SetRequestTime(const std::string &time);
105 
106     [[nodiscard]] const std::string &GetRequestTime() const;
107 
108     [[nodiscard]] HttpDataType GetHttpDataType() const;
109 
110     void SetPriority(uint32_t priority);
111 
112     [[nodiscard]] uint32_t GetPriority() const;
113 
114     [[nodiscard]] UsingHttpProxyType GetUsingHttpProxyType() const;
115 
116     void GetSpecifiedHttpProxy(std::string &host, int32_t &port, std::string &exclusionList);
117 
118     [[nodiscard]] const std::string &GetCaPath() const;
119 
120     [[nodiscard]] const std::string &GetDohUrl() const;
121 
122     [[nodiscard]] std::string GetRangeString() const;
123 
124     [[nodiscard]] const std::vector<std::string> &GetDnsServers() const;
125 
126     void GetClientCert(std::string &cert, std::string &certType, std::string &key, SecureChar &keyPasswd);
127 
128     std::vector<MultiFormData> GetMultiPartDataList();
129 private:
130     std::string url_;
131 
132     std::string body_;
133 
134     std::string method_;
135 
136     std::map<std::string, std::string> header_;
137 
138     uint32_t readTimeout_;
139 
140     uint32_t maxLimit_;
141 
142     uint32_t connectTimeout_;
143 
144     HttpProtocol usingProtocol_;
145 
146     std::string requestTime_;
147 
148     HttpDataType dataType_;
149 
150     uint32_t priority_;
151 
152     UsingHttpProxyType usingHttpProxyType_;
153 
154     std::string httpProxyHost_;
155 
156     int32_t httpProxyPort_;
157 
158     std::string httpProxyExclusions_;
159 
160     std::string caPath_;
161 
162     std::string dohUrl_;
163 
164     std::vector<std::string> dnsServers_;
165 
166     int64_t resumeFromNumber_;
167 
168     int64_t resumeToNumber_;
169 
170     std::string cert_;
171 
172     std::string certType_;
173 
174     std::string key_;
175 
176     SecureChar keyPasswd_;
177 
178     std::vector<MultiFormData> multiFormDataList_;
179 };
180 } // namespace OHOS::NetStack::Http
181 
182 #endif /* HTTP_ENTITY_NET_HTTP_REQUEST_H */
183