jabberd2  2.3.6
callback.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
21 #include "sx.h"
22 
24 void _sx_element_start(void *arg, const char *name, const char **atts) {
25  sx_t s = (sx_t) arg;
26  char buf[1024];
27  char *uri, *elem, *prefix;
28  const char **attr;
29  int ns;
30  int el;
31 
32  if(s->fail) return;
33 
34  /* starting a new nad */
35  if(s->nad == NULL)
36  s->nad = nad_new();
37 
38  /* make a copy */
39  strncpy(buf, name, 1024);
40  buf[1023] = '\0';
41 
42  /* expat gives us:
43  prefixed namespaced elem: uri|elem|prefix
44  default namespaced elem: uri|elem
45  un-namespaced elem: elem
46  */
47 
48  /* extract all the bits */
49  uri = buf;
50  elem = strchr(uri, '|');
51  if(elem != NULL) {
52  *elem = '\0';
53  elem++;
54  prefix = strchr(elem, '|');
55  if(prefix != NULL) {
56  *prefix = '\0';
57  prefix++;
58  }
59  ns = nad_add_namespace(s->nad, uri, prefix);
60  } else {
61  /* un-namespaced, just take it as-is */
62  uri = NULL;
63  elem = buf;
64  prefix = NULL;
65  ns = -1;
66  }
67 
68  /* add it */
69  el = nad_append_elem(s->nad, ns, elem, s->depth - 1);
70 
71  /* now the attributes, one at a time */
72  attr = atts;
73  while(attr[0] != NULL) {
74 
75  /* make a copy */
76  strncpy(buf, attr[0], 1024);
77  buf[1023] = '\0';
78 
79  /* extract all the bits */
80  uri = buf;
81  elem = strchr(uri, '|');
82  if(elem != NULL) {
83  *elem = '\0';
84  elem++;
85  prefix = strchr(elem, '|');
86  if(prefix != NULL) {
87  *prefix = '\0';
88  prefix++;
89  }
90  ns = nad_append_namespace(s->nad, el, uri, prefix);
91  } else {
92  /* un-namespaced, just take it as-is */
93  uri = NULL;
94  elem = buf;
95  prefix = NULL;
96  ns = -1;
97  }
98 
99  /* add it */
100  nad_append_attr(s->nad, ns, elem, (char *) attr[1]);
101 
102  attr += 2;
103  }
104 
105  s->depth++;
106 }
107 
108 void _sx_element_end(void *arg, const char *name) {
109  sx_t s = (sx_t) arg;
110 
111  if(s->fail) return;
112 
113  s->depth--;
114 
115  if(s->depth == 1) {
116  /* completed nad, save it for later processing */
117  jqueue_push(s->rnadq, s->nad, 0);
118  s->nad = NULL;
119 
120  /* and reset read bytes counter */
121  s->rbytes = 0;
122  }
123 
124  /* close received */
125  else if(s->depth == 0)
126  s->depth = -1;
127 }
128 
129 void _sx_cdata(void *arg, const char *str, int len) {
130  sx_t s = (sx_t) arg;
131 
132  if(s->fail) return;
133 
134  /* no nad? no cdata */
135  if(s->nad == NULL)
136  return;
137 
138  /* go */
139  nad_append_cdata(s->nad, (char *) str, len, s->depth - 1);
140 }
141 
142 void _sx_namespace_start(void *arg, const char *prefix, const char *uri) {
143  sx_t s = (sx_t) arg;
144  int ns;
145 
146  if(s->fail) return;
147 
148  /* some versions of MSXML send xmlns='' occassionaally. it seems safe to ignore it */
149  if(uri == NULL) return;
150 
151  /* starting a new nad */
152  if(s->nad == NULL)
153  s->nad = nad_new();
154 
155  ns = nad_add_namespace(s->nad, (char *) uri, (char *) prefix);
156 
157  /* Always set the namespace (to catch cases where nad_add_namespace doesn't add it) */
158  s->nad->scope = ns;
159 }
160 
161 #ifdef HAVE_XML_STOPPARSER
162 /* Stop the parser if an entity declaration is hit. */
163 void _sx_entity_declaration(void *arg, const char *entityName,
164  int is_parameter_entity, const char *value,
165  int value_length, const char *base,
166  const char *systemId, const char *publicId,
167  const char *notationName)
168 {
169  sx_t s = (sx_t) arg;
170 
171  XML_StopParser(s->expat, XML_FALSE);
172 }
173 #endif
174 
nad_t nad_new(void)
create a new nad
Definition: nad.c:125
int nad_append_attr(nad_t nad, int ns, const char *name, const char *val)
attach new attr to the last elem
Definition: nad.c:729
void nad_append_cdata(nad_t nad, const char *cdata, int len, int depth)
append new cdata to the last elem
Definition: nad.c:737
int nad_add_namespace(nad_t nad, const char *uri, const char *prefix)
bring a new namespace into scope
Definition: nad.c:762
int nad_append_elem(nad_t nad, int ns, const char *name, int depth)
create a new elem on the list
Definition: nad.c:695
holds the state for a single stream
Definition: sx.h:253
int depth
Definition: sx.h:320
void _sx_element_end(void *arg, const char *name)
Definition: callback.c:108
int rbytes
Definition: sx.h:309
void jqueue_push(jqueue_t q, void *data, int priority)
Definition: jqueue.c:44
int fail
Definition: sx.h:321
jqueue_t rnadq
Definition: sx.h:303
nad_t nad
Definition: sx.h:324
void _sx_namespace_start(void *arg, const char *prefix, const char *uri)
Definition: callback.c:142
void _sx_cdata(void *arg, const char *str, int len)
Definition: callback.c:129
int scope
Definition: nad.h:107
struct _sx_st * sx_t
Definition: sx.h:51
void _sx_element_start(void *arg, const char *name, const char **atts)
primary expat callbacks
Definition: callback.c:24
XML_Parser expat
Definition: sx.h:319
int nad_append_namespace(nad_t nad, int elem, const char *uri, const char *prefix)
declare a namespace on an already-existing element
Definition: nad.c:798