]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Notes.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / Notes.cc
CommitLineData
b3404bc5
CT
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"
f4f55a21 31#include "AccessLogEntry.h"
b3404bc5
CT
32#include "acl/FilledChecklist.h"
33#include "acl/Gadgets.h"
34#include "ConfigParser.h"
602d9612 35#include "globals.h"
b3404bc5 36#include "HttpReply.h"
602d9612 37#include "HttpRequest.h"
b3404bc5
CT
38#include "SquidConfig.h"
39#include "Store.h"
cf9f0261 40#include "StrList.h"
b3404bc5
CT
41
42#include <algorithm>
43#include <string>
44
45Note::Value::~Value()
46{
47 aclDestroyAclList(&aclList);
48}
49
50Note::Value::Pointer
51Note::addValue(const String &value)
52{
53 Value::Pointer v = new Value(value);
54 values.push_back(v);
55 return v;
56}
57
58const char *
59Note::match(HttpRequest *request, HttpReply *reply)
60{
61
62 typedef Values::iterator VLI;
63 ACLFilledChecklist ch(NULL, request, NULL);
b248c2a3 64 ch.reply = reply;
b3404bc5 65 if (reply)
b248c2a3 66 HTTPMSGLOCK(ch.reply);
b3404bc5
CT
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
78Note::Pointer
cf9f0261 79Notes::add(const String &noteKey)
b3404bc5 80{
cf9f0261 81 typedef Notes::NotesList::iterator AMLI;
b3404bc5
CT
82 for (AMLI i = notes.begin(); i != notes.end(); ++i) {
83 if ((*i)->key == noteKey)
84 return (*i);
85 }
86
cf9f0261
CT
87 Note::Pointer note = new Note(noteKey);
88 notes.push_back(note);
b3404bc5
CT
89 return note;
90}
91
92Note::Pointer
93Notes::parse(ConfigParser &parser)
94{
2eceb328 95 String key = ConfigParser::NextToken();
bde7a8ce 96 String value = ConfigParser::NextQuotedToken();
b3404bc5
CT
97 Note::Pointer note = add(key);
98 Note::Value::Pointer noteValue = note->addValue(value);
6f58d7d7
AR
99
100 String label(key);
101 label.append('=');
102 label.append(value);
103 aclParseAclList(parser, &noteValue->aclList, label.termedBuf());
b3404bc5
CT
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
118void
119Notes::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
133void
134Notes::clean()
135{
136 notes.clean();
137}
cf9f0261 138
b670cb6a
AR
139NotePairs::~NotePairs()
140{
141 while (!entries.empty())
142 delete entries.pop_back();
143}
144
cf9f0261
CT
145const char *
146NotePairs::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
160const char *
161NotePairs::toString(const char *sep) const
162{
7e6ef752 163 static String value;
cf9f0261
CT
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
174const char *
175NotePairs::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
184void
185NotePairs::add(const char *key, const char *note)
186{
187 entries.push_back(new NotePairs::Entry(key, note));
188}
189
190void
191NotePairs::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;
7e6ef752 197 while (strListGetItem(&strValues, ',', &item, &ilen, &pos)) {
cf9f0261
CT
198 String v;
199 v.append(item, ilen);
200 entries.push_back(new NotePairs::Entry(key, v.termedBuf()));
201 }
202}
203
204bool
205NotePairs::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
214void
215NotePairs::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}
f4f55a21 221
f4f55a21
CT
222NotePairs &
223SyncNotes(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}