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 #ifndef HTTP_CACHE_RESPONSE_H
17 #define HTTP_CACHE_RESPONSE_H
18 
19 #include <cstring>
20 #include <iostream>
21 #include <map>
22 
23 #include "constant.h"
24 #include "http_time.h"
25 
26 namespace OHOS::NetStack::Http {
27 class HttpCacheResponse {
28 public:
29     HttpCacheResponse();
30 
31     ~HttpCacheResponse();
32 
33     void ParseCacheResponseHeader(const std::map<std::string, std::string> &cacheResponseHeader);
34 
35     [[nodiscard]] time_t GetDate() const;
36 
37     [[nodiscard]] time_t GetExpires() const;
38 
39     [[nodiscard]] time_t GetLastModified() const;
40 
41     [[nodiscard]] std::string GetLastModifiedStr() const;
42 
43     [[nodiscard]] std::string GetDateStr() const;
44 
45     [[nodiscard]] std::string GetEtag() const;
46 
47     [[nodiscard]] std::string GetAge() const;
48 
49     [[nodiscard]] time_t GetAgeSeconds() const;
50 
51     [[nodiscard]] time_t GetResponseTime() const;
52 
53     [[nodiscard]] bool IsMustRevalidate() const;
54 
55     [[nodiscard]] bool IsNoCache() const;
56 
57     [[nodiscard]] bool IsNoStore() const;
58 
59     [[nodiscard]] bool IsPublicCache() const;
60 
61     [[nodiscard]] bool IsPrivateCache() const;
62 
63     [[nodiscard]] bool IsProxyRevalidate() const;
64 
65     [[nodiscard]] bool IsNoTransform() const;
66 
67     [[nodiscard]] time_t GetMaxAgeSeconds() const;
68 
69     [[nodiscard]] time_t GetSMaxAgeSeconds() const;
70 
71     [[nodiscard]] ResponseCode GetRespCode() const;
72 
73     void SetRespCode(ResponseCode respCode);
74 
75     void SetResponseTime(const std::string &responseTime);
76 
77     void SetRequestTime(const std::string &requestTime);
78 
79     [[nodiscard]] time_t GetRequestTime() const;
80 
81 private:
82     void ParseCacheControl(const std::string &cacheControl);
83 
84     std::string date_;
85     std::string expires_;
86     std::string lastModified_;
87     std::string etag_;
88     std::string age_;
89     std::string responseTime_;
90     std::string requestTime_;
91 
92     bool mustRevalidate_;
93     bool noCache_;
94     bool noStore_;
95     bool publicCache_;
96     bool privateCache_;
97     bool proxyRevalidate_;
98     bool noTransform_;
99 
100     std::string maxAge_;
101     std::string sMaxAge_;
102 
103     ResponseCode respCode_;
104 };
105 } // namespace OHOS::NetStack::Http
106 #endif /* HTTP_CACHE_RESPONSE_H */
107