]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ESICustomParser.cc
Moved authenticated() method from ACL[Filled]Checklist into auth/Acl.{cc,h} to
[thirdparty/squid.git] / src / ESICustomParser.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 86 ESI processing
6 * AUTHOR: Robert Collins
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37 #include "ESICustomParser.h"
38 #include "Trie.h"
39 #include "TrieCharTransform.h"
40 #include "Array.h"
41
42 Trie *ESICustomParser::SearchTrie=NULL;
43
44 RegisterESIParser("custom", ESICustomParser);
45
46 Trie *
47 ESICustomParser::GetTrie()
48 {
49 if (SearchTrie)
50 return SearchTrie;
51
52 SearchTrie = new Trie(new TrieCaseless);
53
54 static const ESITAG_t ESITAG_value = ESITAG;
55
56 assert (SearchTrie->add
57 ("<esi:",5,(void *)&ESITAG_value));
58
59 static const ESITAG_t ESIENDTAG_value = ESIENDTAG;
60
61 assert (SearchTrie->add
62 ("</esi:",6,(void *)&ESIENDTAG_value));
63
64 static const ESITAG_t ESICOMMENT_value = ESICOMMENT;
65
66 assert (SearchTrie->add
67 ("<!--",4,(void *)&ESICOMMENT_value));
68
69 return SearchTrie;
70 }
71
72 ESICustomParser::ESICustomParser(ESIParserClient *aClient) : theClient (aClient)
73 {}
74
75 ESICustomParser::~ESICustomParser()
76 {
77 theClient = NULL;
78 }
79
80 char const *
81 ESICustomParser::findTag(char const *buffer, size_t bufferLength)
82 {
83 size_t myOffset (0);
84 ESITAG_t *resulttype = NULL;
85
86 while (myOffset < bufferLength &&
87 (resulttype = static_cast<ESITAG_t *>(GetTrie()->findPrefix (buffer + myOffset, bufferLength - myOffset)))
88 == NULL)
89 ++myOffset;
90
91 if (myOffset == bufferLength)
92 return NULL;
93
94 debugs(86, 9, "ESICustomParser::findTag: found " << *resulttype);
95
96 lastTag = *resulttype;
97
98 return buffer + myOffset;
99 }
100
101 bool
102 ESICustomParser::parse(char const *dataToParse, size_t const lengthOfData, bool const endOfStream)
103 {
104 debugs(86, 9, "ESICustomParser::parse: Appending data to internal buffer");
105 content.append (dataToParse, lengthOfData);
106
107 if (!endOfStream) {
108 return true;
109 }
110
111 size_t openESITags (0);
112 //erring on the safe side. Probably rawBuf would be ok too
113 char const *currentPos = content.termedBuf();
114 size_t remainingCount = content.size();
115 char const *tag = NULL;
116
117 while ((tag = findTag(currentPos, remainingCount))) {
118 if (tag - currentPos)
119 theClient->parserDefault (currentPos,tag - currentPos);
120
121 switch (lastTag) {
122
123 case ESITAG: {
124 ++openESITags;
125 char *tagEnd = strchr(const_cast<char *>(tag), '>');
126
127 if (!tagEnd) {
128 error = "Could not find end ('>') of tag";
129 return false;
130 }
131
132 if (tagEnd - tag > (ssize_t)remainingCount) {
133 error = "Tag ends beyond the parse buffer.";
134 return false;
135 }
136
137 if (*(tagEnd - 1) == '/')
138 --openESITags;
139
140 char * endofName = strpbrk(const_cast<char *>(tag), w_space);
141
142 if (endofName > tagEnd)
143 endofName = const_cast<char *>(tagEnd);
144
145 *endofName = '\0';
146
147 *tagEnd = '\0';
148
149 Vector<char *>attributes;
150
151 char *attribute = const_cast<char *>(endofName + 1);
152
153 while (attribute > tag && attribute < tagEnd) {
154 /* leading spaces */
155
156 while (attribute < tagEnd && (xisspace(*attribute) || (*attribute == '/')))
157 ++attribute;
158
159 if (! (attribute < tagEnd))
160 break;
161
162 /* attribute name */
163 attributes.push_back(attribute);
164
165 char *nextSpace = strpbrk(attribute, w_space);
166
167 char *equals = strchr(attribute, '=');
168
169 if (!equals) {
170 error = "Missing attribute value.";
171 return false;
172 }
173
174 if (nextSpace && nextSpace < equals)
175 *nextSpace = '\0';
176 else
177 *equals = '\0';
178
179 ++equals;
180
181 while (equals < tagEnd && xisspace(*equals))
182 ++equals;
183
184 char sep = *equals;
185
186 if (sep != '\'' && sep != '"') {
187 error = "Unknown identifier (";
188 error.append (sep);
189 error.append (")");
190 return false;
191 }
192
193 char *value = equals + 1;
194 char *end = strchr (value, sep);
195 attributes.push_back(value);
196 *end = '\0';
197 attribute = end + 1;
198 }
199
200 theClient->start (tag + 1, (const char **)attributes.items, attributes.size() >> 1);
201 /* TODO: attributes */
202
203 if (*(tagEnd - 1) == '/')
204 theClient->end (tag + 1);
205
206 remainingCount -= tagEnd - currentPos + 1;
207
208 currentPos = tagEnd + 1;
209 }
210
211 break;
212
213 case ESIENDTAG: {
214 if (!openESITags)
215 return false;
216
217 char const *tagEnd = strchr(tag, '>');
218
219 if (!tagEnd)
220 return false;
221
222 if (tagEnd - tag > (ssize_t)remainingCount)
223 return false;
224
225 char * endofName = strpbrk(const_cast<char *>(tag), w_space);
226
227 if (endofName > tagEnd)
228 endofName = const_cast<char *>(tagEnd);
229
230 *endofName = '\0';
231
232 theClient->end (tag + 2);
233
234 --openESITags;
235
236 remainingCount -= tagEnd - currentPos + 1;
237
238 currentPos = tagEnd + 1;
239 }
240
241 break;
242
243 case ESICOMMENT: {
244 /* Further optimisation potential:
245 * 1) recognize end comments for esi and don't callback on
246 * comments.
247 * 2) provide the comment length to the caller.
248 */
249 /* Comments must not be nested, without CDATA
250 * and we don't support CDATA
251 */
252 char *commentEnd = strstr (const_cast<char *>(tag), "-->");
253
254 if (!commentEnd) {
255 error = "missing end of comment";
256 return false;
257 }
258
259 if (commentEnd - tag > (ssize_t)remainingCount) {
260 error = "comment ends beyond parse buffer";
261 return false;
262 }
263
264 *commentEnd = '\0';
265 theClient->parserComment (tag + 4);
266 remainingCount -= commentEnd - currentPos + 3;
267 currentPos = commentEnd + 3;
268 }
269
270 break;
271 break;
272
273 default:
274 fatal ("unknown ESI tag type found");
275 };
276
277 /*
278 * Find next esi tag (open or closing) or comment
279 * send tag, or full comment text
280 * rinse
281 */
282 }
283
284 if (remainingCount)
285 theClient->parserDefault (currentPos,remainingCount);
286
287 debugs(86, 5, "ESICustomParser::parse: Finished parsing, will return " << !openESITags);
288
289 if (openESITags)
290 error = "ESI Tags still open";
291
292 return !openESITags;
293 }
294
295 long int
296 ESICustomParser::lineNumber() const
297 {
298 /* We don't track lines in the body */
299 return 0;
300 }
301
302 char const *
303 ESICustomParser::errorString() const
304 {
305 if (error.size())
306 return error.termedBuf();
307 else
308 return "Parsing error strings not implemented";
309 }