Notes.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 <string>
21 #include <vector>
22 
23 class HttpRequest;
24 class HttpReply;
25 class AccessLogEntry;
26 class NotePairs;
27 
30 
37 class Note: public RefCountable
38 {
39 public:
41 
43  class Value: public RefCountable
44  {
45  public:
47  friend class Note;
48 
50 
51  Value(const char *aVal, const bool quoted, const char *descr, const Method method = mhReplace);
52  ~Value() override;
53  Value(const Value&) = delete;
54  Value &operator=(const Value&) = delete;
55 
56  Method method() const { return theMethod; }
57  const SBuf &value() const { return theValue; }
58 
60 
61  private:
64  const SBuf &format(const AccessLogEntryPointer &al);
65 
74  };
75  typedef std::vector<Value::Pointer> Values;
76 
77  Note(const char *aKey, const size_t keyLen): theKey(aKey, keyLen) {}
78  explicit Note(const SBuf aKey): theKey(aKey) {}
79  Note(const Note&) = delete;
80  Note &operator=(const Note&) = delete;
81 
84  Value::Pointer addValue(const char *value, const bool quoted, const char *descr,
85  const Value::Method m = Value::mhAppend);
86 
91  bool match(HttpRequest *request, HttpReply *reply, const AccessLogEntryPointer &al, SBuf &matched);
92  const SBuf &key() const { return theKey; }
93  void updateNotePairs(NotePairsPointer pairs, const CharacterSet *delimiters, const AccessLogEntryPointer &al);
94 
96  void printAsNoteDirective(StoreEntry *, const char *directiveName) const;
97 
100  void printAsAnnotationAclParameters(std::ostream &) const;
101 
102 private:
105 };
106 
107 class ConfigParser;
108 
112 class Notes : public RefCountable
113 {
114 public:
116  typedef std::vector<SBuf> Keys;
117  typedef std::vector<Note::Pointer> NotesList;
118  typedef NotesList::iterator iterator;
119  typedef NotesList::const_iterator const_iterator;
120 
121  explicit Notes(const char *aDescr, const Keys *extraReservedKeys = nullptr, bool allowFormatted = true);
122  Notes() = default;
123  ~Notes() override { notes.clear(); }
124  Notes(const Notes&) = delete;
125  Notes &operator=(const Notes&) = delete;
126 
129 
131  void parseKvPair();
132 
134  void printAsNoteDirectives(StoreEntry *, const char *directiveName) const;
135 
137  void clean() { notes.clear(); }
138 
140  iterator begin() { return notes.begin(); }
142  iterator end() { return notes.end(); }
144  bool empty() const { return notes.empty(); }
145 
148  void printAsAnnotationAclParameters(std::ostream &) const;
149 
150  void updateNotePairs(NotePairsPointer pairs, const CharacterSet *delimiters,
151  const AccessLogEntryPointer &al);
152 private:
154  void banReservedKey(const SBuf &key, const Keys &banned) const;
155 
158  void validateKey(const SBuf &key) const;
159 
164  Note::Pointer add(const SBuf &noteKey);
165  Note::Pointer find(const SBuf &noteKey);
166 
168  const char *descr = nullptr;
169 
171  bool formattedValues = false;
172 
173  static const Notes::Keys &ReservedKeys();
174 };
175 
179 class NotePairs: public RefCountable
180 {
181 public:
183 
185  class Entry : public RefCountable
186  {
188  public:
190 
191  Entry(const SBuf &aKey, const SBuf &aValue)
192  : theName(aKey), theValue(aValue) {}
193  Entry(const char *aKey, const char *aValue)
194  : theName(aKey), theValue(aValue) {}
195  Entry(const Entry &) = delete;
196  Entry &operator=(const Entry &) = delete;
197 
198  const SBuf &name() const { return theName; }
199  const SBuf &value() const { return theValue; }
200 
201  private:
204  };
205  typedef std::vector<Entry::Pointer> Entries;
206  typedef std::vector<SBuf> Names;
207 
209  NotePairs &operator=(NotePairs const &) = delete;
210  NotePairs(NotePairs const &) = delete;
211 
213  void append(const NotePairs *src);
214 
218  void replaceOrAddOrAppend(const NotePairs *src, const Names &appendables);
219 
222  void replaceOrAdd(const NotePairs *src);
223 
226  void appendNewOnly(const NotePairs *src);
227 
231  bool find(SBuf &resultNote, 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:386
void replaceOrAdd(const NotePairs *src)
Definition: Notes.cc:405
NotesList::iterator iterator
iterates over the notes list
Definition: Notes.h:118
ACLList * aclList
The access list used to determine if this value is valid for a request.
Definition: Notes.h:59
SBuf theFormattedValue
The expanded value produced by format(), empty for non-quoted values.
Definition: Notes.h:69
const SBuf & name() const
Definition: Notes.h:198
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:117
const char * findFirst(const char *noteKey) const
Definition: Notes.cc:302
const char * descr
identifies note source in error messages
Definition: Notes.h:168
Method method() const
Definition: Notes.h:56
RefCount< Entry > Pointer
Definition: Notes.h:189
Note & operator=(const Note &)=delete
Entry(const SBuf &aKey, const SBuf &aValue)
Definition: Notes.h:191
Note(const SBuf aKey)
Definition: Notes.h:78
Entry & operator=(const Entry &)=delete
void print(std::ostream &os, const char *nameValueSeparator, const char *entryTerminator) const
Definition: Notes.cc:295
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:104
NotesList notes
The Note::Pointer objects array list.
Definition: Notes.h:167
Definition: SBuf.h:93
NotePairs & operator=(NotePairs const &)=delete
SBuf theKey
The note key.
Definition: Notes.h:103
const SBuf & value() const
Definition: Notes.h:57
std::vector< Value::Pointer > Values
Definition: Notes.h:75
bool empty() const
Definition: Notes.h:260
Note(const char *aKey, const size_t keyLen)
Definition: Notes.h:77
std::vector< SBuf > Names
Definition: Notes.h:206
bool empty() const
Definition: Notes.h:144
RefCount< Value > Pointer
Definition: Notes.h:46
Used to store a note key/value pair.
Definition: Notes.h:185
void printAsAnnotationAclParameters(std::ostream &) const
Definition: Notes.cc:126
@ mhAppend
Definition: Notes.h:49
Definition: Notes.h:112
RefCount< Notes > Pointer
Definition: Notes.h:115
@ mhReplace
Definition: Notes.h:49
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:116
void printAsAnnotationAclParameters(std::ostream &) const
Definition: Notes.cc:270
const SBuf & key() const
Definition: Notes.h:92
bool find(SBuf &resultNote, const char *noteKey, const char *sep=",") const
Definition: Notes.cc:281
Entries entries
The key/value pair entries.
Definition: Notes.h:269
Method theMethod
Definition: Notes.h:73
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:66
const Entries & expandListEntries(const CharacterSet *delimiters) const
Definition: Notes.cc:351
iterator end()
points to the end of list
Definition: Notes.h:142
NotePairs()
Definition: Notes.h:208
void addStrList(const SBuf &key, const SBuf &values, const CharacterSet &delimiters)
Definition: Notes.cc:364
void updateNotePairs(NotePairsPointer pairs, const CharacterSet *delimiters, const AccessLogEntryPointer &al)
Definition: Notes.cc:92
Notes()=default
SBuf theValue
Definition: Notes.h:67
~Notes() override
Definition: Notes.h:123
iterator begin()
points to the first argument
Definition: Notes.h:140
Stores a value for the note.
Definition: Notes.h:43
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:193
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:379
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:170
SBuf theName
Definition: Notes.h:202
bool hasPair(const SBuf &key, const SBuf &value) const
Definition: Notes.cc:370
RefCount< NotePairs > NotePairsPointer
Definition: Notes.h:29
const SBuf & value() const
Definition: Notes.h:199
bool match(HttpRequest *request, HttpReply *reply, const AccessLogEntryPointer &al, SBuf &matched)
Definition: Notes.cc:70
void remove(const char *key)
Definition: Notes.cc:323
RefCount< AccessLogEntry > AccessLogEntryPointer
Definition: Notes.h:26
void add(const SBuf &key, const SBuf &value)
Definition: Notes.cc:317
RefCount< NotePairs > Pointer
Definition: Notes.h:182
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:137
std::vector< Entry::Pointer > Entries
The key/value pair entries.
Definition: Notes.h:205
Definition: Notes.h:37
RefCount< Note > Pointer
Definition: Notes.h:40
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:119
const SBuf & format(const AccessLogEntryPointer &al)
Definition: Notes.cc:50
bool formattedValues
whether to expand quoted logformat codes
Definition: Notes.h:171
SBuf theValue
Definition: Notes.h:203
void replaceOrAddOrAppend(const NotePairs *src, const Names &appendables)
Definition: Notes.cc:395
Value & operator=(const Value &)=delete
~Value() override
Definition: Notes.cc:31

 

Introduction

Documentation

Support

Miscellaneous