]> git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ServiceConfig.cc
Merged from trunk
[thirdparty/squid.git] / src / adaptation / ServiceConfig.cc
1 /*
2 * DEBUG: section XXX
3 */
4
5 #include "squid.h"
6 #include "ConfigParser.h"
7 #include "adaptation/ServiceConfig.h"
8
9 Adaptation::ServiceConfig::ServiceConfig():
10 port(-1), method(methodNone), point(pointNone), bypass(false)
11 {}
12
13 const char *
14 Adaptation::ServiceConfig::methodStr() const
15 {
16 return Adaptation::methodStr(method);
17 }
18
19 const char *
20 Adaptation::ServiceConfig::vectPointStr() const
21 {
22 return Adaptation::vectPointStr(point);
23 }
24
25 Adaptation::Method
26 Adaptation::ServiceConfig::parseMethod(const char *str) const
27 {
28 if (!strncasecmp(str, "REQMOD", 6))
29 return Adaptation::methodReqmod;
30
31 if (!strncasecmp(str, "RESPMOD", 7))
32 return Adaptation::methodRespmod;
33
34 return Adaptation::methodNone;
35 }
36
37 Adaptation::VectPoint
38 Adaptation::ServiceConfig::parseVectPoint(const char *service_configConfig) const
39 {
40 const char *t = service_configConfig;
41 const char *q = strchr(t, '_');
42
43 if (q)
44 t = q + 1;
45
46 if (!strcasecmp(t, "precache"))
47 return Adaptation::pointPreCache;
48
49 if (!strcasecmp(t, "postcache"))
50 return Adaptation::pointPostCache;
51
52 return Adaptation::pointNone;
53 }
54
55 bool
56 Adaptation::ServiceConfig::parse()
57 {
58 char *method_point = NULL;
59
60 ConfigParser::ParseString(&key);
61 ConfigParser::ParseString(&method_point);
62 ConfigParser::ParseBool(&bypass);
63 ConfigParser::ParseString(&uri);
64
65 debugs(3, 5, HERE << cfg_filename << ':' << config_lineno << ": " <<
66 key << " " << method_point << " " << bypass);
67
68 method = parseMethod(method_point);
69 point = parseVectPoint(method_point);
70 safe_free(method_point);
71
72 debugs(3, 5, HERE << cfg_filename << ':' << config_lineno << ": " <<
73 "service_configConfig is " << methodStr() << "_" << vectPointStr());
74
75 // TODO: find core code that parses URLs and extracts various parts
76
77 // extract scheme and use it as the service_configConfig protocol
78 const char *schemeSuffix = "://";
79 if (const String::size_type schemeEnd=uri.find(schemeSuffix))
80 protocol=uri.substr(0,schemeEnd-1);
81
82 debugs(3, 5, HERE << cfg_filename << ':' << config_lineno << ": " <<
83 "service protocol is " << protocol);
84
85 if (protocol.size() == 0)
86 return false;
87
88 // skip scheme
89 const char *s = uri.termedBuf() + protocol.size() + strlen(schemeSuffix);
90
91 const char *e;
92
93 bool have_port = false;
94
95 if ((e = strchr(s, ':')) != NULL) {
96 have_port = true;
97 } else if ((e = strchr(s, '/')) != NULL) {
98 have_port = false;
99 } else {
100 return false;
101 }
102
103 int len = e - s;
104 host.limitInit(s, len);
105 s = e;
106
107 port = -1;
108 if (have_port) {
109 s++;
110
111 if ((e = strchr(s, '/')) != NULL) {
112 char *t;
113 const unsigned long p = strtoul(s, &t, 0);
114
115 if (p > 65535) // port value is too high
116 return false;
117
118 port = static_cast<int>(p);
119
120 if (t != e) // extras after the port
121 return false;
122
123 s = e;
124
125 if (s[0] != '/')
126 return false;
127 }
128 }
129
130 // if no port, the caller may use service_configConfigs or supply the default if neeeded
131
132 s++;
133 e = strchr(s, '\0');
134 len = e - s;
135
136 if (len > 1024) {
137 debugs(3, 0, HERE << cfg_filename << ':' << config_lineno << ": " <<
138 "long resource name (>1024), probably wrong");
139 }
140
141 resource.limitInit(s, len + 1);
142
143 if ((bypass != 0) && (bypass != 1)) {
144 debugs(3, 0, HERE << cfg_filename << ':' << config_lineno << ": " <<
145 "wrong bypass value; 0 or 1 expected: " << bypass);
146 return false;
147 }
148
149 return true;
150 }