]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ServiceConfig.cc
6 #include "ConfigParser.h"
7 #include "adaptation/ServiceConfig.h"
9 Adaptation::ServiceConfig::ServiceConfig():
10 port(-1), method(methodNone
), point(pointNone
),
11 bypass(false), routing(false)
15 Adaptation::ServiceConfig::methodStr() const
17 return Adaptation::methodStr(method
);
21 Adaptation::ServiceConfig::vectPointStr() const
23 return Adaptation::vectPointStr(point
);
27 Adaptation::ServiceConfig::parseMethod(const char *str
) const
29 if (!strncasecmp(str
, "REQMOD", 6))
30 return Adaptation::methodReqmod
;
32 if (!strncasecmp(str
, "RESPMOD", 7))
33 return Adaptation::methodRespmod
;
35 return Adaptation::methodNone
;
39 Adaptation::ServiceConfig::parseVectPoint(const char *service_configConfig
) const
41 const char *t
= service_configConfig
;
42 const char *q
= strchr(t
, '_');
47 if (!strcasecmp(t
, "precache"))
48 return Adaptation::pointPreCache
;
50 if (!strcasecmp(t
, "postcache"))
51 return Adaptation::pointPostCache
;
53 return Adaptation::pointNone
;
57 Adaptation::ServiceConfig::parse()
59 char *method_point
= NULL
;
61 ConfigParser::ParseString(&key
);
62 ConfigParser::ParseString(&method_point
);
63 method
= parseMethod(method_point
);
64 point
= parseVectPoint(method_point
);
66 // reset optional parameters in case we are reconfiguring
67 bypass
= routing
= false;
69 // handle optional service name=value parameters
70 const char *lastOption
= NULL
;
71 while (char *option
= strtok(NULL
, w_space
)) {
72 if (strcmp(option
, "0") == 0) { // backward compatibility
76 if (strcmp(option
, "1") == 0) { // backward compatibility
81 const char *name
= option
;
82 char *value
= strstr(option
, "=");
87 *value
= '\0'; // terminate option name
90 // TODO: warn if option is set twice?
92 if (strcmp(name
, "bypass") == 0)
93 grokked
= grokBool(bypass
, name
, value
);
95 if (strcmp(name
, "routing") == 0)
96 grokked
= grokBool(routing
, name
, value
);
98 debugs(3, 0, cfg_filename
<< ':' << config_lineno
<< ": " <<
99 "unknown adaptation service option: " << name
<< '=' << value
);
105 // what is left must be the service URI
106 if (!grokUri(lastOption
))
109 // there should be nothing else left
110 if (const char *tail
= strtok(NULL
, w_space
)) {
111 debugs(3, 0, cfg_filename
<< ':' << config_lineno
<< ": " <<
112 "garbage after adaptation service URI: " << tail
);
116 debugs(3,5, cfg_filename
<< ':' << config_lineno
<< ": " <<
117 "adaptation_service " << key
<< ' ' <<
118 methodStr() << "_" << vectPointStr() << ' ' <<
119 bypass
<< routing
<< ' ' <<
126 Adaptation::ServiceConfig::grokUri(const char *value
)
128 // TODO: find core code that parses URLs and extracts various parts
130 if (!value
|| !*value
) {
131 debugs(3, 0, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
132 "empty adaptation service URI");
138 // extract scheme and use it as the service_configConfig protocol
139 const char *schemeSuffix
= "://";
140 const String::size_type schemeEnd
= uri
.find(schemeSuffix
);
141 if (schemeEnd
!= String::npos
)
142 protocol
=uri
.substr(0,schemeEnd
);
144 debugs(3, 5, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
145 "service protocol is " << protocol
);
147 if (protocol
.size() == 0)
151 const char *s
= uri
.termedBuf() + protocol
.size() + strlen(schemeSuffix
);
155 bool have_port
= false;
157 if ((e
= strchr(s
, ':')) != NULL
) {
159 } else if ((e
= strchr(s
, '/')) != NULL
) {
166 host
.limitInit(s
, len
);
173 if ((e
= strchr(s
, '/')) != NULL
) {
175 const unsigned long p
= strtoul(s
, &t
, 0);
177 if (p
> 65535) // port value is too high
180 port
= static_cast<int>(p
);
182 if (t
!= e
) // extras after the port
192 // if no port, the caller may use service_configConfigs or supply the default if neeeded
199 debugs(3, 0, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
200 "long resource name (>1024), probably wrong");
203 resource
.limitInit(s
, len
+ 1);
209 Adaptation::ServiceConfig::grokBool(bool &var
, const char *name
, const char *value
)
211 if (!strcmp(value
, "0") || !strcmp(value
, "off"))
214 if (!strcmp(value
, "1") || !strcmp(value
, "on"))
217 debugs(3, 0, HERE
<< cfg_filename
<< ':' << config_lineno
<< ": " <<
218 "wrong value for boolean " << name
<< "; " <<
219 "'0', '1', 'on', or 'off' expected but got: " << value
);