]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ServiceConfig.cc
2 * DEBUG: section 93 Adaptation
6 #include "adaptation/ServiceConfig.h"
7 #include "ConfigParser.h"
13 Adaptation::ServiceConfig::ServiceConfig():
14 port(-1), method(methodNone
), point(pointNone
),
15 bypass(false), maxConn(-1), onOverload(srvWait
),
16 routing(false), ipv6(false)
20 Adaptation::ServiceConfig::methodStr() const
22 return Adaptation::methodStr(method
);
26 Adaptation::ServiceConfig::vectPointStr() const
28 return Adaptation::vectPointStr(point
);
32 Adaptation::ServiceConfig::parseMethod(const char *str
) const
34 if (!strncasecmp(str
, "REQMOD", 6))
35 return Adaptation::methodReqmod
;
37 if (!strncasecmp(str
, "RESPMOD", 7))
38 return Adaptation::methodRespmod
;
40 return Adaptation::methodNone
;
44 Adaptation::ServiceConfig::parseVectPoint(const char *service_configConfig
) const
46 const char *t
= service_configConfig
;
47 const char *q
= strchr(t
, '_');
52 if (!strcmp(t
, "precache"))
53 return Adaptation::pointPreCache
;
55 if (!strcmp(t
, "postcache"))
56 return Adaptation::pointPostCache
;
58 return Adaptation::pointNone
;
62 Adaptation::ServiceConfig::parse()
64 key
= ConfigParser::NextToken();
65 String method_point
= ConfigParser::NextToken();
66 method
= parseMethod(method_point
.termedBuf());
67 point
= parseVectPoint(method_point
.termedBuf());
69 // reset optional parameters in case we are reconfiguring
70 bypass
= routing
= false;
72 // handle optional service name=value parameters
73 bool grokkedUri
= false;
74 bool onOverloadSet
= false;
75 std::set
<std::string
> options
;
77 while (char *option
= ConfigParser::NextToken()) {
78 const char *name
= option
;
79 const char *value
= "";
80 if (strcmp(option
, "0") == 0) { // backward compatibility
83 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT
), "UPGRADE: Please use 'bypass=off' option to disable service bypass");
84 } else if (strcmp(option
, "1") == 0) { // backward compatibility
87 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT
), "UPGRADE: Please use 'bypass=on' option to enable service bypass");
89 char *eq
= strstr(option
, "=");
90 const char *sffx
= strstr(option
, "://");
91 if (!eq
|| (sffx
&& sffx
< eq
)) { //no "=" or has the form "icap://host?arg=val"
94 } else { // a normal name=value option
95 *eq
= '\0'; // terminate option name
96 value
= eq
+ 1; // skip '='
100 // Check if option is set twice
101 if (options
.find(name
) != options
.end()) {
102 debugs(3, DBG_CRITICAL
, cfg_filename
<< ':' << config_lineno
<< ": " <<
103 "Duplicate option \"" << name
<< "\" in adaptation service definition");
106 options
.insert(name
);
108 bool grokked
= false;
109 if (strcmp(name
, "bypass") == 0) {
110 grokked
= grokBool(bypass
, name
, value
);
111 } else if (strcmp(name
, "routing") == 0)
112 grokked
= grokBool(routing
, name
, value
);
113 else if (strcmp(name
, "uri") == 0)
114 grokked
= grokkedUri
= grokUri(value
);
115 else if (strcmp(name
, "ipv6") == 0) {
116 grokked
= grokBool(ipv6
, name
, value
);
117 if (grokked
&& ipv6
&& !Ip::EnableIpv6
)
118 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: IPv6 is disabled. ICAP service option ignored.");
119 } else if (strcmp(name
, "max-conn") == 0)
120 grokked
= grokLong(maxConn
, name
, value
);
121 else if (strcmp(name
, "on-overload") == 0) {
122 grokked
= grokOnOverload(onOverload
, value
);
123 onOverloadSet
= true;
125 grokked
= grokExtension(name
, value
);
131 // set default on-overload value if needed
133 onOverload
= bypass
? srvBypass
: srvWait
;
135 // is the service URI set?
137 debugs(3, DBG_CRITICAL
, cfg_filename
<< ':' << config_lineno
<< ": " <<
138 "No \"uri\" option in adaptation service definition");
142 debugs(3,5, cfg_filename
<< ':' << config_lineno
<< ": " <<
143 "adaptation_service " << key
<< ' ' <<
144 methodStr() << "_" << vectPointStr() << ' ' <<
145 bypass
<< routing
<< ' ' <<
152 Adaptation::ServiceConfig::grokUri(const char *value
)
154 // TODO: find core code that parses URLs and extracts various parts
155 // AYJ: most of this is duplicate of urlParse() in src/url.cc
157 if (!value
|| !*value
) {
158 debugs(3, DBG_CRITICAL
, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
159 "empty adaptation service URI");
165 // extract scheme and use it as the service_configConfig protocol
166 const char *schemeSuffix
= "://";
167 const String::size_type schemeEnd
= uri
.find(schemeSuffix
);
168 if (schemeEnd
!= String::npos
)
169 protocol
=uri
.substr(0,schemeEnd
);
171 debugs(3, 5, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
172 "service protocol is " << protocol
);
174 if (protocol
.size() == 0)
178 const char *s
= uri
.termedBuf() + protocol
.size() + strlen(schemeSuffix
);
182 bool have_port
= false;
187 if ((t
= strchr(s
, ']')) == NULL
)
192 if ((e
= strchr(t
, ':')) != NULL
) {
194 } else if ((e
= strchr(t
, '/')) != NULL
) {
200 if ((e
= strchr(s
, ':')) != NULL
) {
202 } else if ((e
= strchr(s
, '/')) != NULL
) {
210 host
.limitInit(s
, len
);
217 if ((e
= strchr(s
, '/')) != NULL
) {
219 const unsigned long p
= strtoul(s
, &t
, 0);
221 if (p
> 65535) // port value is too high
224 port
= static_cast<int>(p
);
226 if (t
!= e
) // extras after the port
236 // if no port, the caller may use service_configConfigs or supply the default if neeeded
243 debugs(3, DBG_CRITICAL
, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
244 "long resource name (>1024), probably wrong");
247 resource
.limitInit(s
, len
+ 1);
252 Adaptation::ServiceConfig::grokBool(bool &var
, const char *name
, const char *value
)
254 if (!strcmp(value
, "0") || !strcmp(value
, "off"))
256 else if (!strcmp(value
, "1") || !strcmp(value
, "on"))
259 debugs(3, DBG_CRITICAL
, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
260 "wrong value for boolean " << name
<< "; " <<
261 "'0', '1', 'on', or 'off' expected but got: " << value
);
269 Adaptation::ServiceConfig::grokLong(long &var
, const char *name
, const char *value
)
272 const long p
= strtol(value
, &bad
, 0);
273 if (p
< 0 || bad
== value
) {
274 debugs(3, DBG_CRITICAL
, "ERROR: " << cfg_filename
<< ':' <<
275 config_lineno
<< ": " << "wrong value for " << name
<< "; " <<
276 "a non-negative integer expected but got: " << value
);
284 Adaptation::ServiceConfig::grokOnOverload(SrvBehaviour
&var
, const char *value
)
286 if (strcmp(value
, "block") == 0)
288 else if (strcmp(value
, "bypass") == 0)
290 else if (strcmp(value
, "wait") == 0)
292 else if (strcmp(value
, "force") == 0)
295 debugs(3, DBG_CRITICAL
, "ERROR: " << cfg_filename
<< ':' <<
296 config_lineno
<< ": " << "wrong value for on-overload; " <<
297 "'block', 'bypass', 'wait' or 'force' expected but got: " << value
);
304 Adaptation::ServiceConfig::grokExtension(const char *name
, const char *value
)
306 // we do not accept extensions by default
307 debugs(3, DBG_CRITICAL
, cfg_filename
<< ':' << config_lineno
<< ": " <<
308 "ERROR: unknown adaptation service option: " <<
309 name
<< '=' << value
);