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 
24 class HttpRequest;
25 class HttpReply;
26 class AccessLogEntry;
27 class NotePairs;
28 
31 
38 class Note: public RefCountable
39 {
40 public:
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,
86  const Value::Method m = Value::mhAppend);
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 
103 private:
106 };
107 
108 class ConfigParser;
109 
113 class Notes : public RefCountable
114 {
115 public:
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);
153 private:
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 
180 class NotePairs: public RefCountable
181 {
182 public:
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 
268 private:
270 };
271 
272 #endif /* SQUID_SRC_NOTES_H */
273 
void appendNewOnly(const NotePairs *src)
Definition: Notes.cc:391
void replaceOrAdd(const NotePairs *src)
Definition: Notes.cc:410
NotesList::iterator iterator
iterates over the notes list
Definition: Notes.h:119
ACLList * aclList
The access list used to determine if this value is valid for a request.
Definition: Notes.h:60
std::optional< SBuf > find(const char *noteKey, const char *sep=",") const
Definition: Notes.cc:281
SBuf theFormattedValue
The expanded value produced by format(), empty for non-quoted values.
Definition: Notes.h:70
const SBuf & name() const
Definition: Notes.h:199
void parseKvPair()
Parses an annotate line with "key=value" or "key+=value" formats.
Definition: Notes.cc:231
void validateKey(const SBuf &key) const
Definition: Notes.cc:192
std::vector< Note::Pointer > NotesList
Definition: Notes.h:118
const char * findFirst(const char *noteKey) const
Definition: Notes.cc:307
const char * descr
identifies note source in error messages
Definition: Notes.h:169
Method method() const
Definition: Notes.h:57
RefCount< Entry > Pointer
Definition: Notes.h:190
Note & operator=(const Note &)=delete
Entry(const SBuf &aKey, const SBuf &aValue)
Definition: Notes.h:192
Note(const SBuf aKey)
Definition: Notes.h:79
Entry & operator=(const Entry &)=delete
void print(std::ostream &os, const char *nameValueSeparator, const char *entryTerminator) const
Definition: Notes.cc:300
Value(const char *aVal, const bool quoted, const char *descr, const Method method=mhReplace)
Definition: Notes.cc:37
Note::Pointer parse(ConfigParser &parser)
Parses a notes line and returns a pointer to the parsed Note object.
Definition: Notes.cc:210
Values values
The possible values list for the note.
Definition: Notes.h:105
NotesList notes
The Note::Pointer objects array list.
Definition: Notes.h:168
Definition: SBuf.h:93
NotePairs & operator=(NotePairs const &)=delete
SBuf theKey
The note key.
Definition: Notes.h:104
const SBuf & value() const
Definition: Notes.h:58
std::vector< Value::Pointer > Values
Definition: Notes.h:76
bool empty() const
Definition: Notes.h:260
Note(const char *aKey, const size_t keyLen)
Definition: Notes.h:78
std::vector< SBuf > Names
Definition: Notes.h:207
bool empty() const
Definition: Notes.h:145
RefCount< Value > Pointer
Definition: Notes.h:47
Used to store a note key/value pair.
Definition: Notes.h:186
void printAsAnnotationAclParameters(std::ostream &) const
Definition: Notes.cc:126
@ mhAppend
Definition: Notes.h:50
Definition: Notes.h:113
RefCount< Notes > Pointer
Definition: Notes.h:116
@ mhReplace
Definition: Notes.h:50
void updateNotePairs(NotePairsPointer pairs, const CharacterSet *delimiters, const AccessLogEntryPointer &al)
Definition: Notes.cc:256
Note::Pointer find(const SBuf &noteKey)
Definition: Notes.cc:176
std::vector< SBuf > Keys
unordered annotation names
Definition: Notes.h:117
void printAsAnnotationAclParameters(std::ostream &) const
Definition: Notes.cc:270
const SBuf & key() const
Definition: Notes.h:93
Entries entries
The key/value pair entries.
Definition: Notes.h:269
Method theMethod
Definition: Notes.h:74
static const Notes::Keys & ReservedKeys()
always prohibited key names
Definition: Notes.cc:137
void printAsNoteDirective(StoreEntry *, const char *directiveName) const
Prints key and value(s) using a "note" directive format (including directive name).
Definition: Notes.cc:106
Format::Format * valueFormat
Compiled annotation value format.
Definition: Notes.h:67
const Entries & expandListEntries(const CharacterSet *delimiters) const
Definition: Notes.cc:356
iterator end()
points to the end of list
Definition: Notes.h:143
NotePairs()
Definition: Notes.h:209
void addStrList(const SBuf &key, const SBuf &values, const CharacterSet &delimiters)
Definition: Notes.cc:369
void updateNotePairs(NotePairsPointer pairs, const CharacterSet *delimiters, const AccessLogEntryPointer &al)
Definition: Notes.cc:92
Notes()=default
SBuf theValue
Definition: Notes.h:68
~Notes() override
Definition: Notes.h:124
iterator begin()
points to the first argument
Definition: Notes.h:141
Stores a value for the note.
Definition: Notes.h:44
Note::Pointer add(const SBuf &noteKey)
Definition: Notes.cc:167
Value::Pointer addValue(const char *value, const bool quoted, const char *descr, const Value::Method m=Value::mhAppend)
Definition: Notes.cc:63
Entry(const char *aKey, const char *aValue)
Definition: Notes.h:194
Notes & operator=(const Notes &)=delete
void clear()
Definition: Notes.h:262
void append(const NotePairs *src)
Append the entries of the src NotePairs list to our list.
Definition: Notes.cc:384
void printAsNoteDirectives(StoreEntry *, const char *directiveName) const
Prints notes using "note" squid.conf directive format, one directive per stored note.
Definition: Notes.cc:263
Keys reservedKeys
a list of additional prohibited key names
Definition: Notes.h:171
SBuf theName
Definition: Notes.h:203
bool hasPair(const SBuf &key, const SBuf &value) const
Definition: Notes.cc:375
RefCount< NotePairs > NotePairsPointer
Definition: Notes.h:30
const SBuf & value() const
Definition: Notes.h:200
bool match(HttpRequest *request, HttpReply *reply, const AccessLogEntryPointer &al, SBuf &matched)
Definition: Notes.cc:70
void remove(const char *key)
Definition: Notes.cc:328
RefCount< AccessLogEntry > AccessLogEntryPointer
Definition: Notes.h:27
void add(const SBuf &key, const SBuf &value)
Definition: Notes.cc:322
RefCount< NotePairs > Pointer
Definition: Notes.h:183
optimized set of C chars, with quick membership test and merge support
Definition: CharacterSet.h:17
void clean()
clean the notes list
Definition: Notes.h:138
std::vector< Entry::Pointer > Entries
The key/value pair entries.
Definition: Notes.h:206
Definition: Notes.h:38
RefCount< Note > Pointer
Definition: Notes.h:41
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::const_iterator const_iterator
iterates over the notes list
Definition: Notes.h:120
const SBuf & format(const AccessLogEntryPointer &al)
Definition: Notes.cc:50
bool formattedValues
whether to expand quoted logformat codes
Definition: Notes.h:172
SBuf theValue
Definition: Notes.h:204
void replaceOrAddOrAppend(const NotePairs *src, const Names &appendables)
Definition: Notes.cc:400
Value & operator=(const Value &)=delete
~Value() override
Definition: Notes.cc:31

 

Introduction

Documentation

Support

Miscellaneous