libpurple/protocols/jabber/parser.c

Fri, 04 Jul 2008 16:14:35 +0000

author
Evan Schoenberg <evands@pidgin.im>
date
Fri, 04 Jul 2008 16:14:35 +0000
changeset 23569
b76c41ef45d4
parent 23568
bd3e623fa265
child 23570
5990199050f5
permissions
-rw-r--r--

Drop and then recover from XML messages which trigger invalid character
errors rather than disconnecting.

7014
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
1 /*
15884
4de1981757fc sed -ie 's/gaim/purple/g'
Sean Egan <seanegan@pidgin.im>
parents: 15698
diff changeset
2 * purple - Jabber XML parser stuff
7014
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
3 *
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com>
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
5 *
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
9 * (at your option) any later version.
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
10 *
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful,
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
14 * GNU General Public License for more details.
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
15 *
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
19859
71d37b57eff2 The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents: 15884
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
7014
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
19 *
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
20 */
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
21 #include "internal.h"
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
22
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
23 #include <libxml/parser.h>
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
24
7014
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
25 #include "connection.h"
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
26 #include "debug.h"
7014
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
27 #include "jabber.h"
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
28 #include "parser.h"
15159
1acf4d03f878 [gaim-migrate @ 17883]
Nathan Walp <nwalp@pidgin.im>
parents: 15158
diff changeset
29 #include "util.h"
7014
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
30 #include "xmlnode.h"
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
31
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
32 static void
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
33 jabber_parser_element_start_libxml(void *user_data,
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
34 const xmlChar *element_name, const xmlChar *prefix, const xmlChar *namespace,
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
35 int nb_namespaces, const xmlChar **namespaces,
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
36 int nb_attributes, int nb_defaulted, const xmlChar **attributes)
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
37 {
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
38 JabberStream *js = user_data;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
39 xmlnode *node;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
40 int i;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
41
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
42 if(!element_name) {
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
43 return;
14690
9287ecc4adb1 [gaim-migrate @ 17369]
Daniel Atallah <datallah@pidgin.im>
parents: 14685
diff changeset
44 } else if(!xmlStrcmp(element_name, (xmlChar*) "stream")) {
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
45 js->protocol_version = JABBER_PROTO_0_9;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
46 for(i=0; i < nb_attributes * 5; i += 5) {
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
47 int attrib_len = attributes[i+4] - attributes[i+3];
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
48 char *attrib = g_malloc(attrib_len + 1);
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
49 memcpy(attrib, attributes[i+3], attrib_len);
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
50 attrib[attrib_len] = '\0';
14498
ede839a78714 [gaim-migrate @ 17150]
Mark Doliner <markdoliner@pidgin.im>
parents: 14476
diff changeset
51
14690
9287ecc4adb1 [gaim-migrate @ 17369]
Daniel Atallah <datallah@pidgin.im>
parents: 14685
diff changeset
52 if(!xmlStrcmp(attributes[i], (xmlChar*) "version")
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
53 && !strcmp(attrib, "1.0")) {
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
54 js->protocol_version = JABBER_PROTO_1_0;
14685
7622a9ca1794 [gaim-migrate @ 17352]
Daniel Atallah <datallah@pidgin.im>
parents: 14538
diff changeset
55 g_free(attrib);
14690
9287ecc4adb1 [gaim-migrate @ 17369]
Daniel Atallah <datallah@pidgin.im>
parents: 14685
diff changeset
56 } else if(!xmlStrcmp(attributes[i], (xmlChar*) "id")) {
15698
bcec3cb58d30 Patch from MatsMattsson (Adium Trac #6429) which fixes a leak of attrib for any attribute that isn't 'id' or 'version'.
Evan Schoenberg <evands@pidgin.im>
parents: 15558
diff changeset
57 g_free(js->stream_id);
14685
7622a9ca1794 [gaim-migrate @ 17352]
Daniel Atallah <datallah@pidgin.im>
parents: 14538
diff changeset
58 js->stream_id = attrib;
15698
bcec3cb58d30 Patch from MatsMattsson (Adium Trac #6429) which fixes a leak of attrib for any attribute that isn't 'id' or 'version'.
Evan Schoenberg <evands@pidgin.im>
parents: 15558
diff changeset
59 } else {
bcec3cb58d30 Patch from MatsMattsson (Adium Trac #6429) which fixes a leak of attrib for any attribute that isn't 'id' or 'version'.
Evan Schoenberg <evands@pidgin.im>
parents: 15558
diff changeset
60 g_free(attrib);
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
61 }
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
62 }
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
63 if(js->protocol_version == JABBER_PROTO_0_9)
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
64 js->auth_type = JABBER_AUTH_IQ_AUTH;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
65
17813
c5cc8ebf62c0 Introduced an additional connection stage to the jabber connection progress in order to be able to tell in the application, whether SSL/TLS is enabled, so it can display a lock icon next to the connection.
Andreas Monitzer <am@adiumx.com>
parents: 17810
diff changeset
66 if(js->state == JABBER_STREAM_INITIALIZING || js->state == JABBER_STREAM_INITIALIZING_ENCRYPTION)
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
67 jabber_stream_set_state(js, JABBER_STREAM_AUTHENTICATING);
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
68 } else {
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
69
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
70 if(js->current)
14690
9287ecc4adb1 [gaim-migrate @ 17369]
Daniel Atallah <datallah@pidgin.im>
parents: 14685
diff changeset
71 node = xmlnode_new_child(js->current, (const char*) element_name);
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
72 else
14690
9287ecc4adb1 [gaim-migrate @ 17369]
Daniel Atallah <datallah@pidgin.im>
parents: 14685
diff changeset
73 node = xmlnode_new((const char*) element_name);
9287ecc4adb1 [gaim-migrate @ 17369]
Daniel Atallah <datallah@pidgin.im>
parents: 14685
diff changeset
74 xmlnode_set_namespace(node, (const char*) namespace);
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
75
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
76 for(i=0; i < nb_attributes * 5; i+=5) {
15158
501a55683bf2 [gaim-migrate @ 17882]
Nathan Walp <nwalp@pidgin.im>
parents: 14700
diff changeset
77 char *txt;
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
78 int attrib_len = attributes[i+4] - attributes[i+3];
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
79 char *attrib = g_malloc(attrib_len + 1);
15265
a5b0ab58b05d [gaim-migrate @ 17993]
Sean Egan <seanegan@pidgin.im>
parents: 15159
diff changeset
80 char *attrib_ns = NULL;
a5b0ab58b05d [gaim-migrate @ 17993]
Sean Egan <seanegan@pidgin.im>
parents: 15159
diff changeset
81
a5b0ab58b05d [gaim-migrate @ 17993]
Sean Egan <seanegan@pidgin.im>
parents: 15159
diff changeset
82 if (attributes[i+2]) {
20224
d4b827c606db applied changes from 4d50bf3b08569aa2108a9f5da47fb1548d0c7dd9
Luke Schierer <lschiere@pidgin.im>
parents: 19897
diff changeset
83 attrib_ns = g_strdup((char*)attributes[i+2]);
15265
a5b0ab58b05d [gaim-migrate @ 17993]
Sean Egan <seanegan@pidgin.im>
parents: 15159
diff changeset
84 }
a5b0ab58b05d [gaim-migrate @ 17993]
Sean Egan <seanegan@pidgin.im>
parents: 15159
diff changeset
85
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
86 memcpy(attrib, attributes[i+3], attrib_len);
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
87 attrib[attrib_len] = '\0';
15265
a5b0ab58b05d [gaim-migrate @ 17993]
Sean Egan <seanegan@pidgin.im>
parents: 15159
diff changeset
88
15158
501a55683bf2 [gaim-migrate @ 17882]
Nathan Walp <nwalp@pidgin.im>
parents: 14700
diff changeset
89 txt = attrib;
15884
4de1981757fc sed -ie 's/gaim/purple/g'
Sean Egan <seanegan@pidgin.im>
parents: 15698
diff changeset
90 attrib = purple_unescape_html(txt);
15158
501a55683bf2 [gaim-migrate @ 17882]
Nathan Walp <nwalp@pidgin.im>
parents: 14700
diff changeset
91 g_free(txt);
15265
a5b0ab58b05d [gaim-migrate @ 17993]
Sean Egan <seanegan@pidgin.im>
parents: 15159
diff changeset
92 xmlnode_set_attrib_with_namespace(node, (const char*) attributes[i], attrib_ns, attrib);
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
93 g_free(attrib);
15265
a5b0ab58b05d [gaim-migrate @ 17993]
Sean Egan <seanegan@pidgin.im>
parents: 15159
diff changeset
94 g_free(attrib_ns);
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
95 }
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
96
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
97 js->current = node;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
98 }
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
99 }
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
100
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
101 static void
14498
ede839a78714 [gaim-migrate @ 17150]
Mark Doliner <markdoliner@pidgin.im>
parents: 14476
diff changeset
102 jabber_parser_element_end_libxml(void *user_data, const xmlChar *element_name,
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
103 const xmlChar *prefix, const xmlChar *namespace)
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
104 {
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
105 JabberStream *js = user_data;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
106
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
107 if(!js->current)
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
108 return;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
109
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
110 if(js->current->parent) {
14690
9287ecc4adb1 [gaim-migrate @ 17369]
Daniel Atallah <datallah@pidgin.im>
parents: 14685
diff changeset
111 if(!xmlStrcmp((xmlChar*) js->current->name, element_name))
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
112 js->current = js->current->parent;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
113 } else {
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
114 xmlnode *packet = js->current;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
115 js->current = NULL;
17828
6957bf9d7330 Fixed a bug in the XMPP parser involving event handlers that replace the xmlnode packet. This caused double frees in this situation. The replacing function must free the xmlnode, since multiple event handlers could do this, and the intermediate xml trees would leak otherwise.
Andreas Monitzer <am@adiumx.com>
parents: 17813
diff changeset
116 jabber_process_packet(js, &packet);
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
117 xmlnode_free(packet);
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
118 }
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
119 }
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
120
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
121 static void
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
122 jabber_parser_element_text_libxml(void *user_data, const xmlChar *text, int text_len)
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
123 {
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
124 JabberStream *js = user_data;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
125
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
126 if(!js->current)
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
127 return;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
128
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
129 if(!text || !text_len)
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
130 return;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
131
14690
9287ecc4adb1 [gaim-migrate @ 17369]
Daniel Atallah <datallah@pidgin.im>
parents: 14685
diff changeset
132 xmlnode_insert_data(js->current, (const char*) text, text_len);
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
133 }
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
134
23567
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
135 static void
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
136 jabber_parser_structured_error_handler(void *user_data, xmlErrorPtr error)
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
137 {
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
138 JabberStream *js = user_data;
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
139
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
140 purple_debug_error("jabber", "XML parser error for JabberStream %p: "
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
141 "Domain %i, code %i, level %i: %s\n",
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
142 js,
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
143 error->domain, error->code, error->level,
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
144 (error->message ? error->message : "(null)"));
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
145 }
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
146
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
147 static xmlSAXHandler jabber_parser_libxml = {
23077
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
148 NULL, /*internalSubset*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
149 NULL, /*isStandalone*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
150 NULL, /*hasInternalSubset*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
151 NULL, /*hasExternalSubset*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
152 NULL, /*resolveEntity*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
153 NULL, /*getEntity*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
154 NULL, /*entityDecl*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
155 NULL, /*notationDecl*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
156 NULL, /*attributeDecl*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
157 NULL, /*elementDecl*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
158 NULL, /*unparsedEntityDecl*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
159 NULL, /*setDocumentLocator*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
160 NULL, /*startDocument*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
161 NULL, /*endDocument*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
162 NULL, /*startElement*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
163 NULL, /*endElement*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
164 NULL, /*reference*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
165 jabber_parser_element_text_libxml, /*characters*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
166 NULL, /*ignorableWhitespace*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
167 NULL, /*processingInstruction*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
168 NULL, /*comment*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
169 NULL, /*warning*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
170 NULL, /*error*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
171 NULL, /*fatalError*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
172 NULL, /*getParameterEntity*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
173 NULL, /*cdataBlock*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
174 NULL, /*externalSubset*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
175 XML_SAX2_MAGIC, /*initialized*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
176 NULL, /*_private*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
177 jabber_parser_element_start_libxml, /*startElementNs*/
431e35debb40 Using named elements in a struct initialization doesn't work in MSVC.
Daniel Atallah <datallah@pidgin.im>
parents: 21279
diff changeset
178 jabber_parser_element_end_libxml, /*endElementNs*/
23567
9189d89be26f Added a structured error handler callback to the xmlSAXHandler struct.
Evan Schoenberg <evands@pidgin.im>
parents: 23077
diff changeset
179 jabber_parser_structured_error_handler /*serror*/
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
180 };
7014
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
181
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
182 void
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
183 jabber_parser_setup(JabberStream *js)
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
184 {
14538
ada84c622f39 [gaim-migrate @ 17194]
Etan Reisner <deryni@pidgin.im>
parents: 14498
diff changeset
185 /* This seems backwards, but it makes sense. The libxml code creates
ada84c622f39 [gaim-migrate @ 17194]
Etan Reisner <deryni@pidgin.im>
parents: 14498
diff changeset
186 * the parser context when you try to use it (this way, it can figure
ada84c622f39 [gaim-migrate @ 17194]
Etan Reisner <deryni@pidgin.im>
parents: 14498
diff changeset
187 * out the encoding at creation time. So, setting up the parser is
ada84c622f39 [gaim-migrate @ 17194]
Etan Reisner <deryni@pidgin.im>
parents: 14498
diff changeset
188 * just a matter of destroying any current parser. */
17810
c8d4297080cb Fixed a leak: The XML parser was never cleaned up on disconnect.
Andreas Monitzer <am@adiumx.com>
parents: 15884
diff changeset
189 jabber_parser_free(js);
c8d4297080cb Fixed a leak: The XML parser was never cleaned up on disconnect.
Andreas Monitzer <am@adiumx.com>
parents: 15884
diff changeset
190 }
c8d4297080cb Fixed a leak: The XML parser was never cleaned up on disconnect.
Andreas Monitzer <am@adiumx.com>
parents: 15884
diff changeset
191
c8d4297080cb Fixed a leak: The XML parser was never cleaned up on disconnect.
Andreas Monitzer <am@adiumx.com>
parents: 15884
diff changeset
192 void jabber_parser_free(JabberStream *js) {
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
193 if (js->context) {
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
194 xmlParseChunk(js->context, NULL,0,1);
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
195 xmlFreeParserCtxt(js->context);
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
196 js->context = NULL;
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
197 }
7014
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
198 }
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
199
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
200 void jabber_parser_process(JabberStream *js, const char *buf, int len)
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
201 {
23568
bd3e623fa265 xmlParseChunk() never returns less than 0. It retusn an error code from
Evan Schoenberg <evands@pidgin.im>
parents: 23567
diff changeset
202 int ret;
bd3e623fa265 xmlParseChunk() never returns less than 0. It retusn an error code from
Evan Schoenberg <evands@pidgin.im>
parents: 23567
diff changeset
203
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
204 if (js->context == NULL) {
14538
ada84c622f39 [gaim-migrate @ 17194]
Etan Reisner <deryni@pidgin.im>
parents: 14498
diff changeset
205 /* libxml inconsistently starts parsing on creating the
ada84c622f39 [gaim-migrate @ 17194]
Etan Reisner <deryni@pidgin.im>
parents: 14498
diff changeset
206 * parser, so do a ParseChunk right afterwards to force it. */
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
207 js->context = xmlCreatePushParserCtxt(&jabber_parser_libxml, js, buf, len, NULL);
14700
98dc3d9a69e3 [gaim-migrate @ 17384]
Sean Egan <seanegan@pidgin.im>
parents: 14690
diff changeset
208 xmlParseChunk(js->context, "", 0, 0);
23568
bd3e623fa265 xmlParseChunk() never returns less than 0. It retusn an error code from
Evan Schoenberg <evands@pidgin.im>
parents: 23567
diff changeset
209 } else if ((ret = xmlParseChunk(js->context, buf, len, 0)) != XML_ERR_OK) {
bd3e623fa265 xmlParseChunk() never returns less than 0. It retusn an error code from
Evan Schoenberg <evands@pidgin.im>
parents: 23567
diff changeset
210 purple_debug_error("jabber", "xmlParseChunk returned error %i", ret);
bd3e623fa265 xmlParseChunk() never returns less than 0. It retusn an error code from
Evan Schoenberg <evands@pidgin.im>
parents: 23567
diff changeset
211
23569
b76c41ef45d4 Drop and then recover from XML messages which trigger invalid character
Evan Schoenberg <evands@pidgin.im>
parents: 23568
diff changeset
212 if ((ret >= XML_ERR_INVALID_HEX_CHARREF) && (ret <= XML_ERR_INVALID_CHAR)) {
b76c41ef45d4 Drop and then recover from XML messages which trigger invalid character
Evan Schoenberg <evands@pidgin.im>
parents: 23568
diff changeset
213 /* If the error involves an invalid character, just drop this message.
b76c41ef45d4 Drop and then recover from XML messages which trigger invalid character
Evan Schoenberg <evands@pidgin.im>
parents: 23568
diff changeset
214 * We'll create a new parser next time it's needed. */
b76c41ef45d4 Drop and then recover from XML messages which trigger invalid character
Evan Schoenberg <evands@pidgin.im>
parents: 23568
diff changeset
215 jabber_parser_free(js);
b76c41ef45d4 Drop and then recover from XML messages which trigger invalid character
Evan Schoenberg <evands@pidgin.im>
parents: 23568
diff changeset
216 } else {
b76c41ef45d4 Drop and then recover from XML messages which trigger invalid character
Evan Schoenberg <evands@pidgin.im>
parents: 23568
diff changeset
217 purple_connection_error_reason (js->gc,
b76c41ef45d4 Drop and then recover from XML messages which trigger invalid character
Evan Schoenberg <evands@pidgin.im>
parents: 23568
diff changeset
218 PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
b76c41ef45d4 Drop and then recover from XML messages which trigger invalid character
Evan Schoenberg <evands@pidgin.im>
parents: 23568
diff changeset
219 _("XML Parse error"));
b76c41ef45d4 Drop and then recover from XML messages which trigger invalid character
Evan Schoenberg <evands@pidgin.im>
parents: 23568
diff changeset
220 }
13808
411ee56da344 [gaim-migrate @ 16229]
Sean Egan <seanegan@pidgin.im>
parents: 8296
diff changeset
221 }
7014
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
222 }
11471fae7ba0 [gaim-migrate @ 7577]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
223

mercurial