]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Notes.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / Notes.h
1 #ifndef SQUID_NOTES_H
2 #define SQUID_NOTES_H
3
4 #include "acl/forward.h"
5 #include "base/RefCount.h"
6 #include "base/Vector.h"
7 #include "CbDataList.h"
8 #include "MemPool.h"
9 #include "SquidString.h"
10 #include "typedefs.h"
11
12 #if HAVE_STRING
13 #include <string>
14 #endif
15
16 class HttpRequest;
17 class HttpReply;
18
19 /**
20 * Used to store a note configuration. The notes are custom key:value
21 * pairs ICAP request headers or ECAP options used to pass
22 * custom transaction-state related meta information to squid
23 * internal subsystems or to adaptation services.
24 */
25 class Note: public RefCountable
26 {
27 public:
28 typedef RefCount<Note> Pointer;
29 /// Stores a value for the note.
30 class Value: public RefCountable
31 {
32 public:
33 typedef RefCount<Value> Pointer;
34 String value; ///< a note value
35 ACLList *aclList; ///< The access list used to determine if this value is valid for a request
36 explicit Value(const String &aVal) : value(aVal), aclList(NULL) {}
37 ~Value();
38 };
39 typedef Vector<Value::Pointer> Values;
40
41 explicit Note(const String &aKey): key(aKey) {}
42
43 /**
44 * Adds a value to the note and returns a pointer to the
45 * related Value object.
46 */
47 Value::Pointer addValue(const String &value);
48
49 /**
50 * Walks through the possible values list of the note and selects
51 * the first value which matches the given HttpRequest and HttpReply
52 * or NULL if none matches.
53 */
54 const char *match(HttpRequest *request, HttpReply *reply);
55
56 String key; ///< The note key
57 Values values; ///< The possible values list for the note
58 };
59
60 class ConfigParser;
61 /**
62 * Used to store a notes configuration list.
63 */
64 class Notes
65 {
66 public:
67 typedef Vector<Note::Pointer> NotesList;
68 typedef NotesList::iterator iterator; ///< iterates over the notes list
69 typedef NotesList::const_iterator const_iterator; ///< iterates over the notes list
70
71 Notes(const char *aDescr, const char **metasBlacklist): descr(aDescr), blacklisted(metasBlacklist) {}
72 Notes(): descr(NULL), blacklisted(NULL) {}
73 ~Notes() { notes.clean(); }
74 /**
75 * Parse a notes line and returns a pointer to the
76 * parsed Note object.
77 */
78 Note::Pointer parse(ConfigParser &parser);
79 /**
80 * Dump the notes list to the given StoreEntry object.
81 */
82 void dump(StoreEntry *entry, const char *name);
83 void clean(); /// clean the notes list
84
85 /// points to the first argument
86 iterator begin() { return notes.begin(); }
87 /// points to the end of list
88 iterator end() { return notes.end(); }
89 /// return true if the notes list is empty
90 bool empty() { return notes.empty(); }
91
92 NotesList notes; ///< The Note::Pointer objects array list
93 const char *descr; ///< A short description for notes list
94 const char **blacklisted; ///< Null terminated list of blacklisted note keys
95
96 private:
97 /**
98 * Adds a note to the notes list and returns a pointer to the
99 * related Note object. If the note key already exists in list,
100 * returns a pointer to the existing object.
101 */
102 Note::Pointer add(const String &noteKey);
103 };
104
105 /**
106 * Used to store list of notes
107 */
108 class NotePairs: public RefCountable
109 {
110 public:
111 typedef RefCount<NotePairs> Pointer;
112
113 /**
114 * Used to store a note key/value pair.
115 */
116 class Entry
117 {
118 public:
119 Entry(const char *aKey, const char *aValue): name(aKey), value(aValue) {}
120 String name;
121 String value;
122 MEMPROXY_CLASS(Entry);
123 };
124
125 NotePairs() {}
126 ~NotePairs();
127
128 /**
129 * Append the entries of the src NotePairs list to our list.
130 */
131 void append(const NotePairs *src);
132
133 /**
134 * Returns a comma separated list of notes with key 'noteKey'.
135 * Use findFirst instead when a unique kv-pair is needed.
136 */
137 const char *find(const char *noteKey) const;
138
139 /**
140 * Returns the first note value for this key or an empty string.
141 */
142 const char *findFirst(const char *noteKey) const;
143
144 /**
145 * Adds a note key and value to the notes list.
146 * If the key name already exists in list, add the given value to its set
147 * of values.
148 */
149 void add(const char *key, const char *value);
150
151 /**
152 * Adds a note key and values strList to the notes list.
153 * If the key name already exists in list, add the new values to its set
154 * of values.
155 */
156 void addStrList(const char *key, const char *values);
157
158 /**
159 * Return true if the key/value pair is already stored
160 */
161 bool hasPair(const char *key, const char *value) const;
162
163 /**
164 * Convert NotePairs list to a string consist of "Key: Value"
165 * entries separated by sep string.
166 */
167 const char *toString(const char *sep = "\r\n") const;
168
169 /**
170 * True if there are not entries in the list
171 */
172 bool empty() const {return entries.empty();}
173
174 Vector<NotePairs::Entry *> entries; ///< The key/value pair entries
175
176 private:
177 NotePairs &operator = (NotePairs const &); // Not implemented
178 NotePairs(NotePairs const &); // Not implemented
179 };
180
181 MEMPROXY_CLASS_INLINE(NotePairs::Entry);
182
183 class AccessLogEntry;
184 /**
185 * Keep in sync HttpRequest and the corresponding AccessLogEntry objects
186 */
187 NotePairs &SyncNotes(AccessLogEntry &ale, HttpRequest &request);
188
189 #endif