1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 /*****************************************************************************
25  *
26  *  CacheControl.h - Interface to Cache Control system
27  *
28  *
29  ****************************************************************************/
30 
31 #pragma once
32 
33 #include "P_EventSystem.h"
34 #include "ControlBase.h"
35 #include "tscore/Result.h"
36 
37 struct RequestData;
38 
39 const int CC_UNSET_TIME = -1;
40 
41 #define CACHE_CONTROL_TIMEOUT (HRTIME_HOUR * 1)
42 
43 //   Use 10 second time for purify testing under low
44 //     load to verify memory allocation
45 //#define CACHE_CONTROL_TIMEOUT            (HRTIME_SECOND*10)
46 
47 enum CacheControlType {
48   CC_INVALID = 0,
49   CC_REVALIDATE_AFTER,
50   CC_NEVER_CACHE,
51   CC_STANDARD_CACHE,
52   CC_IGNORE_NO_CACHE,
53   CC_IGNORE_CLIENT_NO_CACHE,
54   CC_IGNORE_SERVER_NO_CACHE,
55   CC_PIN_IN_CACHE,
56   CC_TTL_IN_CACHE,
57   CC_NUM_TYPES
58 };
59 
60 struct matcher_line;
61 
62 class CacheControlResult
63 {
64 public:
65   inkcoreapi CacheControlResult();
66   void Print() const;
67 
68   // Data for external use
69   //
70   //   Describes the cache-control for a specific URL
71   //
72   int revalidate_after;
73   int pin_in_cache_for;
74   int ttl_in_cache;
75   bool never_cache               = false;
76   bool ignore_client_no_cache    = false;
77   bool ignore_server_no_cache    = false;
78   bool ignore_client_cc_max_age  = true;
79   int cache_responses_to_cookies = -1; ///< Override for caching cookied responses.
80 
81   // Data for internal use only
82   //
83   //   Keeps track of the last line number
84   //    on which a parameter was set
85   //   Used to tell if a parameter needs to
86   //    be overridden by something that appeared
87   //    earlier in the the config file
88   //
89   int reval_line         = -1;
90   int never_line         = -1;
91   int pin_line           = -1;
92   int ttl_line           = -1;
93   int ignore_client_line = -1;
94   int ignore_server_line = -1;
95 };
96 
CacheControlResult()97 inline CacheControlResult::CacheControlResult()
98   : revalidate_after(CC_UNSET_TIME), pin_in_cache_for(CC_UNSET_TIME), ttl_in_cache(CC_UNSET_TIME)
99 
100 {
101 }
102 
103 class CacheControlRecord : public ControlBase
104 {
105 public:
106   CacheControlRecord();
107   CacheControlType directive     = CC_INVALID;
108   int time_arg                   = 0;
109   int cache_responses_to_cookies = -1;
110   Result Init(matcher_line *line_info);
111   inkcoreapi void UpdateMatch(CacheControlResult *result, RequestData *rdata);
112   void Print() const;
113 };
114 
CacheControlRecord()115 inline CacheControlRecord::CacheControlRecord() : ControlBase() {}
116 
117 //
118 // API to outside world
119 //
120 class URL;
121 struct HttpConfigParams;
122 struct OverridableHttpConfigParams;
123 
124 inkcoreapi void getCacheControl(CacheControlResult *result, HttpRequestData *rdata, const OverridableHttpConfigParams *h_txn_conf,
125                                 char *tag = nullptr);
126 inkcoreapi bool host_rule_in_CacheControlTable();
127 inkcoreapi bool ip_rule_in_CacheControlTable();
128 
129 void initCacheControl();
130 void reloadCacheControl();
131