]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Notes.h
Cleanup: use SBuf::npos instead of npos in SBuf::append()
[thirdparty/squid.git] / src / Notes.h
CommitLineData
bbc27441
AJ
1/*
2 * Copyright (C) 1996-2014 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
b3404bc5
CT
9#ifndef SQUID_NOTES_H
10#define SQUID_NOTES_H
11
6f58d7d7 12#include "acl/forward.h"
cf9f0261
CT
13#include "base/RefCount.h"
14#include "CbDataList.h"
af0ded40 15#include "format/Format.h"
cf9f0261
CT
16#include "MemPool.h"
17#include "SquidString.h"
b3404bc5
CT
18#include "typedefs.h"
19
b3404bc5 20#include <string>
81481ec0 21#include <vector>
b3404bc5 22
b3404bc5
CT
23class HttpRequest;
24class HttpReply;
af0ded40 25typedef RefCount<AccessLogEntry> AccessLogEntryPointer;
b3404bc5
CT
26
27/**
cf9f0261
CT
28 * Used to store a note configuration. The notes are custom key:value
29 * pairs ICAP request headers or ECAP options used to pass
b3404bc5 30 * custom transaction-state related meta information to squid
cf9f0261 31 * internal subsystems or to adaptation services.
b3404bc5
CT
32 */
33class Note: public RefCountable
34{
35public:
36 typedef RefCount<Note> Pointer;
37 /// Stores a value for the note.
38 class Value: public RefCountable
39 {
40 public:
41 typedef RefCount<Value> Pointer;
af0ded40 42 String value; ///< Configured annotation value, possibly with %macros
b3404bc5 43 ACLList *aclList; ///< The access list used to determine if this value is valid for a request
af0ded40
CT
44 /// Compiled annotation value format
45 Format::Format *valueFormat;
46 explicit Value(const String &aVal) : value(aVal), aclList(NULL), valueFormat(NULL) {}
b3404bc5
CT
47 ~Value();
48 };
81481ec0 49 typedef std::vector<Value::Pointer> Values;
b3404bc5
CT
50
51 explicit Note(const String &aKey): key(aKey) {}
52
53 /**
54 * Adds a value to the note and returns a pointer to the
55 * related Value object.
56 */
57 Value::Pointer addValue(const String &value);
58
59 /**
60 * Walks through the possible values list of the note and selects
61 * the first value which matches the given HttpRequest and HttpReply
62 * or NULL if none matches.
af0ded40
CT
63 * If an AccessLogEntry given and Value::valueFormat is not null, the
64 * formatted value returned.
b3404bc5 65 */
af0ded40 66 const char *match(HttpRequest *request, HttpReply *reply, const AccessLogEntryPointer &al);
fd7f26ea 67
b3404bc5
CT
68 String key; ///< The note key
69 Values values; ///< The possible values list for the note
70};
71
72class ConfigParser;
73/**
cf9f0261 74 * Used to store a notes configuration list.
b3404bc5 75 */
ffb82151
A
76class Notes
77{
b3404bc5 78public:
81481ec0 79 typedef std::vector<Note::Pointer> NotesList;
b3404bc5 80 typedef NotesList::iterator iterator; ///< iterates over the notes list
bc600349 81 typedef NotesList::const_iterator const_iterator; ///< iterates over the notes list
b3404bc5 82
af0ded40 83 Notes(const char *aDescr, const char **metasBlacklist, bool allowFormatted = false): descr(aDescr), blacklisted(metasBlacklist), formattedValues(allowFormatted) {}
b3404bc5 84 Notes(): descr(NULL), blacklisted(NULL) {}
c33a88ca 85 ~Notes() { notes.clear(); }
b3404bc5
CT
86 /**
87 * Parse a notes line and returns a pointer to the
88 * parsed Note object.
89 */
90 Note::Pointer parse(ConfigParser &parser);
91 /**
92 * Dump the notes list to the given StoreEntry object.
93 */
94 void dump(StoreEntry *entry, const char *name);
95 void clean(); /// clean the notes list
96
97 /// points to the first argument
98 iterator begin() { return notes.begin(); }
99 /// points to the end of list
100 iterator end() { return notes.end(); }
101 /// return true if the notes list is empty
102 bool empty() { return notes.empty(); }
103
104 NotesList notes; ///< The Note::Pointer objects array list
105 const char *descr; ///< A short description for notes list
106 const char **blacklisted; ///< Null terminated list of blacklisted note keys
af0ded40 107 bool formattedValues; ///< Whether the formatted values are supported
fd7f26ea 108
b3404bc5
CT
109private:
110 /**
111 * Adds a note to the notes list and returns a pointer to the
112 * related Note object. If the note key already exists in list,
113 * returns a pointer to the existing object.
114 */
115 Note::Pointer add(const String &noteKey);
116};
117
cf9f0261
CT
118/**
119 * Used to store list of notes
120 */
121class NotePairs: public RefCountable
ffb82151 122{
b3404bc5 123public:
cf9f0261
CT
124 typedef RefCount<NotePairs> Pointer;
125
126 /**
127 * Used to store a note key/value pair.
128 */
7e6ef752
A
129 class Entry
130 {
cf9f0261 131 public:
7e6ef752 132 Entry(const char *aKey, const char *aValue): name(aKey), value(aValue) {}
cf9f0261
CT
133 String name;
134 String value;
135 MEMPROXY_CLASS(Entry);
136 };
137
d74740bd 138 NotePairs() {}
b670cb6a 139 ~NotePairs();
cf9f0261
CT
140
141 /**
142 * Append the entries of the src NotePairs list to our list.
143 */
144 void append(const NotePairs *src);
145
457857fe
CT
146 /**
147 * Replace existing list entries with the src NotePairs entries.
148 * Entries which do not exist in the destination set are added.
149 */
150 void replaceOrAdd(const NotePairs *src);
151
71e7400c
AJ
152 /**
153 * Append any new entries of the src NotePairs list to our list.
154 * Entries which already exist in the destination set are ignored.
155 */
156 void appendNewOnly(const NotePairs *src);
157
cf9f0261
CT
158 /**
159 * Returns a comma separated list of notes with key 'noteKey'.
160 * Use findFirst instead when a unique kv-pair is needed.
161 */
c7bcf010 162 const char *find(const char *noteKey, const char *sep = ",") const;
cf9f0261
CT
163
164 /**
165 * Returns the first note value for this key or an empty string.
166 */
167 const char *findFirst(const char *noteKey) const;
168
169 /**
170 * Adds a note key and value to the notes list.
171 * If the key name already exists in list, add the given value to its set
172 * of values.
173 */
174 void add(const char *key, const char *value);
175
457857fe
CT
176 /**
177 * Remove all notes with a given key.
178 */
179 void remove(const char *key);
180
cf9f0261
CT
181 /**
182 * Adds a note key and values strList to the notes list.
183 * If the key name already exists in list, add the new values to its set
184 * of values.
185 */
186 void addStrList(const char *key, const char *values);
187
188 /**
189 * Return true if the key/value pair is already stored
190 */
191 bool hasPair(const char *key, const char *value) const;
192
193 /**
194 * Convert NotePairs list to a string consist of "Key: Value"
195 * entries separated by sep string.
196 */
197 const char *toString(const char *sep = "\r\n") const;
198
199 /**
200 * True if there are not entries in the list
201 */
202 bool empty() const {return entries.empty();}
203
81481ec0 204 std::vector<NotePairs::Entry *> entries; ///< The key/value pair entries
95c0dbfc
CT
205
206private:
207 NotePairs &operator = (NotePairs const &); // Not implemented
208 NotePairs(NotePairs const &); // Not implemented
b3404bc5
CT
209};
210
cf9f0261
CT
211MEMPROXY_CLASS_INLINE(NotePairs::Entry);
212
f4f55a21
CT
213class AccessLogEntry;
214/**
215 * Keep in sync HttpRequest and the corresponding AccessLogEntry objects
216 */
217NotePairs &SyncNotes(AccessLogEntry &ale, HttpRequest &request);
218
457857fe
CT
219class ConnStateData;
220/**
221 * Updates ConnStateData ids and HttpRequest notes from helpers received notes.
222 */
223void UpdateRequestNotes(ConnStateData *csd, HttpRequest &request, NotePairs const &notes);
b3404bc5 224#endif