]> 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 "globals.h"
32 #include "AccessLogEntry.h"
33 #include "acl/FilledChecklist.h"
34 #include "acl/Gadgets.h"
35 #include "ConfigParser.h"
36 #include "HttpRequest.h"
37 #include "HttpReply.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, value;
96 ConfigParser::ParseString(&key);
97 ConfigParser::ParseQuotedString(&value);
98 Note::Pointer note = add(key);
99 Note::Value::Pointer noteValue = note->addValue(value);
100 aclParseAclList(parser, &noteValue->aclList);
101
102 if (blacklisted) {
103 for (int i = 0; blacklisted[i] != NULL; ++i) {
104 if (note->key.caseCmp(blacklisted[i]) == 0) {
105 fatalf("%s:%d: meta key \"%s\" is a reserved %s name",
106 cfg_filename, config_lineno, note->key.termedBuf(),
107 descr ? descr : "");
108 }
109 }
110 }
111
112 return note;
113 }
114
115 void
116 Notes::dump(StoreEntry *entry, const char *key)
117 {
118 typedef Notes::NotesList::iterator AMLI;
119 for (AMLI m = notes.begin(); m != notes.end(); ++m) {
120 typedef Note::Values::iterator VLI;
121 for (VLI v =(*m)->values.begin(); v != (*m)->values.end(); ++v ) {
122 storeAppendPrintf(entry, "%s " SQUIDSTRINGPH " %s",
123 key, SQUIDSTRINGPRINT((*m)->key), ConfigParser::QuoteString((*v)->value));
124 dump_acl_list(entry, (*v)->aclList);
125 storeAppendPrintf(entry, "\n");
126 }
127 }
128 }
129
130 void
131 Notes::clean()
132 {
133 notes.clean();
134 }
135
136 NotePairs::~NotePairs()
137 {
138 while (!entries.empty())
139 delete entries.pop_back();
140 }
141
142 const char *
143 NotePairs::find(const char *noteKey) const
144 {
145 static String value;
146 value.clean();
147 for (Vector<NotePairs::Entry *>::const_iterator i = entries.begin(); i != entries.end(); ++i) {
148 if ((*i)->name.cmp(noteKey) == 0) {
149 if (value.size())
150 value.append(", ");
151 value.append(ConfigParser::QuoteString((*i)->value));
152 }
153 }
154 return value.size() ? value.termedBuf() : NULL;
155 }
156
157 const char *
158 NotePairs::toString(const char *sep) const
159 {
160 static String value;
161 value.clean();
162 for (Vector<NotePairs::Entry *>::const_iterator i = entries.begin(); i != entries.end(); ++i) {
163 value.append((*i)->name);
164 value.append(": ");
165 value.append(ConfigParser::QuoteString((*i)->value));
166 value.append(sep);
167 }
168 return value.size() ? value.termedBuf() : NULL;
169 }
170
171 const char *
172 NotePairs::findFirst(const char *noteKey) const
173 {
174 for (Vector<NotePairs::Entry *>::const_iterator i = entries.begin(); i != entries.end(); ++i) {
175 if ((*i)->name.cmp(noteKey) == 0)
176 return (*i)->value.termedBuf();
177 }
178 return NULL;
179 }
180
181 void
182 NotePairs::add(const char *key, const char *note)
183 {
184 entries.push_back(new NotePairs::Entry(key, note));
185 }
186
187 void
188 NotePairs::addStrList(const char *key, const char *values)
189 {
190 String strValues(values);
191 const char *item;
192 const char *pos = NULL;
193 int ilen = 0;
194 while (strListGetItem(&strValues, ',', &item, &ilen, &pos)) {
195 String v;
196 v.append(item, ilen);
197 entries.push_back(new NotePairs::Entry(key, v.termedBuf()));
198 }
199 }
200
201 bool
202 NotePairs::hasPair(const char *key, const char *value) const
203 {
204 for (Vector<NotePairs::Entry *>::const_iterator i = entries.begin(); i != entries.end(); ++i) {
205 if ((*i)->name.cmp(key) == 0 || (*i)->value.cmp(value) == 0)
206 return true;
207 }
208 return false;
209 }
210
211 void
212 NotePairs::append(const NotePairs *src)
213 {
214 for (Vector<NotePairs::Entry *>::const_iterator i = src->entries.begin(); i != src->entries.end(); ++i) {
215 entries.push_back(new NotePairs::Entry((*i)->name.termedBuf(), (*i)->value.termedBuf()));
216 }
217 }
218
219 NotePairs &
220 SyncNotes(AccessLogEntry &ale, HttpRequest &request)
221 {
222 if (!ale.notes) {
223 assert(!request.notes);
224 ale.notes = request.notes = new NotePairs;
225 } else {
226 assert(ale.notes == request.notes);
227 }
228 return *ale.notes;
229 }