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 class HttpClientResponse {
67 public:
68     /**
69      * Default constructor for HttpClientResponse.
70      */
HttpClientResponse()71     HttpClientResponse() : responseCode_(NONE), result_(""){};
72 
73     /**
74      * Get the response code of the HTTP response.
75      * @return The response code.
76      */
77     [[nodiscard]] ResponseCode GetResponseCode() const;
78 
79     /**
80      * Get the header of the HTTP response.
81      * @return The header of the response.
82      */
83     [[nodiscard]] const std::string &GetHeader() const;
84 
85     /**
86      * Get the cookies of the HTTP response.
87      * @return The cookies of the response.
88      */
89     [[nodiscard]] const std::string &GetCookies() const;
90 
91     /**
92      * Get the request time of the HTTP response.
93      * @return The request time of the response.
94      */
95     [[nodiscard]] const std::string &GetRequestTime() const;
96 
97     /**
98      * Get the response time of the HTTP response.
99      * @return The response time of the response.
100      */
101     [[nodiscard]] const std::string &GetResponseTime() const;
102 
103     /**
104      * Set the request time of the HTTP response.
105      * @param time The request time to be set.
106      */
107     void SetRequestTime(const std::string &time);
108 
109     /**
110      * @brief Set the response time of the HTTP response.
111      * @param time The response time to be set.
112      */
113     void SetResponseTime(const std::string &time);
114 
115     /**
116      * Set the response code of the HTTP response.
117      * @param code The response code to be set.
118      */
119     void SetResponseCode(ResponseCode code);
120 
121     /**
122      * Parses the headers of the HTTP response.
123      */
124     void ParseHeaders();
125 
126     /**
127      * Retrieves the headers of the HTTP response.
128      * @return A constant reference to a map of header key-value pairs.
129      */
130     [[nodiscard]] const std::map<std::string, std::string> &GetHeaders() const;
131 
132     /**
133      * Sets a warning message for the HTTP response.
134      * @param val The warning message.
135      */
136     void SetWarning(const std::string &val);
137 
138     /**
139      * Sets a raw header for the HTTP response.
140      * @param header The raw header string.
141      */
142     void SetRawHeader(const std::string &header);
143 
144     /**
145      * Sets the cookies for the HTTP response.
146      * @param cookies The cookie string.
147      */
148     void SetCookies(const std::string &cookies);
149 
150     /**
151      * Sets the result of the HTTP response.
152      * @param res The result string.
153      */
154     void SetResult(const std::string &res);
155 
156     /**
157      * Retrieves the result of the HTTP response.
158      * @return A constant reference to the result string.
159      */
160     [[nodiscard]] const std::string &GetResult() const;
161 
162 private:
163     friend class HttpClientTask;
164 
165     /**
166      * @brief Append data to the header of the HTTP response.
167      * @param data Pointer to the data.
168      * @param length Length of the data.
169      */
170     void AppendHeader(const char *data, size_t length);
171 
172     /**
173      * Append data to the cookies of the HTTP response.
174      * @param data Pointer to the data.
175      * @param length Length of the data.
176      */
177     void AppendCookies(const char *data, size_t length);
178     void AppendResult(const void *data, size_t length);
179 
180     ResponseCode responseCode_;
181     std::string rawHeader_;
182     std::map<std::string, std::string> headers_;
183     std::string cookies_;
184     std::string responseTime_;
185     std::string requestTime_;
186     std::string result_;
187 };
188 } // namespace HttpClient
189 } // namespace NetStack
190 } // namespace OHOS
191 
192 #endif // COMMUNICATIONNETSTACK_HTTP_CLIENT_RESPONSE_H
193