3127
|
1 /* --------------------------------------------------------------------------
|
|
2 *
|
|
3 * License
|
|
4 *
|
|
5 * The contents of this file are subject to the Jabber Open Source License
|
|
6 * Version 1.0 (the "JOSL"). You may not copy or use this file, in either
|
|
7 * source code or executable form, except in compliance with the JOSL. You
|
|
8 * may obtain a copy of the JOSL at http://www.jabber.org/ or at
|
|
9 * http://www.opensource.org/.
|
2086
|
10 *
|
3127
|
11 * Software distributed under the JOSL is distributed on an "AS IS" basis,
|
|
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL
|
|
13 * for the specific language governing rights and limitations under the
|
|
14 * JOSL.
|
|
15 *
|
|
16 * Copyrights
|
|
17 *
|
|
18 * Portions created by or assigned to Jabber.com, Inc. are
|
|
19 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact
|
|
20 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
|
2086
|
21 *
|
3127
|
22 * Portions Copyright (c) 1998-1999 Jeremie Miller.
|
|
23 *
|
|
24 * Acknowledgements
|
|
25 *
|
|
26 * Special thanks to the Jabber Open Source Contributors for their
|
|
27 * suggestions and support of Jabber.
|
|
28 *
|
|
29 * Alternatively, the contents of this file may be used under the terms of the
|
|
30 * GNU General Public License Version 2 or later (the "GPL"), in which case
|
|
31 * the provisions of the GPL are applicable instead of those above. If you
|
|
32 * wish to allow use of your version of this file only under the terms of the
|
|
33 * GPL and not to allow others to use your version of this file under the JOSL,
|
|
34 * indicate your decision by deleting the provisions above and replace them
|
|
35 * with the notice and other provisions required by the GPL. If you do not
|
|
36 * delete the provisions above, a recipient may use your version of this file
|
|
37 * under either the JOSL or the GPL.
|
|
38 *
|
|
39 *
|
|
40 * --------------------------------------------------------------------------*/
|
2086
|
41
|
3127
|
42 #include "lib.h"
|
2086
|
43
|
|
44 /* xstream is a way to have a consistent method of handling incoming XML Stream based events... it doesn't handle the generation of an XML Stream, but provides some facilities to help do that */
|
|
45
|
|
46 /******* internal expat callbacks *********/
|
|
47 void _xstream_startElement(xstream xs, const char* name, const char** atts)
|
|
48 {
|
|
49 pool p;
|
|
50
|
|
51 /* if xstream is bad, get outa here */
|
|
52 if(xs->status > XSTREAM_NODE) return;
|
|
53
|
|
54 if(xs->node == NULL)
|
|
55 {
|
|
56 p = pool_heap(5*1024); /* 5k, typically 1-2k each plus copy of self and workspace */
|
|
57 xs->node = xmlnode_new_tag_pool(p,name);
|
|
58 xmlnode_put_expat_attribs(xs->node, atts);
|
|
59
|
|
60 if(xs->status == XSTREAM_ROOT)
|
|
61 {
|
|
62 xs->status = XSTREAM_NODE; /* flag status that we're processing nodes now */
|
|
63 (xs->f)(XSTREAM_ROOT, xs->node, xs->arg); /* send the root, f must free all nodes */
|
|
64 xs->node = NULL;
|
|
65 }
|
|
66 }else{
|
|
67 xs->node = xmlnode_insert_tag(xs->node, name);
|
|
68 xmlnode_put_expat_attribs(xs->node, atts);
|
|
69 }
|
|
70
|
|
71 /* depth check */
|
|
72 xs->depth++;
|
|
73 if(xs->depth > XSTREAM_MAXDEPTH)
|
|
74 xs->status = XSTREAM_ERR;
|
|
75 }
|
|
76
|
|
77
|
|
78 void _xstream_endElement(xstream xs, const char* name)
|
|
79 {
|
|
80 xmlnode parent;
|
|
81
|
|
82 /* if xstream is bad, get outa here */
|
|
83 if(xs->status > XSTREAM_NODE) return;
|
|
84
|
|
85 /* if it's already NULL we've received </stream>, tell the app and we're outta here */
|
|
86 if(xs->node == NULL)
|
|
87 {
|
|
88 xs->status = XSTREAM_CLOSE;
|
|
89 (xs->f)(XSTREAM_CLOSE, NULL, xs->arg);
|
|
90 }else{
|
|
91 parent = xmlnode_get_parent(xs->node);
|
|
92
|
|
93 /* we are the top-most node, feed to the app who is responsible to delete it */
|
|
94 if(parent == NULL)
|
|
95 (xs->f)(XSTREAM_NODE, xs->node, xs->arg);
|
|
96
|
|
97 xs->node = parent;
|
|
98 }
|
|
99 xs->depth--;
|
|
100 }
|
|
101
|
|
102
|
|
103 void _xstream_charData(xstream xs, const char *str, int len)
|
|
104 {
|
|
105 /* if xstream is bad, get outa here */
|
|
106 if(xs->status > XSTREAM_NODE) return;
|
|
107
|
|
108 if(xs->node == NULL)
|
|
109 {
|
|
110 /* we must be in the root of the stream where CDATA is irrelevant */
|
|
111 return;
|
|
112 }
|
|
113
|
|
114 xmlnode_insert_cdata(xs->node, str, len);
|
|
115 }
|
|
116
|
|
117
|
|
118 void _xstream_cleanup(void *arg)
|
|
119 {
|
|
120 xstream xs = (xstream)arg;
|
|
121
|
|
122 xmlnode_free(xs->node); /* cleanup anything left over */
|
|
123 XML_ParserFree(xs->parser);
|
|
124 }
|
|
125
|
|
126
|
|
127 /* creates a new xstream with given pool, xstream will be cleaned up w/ pool */
|
|
128 xstream xstream_new(pool p, xstream_onNode f, void *arg)
|
|
129 {
|
|
130 xstream newx;
|
|
131
|
|
132 if(p == NULL || f == NULL)
|
|
133 {
|
|
134 fprintf(stderr,"Fatal Programming Error: xstream_new() was improperly called with NULL.\n");
|
|
135 return NULL;
|
|
136 }
|
|
137
|
|
138 newx = pmalloco(p, sizeof(_xstream));
|
|
139 newx->p = p;
|
|
140 newx->f = f;
|
|
141 newx->arg = arg;
|
|
142
|
|
143 /* create expat parser and ensure cleanup */
|
|
144 newx->parser = XML_ParserCreate(NULL);
|
|
145 XML_SetUserData(newx->parser, (void *)newx);
|
|
146 XML_SetElementHandler(newx->parser, (void *)_xstream_startElement, (void *)_xstream_endElement);
|
|
147 XML_SetCharacterDataHandler(newx->parser, (void *)_xstream_charData);
|
|
148 pool_cleanup(p, _xstream_cleanup, (void *)newx);
|
|
149
|
|
150 return newx;
|
|
151 }
|
|
152
|
|
153 /* attempts to parse the buff onto this stream firing events to the handler, returns the last known status */
|
|
154 int xstream_eat(xstream xs, char *buff, int len)
|
|
155 {
|
3159
|
156 char *err = NULL;
|
2086
|
157 xmlnode xerr;
|
|
158 static char maxerr[] = "maximum node size reached";
|
|
159 static char deeperr[] = "maximum node depth reached";
|
|
160
|
|
161 if(xs == NULL)
|
|
162 {
|
|
163 fprintf(stderr,"Fatal Programming Error: xstream_eat() was improperly called with NULL.\n");
|
|
164 return XSTREAM_ERR;
|
|
165 }
|
|
166
|
|
167 if(len == 0 || buff == NULL)
|
|
168 return xs->status;
|
|
169
|
|
170 if(len == -1) /* easy for hand-fed eat calls */
|
|
171 len = strlen(buff);
|
|
172
|
|
173 if(!XML_Parse(xs->parser, buff, len, 0))
|
|
174 {
|
|
175 err = (char *)XML_ErrorString(XML_GetErrorCode(xs->parser));
|
|
176 xs->status = XSTREAM_ERR;
|
|
177 }else if(pool_size(xmlnode_pool(xs->node)) > XSTREAM_MAXNODE || xs->cdata_len > XSTREAM_MAXNODE){
|
|
178 err = maxerr;
|
|
179 xs->status = XSTREAM_ERR;
|
|
180 }else if(xs->status == XSTREAM_ERR){ /* set within expat handlers */
|
|
181 err = deeperr;
|
|
182 }
|
|
183
|
|
184 /* fire parsing error event, make a node containing the error string */
|
|
185 if(xs->status == XSTREAM_ERR)
|
|
186 {
|
|
187 xerr = xmlnode_new_tag("error");
|
|
188 xmlnode_insert_cdata(xerr,err,-1);
|
|
189 (xs->f)(XSTREAM_ERR, xerr, xs->arg);
|
|
190 }
|
|
191
|
|
192 return xs->status;
|
|
193 }
|
|
194
|
|
195
|
|
196 /* STREAM CREATION UTILITIES */
|
|
197
|
|
198 /* give a standard template xmlnode to work from */
|
|
199 xmlnode xstream_header(char *namespace, char *to, char *from)
|
|
200 {
|
|
201 xmlnode x;
|
|
202 char id[10];
|
|
203
|
|
204 sprintf(id,"%X",(int)time(NULL));
|
|
205
|
|
206 x = xmlnode_new_tag("stream:stream");
|
|
207 xmlnode_put_attrib(x, "xmlns:stream", "http://etherx.jabber.org/streams");
|
|
208 xmlnode_put_attrib(x, "id", id);
|
|
209 if(namespace != NULL)
|
|
210 xmlnode_put_attrib(x, "xmlns", namespace);
|
|
211 if(to != NULL)
|
|
212 xmlnode_put_attrib(x, "to", to);
|
|
213 if(from != NULL)
|
|
214 xmlnode_put_attrib(x, "from", from);
|
|
215
|
|
216 return x;
|
|
217 }
|
|
218
|
|
219 /* trim the xmlnode to only the opening header :) [NO CHILDREN ALLOWED] */
|
|
220 char *xstream_header_char(xmlnode x)
|
|
221 {
|
|
222 spool s;
|
|
223 char *fixr, *head;
|
|
224
|
|
225 if(xmlnode_has_children(x))
|
|
226 {
|
|
227 fprintf(stderr,"Fatal Programming Error: xstream_header_char() was sent a header with children!\n");
|
|
228 return NULL;
|
|
229 }
|
|
230
|
|
231 s = spool_new(xmlnode_pool(x));
|
|
232 spooler(s,"<?xml version='1.0'?>",xmlnode2str(x),s);
|
|
233 head = spool_print(s);
|
|
234 fixr = strstr(head,"/>");
|
|
235 *fixr = '>';
|
|
236 ++fixr;
|
|
237 *fixr = '\0';
|
|
238
|
|
239 return head;
|
|
240 }
|
|
241
|