25
|
1 /*
|
|
2 * This program is free software; you can redistribute it and/or modify
|
|
3 * it under the terms of the GNU General Public License as published by
|
|
4 * the Free Software Foundation; either version 2 of the License, or
|
|
5 * (at your option) any later version.
|
|
6 *
|
|
7 * This program is distributed in the hope that it will be useful,
|
|
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 * GNU General Public License for more details.
|
|
11 *
|
|
12 * You should have received a copy of the GNU General Public License
|
|
13 * along with this program; if not, write to the Free Software
|
|
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
15 *
|
|
16 * Jabber
|
|
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
|
|
18 */
|
|
19
|
235
|
20 #include <time.h>
|
25
|
21 #include <libxode.h>
|
|
22
|
|
23 /* 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 */
|
|
24
|
|
25 /******* internal expat callbacks *********/
|
|
26 void _xstream_startElement(xstream xs, const char* name, const char** atts)
|
|
27 {
|
|
28 pool p;
|
|
29
|
|
30 /* if xstream is bad, get outa here */
|
|
31 if(xs->status > XSTREAM_NODE) return;
|
|
32
|
|
33 if(xs->node == NULL)
|
|
34 {
|
|
35 p = pool_heap(5*1024); /* 5k, typically 1-2k each plus copy of self and workspace */
|
|
36 xs->node = xmlnode_new_tag_pool(p,name);
|
|
37 xmlnode_put_expat_attribs(xs->node, atts);
|
|
38
|
|
39 if(xs->status == XSTREAM_ROOT)
|
|
40 {
|
|
41 xs->status = XSTREAM_NODE; /* flag status that we're processing nodes now */
|
|
42 (xs->f)(XSTREAM_ROOT, xs->node, xs->arg); /* send the root, f must free all nodes */
|
|
43 xs->node = NULL;
|
|
44 }
|
|
45 }else{
|
|
46 xs->node = xmlnode_insert_tag(xs->node, name);
|
|
47 xmlnode_put_expat_attribs(xs->node, atts);
|
|
48 }
|
|
49
|
|
50 /* depth check */
|
|
51 xs->depth++;
|
|
52 if(xs->depth > XSTREAM_MAXDEPTH)
|
|
53 xs->status = XSTREAM_ERR;
|
|
54 }
|
|
55
|
|
56
|
|
57 void _xstream_endElement(xstream xs, const char* name)
|
|
58 {
|
|
59 xmlnode parent;
|
|
60
|
|
61 /* if xstream is bad, get outa here */
|
|
62 if(xs->status > XSTREAM_NODE) return;
|
|
63
|
|
64 /* if it's already NULL we've received </stream>, tell the app and we're outta here */
|
|
65 if(xs->node == NULL)
|
|
66 {
|
|
67 xs->status = XSTREAM_CLOSE;
|
|
68 (xs->f)(XSTREAM_CLOSE, NULL, xs->arg);
|
|
69 }else{
|
|
70 parent = xmlnode_get_parent(xs->node);
|
|
71
|
|
72 /* we are the top-most node, feed to the app who is responsible to delete it */
|
|
73 if(parent == NULL)
|
|
74 (xs->f)(XSTREAM_NODE, xs->node, xs->arg);
|
|
75
|
|
76 xs->node = parent;
|
|
77 }
|
|
78 xs->depth--;
|
|
79 }
|
|
80
|
|
81
|
|
82 void _xstream_charData(xstream xs, const char *str, int len)
|
|
83 {
|
|
84 /* if xstream is bad, get outa here */
|
|
85 if(xs->status > XSTREAM_NODE) return;
|
|
86
|
|
87 if(xs->node == NULL)
|
|
88 {
|
|
89 /* we must be in the root of the stream where CDATA is irrelevant */
|
|
90 return;
|
|
91 }
|
|
92
|
|
93 xmlnode_insert_cdata(xs->node, str, len);
|
|
94 }
|
|
95
|
|
96
|
|
97 void _xstream_cleanup(void *arg)
|
|
98 {
|
|
99 xstream xs = (xstream)arg;
|
|
100
|
|
101 xmlnode_free(xs->node); /* cleanup anything left over */
|
|
102 XML_ParserFree(xs->parser);
|
|
103 }
|
|
104
|
|
105
|
|
106 /* creates a new xstream with given pool, xstream will be cleaned up w/ pool */
|
|
107 xstream xstream_new(pool p, xstream_onNode f, void *arg)
|
|
108 {
|
|
109 xstream newx;
|
|
110
|
|
111 if(p == NULL || f == NULL)
|
|
112 {
|
|
113 fprintf(stderr,"Fatal Programming Error: xstream_new() was improperly called with NULL.\n");
|
|
114 return NULL;
|
|
115 }
|
|
116
|
|
117 newx = pmalloco(p, sizeof(_xstream));
|
|
118 newx->p = p;
|
|
119 newx->f = f;
|
|
120 newx->arg = arg;
|
|
121
|
|
122 /* create expat parser and ensure cleanup */
|
|
123 newx->parser = XML_ParserCreate(NULL);
|
|
124 XML_SetUserData(newx->parser, (void *)newx);
|
|
125 XML_SetElementHandler(newx->parser, (void *)_xstream_startElement, (void *)_xstream_endElement);
|
|
126 XML_SetCharacterDataHandler(newx->parser, (void *)_xstream_charData);
|
|
127 pool_cleanup(p, _xstream_cleanup, (void *)newx);
|
|
128
|
|
129 return newx;
|
|
130 }
|
|
131
|
|
132 /* attempts to parse the buff onto this stream firing events to the handler, returns the last known status */
|
|
133 int xstream_eat(xstream xs, char *buff, int len)
|
|
134 {
|
|
135 char *err;
|
|
136 xmlnode xerr;
|
|
137 static char maxerr[] = "maximum node size reached";
|
|
138 static char deeperr[] = "maximum node depth reached";
|
|
139
|
|
140 if(xs == NULL)
|
|
141 {
|
|
142 fprintf(stderr,"Fatal Programming Error: xstream_eat() was improperly called with NULL.\n");
|
|
143 return XSTREAM_ERR;
|
|
144 }
|
|
145
|
|
146 if(len == 0 || buff == NULL)
|
|
147 return xs->status;
|
|
148
|
|
149 if(len == -1) /* easy for hand-fed eat calls */
|
|
150 len = strlen(buff);
|
|
151
|
|
152 if(!XML_Parse(xs->parser, buff, len, 0))
|
|
153 {
|
|
154 err = (char *)XML_ErrorString(XML_GetErrorCode(xs->parser));
|
|
155 xs->status = XSTREAM_ERR;
|
|
156 }else if(pool_size(xmlnode_pool(xs->node)) > XSTREAM_MAXNODE || xs->cdata_len > XSTREAM_MAXNODE){
|
|
157 err = maxerr;
|
|
158 xs->status = XSTREAM_ERR;
|
|
159 }else if(xs->status == XSTREAM_ERR){ /* set within expat handlers */
|
|
160 err = deeperr;
|
|
161 }
|
|
162
|
|
163 /* fire parsing error event, make a node containing the error string */
|
|
164 if(xs->status == XSTREAM_ERR)
|
|
165 {
|
|
166 xerr = xmlnode_new_tag("error");
|
|
167 xmlnode_insert_cdata(xerr,err,-1);
|
|
168 (xs->f)(XSTREAM_ERR, xerr, xs->arg);
|
|
169 }
|
|
170
|
|
171 return xs->status;
|
|
172 }
|
|
173
|
|
174
|
|
175 /* STREAM CREATION UTILITIES */
|
|
176
|
|
177 /* give a standard template xmlnode to work from */
|
|
178 xmlnode xstream_header(char *namespace, char *to, char *from)
|
|
179 {
|
|
180 xmlnode x;
|
|
181 char id[10];
|
|
182
|
|
183 sprintf(id,"%X",(int)time(NULL));
|
|
184
|
|
185 x = xmlnode_new_tag("stream:stream");
|
|
186 xmlnode_put_attrib(x, "xmlns:stream", "http://etherx.jabber.org/streams");
|
|
187 xmlnode_put_attrib(x, "id", id);
|
|
188 if(namespace != NULL)
|
|
189 xmlnode_put_attrib(x, "xmlns", namespace);
|
|
190 if(to != NULL)
|
|
191 xmlnode_put_attrib(x, "to", to);
|
|
192 if(from != NULL)
|
|
193 xmlnode_put_attrib(x, "from", from);
|
|
194
|
|
195 return x;
|
|
196 }
|
|
197
|
|
198 /* trim the xmlnode to only the opening header :) [NO CHILDREN ALLOWED] */
|
|
199 char *xstream_header_char(xmlnode x)
|
|
200 {
|
|
201 spool s;
|
|
202 char *fixr, *head;
|
|
203
|
|
204 if(xmlnode_has_children(x))
|
|
205 {
|
|
206 fprintf(stderr,"Fatal Programming Error: xstream_header_char() was sent a header with children!\n");
|
|
207 return NULL;
|
|
208 }
|
|
209
|
|
210 s = spool_new(xmlnode_pool(x));
|
|
211 spooler(s,"<?xml version='1.0'?>",xmlnode2str(x),s);
|
|
212 head = spool_print(s);
|
|
213 fixr = strstr(head,"/>");
|
|
214 *fixr = '>';
|
|
215 ++fixr;
|
|
216 *fixr = '\0';
|
|
217
|
|
218 return head;
|
|
219 }
|
|
220
|