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 #ifndef NET_HTTP_CACHE_REQUEST_H
17 #define NET_HTTP_CACHE_REQUEST_H
18 
19 #include <cstring>
20 #include <iostream>
21 #include <map>
22 
23 #include "constant.h"
24 #include "net_http_utils.h"
25 
26 namespace OHOS::NetStack::Http {
27 class HttpCacheRequest {
28 public:
29     HttpCacheRequest();
30 
31     ~HttpCacheRequest();
32 
33     void ParseRequestHeader(const std::map<std::string, std::string> &requestHeader);
34 
35     void SetRequestTime(const std::string &requestTime);
36 
37     [[nodiscard]] time_t GetIfModifiedSince() const;
38 
39     [[nodiscard]] time_t GetRequestTime() const;
40 
41     [[nodiscard]] time_t GetMaxAgeSeconds() const;
42 
43     [[nodiscard]] time_t GetMaxStaleSeconds() const;
44 
45     [[nodiscard]] time_t GetMinFreshSeconds() const;
46 
47     [[nodiscard]] bool IsNoCache() const;
48 
49     [[nodiscard]] bool IsNoStore() const;
50 
51     [[nodiscard]] bool IsNoTransform() const;
52 
53     [[nodiscard]] bool IsOnlyIfCached() const;
54 
55     [[nodiscard]] std::string GetIfNoneMatch() const;
56 
57 private:
58     void ParseCacheControl(const std::string &cacheControl);
59 
60 private:
61     std::string requestTime_;
62     std::string ifModifiedSince_;
63     std::string ifNoneMatch_;
64 
65     std::string maxAge_;
66     std::string maxStale_;
67     std::string minFresh_;
68 
69     bool noCache_ = false;
70     bool noStore_ = false;
71     bool noTransform_ = false;
72     bool onlyIfCached_ = false;
73 };
74 
75 
76 class HttpCacheResponse {
77 public:
78     HttpCacheResponse();
79 
80     ~HttpCacheResponse();
81 
82     void ParseCacheResponseHeader(const std::map<std::string, std::string> &cacheResponseHeader);
83 
84     [[nodiscard]] time_t GetDate() const;
85 
86     [[nodiscard]] time_t GetExpires() const;
87 
88     [[nodiscard]] time_t GetLastModified() const;
89 
90     [[nodiscard]] std::string GetLastModifiedStr() const;
91 
92     [[nodiscard]] std::string GetDateStr() const;
93 
94     [[nodiscard]] std::string GetEtag() const;
95 
96     [[nodiscard]] std::string GetAge() const;
97 
98     [[nodiscard]] time_t GetAgeSeconds() const;
99 
100     [[nodiscard]] time_t GetResponseTime() const;
101 
102     [[nodiscard]] bool IsMustRevalidate() const;
103 
104     [[nodiscard]] bool IsNoCache() const;
105 
106     [[nodiscard]] bool IsNoStore() const;
107 
108     [[nodiscard]] bool IsPublicCache() const;
109 
110     [[nodiscard]] bool IsPrivateCache() const;
111 
112     [[nodiscard]] bool IsProxyRevalidate() const;
113 
114     [[nodiscard]] bool IsNoTransform() const;
115 
116     [[nodiscard]] time_t GetMaxAgeSeconds() const;
117 
118     [[nodiscard]] time_t GetSMaxAgeSeconds() const;
119 
120     [[nodiscard]] ResponseCode GetRespCode() const;
121 
122     void SetRespCode(ResponseCode respCode);
123 
124     void SetResponseTime(const std::string &responseTime);
125 
126     void SetRequestTime(const std::string &requestTime);
127 
128     [[nodiscard]] time_t GetRequestTime() const;
129 
130 private:
131     void ParseCacheControl(const std::string &cacheControl);
132 
133     std::string date_;
134     std::string expires_;
135     std::string lastModified_;
136     std::string etag_;
137     std::string age_;
138     std::string responseTime_;
139     std::string requestTime_;
140 
141     bool mustRevalidate_;
142     bool noCache_;
143     bool noStore_;
144     bool publicCache_;
145     bool privateCache_;
146     bool proxyRevalidate_;
147     bool noTransform_;
148 
149     std::string maxAge_;
150     std::string sMaxAge_;
151 
152     ResponseCode respCode_;
153 };
154 } // namespace OHOS::NetStack::Http
155 #endif // NET_HTTP_CACHE_REQUEST_H
156