]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Notes.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / Notes.cc
1 /*
2 * SQUID Web Proxy Cache http://www.squid-cache.org/
3 * ----------------------------------------------------------
4 *
5 * Squid is the result of efforts by numerous individuals from
6 * the Internet community; see the CONTRIBUTORS file for full
7 * details. Many organizations have provided support for Squid's
8 * development; see the SPONSORS file for full details. Squid is
9 * Copyrighted (C) 2001 by the Regents of the University of
10 * California; see the COPYRIGHT file for full details. Squid
11 * incorporates software developed and/or copyrighted by other
12 * sources; see the CREDITS file for full details.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
27 *
28 */
29
30 #include "squid.h"
31 #include "AccessLogEntry.h"
32 #include "acl/FilledChecklist.h"
33 #include "acl/Gadgets.h"
34 #include "ConfigParser.h"
35 #include "globals.h"
36 #include "HttpReply.h"
37 #include "HttpRequest.h"
38 #include "SquidConfig.h"
39 #include "Store.h"
40 #include "StrList.h"
41
42 #include <algorithm>
43 #include <string>
44
45 Note::Value::~Value()
46 {
47 aclDestroyAclList(&aclList);
48 }
49
50 Note::Value::Pointer
51 Note::addValue(const String &value)
52 {
53 Value::Pointer v = new Value(value);
54 values.push_back(v);
55 return v;
56 }
57
58 const char *
59 Note::match(HttpRequest *request, HttpReply *reply)
60 {
61
62 typedef Values::iterator VLI;
63 ACLFilledChecklist ch(NULL, request, NULL);
64 ch.reply = reply;
65 if (reply)
66 HTTPMSGLOCK(ch.reply);
67
68 for (VLI i = values.begin(); i != values.end(); ++i ) {
69 const int ret= ch.fastCheck((*i)->aclList);
70 debugs(93, 5, HERE << "Check for header name: " << key << ": " << (*i)->value
71 <<", HttpRequest: " << request << " HttpReply: " << reply << " matched: " << ret);
72 if (ret == ACCESS_ALLOWED)
73 return (*i)->value.termedBuf();
74 }
75 return NULL;
76 }
77
78 Note::Pointer
79 Notes::add(const String &noteKey)
80 {
81 typedef Notes::NotesList::iterator AMLI;
82 for (AMLI i = notes.begin(); i != notes.end(); ++i) {
83 if ((*i)->key == noteKey)
84 return (*i);
85 }
86
87 Note::Pointer note = new Note(noteKey);
88 notes.push_back(note);
89 return note;
90 }
91
92 Note::Pointer
93 Notes::parse(ConfigParser &parser)
94 {
95 String key = ConfigParser::NextToken();
96 String value = ConfigParser::NextQuotedToken();
97 Note::Pointer note = add(key);
98 Note::Value::Pointer noteValue = note->addValue(value);
99
100 String label(key);
101 label.append('=');
102 label.append(value);
103 aclParseAclList(parser, &noteValue->aclList, label.termedBuf());
104
105 if (blacklisted) {
106 for (int i = 0; blacklisted[i] != NULL; ++i) {
107 if (note->key.caseCmp(blacklisted[i]) == 0) {
108 fatalf("%s:%d: meta key \"%s\" is a reserved %s name",
109 cfg_filename, config_lineno, note->key.termedBuf(),
110 descr ? descr : "");
111 }
112 }
113 }
114
115 return note;
116 }
117
118 void
119 Notes::dump(StoreEntry *entry, const char *key)
120 {
121 typedef Notes::NotesList::iterator AMLI;
122 for (AMLI m = notes.begin(); m != notes.end(); ++m) {
123 typedef Note::Values::iterator VLI;
124 for (VLI v =(*m)->values.begin(); v != (*m)->values.end(); ++v ) {
125 storeAppendPrintf(entry, "%s " SQUIDSTRINGPH " %s",
126 key, SQUIDSTRINGPRINT((*m)->key), ConfigParser::QuoteString((*v)->value));
127 dump_acl_list(entry, (*v)->aclList);
128 storeAppendPrintf(entry, "\n");
129 }
130 }
131 }
132
133 void
134 Notes::clean()
135 {
136 notes.clean();
137 }
138
139 NotePairs::~NotePairs()
140 {
141 while (!entries.empty())
142 delete entries.pop_back();
143 }
144
145 const char *
146 NotePairs::find(const char *noteKey) const
147 {
148 static String value;
149 value.clean();
150 for (Vector<NotePairs::Entry *>::const_iterator i = entries.begin(); i != entries.end(); ++i) {
151 if ((*i)->name.cmp(noteKey) == 0) {
152 if (value.size())
153 value.append(", ");
154 value.append(ConfigParser::QuoteString((*i)->value));
155 }
156 }
157 return value.size() ? value.termedBuf() : NULL;
158 }
159
160 const char *
161 NotePairs::toString(const char *sep) const
162 {
163 static String value;
164 value.clean();
165 for (Vector<NotePairs::Entry *>::const_iterator i = entries.begin(); i != entries.end(); ++i) {
166 value.append((*i)->name);
167 value.append(": ");
168 value.append(ConfigParser::QuoteString((*i)->value));
169 value.append(sep);
170 }
171 return value.size() ? value.termedBuf() : NULL;
172 }
173
174 const char *
175 NotePairs::findFirst(const char *noteKey) const
176 {
177 for (Vector<NotePairs::Entry *>::const_iterator i = entries.begin(); i != entries.end(); ++i) {
178 if ((*i)->name.cmp(noteKey) == 0)
179 return (*i)->value.termedBuf();
180 }
181 return NULL;
182 }
183
184 void
185 NotePairs::add(const char *key, const char *note)
186 {
187 entries.push_back(new NotePairs::Entry(key, note));
188 }
189
190 void
191 NotePairs::addStrList(const char *key, const char *values)
192 {
193 String strValues(values);
194 const char *item;
195 const char *pos = NULL;
196 int ilen = 0;
197 while (strListGetItem(&strValues, ',', &item, &ilen, &pos)) {
198 String v;
199 v.append(item, ilen);
200 entries.push_back(new NotePairs::Entry(key, v.termedBuf()));
201 }
202 }
203
204 bool
205 NotePairs::hasPair(const char *key, const char *value) const
206 {
207 for (Vector<NotePairs::Entry *>::const_iterator i = entries.begin(); i != entries.end(); ++i) {
208 if ((*i)->name.cmp(key) == 0 || (*i)->value.cmp(value) == 0)
209 return true;
210 }
211 return false;
212 }
213
214 void
215 NotePairs::append(const NotePairs *src)
216 {
217 for (Vector<NotePairs::Entry *>::const_iterator i = src->entries.begin(); i != src->entries.end(); ++i) {
218 entries.push_back(new NotePairs::Entry((*i)->name.termedBuf(), (*i)->value.termedBuf()));
219 }
220 }
221
222 NotePairs &
223 SyncNotes(AccessLogEntry &ale, HttpRequest &request)
224 {
225 if (!ale.notes) {
226 assert(!request.notes);
227 ale.notes = request.notes = new NotePairs;
228 } else {
229 assert(ale.notes == request.notes);
230 }
231 return *ale.notes;
232 }