1 /*
2  * Copyright (c) 2022 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 "http_cache_response.h"
17 
18 #include "casche_constant.h"
19 #include "netstack_common_utils.h"
20 
21 namespace OHOS::NetStack::Http {
ParseCacheControl(const std::string & cacheControl)22 void HttpCacheResponse::ParseCacheControl(const std::string &cacheControl)
23 {
24     auto vec = CommonUtils::Split(cacheControl, SPLIT);
25 
26     for (const auto &str : vec) {
27         if (str == NO_CACHE) {
28             noCache_ = true;
29         } else if (str == NO_STORE) {
30             noStore_ = true;
31         } else if (str == NO_TRANSFORM) {
32             noTransform_ = true;
33         } else if (str == MUST_REVALIDATE) {
34             mustRevalidate_ = true;
35         } else if (str == PUBLIC) {
36             publicCache_ = true;
37         } else if (str == PRIVATE) {
38             privateCache_ = true;
39         } else if (str == PROXY_REVALIDATE) {
40             proxyRevalidate_ = true;
41         }
42         auto pos = str.find('=');
43         if (pos != std::string::npos) {
44             std::string key = str.substr(0, pos);
45             std::string value = str.substr(pos + 1);
46             if (key == MAX_AGE) {
47                 maxAge_ = value;
48             } else if (key == S_MAXAGE) {
49                 sMaxAge_ = value;
50             }
51         }
52     }
53 }
54 
ParseCacheResponseHeader(const std::map<std::string,std::string> & cacheResponseHeader)55 void HttpCacheResponse::ParseCacheResponseHeader(const std::map<std::string, std::string> &cacheResponseHeader)
56 {
57     if (cacheResponseHeader.empty()) {
58         return;
59     }
60     for (const auto &iterCacheResponse : cacheResponseHeader) {
61         std::string key = CommonUtils::ToLower(iterCacheResponse.first);
62         std::string value = iterCacheResponse.second;
63 
64         if (key == CACHE_CONTROL) {
65             ParseCacheControl(value);
66         } else if (key == EXPIRES) {
67             expires_ = value;
68         } else if (key == LAST_MODIFIED) {
69             lastModified_ = value;
70         } else if (key == ETAG) {
71             etag_ = value;
72         } else if (key == AGE) {
73             age_ = value;
74         } else if (key == DATE) {
75             date_ = value;
76         }
77     }
78 }
79 
GetDate() const80 time_t HttpCacheResponse::GetDate() const
81 {
82     if (date_.empty()) {
83         return INVALID_TIME;
84     }
85 
86     return HttpTime::StrTimeToTimestamp(date_);
87 }
88 
GetExpires() const89 time_t HttpCacheResponse::GetExpires() const
90 {
91     if (expires_.empty()) {
92         return INVALID_TIME;
93     }
94 
95     return HttpTime::StrTimeToTimestamp(expires_);
96 }
97 
GetLastModified() const98 time_t HttpCacheResponse::GetLastModified() const
99 {
100     if (lastModified_.empty()) {
101         return INVALID_TIME;
102     }
103 
104     return HttpTime::StrTimeToTimestamp(lastModified_);
105 }
106 
GetLastModifiedStr() const107 std::string HttpCacheResponse::GetLastModifiedStr() const
108 {
109     return lastModified_;
110 }
111 
GetDateStr() const112 std::string HttpCacheResponse::GetDateStr() const
113 {
114     return date_;
115 }
116 
GetEtag() const117 std::string HttpCacheResponse::GetEtag() const
118 {
119     return etag_;
120 }
121 
GetAge() const122 std::string HttpCacheResponse::GetAge() const
123 {
124     return age_;
125 }
126 
GetAgeSeconds() const127 time_t HttpCacheResponse::GetAgeSeconds() const
128 {
129     if (age_.empty()) {
130         return INVALID_TIME;
131     }
132     return std::strtol(age_.c_str(), nullptr, DECIMAL);
133 }
134 
GetMaxAgeSeconds() const135 time_t HttpCacheResponse::GetMaxAgeSeconds() const
136 {
137     if (maxAge_.empty()) {
138         return INVALID_TIME;
139     }
140     return std::strtol(maxAge_.c_str(), nullptr, DECIMAL);
141 }
142 
GetSMaxAgeSeconds() const143 time_t HttpCacheResponse::GetSMaxAgeSeconds() const
144 {
145     if (sMaxAge_.empty()) {
146         return INVALID_TIME;
147     }
148     return std::strtol(sMaxAge_.c_str(), nullptr, DECIMAL);
149 }
150 
GetResponseTime() const151 time_t HttpCacheResponse::GetResponseTime() const
152 {
153     if (responseTime_.empty()) {
154         return INVALID_TIME;
155     }
156     return HttpTime::StrTimeToTimestamp(responseTime_);
157 }
158 
IsMustRevalidate() const159 bool HttpCacheResponse::IsMustRevalidate() const
160 {
161     return mustRevalidate_;
162 }
163 
IsNoCache() const164 bool HttpCacheResponse::IsNoCache() const
165 {
166     return noCache_;
167 }
168 
IsNoStore() const169 bool HttpCacheResponse::IsNoStore() const
170 {
171     return noStore_;
172 }
173 
IsPublicCache() const174 bool HttpCacheResponse::IsPublicCache() const
175 {
176     return publicCache_;
177 }
178 
IsPrivateCache() const179 bool HttpCacheResponse::IsPrivateCache() const
180 {
181     return privateCache_;
182 }
183 
IsProxyRevalidate() const184 bool HttpCacheResponse::IsProxyRevalidate() const
185 {
186     return proxyRevalidate_;
187 }
188 
IsNoTransform() const189 bool HttpCacheResponse::IsNoTransform() const
190 {
191     return noTransform_;
192 }
193 
GetRespCode() const194 ResponseCode HttpCacheResponse::GetRespCode() const
195 {
196     return respCode_;
197 }
198 
SetResponseTime(const std::string & responseTime)199 void HttpCacheResponse::SetResponseTime(const std::string &responseTime)
200 {
201     responseTime_ = responseTime;
202 }
203 
SetRespCode(ResponseCode respCode)204 void HttpCacheResponse::SetRespCode(ResponseCode respCode)
205 {
206     respCode_ = respCode;
207 }
208 
SetRequestTime(const std::string & requestTime)209 void HttpCacheResponse::SetRequestTime(const std::string &requestTime)
210 {
211     requestTime_ = requestTime;
212 }
213 
GetRequestTime() const214 time_t HttpCacheResponse::GetRequestTime() const
215 {
216     if (requestTime_.empty()) {
217         return INVALID_TIME;
218     }
219     return HttpTime::StrTimeToTimestamp(requestTime_);
220 }
221 
HttpCacheResponse()222 HttpCacheResponse::HttpCacheResponse()
223     : mustRevalidate_(false),
224       noCache_(false),
225       noStore_(false),
226       publicCache_(false),
227       privateCache_(false),
228       proxyRevalidate_(false),
229       noTransform_(false),
230       respCode_(static_cast<ResponseCode>(0))
231 {
232 }
233 
234 HttpCacheResponse::~HttpCacheResponse() = default;
235 } // namespace OHOS::NetStack::Http
236