]> git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/Config.cc
Merged from trunk.
[thirdparty/squid.git] / src / adaptation / Config.cc
1
2 /*
3 * $Id$
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 #include "squid.h"
34 #include "structs.h"
35
36 #include "ConfigParser.h"
37 #include "acl/Gadgets.h"
38 #include "Store.h"
39 #include "Array.h" // really Vector
40 #include "adaptation/Config.h"
41 #include "adaptation/Service.h"
42 #include "adaptation/AccessRule.h"
43 #include "adaptation/ServiceGroups.h"
44
45
46 bool Adaptation::Config::Enabled = false;
47
48 void
49 Adaptation::Config::parseService()
50 {
51 ServiceConfig *cfg = new ServiceConfig;
52 cfg->parse();
53 serviceConfigs.push_back(cfg);
54 }
55
56 void
57 Adaptation::Config::freeService()
58 {
59 while (!serviceConfigs.empty()) {
60 delete serviceConfigs.back();
61 serviceConfigs.pop_back();
62 }
63 }
64
65 void
66 Adaptation::Config::dumpService(StoreEntry *entry, const char *name) const
67 {
68 typedef Services::iterator SCI;
69 for (SCI i = AllServices().begin(); i != AllServices().end(); ++i) {
70 const ServiceConfig &cfg = (*i)->cfg();
71 storeAppendPrintf(entry, "%s " SQUIDSTRINGPH "_%s %s %d " SQUIDSTRINGPH "\n",
72 name,
73 SQUIDSTRINGPRINT(cfg.key),
74 cfg.methodStr(), cfg.vectPointStr(), cfg.bypass,
75 SQUIDSTRINGPRINT(cfg.uri));
76 }
77 }
78
79 void
80 Adaptation::Config::finalize()
81 {
82 // create service reps from service configs
83 typedef Vector<ServiceConfig*>::const_iterator VISCI;
84 const Vector<ServiceConfig*> &configs = serviceConfigs;
85 debugs(93,3, HERE << "Found " << configs.size() << " service configs.");
86 for (VISCI i = configs.begin(); i != configs.end(); ++i) {
87 const ServiceConfig &cfg = **i;
88 if (FindService(cfg.key) != NULL) {
89 debugs(93,0, "ERROR: Duplicate adaptation service name: " <<
90 cfg.key);
91 continue; // TODO: make fatal
92 }
93 ServicePointer s = createService(**i);
94 if (s != NULL)
95 AllServices().push_back(s);
96 }
97
98 debugs(93,3, HERE << "Created " << configs.size() <<
99 " message adaptation services.");
100 }
101
102 // poor man for_each
103 template <class Collection>
104 static void
105 FinalizeEach(Collection &collection, const char *label)
106 {
107 typedef typename Collection::iterator CI;
108 for (CI i = collection.begin(); i != collection.end(); ++i)
109 (*i)->finalize();
110
111 debugs(93,2, HERE << "Initialized " << collection.size() << ' ' << label);
112 }
113
114 void
115 Adaptation::Config::Finalize(bool enabled)
116 {
117 Enabled = enabled;
118 debugs(93,1, "Adaptation support is " << (Enabled ? "on" : "off."));
119
120 FinalizeEach(AllServices(), "message adaptation services");
121 FinalizeEach(AllGroups(), "message adaptation service groups");
122 FinalizeEach(AllRules(), "message adaptation access rules");
123 }
124
125 void
126 Adaptation::Config::ParseServiceSet()
127 {
128 ServiceSet *g = new ServiceSet();
129 g->parse();
130 AllGroups().push_back(g);
131 }
132
133 void
134 Adaptation::Config::FreeServiceSet()
135 {
136 while (!AllGroups().empty()) {
137 delete AllGroups().back();
138 AllGroups().pop_back();
139 }
140 }
141
142 void
143 Adaptation::Config::DumpServiceSet(StoreEntry *entry, const char *name)
144 {
145 typedef Groups::iterator GI;
146 for (GI i = AllGroups().begin(); i != AllGroups().end(); ++i)
147 storeAppendPrintf(entry, "%s " SQUIDSTRINGPH "\n", name, SQUIDSTRINGPRINT((*i)->id));
148 }
149
150 void
151 Adaptation::Config::ParseAccess(ConfigParser &parser)
152 {
153 String groupId;
154 ConfigParser::ParseString(&groupId);
155 AccessRule *r;
156 if (!(r=FindRuleByGroupId(groupId))) {
157 r = new AccessRule(groupId);
158 AllRules().push_back(r);
159 }
160 r->parse(parser);
161 }
162
163 void
164 Adaptation::Config::FreeAccess()
165 {
166 while (!AllRules().empty()) {
167 delete AllRules().back();
168 AllRules().pop_back();
169 }
170 }
171
172 void
173 Adaptation::Config::DumpAccess(StoreEntry *entry, const char *name)
174 {
175 LOCAL_ARRAY(char, nom, 64);
176
177 typedef AccessRules::iterator CI;
178 for (CI i = AllRules().begin(); i != AllRules().end(); ++i) {
179 snprintf(nom, 64, "%s " SQUIDSTRINGPH, name, SQUIDSTRINGPRINT((*i)->groupId));
180 dump_acl_access(entry, nom, (*i)->acl);
181 }
182 }
183
184 Adaptation::Config::Config()
185 {
186 // XXX: should we init members?
187 }
188
189 // XXX: this is called for ICAP and eCAP configs, but deals mostly
190 // with global arrays shared by those individual configs
191 Adaptation::Config::~Config()
192 {
193 FreeAccess();
194 FreeServiceSet();
195
196 // invalidate each service so that it can be deleted when refcount=0
197 while (!AllServices().empty()) {
198 AllServices().back()->invalidate();
199 AllServices().pop_back();
200 }
201
202 freeService();
203 }