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 #include "net_http_cache_entity.h"
17
18 #include "netstack_common_utils.h"
19
20 namespace OHOS::NetStack::Http {
21
ParseCacheControl(const std::string & cacheControl)22 void HttpCacheRequest::ParseCacheControl(const std::string &cacheControl)
23 {
24 auto vec = CommonUtils::Split(cacheControl, SPLIT);
25 for (const auto &str : vec) {
26 if (str == NO_CACHE) {
27 noCache_ = true;
28 } else if (str == NO_STORE) {
29 noStore_ = true;
30 } else if (str == NO_TRANSFORM) {
31 noTransform_ = true;
32 } else if (str == ONLY_IF_CACHED) {
33 onlyIfCached_ = true;
34 }
35 auto pos = str.find(EQUAL);
36 if (pos != std::string::npos) {
37 std::string key = str.substr(0, pos);
38 std::string value = str.substr(pos + 1);
39 if (key == MAX_AGE) {
40 maxAge_ = value;
41 } else if (key == MAX_STALE) {
42 maxStale_ = value;
43 } else if (key == MIN_FRESH) {
44 minFresh_ = value;
45 }
46 }
47 }
48 }
49
ParseRequestHeader(const std::map<std::string,std::string> & requestHeader)50 void HttpCacheRequest::ParseRequestHeader(const std::map<std::string, std::string> &requestHeader)
51 {
52 if (requestHeader.empty()) {
53 return;
54 }
55
56 for (const auto &iterRequest : requestHeader) {
57 std::string key = CommonUtils::ToLower(iterRequest.first);
58 std::string value = iterRequest.second;
59
60 if (key == CACHE_CONTROL) {
61 ParseCacheControl(value);
62 } else if (key == IF_MODIFIED_SINCE) {
63 ifModifiedSince_ = value;
64 } else if (key == IF_NONE_MATCH) {
65 ifNoneMatch_ = value;
66 }
67 }
68 }
69
SetRequestTime(const std::string & requestTime)70 void HttpCacheRequest::SetRequestTime(const std::string &requestTime)
71 {
72 requestTime_ = requestTime;
73 }
74
GetIfModifiedSince() const75 time_t HttpCacheRequest::GetIfModifiedSince() const
76 {
77 if (ifModifiedSince_.empty()) {
78 return INVALID_TIME;
79 }
80 return StrTimeToTimestamp(ifModifiedSince_);
81 }
82
GetRequestTime() const83 time_t HttpCacheRequest::GetRequestTime() const
84 {
85 if (requestTime_.empty()) {
86 return INVALID_TIME;
87 }
88 return StrTimeToTimestamp(requestTime_);
89 }
90
GetMaxAgeSeconds() const91 time_t HttpCacheRequest::GetMaxAgeSeconds() const
92 {
93 if (maxAge_.empty()) {
94 return INVALID_TIME;
95 }
96
97 return std::strtol(maxAge_.c_str(), nullptr, DECIMAL);
98 }
99
GetMaxStaleSeconds() const100 time_t HttpCacheRequest::GetMaxStaleSeconds() const
101 {
102 if (maxStale_.empty()) {
103 return INVALID_TIME;
104 }
105
106 return std::strtol(maxStale_.c_str(), nullptr, DECIMAL);
107 }
108
GetMinFreshSeconds() const109 time_t HttpCacheRequest::GetMinFreshSeconds() const
110 {
111 if (minFresh_.empty()) {
112 return INVALID_TIME;
113 }
114 return std::strtol(minFresh_.c_str(), nullptr, DECIMAL);
115 }
116
IsNoCache() const117 bool HttpCacheRequest::IsNoCache() const
118 {
119 return noCache_;
120 }
121
IsNoStore() const122 bool HttpCacheRequest::IsNoStore() const
123 {
124 return noStore_;
125 }
126
IsNoTransform() const127 bool HttpCacheRequest::IsNoTransform() const
128 {
129 return noTransform_;
130 }
131
IsOnlyIfCached() const132 bool HttpCacheRequest::IsOnlyIfCached() const
133 {
134 return onlyIfCached_;
135 }
136
GetIfNoneMatch() const137 std::string HttpCacheRequest::GetIfNoneMatch() const
138 {
139 return ifNoneMatch_;
140 }
141
HttpCacheRequest()142 HttpCacheRequest::HttpCacheRequest() : noCache_(false), noStore_(false), noTransform_(false), onlyIfCached_(false) {}
143
144 HttpCacheRequest::~HttpCacheRequest() = default;
145
ParseCacheControl(const std::string & cacheControl)146 void HttpCacheResponse::ParseCacheControl(const std::string &cacheControl)
147 {
148 auto vec = CommonUtils::Split(cacheControl, SPLIT);
149
150 for (const auto &str : vec) {
151 if (str == NO_CACHE) {
152 noCache_ = true;
153 } else if (str == NO_STORE) {
154 noStore_ = true;
155 } else if (str == NO_TRANSFORM) {
156 noTransform_ = true;
157 } else if (str == MUST_REVALIDATE) {
158 mustRevalidate_ = true;
159 } else if (str == PUBLIC) {
160 publicCache_ = true;
161 } else if (str == PRIVATE) {
162 privateCache_ = true;
163 } else if (str == PROXY_REVALIDATE) {
164 proxyRevalidate_ = true;
165 }
166 auto pos = str.find('=');
167 if (pos != std::string::npos) {
168 std::string key = str.substr(0, pos);
169 std::string value = str.substr(pos + 1);
170 if (key == MAX_AGE) {
171 maxAge_ = value;
172 } else if (key == S_MAXAGE) {
173 sMaxAge_ = value;
174 }
175 }
176 }
177 }
178
ParseCacheResponseHeader(const std::map<std::string,std::string> & cacheResponseHeader)179 void HttpCacheResponse::ParseCacheResponseHeader(const std::map<std::string, std::string> &cacheResponseHeader)
180 {
181 if (cacheResponseHeader.empty()) {
182 return;
183 }
184 for (const auto &iterCacheResponse : cacheResponseHeader) {
185 std::string key = CommonUtils::ToLower(iterCacheResponse.first);
186 std::string value = iterCacheResponse.second;
187
188 if (key == CACHE_CONTROL) {
189 ParseCacheControl(value);
190 } else if (key == EXPIRES) {
191 expires_ = value;
192 } else if (key == LAST_MODIFIED) {
193 lastModified_ = value;
194 } else if (key == ETAG) {
195 etag_ = value;
196 } else if (key == AGE) {
197 age_ = value;
198 } else if (key == DATE) {
199 date_ = value;
200 }
201 }
202 }
203
GetDate() const204 time_t HttpCacheResponse::GetDate() const
205 {
206 if (date_.empty()) {
207 return INVALID_TIME;
208 }
209
210 return StrTimeToTimestamp(date_);
211 }
212
GetExpires() const213 time_t HttpCacheResponse::GetExpires() const
214 {
215 if (expires_.empty()) {
216 return INVALID_TIME;
217 }
218
219 return StrTimeToTimestamp(expires_);
220 }
221
GetLastModified() const222 time_t HttpCacheResponse::GetLastModified() const
223 {
224 if (lastModified_.empty()) {
225 return INVALID_TIME;
226 }
227
228 return StrTimeToTimestamp(lastModified_);
229 }
230
GetLastModifiedStr() const231 std::string HttpCacheResponse::GetLastModifiedStr() const
232 {
233 return lastModified_;
234 }
235
GetDateStr() const236 std::string HttpCacheResponse::GetDateStr() const
237 {
238 return date_;
239 }
240
GetEtag() const241 std::string HttpCacheResponse::GetEtag() const
242 {
243 return etag_;
244 }
245
GetAge() const246 std::string HttpCacheResponse::GetAge() const
247 {
248 return age_;
249 }
250
GetAgeSeconds() const251 time_t HttpCacheResponse::GetAgeSeconds() const
252 {
253 if (age_.empty()) {
254 return INVALID_TIME;
255 }
256 return std::strtol(age_.c_str(), nullptr, DECIMAL);
257 }
258
GetMaxAgeSeconds() const259 time_t HttpCacheResponse::GetMaxAgeSeconds() const
260 {
261 if (maxAge_.empty()) {
262 return INVALID_TIME;
263 }
264 return std::strtol(maxAge_.c_str(), nullptr, DECIMAL);
265 }
266
GetSMaxAgeSeconds() const267 time_t HttpCacheResponse::GetSMaxAgeSeconds() const
268 {
269 if (sMaxAge_.empty()) {
270 return INVALID_TIME;
271 }
272 return std::strtol(sMaxAge_.c_str(), nullptr, DECIMAL);
273 }
274
GetResponseTime() const275 time_t HttpCacheResponse::GetResponseTime() const
276 {
277 if (responseTime_.empty()) {
278 return INVALID_TIME;
279 }
280 return StrTimeToTimestamp(responseTime_);
281 }
282
IsMustRevalidate() const283 bool HttpCacheResponse::IsMustRevalidate() const
284 {
285 return mustRevalidate_;
286 }
287
IsNoCache() const288 bool HttpCacheResponse::IsNoCache() const
289 {
290 return noCache_;
291 }
292
IsNoStore() const293 bool HttpCacheResponse::IsNoStore() const
294 {
295 return noStore_;
296 }
297
IsPublicCache() const298 bool HttpCacheResponse::IsPublicCache() const
299 {
300 return publicCache_;
301 }
302
IsPrivateCache() const303 bool HttpCacheResponse::IsPrivateCache() const
304 {
305 return privateCache_;
306 }
307
IsProxyRevalidate() const308 bool HttpCacheResponse::IsProxyRevalidate() const
309 {
310 return proxyRevalidate_;
311 }
312
IsNoTransform() const313 bool HttpCacheResponse::IsNoTransform() const
314 {
315 return noTransform_;
316 }
317
GetRespCode() const318 ResponseCode HttpCacheResponse::GetRespCode() const
319 {
320 return respCode_;
321 }
322
SetResponseTime(const std::string & responseTime)323 void HttpCacheResponse::SetResponseTime(const std::string &responseTime)
324 {
325 responseTime_ = responseTime;
326 }
327
SetRespCode(ResponseCode respCode)328 void HttpCacheResponse::SetRespCode(ResponseCode respCode)
329 {
330 respCode_ = respCode;
331 }
332
SetRequestTime(const std::string & requestTime)333 void HttpCacheResponse::SetRequestTime(const std::string &requestTime)
334 {
335 requestTime_ = requestTime;
336 }
337
GetRequestTime() const338 time_t HttpCacheResponse::GetRequestTime() const
339 {
340 if (requestTime_.empty()) {
341 return INVALID_TIME;
342 }
343 return StrTimeToTimestamp(requestTime_);
344 }
345
HttpCacheResponse()346 HttpCacheResponse::HttpCacheResponse()
347 : mustRevalidate_(false),
348 noCache_(false),
349 noStore_(false),
350 publicCache_(false),
351 privateCache_(false),
352 proxyRevalidate_(false),
353 noTransform_(false),
354 respCode_(static_cast<ResponseCode>(0))
355 {
356 }
357
358 HttpCacheResponse::~HttpCacheResponse() = default;
359 } // namespace OHOS::NetStack::Http
360