4 * AUTHOR: Joachim Bauch (mail@joachim-bauch.de)
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
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.
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.
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.
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.
35 * The ESI Libxml2 parser is Copyright (c) 2004 by Joachim Bauch
36 * http://www.joachim-bauch.de
37 * mail@joachim-bauch.de
42 #if USE_SQUID_ESI && HAVE_LIBXML2
44 #include "esi/Libxml2Parser.h"
46 // the global document that will store the resolved entity
48 static htmlDocPtr entity_doc
= NULL
;
50 EsiParserDefinition(ESILibxml2Parser
);
52 // the SAX callback functions
53 void esi_startElementSAXFunc(void * ctx
, const xmlChar
* name
, const xmlChar
** atts
)
56 xmlChar
**tmp
= (xmlChar
**)atts
;
58 while (tmp
&& *tmp
!= NULL
) {
63 // we increased on every key and value
66 ESILibxml2Parser
*p
= (ESILibxml2Parser
*)ctx
;
68 p
->getClient()->start((const char *)name
, (const char **)atts
, count
);
71 void esi_endElementSAXFunc(void * ctx
, const xmlChar
* name
)
73 ESILibxml2Parser
*p
= (ESILibxml2Parser
*)ctx
;
74 p
->getClient()->end((const char *)name
);
77 void esi_commentSAXFunc(void * ctx
, const xmlChar
* value
)
79 ESILibxml2Parser
*p
= (ESILibxml2Parser
*)ctx
;
80 p
->getClient()->parserComment((const char *)value
);
83 void esi_charactersSAXFunc(void *ctx
, const xmlChar
*ch
, int len
)
85 ESILibxml2Parser
*p
= (ESILibxml2Parser
*)ctx
;
86 p
->getClient()->parserDefault((const char *)ch
, len
);
89 xmlEntityPtr
esi_getEntitySAXFunc(void * ctx
, const xmlChar
* name
)
91 xmlEntityPtr res
= xmlGetDocEntity(entity_doc
, name
);
94 const htmlEntityDesc
*ent
= htmlEntityLookup(name
);
98 snprintf(tmp
, 32, "&#%d;", ent
->value
);
99 res
= xmlAddDocEntity(entity_doc
, (const xmlChar
*)name
, XML_INTERNAL_GENERAL_ENTITY
, NULL
, NULL
, (const xmlChar
*)tmp
);
106 ESILibxml2Parser::ESILibxml2Parser(ESIParserClient
*aClient
) : theClient (aClient
)
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
;
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);
121 if (entity_doc
== NULL
)
122 entity_doc
= htmlNewDoc(NULL
, NULL
);
125 ESILibxml2Parser::~ESILibxml2Parser()
127 xmlFreeParserCtxt(parser
);
132 ESILibxml2Parser::parse(char const *dataToParse
, size_t const lengthOfData
, bool const endOfStream
)
134 return (xmlParseChunk(parser
, dataToParse
, lengthOfData
, endOfStream
) == 0);
138 ESILibxml2Parser::lineNumber() const
140 return (long int)xmlSAX2GetLineNumber(parser
);
144 ESILibxml2Parser::errorString() const
146 xmlErrorPtr error
= xmlGetLastError();
151 return error
->message
;
154 #endif /* USE_SQUID_ESI */