]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Notes.cc
Merge mime.cc refactoring work
[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 "acl/FilledChecklist.h"
33 #include "acl/Gadgets.h"
34 #include "ConfigParser.h"
35 #include "HttpRequest.h"
36 #include "HttpReply.h"
37 #include "SquidConfig.h"
38 #include "Store.h"
39
40 #include <algorithm>
41 #include <string>
42
43 Note::Value::~Value()
44 {
45 aclDestroyAclList(&aclList);
46 }
47
48 Note::Value::Pointer
49 Note::addValue(const String &value)
50 {
51 Value::Pointer v = new Value(value);
52 values.push_back(v);
53 return v;
54 }
55
56 const char *
57 Note::match(HttpRequest *request, HttpReply *reply)
58 {
59
60 typedef Values::iterator VLI;
61 ACLFilledChecklist ch(NULL, request, NULL);
62 ch.reply = reply;
63 if (reply)
64 HTTPMSGLOCK(ch.reply);
65
66 for (VLI i = values.begin(); i != values.end(); ++i ) {
67 const int ret= ch.fastCheck((*i)->aclList);
68 debugs(93, 5, HERE << "Check for header name: " << key << ": " << (*i)->value
69 <<", HttpRequest: " << request << " HttpReply: " << reply << " matched: " << ret);
70 if (ret == ACCESS_ALLOWED)
71 return (*i)->value.termedBuf();
72 }
73 return NULL;
74 }
75
76 Note::Pointer
77 Notes::find(const String &noteKey) const
78 {
79 typedef Notes::NotesList::const_iterator AMLI;
80 for (AMLI i = notes.begin(); i != notes.end(); ++i) {
81 if ((*i)->key == noteKey)
82 return (*i);
83 }
84
85 return Note::Pointer();
86 }
87
88 void
89 Notes::add(const String &noteKey, const String &noteValue)
90 {
91 Note::Pointer key = add(noteKey);
92 key->addValue(noteValue);
93 }
94
95 Note::Pointer
96 Notes::add(const String &noteKey)
97 {
98 Note::Pointer note = find(noteKey);
99 if (note == NULL) {
100 note = new Note(noteKey);
101 notes.push_back(note);
102 }
103 return note;
104 }
105
106 void
107 Notes::add(const Notes &src)
108 {
109 typedef Notes::NotesList::const_iterator AMLI;
110 typedef Note::Values::iterator VLI;
111
112 for (AMLI i = src.notes.begin(); i != src.notes.end(); ++i) {
113
114 // ensure we have a key by that name to fill out values for...
115 // NP: not sharing pointers at the key level since merging other helpers
116 // details later would affect this src objects keys, which is a bad idea.
117 Note::Pointer ourKey = add((*i)->key);
118
119 // known key names, merge the values lists...
120 for (VLI v = (*i)->values.begin(); v != (*i)->values.end(); ++v ) {
121 // 2012-11-29: values are read-only and Pointer can safely be shared
122 // for now we share pointers to save memory and gain speed.
123 // If that ever ceases to be true, convert this to a full copy.
124 ourKey->values.push_back(*v);
125 // TODO: prune/skip duplicates ?
126 }
127 }
128 }
129
130 Note::Pointer
131 Notes::parse(ConfigParser &parser)
132 {
133 String key, value;
134 ConfigParser::ParseString(&key);
135 ConfigParser::ParseQuotedString(&value);
136 Note::Pointer note = add(key);
137 Note::Value::Pointer noteValue = note->addValue(value);
138 aclParseAclList(parser, &noteValue->aclList);
139
140 if (blacklisted) {
141 for (int i = 0; blacklisted[i] != NULL; ++i) {
142 if (note->key.caseCmp(blacklisted[i]) == 0) {
143 fatalf("%s:%d: meta key \"%s\" is a reserved %s name",
144 cfg_filename, config_lineno, note->key.termedBuf(),
145 descr ? descr : "");
146 }
147 }
148 }
149
150 return note;
151 }
152
153 void
154 Notes::dump(StoreEntry *entry, const char *key)
155 {
156 typedef Notes::NotesList::iterator AMLI;
157 for (AMLI m = notes.begin(); m != notes.end(); ++m) {
158 typedef Note::Values::iterator VLI;
159 for (VLI v =(*m)->values.begin(); v != (*m)->values.end(); ++v ) {
160 storeAppendPrintf(entry, "%s " SQUIDSTRINGPH " %s",
161 key, SQUIDSTRINGPRINT((*m)->key), ConfigParser::QuoteString((*v)->value));
162 dump_acl_list(entry, (*v)->aclList);
163 storeAppendPrintf(entry, "\n");
164 }
165 }
166 }
167
168 void
169 Notes::clean()
170 {
171 notes.clean();
172 }