]> git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/Config.cc
Merge 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 #include "adaptation/History.h"
45
46
47 bool Adaptation::Config::Enabled = false;
48 char *Adaptation::Config::masterx_shared_name = NULL;
49 int Adaptation::Config::service_iteration_limit = 16;
50
51 void
52 Adaptation::Config::parseService()
53 {
54 ServiceConfig *cfg = new ServiceConfig;
55 if (!cfg->parse()) {
56 fatalf("%s:%d: malformed adaptation service configuration",
57 cfg_filename, config_lineno);
58 }
59 serviceConfigs.push_back(cfg);
60 }
61
62 void
63 Adaptation::Config::freeService()
64 {
65 while (!serviceConfigs.empty()) {
66 delete serviceConfigs.back();
67 serviceConfigs.pop_back();
68 }
69 }
70
71 void
72 Adaptation::Config::dumpService(StoreEntry *entry, const char *name) const
73 {
74 typedef Services::iterator SCI;
75 for (SCI i = AllServices().begin(); i != AllServices().end(); ++i) {
76 const ServiceConfig &cfg = (*i)->cfg();
77 storeAppendPrintf(entry, "%s " SQUIDSTRINGPH "_%s %s %d " SQUIDSTRINGPH "\n",
78 name,
79 SQUIDSTRINGPRINT(cfg.key),
80 cfg.methodStr(), cfg.vectPointStr(), cfg.bypass,
81 SQUIDSTRINGPRINT(cfg.uri));
82 }
83 }
84
85 void
86 Adaptation::Config::finalize()
87 {
88 // create service reps from service configs
89 typedef Vector<ServiceConfig*>::const_iterator VISCI;
90 const Vector<ServiceConfig*> &configs = serviceConfigs;
91 debugs(93,3, HERE << "Found " << configs.size() << " service configs.");
92 for (VISCI i = configs.begin(); i != configs.end(); ++i) {
93 const ServiceConfig &cfg = **i;
94 if (FindService(cfg.key) != NULL) {
95 debugs(93,0, "ERROR: Duplicate adaptation service name: " <<
96 cfg.key);
97 continue; // TODO: make fatal
98 }
99 ServicePointer s = createService(**i);
100 if (s != NULL)
101 AllServices().push_back(s);
102 }
103
104 debugs(93,3, HERE << "Created " << configs.size() <<
105 " message adaptation services.");
106 }
107
108 // poor man for_each
109 template <class Collection>
110 static void
111 FinalizeEach(Collection &collection, const char *label)
112 {
113 typedef typename Collection::iterator CI;
114 for (CI i = collection.begin(); i != collection.end(); ++i)
115 (*i)->finalize();
116
117 debugs(93,2, HERE << "Initialized " << collection.size() << ' ' << label);
118 }
119
120 void
121 Adaptation::Config::Finalize(bool enabled)
122 {
123 Enabled = enabled;
124 debugs(93,1, "Adaptation support is " << (Enabled ? "on" : "off."));
125
126 FinalizeEach(AllServices(), "message adaptation services");
127 FinalizeEach(AllGroups(), "message adaptation service groups");
128 FinalizeEach(AllRules(), "message adaptation access rules");
129 }
130
131 void
132 Adaptation::Config::ParseServiceSet()
133 {
134 Adaptation::Config::ParseServiceGroup(new ServiceSet);
135 }
136
137 void
138 Adaptation::Config::ParseServiceChain()
139 {
140 Adaptation::Config::ParseServiceGroup(new ServiceChain);
141 }
142
143 void
144 Adaptation::Config::ParseServiceGroup(ServiceGroupPointer g)
145 {
146 assert(g != NULL);
147 g->parse();
148 AllGroups().push_back(g);
149 }
150
151 void
152 Adaptation::Config::FreeServiceGroups()
153 {
154 while (!AllGroups().empty()) {
155 // groups are refcounted so we do not explicitly delete them
156 AllGroups().pop_back();
157 }
158 }
159
160 void
161 Adaptation::Config::DumpServiceGroups(StoreEntry *entry, const char *name)
162 {
163 typedef Groups::iterator GI;
164 for (GI i = AllGroups().begin(); i != AllGroups().end(); ++i)
165 storeAppendPrintf(entry, "%s " SQUIDSTRINGPH "\n", name, SQUIDSTRINGPRINT((*i)->id));
166 }
167
168 void
169 Adaptation::Config::ParseAccess(ConfigParser &parser)
170 {
171 String groupId;
172 ConfigParser::ParseString(&groupId);
173 AccessRule *r;
174 if (!(r=FindRuleByGroupId(groupId))) {
175 r = new AccessRule(groupId);
176 AllRules().push_back(r);
177 }
178 r->parse(parser);
179 }
180
181 void
182 Adaptation::Config::FreeAccess()
183 {
184 while (!AllRules().empty()) {
185 delete AllRules().back();
186 AllRules().pop_back();
187 }
188 }
189
190 void
191 Adaptation::Config::DumpAccess(StoreEntry *entry, const char *name)
192 {
193 LOCAL_ARRAY(char, nom, 64);
194
195 typedef AccessRules::iterator CI;
196 for (CI i = AllRules().begin(); i != AllRules().end(); ++i) {
197 snprintf(nom, 64, "%s " SQUIDSTRINGPH, name, SQUIDSTRINGPRINT((*i)->groupId));
198 dump_acl_access(entry, nom, (*i)->acl);
199 }
200 }
201
202 Adaptation::Config::Config()
203 {
204 // XXX: should we init members?
205 }
206
207 // XXX: this is called for ICAP and eCAP configs, but deals mostly
208 // with global arrays shared by those individual configs
209 Adaptation::Config::~Config()
210 {
211 FreeAccess();
212 FreeServiceGroups();
213
214 // invalidate each service so that it can be deleted when refcount=0
215 while (!AllServices().empty()) {
216 AllServices().back()->invalidate();
217 AllServices().pop_back();
218 }
219
220 freeService();
221 }