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