• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef COMMUNICATIONNETSTACK_HTTP_CLIENT_RESPONSE_H
17 #define COMMUNICATIONNETSTACK_HTTP_CLIENT_RESPONSE_H
18 
19 #include <map>
20 #include <string>
21 
22 namespace OHOS {
23 namespace NetStack {
24 namespace HttpClient {
25 static constexpr const char *WARNING = "Warning";
26 
27 enum ResponseCode {
28     NONE = 0,
29     OK = 200,
30     CREATED,
31     ACCEPTED,
32     NOT_AUTHORITATIVE,
33     NO_CONTENT,
34     RESET,
35     PARTIAL,
36     MULT_CHOICE = 300,
37     MOVED_PERM,
38     MOVED_TEMP,
39     SEE_OTHER,
40     NOT_MODIFIED,
41     USE_PROXY,
42     BAD_REQUEST = 400,
43     UNAUTHORIZED,
44     PAYMENT_REQUIRED,
45     FORBIDDEN,
46     NOT_FOUND,
47     BAD_METHOD,
48     NOT_ACCEPTABLE,
49     PROXY_AUTH,
50     CLIENT_TIMEOUT,
51     CONFLICT,
52     GONE,
53     LENGTH_REQUIRED,
54     PRECON_FAILED,
55     ENTITY_TOO_LARGE,
56     REQ_TOO_LONG,
57     UNSUPPORTED_TYPE,
58     INTERNAL_ERROR = 500,
59     NOT_IMPLEMENTED,
60     BAD_GATEWAY,
61     UNAVAILABLE,
62     GATEWAY_TIMEOUT,
63     VERSION,
64 };
65 
66 /**
67  * Counting the time taken of various stages of HTTP request.
68  */
69 struct PerformanceInfo {
70     /** Time taken from startup to DNS resolution completion, in milliseconds. */
71     double dnsTiming = 0.0;
72     /** Time taken from startup to TCP connection completion, in milliseconds. */
73     double connectTiming = 0.0;
74     /** Time taken from startup to TLS connection completion, in milliseconds. */
75     double tlsTiming = 0.0;
76     /** Time taken from startup to start sending the first byte, in milliseconds. */
77     double firstSendTiming = 0.0;
78     /** Time taken from startup to receiving the first byte, in milliseconds. */
79     double firstReceiveTiming = 0.0;
80     /** Time taken from startup to the completion of the request, in milliseconds. */
81     double totalTiming = 0.0;
82     /** Time taken from startup to completion of all redirection steps, in milliseconds. */
83     double redirectTiming = 0.0;
84 };
85 
86 class HttpClientResponse {
87 public:
88     /**
89      * Default constructor for HttpClientResponse.
90      */
HttpClientResponse()91     HttpClientResponse() : responseCode_(NONE), result_(""){};
92 
93     /**
94      * Get the response code of the HTTP response.
95      * @return The response code.
96      */
97     [[nodiscard]] ResponseCode GetResponseCode() const;
98 
99     /**
100      * Get the header of the HTTP response.
101      * @return The header of the response.
102      */
103     [[nodiscard]] const std::string &GetHeader() const;
104 
105     /**
106      * Get the cookies of the HTTP response.
107      * @return The cookies of the response.
108      */
109     [[nodiscard]] const std::string &GetCookies() const;
110 
111     /**
112      * Get the request time of the HTTP response.
113      * @return The request time of the response.
114      */
115     [[nodiscard]] const std::string &GetRequestTime() const;
116 
117     /**
118      * Get the response time of the HTTP response.
119      * @return The response time of the response.
120      */
121     [[nodiscard]] const std::string &GetResponseTime() const;
122 
123     /**
124      * Set the request time of the HTTP response.
125      * @param time The request time to be set.
126      */
127     void SetRequestTime(const std::string &time);
128 
129     /**
130      * @brief Set the response time of the HTTP response.
131      * @param time The response time to be set.
132      */
133     void SetResponseTime(const std::string &time);
134 
135     /**
136      * Set the response code of the HTTP response.
137      * @param code The response code to be set.
138      */
139     void SetResponseCode(ResponseCode code);
140 
141     /**
142      * Parses the headers of the HTTP response.
143      */
144     void ParseHeaders();
145 
146     /**
147      * Retrieves the headers of the HTTP response.
148      * @return A constant reference to a map of header key-value pairs.
149      */
150     [[nodiscard]] const std::map<std::string, std::string> &GetHeaders() const;
151 
152     /**
153      * Sets a warning message for the HTTP response.
154      * @param val The warning message.
155      */
156     void SetWarning(const std::string &val);
157 
158     /**
159      * Sets a raw header for the HTTP response.
160      * @param header The raw header string.
161      */
162     void SetRawHeader(const std::string &header);
163 
164     /**
165      * Sets the cookies for the HTTP response.
166      * @param cookies The cookie string.
167      */
168     void SetCookies(const std::string &cookies);
169 
170     /**
171      * Sets the result of the HTTP response.
172      * @param res The result string.
173      */
174     void SetResult(const std::string &res);
175 
176     /**
177      * Retrieves the result of the HTTP response.
178      * @return A constant reference to the result string.
179      */
180     [[nodiscard]] const std::string &GetResult() const;
181 
182     /**
183      * Get the time taken of various stages of HTTP request.
184      * @return The performance info including the time taken of various stages of HTTP request.
185      */
186     [[nodiscard]] PerformanceInfo GetPerformanceTiming() const;
187 
188 private:
189     friend class HttpClientTask;
190 
191     /**
192      * @brief Append data to the header of the HTTP response.
193      * @param data Pointer to the data.
194      * @param length Length of the data.
195      */
196     void AppendHeader(const char *data, size_t length);
197 
198     /**
199      * Append data to the cookies of the HTTP response.
200      * @param data Pointer to the data.
201      * @param length Length of the data.
202      */
203     void AppendCookies(const char *data, size_t length);
204     void AppendResult(const void *data, size_t length);
205 
206     ResponseCode responseCode_;
207     std::string rawHeader_;
208     std::map<std::string, std::string> headers_;
209     std::string cookies_;
210     std::string responseTime_;
211     std::string requestTime_;
212     std::string result_;
213     PerformanceInfo performanceInfo_;
214 };
215 } // namespace HttpClient
216 } // namespace NetStack
217 } // namespace OHOS
218 
219 #endif // COMMUNICATIONNETSTACK_HTTP_CLIENT_RESPONSE_H
220