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_request.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 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 HttpTime::StrTimeToTimestamp(ifModifiedSince_);
81 }
82 
GetRequestTime() const83 time_t HttpCacheRequest::GetRequestTime() const
84 {
85     if (requestTime_.empty()) {
86         return INVALID_TIME;
87     }
88     return HttpTime::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 } // namespace OHOS::NetStack::Http
146