]> git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ServiceConfig.cc
Author: Henrik Nordstrom <henrik@henriknordstrom.net>
[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
71 debugs(3, 5, HERE << cfg_filename << ':' << config_lineno << ": " <<
72 "service_configConfig is " << methodStr() << "_" << vectPointStr());
73
74 // TODO: find core code that parses URLs and extracts various parts
75
76 // extract scheme and use it as the service_configConfig protocol
77 const char *schemeSuffix = "://";
78 if (const String::size_type schemeEnd=uri.find(schemeSuffix))
79 protocol=uri.substr(0,schemeEnd);
80
81 debugs(3, 5, HERE << cfg_filename << ':' << config_lineno << ": " <<
82 "service protocol is " << protocol);
83
84 if (protocol.size() == 0)
85 return false;
86
87 // skip scheme
88 const char *s = uri.termedBuf() + protocol.size() + strlen(schemeSuffix);
89
90 const char *e;
91
92 bool have_port = false;
93
94 if ((e = strchr(s, ':')) != NULL) {
95 have_port = true;
96 } else if ((e = strchr(s, '/')) != NULL) {
97 have_port = false;
98 } else {
99 return false;
100 }
101
102 int len = e - s;
103 host.limitInit(s, len);
104 s = e;
105
106 port = -1;
107 if (have_port) {
108 s++;
109
110 if ((e = strchr(s, '/')) != NULL) {
111 char *t;
112 const unsigned long p = strtoul(s, &t, 0);
113
114 if (p > 65535) // port value is too high
115 return false;
116
117 port = static_cast<int>(p);
118
119 if (t != e) // extras after the port
120 return false;
121
122 s = e;
123
124 if (s[0] != '/')
125 return false;
126 }
127 }
128
129 // if no port, the caller may use service_configConfigs or supply the default if neeeded
130
131 s++;
132 e = strchr(s, '\0');
133 len = e - s;
134
135 if (len > 1024) {
136 debugs(3, 0, HERE << cfg_filename << ':' << config_lineno << ": " <<
137 "long resource name (>1024), probably wrong");
138 }
139
140 resource.limitInit(s, len + 1);
141
142 if ((bypass != 0) && (bypass != 1)) {
143 debugs(3, 0, HERE << cfg_filename << ':' << config_lineno << ": " <<
144 "wrong bypass value; 0 or 1 expected: " << bypass);
145 return false;
146 }
147
148 return true;
149 }