Squid Web Cache v8/master
Loading...
Searching...
No Matches
Notes.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#ifndef SQUID_SRC_NOTES_H
10#define SQUID_SRC_NOTES_H
11
12#include "acl/forward.h"
13#include "base/RefCount.h"
14#include "format/Format.h"
15#include "mem/forward.h"
16#include "sbuf/forward.h"
17#include "SquidString.h"
18
19#include <iosfwd>
20#include <optional>
21#include <string>
22#include <vector>
23
24class HttpRequest;
25class HttpReply;
26class AccessLogEntry;
27class NotePairs;
28
31
38class Note: public RefCountable
39{
40public:
42
44 class Value: public RefCountable
45 {
46 public:
48 friend class Note;
49
51
52 Value(const char *aVal, const bool quoted, const char *descr, const Method method = mhReplace);
53 ~Value() override;
54 Value(const Value&) = delete;
55 Value &operator=(const Value&) = delete;
56
57 Method method() const { return theMethod; }
58 const SBuf &value() const { return theValue; }
59
61
62 private:
65 const SBuf &format(const AccessLogEntryPointer &al);
66
75 };
76 typedef std::vector<Value::Pointer> Values;
77
78 Note(const char *aKey, const size_t keyLen): theKey(aKey, keyLen) {}
79 explicit Note(const SBuf aKey): theKey(aKey) {}
80 Note(const Note&) = delete;
81 Note &operator=(const Note&) = delete;
82
85 Value::Pointer addValue(const char *value, const bool quoted, const char *descr,
87
92 bool match(HttpRequest *request, HttpReply *reply, const AccessLogEntryPointer &al, SBuf &matched);
93 const SBuf &key() const { return theKey; }
94 void updateNotePairs(NotePairsPointer pairs, const CharacterSet *delimiters, const AccessLogEntryPointer &al);
95
97 void printAsNoteDirective(StoreEntry *, const char *directiveName) const;
98
101 void printAsAnnotationAclParameters(std::ostream &) const;
102
103private:
106};
107
108class ConfigParser;
109
113class Notes : public RefCountable
114{
115public:
117 typedef std::vector<SBuf> Keys;
118 typedef std::vector<Note::Pointer> NotesList;
119 typedef NotesList::iterator iterator;
120 typedef NotesList::const_iterator const_iterator;
121
122 explicit Notes(const char *aDescr, const Keys *extraReservedKeys = nullptr, bool allowFormatted = true);
123 Notes() = default;
124 ~Notes() override { notes.clear(); }
125 Notes(const Notes&) = delete;
126 Notes &operator=(const Notes&) = delete;
127
130
132 void parseKvPair();
133
135 void printAsNoteDirectives(StoreEntry *, const char *directiveName) const;
136
138 void clean() { notes.clear(); }
139
141 iterator begin() { return notes.begin(); }
143 iterator end() { return notes.end(); }
145 bool empty() const { return notes.empty(); }
146
149 void printAsAnnotationAclParameters(std::ostream &) const;
150
151 void updateNotePairs(NotePairsPointer pairs, const CharacterSet *delimiters,
152 const AccessLogEntryPointer &al);
153private:
155 void banReservedKey(const SBuf &key, const Keys &banned) const;
156
159 void validateKey(const SBuf &key) const;
160
165 Note::Pointer add(const SBuf &noteKey);
166 Note::Pointer find(const SBuf &noteKey);
167
169 const char *descr = nullptr;
170
172 bool formattedValues = false;
173
174 static const Notes::Keys &ReservedKeys();
175};
176
181{
182public:
184
186 class Entry : public RefCountable
187 {
189 public:
191
192 Entry(const SBuf &aKey, const SBuf &aValue)
193 : theName(aKey), theValue(aValue) {}
194 Entry(const char *aKey, const char *aValue)
195 : theName(aKey), theValue(aValue) {}
196 Entry(const Entry &) = delete;
197 Entry &operator=(const Entry &) = delete;
198
199 const SBuf &name() const { return theName; }
200 const SBuf &value() const { return theValue; }
201
202 private:
205 };
206 typedef std::vector<Entry::Pointer> Entries;
207 typedef std::vector<SBuf> Names;
208
210 NotePairs &operator=(NotePairs const &) = delete;
211 NotePairs(NotePairs const &) = delete;
212
214 void append(const NotePairs *src);
215
219 void replaceOrAddOrAppend(const NotePairs *src, const Names &appendables);
220
223 void replaceOrAdd(const NotePairs *src);
224
227 void appendNewOnly(const NotePairs *src);
228
231 std::optional<SBuf> find(const char *noteKey, const char *sep = ",") const;
232
234 const char *findFirst(const char *noteKey) const;
235
239 void add(const SBuf &key, const SBuf &value);
240 void add(const char *key, const char *value);
241
244 void remove(const char *key);
245 void remove(const SBuf &key);
246
250 void addStrList(const SBuf &key, const SBuf &values, const CharacterSet &delimiters);
251
253 bool hasPair(const SBuf &key, const SBuf &value) const;
254
257 void print(std::ostream &os, const char *nameValueSeparator, const char *entryTerminator) const;
258
260 bool empty() const {return entries.empty();}
261
262 void clear() { entries.clear(); }
263
266 const Entries &expandListEntries(const CharacterSet *delimiters) const;
267
268private:
270};
271
272#endif /* SQUID_SRC_NOTES_H */
273
#define RefCountable
The locking interface for use on Reference-Counted classes.
Definition Lock.h:66
RefCount< NotePairs > NotePairsPointer
Definition Notes.h:30
RefCount< AccessLogEntry > AccessLogEntryPointer
Definition Notes.h:29
optimized set of C chars, with quick membership test and merge support
Used to store a note key/value pair.
Definition Notes.h:187
Entry(const char *aKey, const char *aValue)
Definition Notes.h:194
const SBuf & value() const
Definition Notes.h:200
Entry(const SBuf &aKey, const SBuf &aValue)
Definition Notes.h:192
Entry & operator=(const Entry &)=delete
const SBuf & name() const
Definition Notes.h:199
RefCount< Entry > Pointer
Definition Notes.h:190
Entry(const Entry &)=delete
void appendNewOnly(const NotePairs *src)
Definition Notes.cc:391
void append(const NotePairs *src)
Append the entries of the src NotePairs list to our list.
Definition Notes.cc:384
bool empty() const
Definition Notes.h:260
void clear()
Definition Notes.h:262
std::vector< SBuf > Names
Definition Notes.h:207
std::vector< Entry::Pointer > Entries
The key/value pair entries.
Definition Notes.h:206
NotePairs(NotePairs const &)=delete
std::optional< SBuf > find(const char *noteKey, const char *sep=",") const
Definition Notes.cc:281
void add(const SBuf &key, const SBuf &value)
Definition Notes.cc:322
void print(std::ostream &os, const char *nameValueSeparator, const char *entryTerminator) const
Definition Notes.cc:300
void remove(const char *key)
Definition Notes.cc:328
void replaceOrAdd(const NotePairs *src)
Definition Notes.cc:410
RefCount< NotePairs > Pointer
Definition Notes.h:183
bool hasPair(const SBuf &key, const SBuf &value) const
Definition Notes.cc:375
const Entries & expandListEntries(const CharacterSet *delimiters) const
Definition Notes.cc:356
const char * findFirst(const char *noteKey) const
Definition Notes.cc:307
void addStrList(const SBuf &key, const SBuf &values, const CharacterSet &delimiters)
Definition Notes.cc:369
Entries entries
The key/value pair entries.
Definition Notes.h:269
NotePairs()
Definition Notes.h:209
NotePairs & operator=(NotePairs const &)=delete
void replaceOrAddOrAppend(const NotePairs *src, const Names &appendables)
Definition Notes.cc:400
Stores a value for the note.
Definition Notes.h:45
RefCount< Value > Pointer
Definition Notes.h:47
Method method() const
Definition Notes.h:57
@ mhReplace
Definition Notes.h:50
@ mhAppend
Definition Notes.h:50
SBuf theFormattedValue
The expanded value produced by format(), empty for non-quoted values.
Definition Notes.h:70
~Value() override
Definition Notes.cc:31
SBuf theValue
Definition Notes.h:68
Format::Format * valueFormat
Compiled annotation value format.
Definition Notes.h:67
Value(const Value &)=delete
ACLList * aclList
The access list used to determine if this value is valid for a request.
Definition Notes.h:60
const SBuf & value() const
Definition Notes.h:58
const SBuf & format(const AccessLogEntryPointer &al)
Definition Notes.cc:50
Value & operator=(const Value &)=delete
Method theMethod
Definition Notes.h:74
Definition Notes.h:39
Value::Pointer addValue(const char *value, const bool quoted, const char *descr, const Value::Method m=Value::mhAppend)
Definition Notes.cc:63
Note(const char *aKey, const size_t keyLen)
Definition Notes.h:78
bool match(HttpRequest *request, HttpReply *reply, const AccessLogEntryPointer &al, SBuf &matched)
Definition Notes.cc:70
Values values
The possible values list for the note.
Definition Notes.h:105
Note(const SBuf aKey)
Definition Notes.h:79
Note(const Note &)=delete
void printAsNoteDirective(StoreEntry *, const char *directiveName) const
Prints key and value(s) using a "note" directive format (including directive name).
Definition Notes.cc:106
SBuf theKey
The note key.
Definition Notes.h:104
void printAsAnnotationAclParameters(std::ostream &) const
Definition Notes.cc:126
Note & operator=(const Note &)=delete
const SBuf & key() const
Definition Notes.h:93
std::vector< Value::Pointer > Values
Definition Notes.h:76
RefCount< Note > Pointer
Definition Notes.h:41
void updateNotePairs(NotePairsPointer pairs, const CharacterSet *delimiters, const AccessLogEntryPointer &al)
Definition Notes.cc:92
Definition Notes.h:114
Note::Pointer find(const SBuf &noteKey)
Definition Notes.cc:176
NotesList::const_iterator const_iterator
iterates over the notes list
Definition Notes.h:120
Note::Pointer parse(ConfigParser &parser)
Parses a notes line and returns a pointer to the parsed Note object.
Definition Notes.cc:210
void clean()
clean the notes list
Definition Notes.h:138
Notes()=default
bool empty() const
Definition Notes.h:145
std::vector< SBuf > Keys
unordered annotation names
Definition Notes.h:117
Note::Pointer add(const SBuf &noteKey)
Definition Notes.cc:167
iterator begin()
points to the first argument
Definition Notes.h:141
void updateNotePairs(NotePairsPointer pairs, const CharacterSet *delimiters, const AccessLogEntryPointer &al)
Definition Notes.cc:256
const char * descr
identifies note source in error messages
Definition Notes.h:169
void printAsNoteDirectives(StoreEntry *, const char *directiveName) const
Prints notes using "note" squid.conf directive format, one directive per stored note.
Definition Notes.cc:263
iterator end()
points to the end of list
Definition Notes.h:143
Notes & operator=(const Notes &)=delete
std::vector< Note::Pointer > NotesList
Definition Notes.h:118
NotesList::iterator iterator
iterates over the notes list
Definition Notes.h:119
void banReservedKey(const SBuf &key, const Keys &banned) const
Makes sure the given key is not on the given list of banned names.
Definition Notes.cc:185
NotesList notes
The Note::Pointer objects array list.
Definition Notes.h:168
bool formattedValues
whether to expand quoted logformat codes
Definition Notes.h:172
static const Notes::Keys & ReservedKeys()
always prohibited key names
Definition Notes.cc:137
~Notes() override
Definition Notes.h:124
Notes(const Notes &)=delete
void printAsAnnotationAclParameters(std::ostream &) const
Definition Notes.cc:270
RefCount< Notes > Pointer
Definition Notes.h:116
void validateKey(const SBuf &key) const
Definition Notes.cc:192
void parseKvPair()
Parses an annotate line with "key=value" or "key+=value" formats.
Definition Notes.cc:231
Keys reservedKeys
a list of additional prohibited key names
Definition Notes.h:171
Definition SBuf.h:94