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_STRATEGY_H
17 #define NET_HTTP_CACHE_STRATEGY_H
18 
19 #include <cstring>
20 #include <iostream>
21 #include <tuple>
22 
23 #include "net_http_cache_entity.h"
24 #include "net_http_request.h"
25 #include "net_http_response.h"
26 
27 namespace OHOS::NetStack::Http {
28 enum CacheStatus {
29     FRESH,
30     STALE,
31     DENY
32 };
33 
34 class HttpCacheStrategy {
35 public:
36     HttpCacheStrategy() = delete;
37 
38     explicit HttpCacheStrategy(HttpRequest &requestOptions);
39 
40     CacheStatus RunStrategy(HttpResponse &response);
41 
42     bool IsCacheable(const HttpResponse &response);
43 
44     bool CouldUseCache();
45 
46 private:
47     CacheStatus RunStrategyInternal(HttpResponse &response);
48 
49     bool IsCacheable(const HttpCacheResponse &cacheResponse);
50 
51     int64_t CacheResponseAgeMillis();
52 
53     std::tuple<int64_t, int64_t, int64_t, int64_t> GetFreshness();
54 
55     int64_t ComputeFreshnessLifetimeMillis();
56 
57     int64_t ComputeFreshnessLifetimeSecondsInternal();
58 
59     void UpdateRequestHeader(const std::string &etag, const std::string &lastModified, const std::string &date);
60 
61     HttpRequest &requestOptions_;
62 
63     HttpCacheResponse cacheResponse_;
64 
65     HttpCacheRequest cacheRequest_;
66 };
67 } // namespace OHOS::NetStack::Http
68 #endif /* NET_HTTP_CACHE_STRATEGY_H */
69