]>
Commit | Line | Data |
---|---|---|
964b44c3 | 1 | /* |
262a0e14 | 2 | * $Id$ |
964b44c3 | 3 | * |
4 | * AUTHOR: Joachim Bauch (mail@joachim-bauch.de) | |
5 | * | |
6 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
7 | * ---------------------------------------------------------- | |
8 | * | |
9 | * Squid is the result of efforts by numerous individuals from | |
10 | * the Internet community; see the CONTRIBUTORS file for full | |
11 | * details. Many organizations have provided support for Squid's | |
12 | * development; see the SPONSORS file for full details. Squid is | |
13 | * Copyrighted (C) 2001 by the Regents of the University of | |
14 | * California; see the COPYRIGHT file for full details. Squid | |
15 | * incorporates software developed and/or copyrighted by other | |
16 | * sources; see the CREDITS file for full details. | |
17 | * | |
18 | * This program is free software; you can redistribute it and/or modify | |
19 | * it under the terms of the GNU General Public License as published by | |
20 | * the Free Software Foundation; either version 2 of the License, or | |
21 | * (at your option) any later version. | |
26ac0430 | 22 | * |
964b44c3 | 23 | * This program is distributed in the hope that it will be useful, |
24 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
26 | * GNU General Public License for more details. | |
26ac0430 | 27 | * |
964b44c3 | 28 | * You should have received a copy of the GNU General Public License |
29 | * along with this program; if not, write to the Free Software | |
30 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
31 | * | |
32 | */ | |
33 | ||
34 | /* | |
35 | * The ESI Libxml2 parser is Copyright (c) 2004 by Joachim Bauch | |
36 | * http://www.joachim-bauch.de | |
37 | * mail@joachim-bauch.de | |
38 | */ | |
39 | ||
40 | #include "squid.h" | |
964b44c3 | 41 | |
da783331 | 42 | #if USE_SQUID_ESI |
964b44c3 | 43 | |
da783331 | 44 | #include "ESILibxml2Parser.h" |
964b44c3 | 45 | |
46 | // the global document that will store the resolved entity | |
47 | // definitions | |
48 | static htmlDocPtr entity_doc = NULL; | |
49 | ||
c2d4889f | 50 | RegisterESIParser("libxml2", ESILibxml2Parser); |
51 | ||
964b44c3 | 52 | // the SAX callback functions |
53 | void esi_startElementSAXFunc(void * ctx, const xmlChar * name, const xmlChar ** atts) | |
54 | { | |
55 | int count=0; | |
56 | xmlChar **tmp = (xmlChar **)atts; | |
57 | ||
58 | while (tmp && *tmp != NULL) { | |
59 | count++; | |
60 | tmp++; | |
61 | } | |
62 | ||
63 | // we increased on every key and value | |
64 | count /= 2; | |
65 | ||
66 | ESILibxml2Parser *p = (ESILibxml2Parser *)ctx; | |
67 | ||
68 | p->getClient()->start((const char *)name, (const char **)atts, count); | |
69 | } | |
70 | ||
71 | void esi_endElementSAXFunc(void * ctx, const xmlChar * name) | |
72 | { | |
73 | ESILibxml2Parser *p = (ESILibxml2Parser *)ctx; | |
74 | p->getClient()->end((const char *)name); | |
75 | } | |
76 | ||
77 | void esi_commentSAXFunc(void * ctx, const xmlChar * value) | |
78 | { | |
79 | ESILibxml2Parser *p = (ESILibxml2Parser *)ctx; | |
80 | p->getClient()->parserComment((const char *)value); | |
81 | } | |
82 | ||
83 | void esi_charactersSAXFunc(void *ctx, const xmlChar *ch, int len) | |
84 | { | |
85 | ESILibxml2Parser *p = (ESILibxml2Parser *)ctx; | |
86 | p->getClient()->parserDefault((const char *)ch, len); | |
87 | } | |
88 | ||
89 | xmlEntityPtr esi_getEntitySAXFunc(void * ctx, const xmlChar * name) | |
90 | { | |
91 | xmlEntityPtr res = xmlGetDocEntity(entity_doc, name); | |
92 | ||
93 | if (res == NULL) { | |
94 | const htmlEntityDesc *ent = htmlEntityLookup(name); | |
95 | ||
96 | if (ent != NULL) { | |
97 | char tmp[32]; | |
da783331 | 98 | snprintf(tmp, 32, "&#%d;", ent->value); |
964b44c3 | 99 | res = xmlAddDocEntity(entity_doc, (const xmlChar *)name, XML_INTERNAL_GENERAL_ENTITY, NULL, NULL, (const xmlChar *)tmp); |
100 | } | |
101 | } | |
102 | ||
103 | return res; | |
104 | } | |
105 | ||
106 | ESILibxml2Parser::ESILibxml2Parser(ESIParserClient *aClient) : theClient (aClient) | |
107 | { | |
108 | xmlSAXHandler sax; | |
109 | htmlDefaultSAXHandlerInit(); | |
110 | memset(&sax, 0, sizeof(sax)); | |
111 | sax.startElement = esi_startElementSAXFunc; | |
112 | sax.endElement = esi_endElementSAXFunc; | |
113 | sax.comment = esi_commentSAXFunc; | |
114 | sax.characters = esi_charactersSAXFunc; | |
115 | sax.getEntity = esi_getEntitySAXFunc; | |
116 | ||
117 | /* TODO: grab the document encoding from the headers */ | |
118 | parser = xmlCreatePushParserCtxt(&sax, static_cast<void *>(this), NULL, 0, NULL); | |
119 | xmlSetFeature(parser, "substitute entities", 0); | |
120 | ||
121 | if (entity_doc == NULL) | |
122 | entity_doc = htmlNewDoc(NULL, NULL); | |
123 | } | |
124 | ||
125 | ESILibxml2Parser::~ESILibxml2Parser() | |
126 | { | |
127 | xmlFreeParserCtxt(parser); | |
128 | parser = NULL; | |
129 | } | |
130 | ||
131 | bool | |
132 | ESILibxml2Parser::parse(char const *dataToParse, size_t const lengthOfData, bool const endOfStream) | |
133 | { | |
134 | return (xmlParseChunk(parser, dataToParse, lengthOfData, endOfStream) == 0); | |
135 | } | |
136 | ||
137a13ea | 137 | long int |
964b44c3 | 138 | ESILibxml2Parser::lineNumber() const |
139 | { | |
137a13ea | 140 | return (long int)xmlSAX2GetLineNumber(parser); |
964b44c3 | 141 | } |
142 | ||
143 | char const * | |
144 | ESILibxml2Parser::errorString() const | |
145 | { | |
146 | xmlErrorPtr error = xmlGetLastError(); | |
147 | ||
148 | if (error == NULL) | |
149 | return NULL; | |
150 | ||
151 | return error->message; | |
152 | } | |
da783331 AJ |
153 | |
154 | #endif /* USE_SQUID_ESI */ |