]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ServiceConfig.cc
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 93 Adaptation */
12 #include "adaptation/ServiceConfig.h"
13 #include "ConfigParser.h"
19 Adaptation::ServiceConfig::ServiceConfig():
20 port(-1), method(methodNone
), point(pointNone
),
21 bypass(false), maxConn(-1), onOverload(srvWait
),
22 routing(false), ipv6(false)
26 Adaptation::ServiceConfig::methodStr() const
28 return Adaptation::methodStr(method
);
32 Adaptation::ServiceConfig::vectPointStr() const
34 return Adaptation::vectPointStr(point
);
38 Adaptation::ServiceConfig::parseMethod(const char *str
) const
40 if (!strncasecmp(str
, "REQMOD", 6))
41 return Adaptation::methodReqmod
;
43 if (!strncasecmp(str
, "RESPMOD", 7))
44 return Adaptation::methodRespmod
;
46 return Adaptation::methodNone
;
50 Adaptation::ServiceConfig::parseVectPoint(const char *service_configConfig
) const
52 const char *t
= service_configConfig
;
53 const char *q
= strchr(t
, '_');
58 if (!strcmp(t
, "precache"))
59 return Adaptation::pointPreCache
;
61 if (!strcmp(t
, "postcache"))
62 return Adaptation::pointPostCache
;
64 return Adaptation::pointNone
;
68 Adaptation::ServiceConfig::parse()
70 key
= ConfigParser::NextToken();
71 String method_point
= ConfigParser::NextToken();
72 method
= parseMethod(method_point
.termedBuf());
73 point
= parseVectPoint(method_point
.termedBuf());
75 // reset optional parameters in case we are reconfiguring
76 bypass
= routing
= false;
78 // handle optional service name=value parameters
79 bool grokkedUri
= false;
80 bool onOverloadSet
= false;
81 std::set
<std::string
> options
;
83 while (char *option
= ConfigParser::NextToken()) {
84 const char *name
= option
;
85 const char *value
= "";
86 if (strcmp(option
, "0") == 0) { // backward compatibility
89 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT
), "UPGRADE: Please use 'bypass=off' option to disable service bypass");
90 } else if (strcmp(option
, "1") == 0) { // backward compatibility
93 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT
), "UPGRADE: Please use 'bypass=on' option to enable service bypass");
95 char *eq
= strstr(option
, "=");
96 const char *sffx
= strstr(option
, "://");
97 if (!eq
|| (sffx
&& sffx
< eq
)) { //no "=" or has the form "icap://host?arg=val"
100 } else { // a normal name=value option
101 *eq
= '\0'; // terminate option name
102 value
= eq
+ 1; // skip '='
106 // Check if option is set twice
107 if (options
.find(name
) != options
.end()) {
108 debugs(3, DBG_CRITICAL
, cfg_filename
<< ':' << config_lineno
<< ": " <<
109 "Duplicate option \"" << name
<< "\" in adaptation service definition");
112 options
.insert(name
);
114 bool grokked
= false;
115 if (strcmp(name
, "bypass") == 0) {
116 grokked
= grokBool(bypass
, name
, value
);
117 } else if (strcmp(name
, "routing") == 0)
118 grokked
= grokBool(routing
, name
, value
);
119 else if (strcmp(name
, "uri") == 0)
120 grokked
= grokkedUri
= grokUri(value
);
121 else if (strcmp(name
, "ipv6") == 0) {
122 grokked
= grokBool(ipv6
, name
, value
);
123 if (grokked
&& ipv6
&& !Ip::EnableIpv6
)
124 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: IPv6 is disabled. ICAP service option ignored.");
125 } else if (strcmp(name
, "max-conn") == 0)
126 grokked
= grokLong(maxConn
, name
, value
);
127 else if (strcmp(name
, "on-overload") == 0) {
128 grokked
= grokOnOverload(onOverload
, value
);
129 onOverloadSet
= true;
130 } else if (strncmp(name
, "ssl", 3) == 0 || strncmp(name
, "tls-", 4) == 0) {
132 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: adaptation option '" << name
<< "' requires --with-openssl. ICAP service option ignored.");
134 // name prefix is "ssl" or "tls-"
135 std::string tmp
= name
+ (name
[0] == 's' ? 3 : 4);
138 secure
.parse(tmp
.c_str());
139 secure
.encryptTransport
= true;
143 grokked
= grokExtension(name
, value
);
149 // set default on-overload value if needed
151 onOverload
= bypass
? srvBypass
: srvWait
;
153 // is the service URI set?
155 debugs(3, DBG_CRITICAL
, cfg_filename
<< ':' << config_lineno
<< ": " <<
156 "No \"uri\" option in adaptation service definition");
160 debugs(3,5, cfg_filename
<< ':' << config_lineno
<< ": " <<
161 "adaptation_service " << key
<< ' ' <<
162 methodStr() << "_" << vectPointStr() << ' ' <<
163 bypass
<< routing
<< ' ' <<
170 Adaptation::ServiceConfig::grokUri(const char *value
)
172 // TODO: find core code that parses URLs and extracts various parts
173 // AYJ: most of this is duplicate of urlParse() in src/url.cc
175 if (!value
|| !*value
) {
176 debugs(3, DBG_CRITICAL
, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
177 "empty adaptation service URI");
183 // extract scheme and use it as the service_configConfig protocol
184 const char *schemeSuffix
= "://";
185 const String::size_type schemeEnd
= uri
.find(schemeSuffix
);
186 if (schemeEnd
!= String::npos
)
187 protocol
=uri
.substr(0,schemeEnd
);
189 debugs(3, 5, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
190 "service protocol is " << protocol
);
192 if (protocol
.size() == 0)
196 const char *s
= uri
.termedBuf() + protocol
.size() + strlen(schemeSuffix
);
200 bool have_port
= false;
205 if ((t
= strchr(s
, ']')) == NULL
)
210 if ((e
= strchr(t
, ':')) != NULL
) {
212 } else if ((e
= strchr(t
, '/')) != NULL
) {
218 if ((e
= strchr(s
, ':')) != NULL
) {
220 } else if ((e
= strchr(s
, '/')) != NULL
) {
228 host
.limitInit(s
, len
);
230 if (secure
.sslDomain
.isEmpty())
231 secure
.sslDomain
.assign(host
.rawBuf(), host
.size());
239 if ((e
= strchr(s
, '/')) != NULL
) {
241 const unsigned long p
= strtoul(s
, &t
, 0);
243 if (p
> 65535) // port value is too high
246 port
= static_cast<int>(p
);
248 if (t
!= e
) // extras after the port
258 // if no port, the caller may use service_configConfigs or supply the default if neeeded
265 debugs(3, DBG_CRITICAL
, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
266 "long resource name (>1024), probably wrong");
269 resource
.limitInit(s
, len
+ 1);
274 Adaptation::ServiceConfig::grokBool(bool &var
, const char *name
, const char *value
)
276 if (!strcmp(value
, "0") || !strcmp(value
, "off"))
278 else if (!strcmp(value
, "1") || !strcmp(value
, "on"))
281 debugs(3, DBG_CRITICAL
, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
282 "wrong value for boolean " << name
<< "; " <<
283 "'0', '1', 'on', or 'off' expected but got: " << value
);
291 Adaptation::ServiceConfig::grokLong(long &var
, const char *name
, const char *value
)
294 const long p
= strtol(value
, &bad
, 0);
295 if (p
< 0 || bad
== value
) {
296 debugs(3, DBG_CRITICAL
, "ERROR: " << cfg_filename
<< ':' <<
297 config_lineno
<< ": " << "wrong value for " << name
<< "; " <<
298 "a non-negative integer expected but got: " << value
);
306 Adaptation::ServiceConfig::grokOnOverload(SrvBehaviour
&var
, const char *value
)
308 if (strcmp(value
, "block") == 0)
310 else if (strcmp(value
, "bypass") == 0)
312 else if (strcmp(value
, "wait") == 0)
314 else if (strcmp(value
, "force") == 0)
317 debugs(3, DBG_CRITICAL
, "ERROR: " << cfg_filename
<< ':' <<
318 config_lineno
<< ": " << "wrong value for on-overload; " <<
319 "'block', 'bypass', 'wait' or 'force' expected but got: " << value
);
326 Adaptation::ServiceConfig::grokExtension(const char *name
, const char *value
)
328 // we do not accept extensions by default
329 debugs(3, DBG_CRITICAL
, cfg_filename
<< ':' << config_lineno
<< ": " <<
330 "ERROR: unknown adaptation service option: " <<
331 name
<< '=' << value
);