]>
Commit | Line | Data |
---|---|---|
d81a31f1 AR |
1 | /* |
2 | * DEBUG: section XXX | |
3 | */ | |
4 | ||
5 | #include "squid.h" | |
6 | #include "ConfigParser.h" | |
7 | #include "adaptation/ServiceConfig.h" | |
8 | ||
26ac0430 AJ |
9 | Adaptation::ServiceConfig::ServiceConfig(): |
10 | port(-1), method(methodNone), point(pointNone), bypass(false) | |
d81a31f1 AR |
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 << ": " << | |
99524de7 | 66 | key.unsafeBuf() << " " << method_point << " " << bypass); |
d81a31f1 AR |
67 | |
68 | method = parseMethod(method_point); | |
69 | point = parseVectPoint(method_point); | |
70 | ||
71 | debugs(3, 5, HERE << cfg_filename << ':' << config_lineno << ": " << | |
26ac0430 | 72 | "service_configConfig is " << methodStr() << "_" << vectPointStr()); |
d81a31f1 AR |
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 char *schemeEnd = uri.pos(schemeSuffix)) | |
99524de7 | 79 | protocol.limitInit(uri.unsafeBuf(), schemeEnd - uri.unsafeBuf()); |
26ac0430 AJ |
80 | debugs(3, 5, HERE << cfg_filename << ':' << config_lineno << ": " << |
81 | "service protocol is " << protocol); | |
82 | if (!protocol.size()) | |
83 | return false; | |
d81a31f1 AR |
84 | |
85 | // skip scheme | |
99524de7 | 86 | const char *s = uri.unsafeBuf() + protocol.size() + strlen(schemeSuffix); |
d81a31f1 AR |
87 | |
88 | const char *e; | |
89 | ||
90 | bool have_port = false; | |
91 | ||
92 | if ((e = strchr(s, ':')) != NULL) { | |
93 | have_port = true; | |
94 | } else if ((e = strchr(s, '/')) != NULL) { | |
95 | have_port = false; | |
96 | } else { | |
97 | return false; | |
98 | } | |
99 | ||
100 | int len = e - s; | |
101 | host.limitInit(s, len); | |
102 | s = e; | |
103 | ||
26ac0430 | 104 | port = -1; |
d81a31f1 AR |
105 | if (have_port) { |
106 | s++; | |
107 | ||
108 | if ((e = strchr(s, '/')) != NULL) { | |
109 | char *t; | |
26ac0430 | 110 | const unsigned long p = strtoul(s, &t, 0); |
d81a31f1 | 111 | |
26ac0430 AJ |
112 | if (p > 65535) // port value is too high |
113 | return false; | |
d81a31f1 AR |
114 | |
115 | port = static_cast<int>(p); | |
116 | ||
117 | if (t != e) // extras after the port | |
118 | return false; | |
119 | ||
120 | s = e; | |
121 | ||
122 | if (s[0] != '/') | |
123 | return false; | |
124 | } | |
125 | } | |
126 | ||
127 | // if no port, the caller may use service_configConfigs or supply the default if neeeded | |
128 | ||
129 | s++; | |
130 | e = strchr(s, '\0'); | |
131 | len = e - s; | |
132 | ||
133 | if (len > 1024) { | |
134 | debugs(3, 0, HERE << cfg_filename << ':' << config_lineno << ": " << | |
26ac0430 | 135 | "long resource name (>1024), probably wrong"); |
d81a31f1 AR |
136 | } |
137 | ||
138 | resource.limitInit(s, len + 1); | |
139 | ||
140 | if ((bypass != 0) && (bypass != 1)) { | |
141 | debugs(3, 0, HERE << cfg_filename << ':' << config_lineno << ": " << | |
26ac0430 | 142 | "wrong bypass value; 0 or 1 expected: " << bypass); |
d81a31f1 AR |
143 | return false; |
144 | } | |
145 | ||
146 | return true; | |
147 | } |