| 79 xmlnode_set_attrib(restart, "xmlns:xmpp", "urn:xmpp:xbosh"); |
79 xmlnode_set_attrib(restart, "xmlns:xmpp", "urn:xmpp:xbosh"); |
| 80 |
80 |
| 81 jabber_bosh_connection_send_native(conn, restart); |
81 jabber_bosh_connection_send_native(conn, restart); |
| 82 } |
82 } |
| 83 |
83 |
| |
84 gboolean jabber_bosh_connection_error_check(PurpleBOSHConnection *conn, xmlnode *node) { |
| |
85 char *type; |
| |
86 |
| |
87 if (!node) return; |
| |
88 type = xmlnode_get_attrib(node, "type"); |
| |
89 |
| |
90 if (type != NULL && !strcmp(type, "terminate")) { |
| |
91 conn->ready = FALSE; |
| |
92 return TRUE; |
| |
93 } |
| |
94 return FALSE; |
| |
95 } |
| |
96 |
| 84 void jabber_bosh_connection_received(PurpleBOSHConnection *conn, xmlnode *node) { |
97 void jabber_bosh_connection_received(PurpleBOSHConnection *conn, xmlnode *node) { |
| 85 xmlnode *child; |
98 xmlnode *child; |
| 86 JabberStream *js = conn->js; |
99 JabberStream *js = conn->js; |
| 87 |
100 |
| 88 if (node == NULL) return; |
101 if (node == NULL) return; |
| 89 |
102 |
| |
103 if (jabber_bosh_connection_error_check(conn, node) == TRUE) return; |
| |
104 |
| 90 child = node->child; |
105 child = node->child; |
| 91 while (child != 0) { |
106 while (child != 0) { |
| 92 if (child->type == XMLNODE_TYPE_TAG) { |
107 if (child->type == XMLNODE_TYPE_TAG) { |
| |
108 xmlnode *session = NULL; |
| |
109 if (!strcmp(child->name, "iq")) session = xmlnode_get_child(child, "session"); |
| |
110 if (session) { |
| |
111 conn->ready = TRUE; |
| |
112 } |
| 93 jabber_process_packet(js, &child); |
113 jabber_process_packet(js, &child); |
| 94 } |
114 } |
| 95 child = child->next; |
115 child = child->next; |
| 96 } |
116 } |
| 97 xmlnode_free(node); |
117 xmlnode_free(node); |
| 98 } |
118 } |
| 99 |
119 |
| 100 void jabber_bosh_connection_auth_response(PurpleBOSHConnection *conn, xmlnode *node) { |
120 void jabber_bosh_connection_auth_response(PurpleBOSHConnection *conn, xmlnode *node) { |
| 101 xmlnode *child = node->child; |
121 xmlnode *child = node->child; |
| |
122 |
| |
123 if (jabber_bosh_connection_error_check(conn, node) == TRUE) return; |
| 102 |
124 |
| 103 while(child != NULL && child->type != XMLNODE_TYPE_TAG) { |
125 while(child != NULL && child->type != XMLNODE_TYPE_TAG) { |
| 104 child = child->next; |
126 child = child->next; |
| 105 } |
127 } |
| 106 |
128 |
| 107 if (child != NULL && child->type == XMLNODE_TYPE_TAG) { |
129 if (child != NULL && child->type == XMLNODE_TYPE_TAG) { |
| 108 JabberStream *js = conn->js; |
130 JabberStream *js = conn->js; |
| 109 if (!strcmp(child->name, "success")) { |
131 if (!strcmp(child->name, "success")) { |
| 110 jabber_bosh_connection_stream_restart(conn); |
132 jabber_bosh_connection_stream_restart(conn); |
| 111 jabber_process_packet(js, &child); |
133 jabber_process_packet(js, &child); |
| 112 conn->ready = TRUE; |
|
| 113 conn->receive_cb = jabber_bosh_connection_received; |
134 conn->receive_cb = jabber_bosh_connection_received; |
| 114 } else { |
135 } else { |
| 115 js->state = JABBER_STREAM_AUTHENTICATING; |
136 js->state = JABBER_STREAM_AUTHENTICATING; |
| 116 jabber_process_packet(js, &child); |
137 jabber_process_packet(js, &child); |
| 117 } |
138 } |
| 118 } else printf("\n!! no child!!\n"); |
139 } else printf("\n!! no child!!\n"); |
| 119 } |
140 } |
| 120 |
141 |
| 121 void jabber_bosh_connection_boot_response(PurpleBOSHConnection *conn, xmlnode *node) { |
142 void jabber_bosh_connection_boot_response(PurpleBOSHConnection *conn, xmlnode *node) { |
| 122 char *version; |
143 char *version; |
| |
144 |
| |
145 if (jabber_bosh_connection_error_check(conn, node) == TRUE) return; |
| |
146 |
| 123 if (xmlnode_get_attrib(node, "sid")) { |
147 if (xmlnode_get_attrib(node, "sid")) { |
| 124 conn->sid = g_strdup(xmlnode_get_attrib(node, "sid")); |
148 conn->sid = g_strdup(xmlnode_get_attrib(node, "sid")); |
| 125 } else { |
149 } else { |
| 126 purple_debug_info("jabber", "Connection manager doesn't behave BOSH-like!\n"); |
150 purple_debug_info("jabber", "Connection manager doesn't behave BOSH-like!\n"); |
| 127 } |
151 } |
| 186 xmlnode_set_attrib(packet, "sid", conn->sid); |
210 xmlnode_set_attrib(packet, "sid", conn->sid); |
| 187 tmp = g_strdup_printf("%d", conn->rid); |
211 tmp = g_strdup_printf("%d", conn->rid); |
| 188 xmlnode_set_attrib(packet, "rid", tmp); |
212 xmlnode_set_attrib(packet, "rid", tmp); |
| 189 g_free(tmp); |
213 g_free(tmp); |
| 190 |
214 |
| 191 if (node) xmlnode_insert_child(packet, node); |
215 if (node) { |
| 192 |
216 xmlnode_insert_child(packet, node); |
| |
217 if (conn->ready == TRUE) xmlnode_set_attrib(node, "xmlns", "jabber:client"); |
| |
218 } |
| 193 jabber_bosh_connection_send_native(conn, packet); |
219 jabber_bosh_connection_send_native(conn, packet); |
| 194 } |
220 } |
| 195 |
221 |
| 196 void jabber_bosh_connection_send_native(PurpleBOSHConnection *conn, xmlnode *node) { |
222 void jabber_bosh_connection_send_native(PurpleBOSHConnection *conn, xmlnode *node) { |
| 197 PurpleHTTPRequest *request = g_new0(PurpleHTTPRequest, 1); |
223 PurpleHTTPRequest *request = g_new0(PurpleHTTPRequest, 1); |
| 291 |
318 |
| 292 if (!response) { |
319 if (!response) { |
| 293 /* check for header footer */ |
320 /* check for header footer */ |
| 294 char *found = NULL; |
321 char *found = NULL; |
| 295 if (found = g_strstr_len(conn->current_data, conn->current_len, "\r\n\r\n")) { |
322 if (found = g_strstr_len(conn->current_data, conn->current_len, "\r\n\r\n")) { |
| |
323 |
| 296 // new response |
324 // new response |
| 297 response = conn->current_response = g_new0(PurpleHTTPResponse, 1); |
325 response = conn->current_response = g_new0(PurpleHTTPResponse, 1); |
| 298 jabber_bosh_http_response_init(response); |
326 jabber_bosh_http_response_init(response); |
| 299 jabber_bosh_http_connection_receive_parse_header(response, &(conn->current_data), &(conn->current_len)); |
327 jabber_bosh_http_connection_receive_parse_header(response, &(conn->current_data), &(conn->current_len)); |
| 300 response->data_len = atoi(g_hash_table_lookup(response->header, "Content-Length")); |
328 response->data_len = atoi(g_hash_table_lookup(response->header, "Content-Length")); |
| 301 jabber_bosh_http_connection_receive(data, source, condition); |
|
| 302 } else { |
329 } else { |
| 303 printf("\nDATA: %s \n", conn->current_data); |
|
| 304 printf("\nDid not receive HTTP header\n"); |
330 printf("\nDid not receive HTTP header\n"); |
| 305 } |
331 } |
| 306 } |
332 } |
| 307 |
333 |
| 308 if (response) { |
334 if (response) { |
| 314 jabber_bosh_connection_send(bosh_conn, NULL); |
340 jabber_bosh_connection_send(bosh_conn, NULL); |
| 315 printf("\n SEND AN EMPTY REQUEST \n"); |
341 printf("\n SEND AN EMPTY REQUEST \n"); |
| 316 } |
342 } |
| 317 |
343 |
| 318 if (request) { |
344 if (request) { |
| |
345 char *old_data = conn->current_data; |
| 319 response->data = g_memdup(conn->current_data, response->data_len); |
346 response->data = g_memdup(conn->current_data, response->data_len); |
| 320 request->cb(request, response, conn->userdata); |
347 conn->current_data = g_strdup(&conn->current_data[response->data_len]); |
| |
348 conn->current_len -= response->data_len; |
| |
349 g_free(old_data); |
| |
350 |
| |
351 if (request->cb) request->cb(request, response, conn->userdata); |
| |
352 else purple_debug_info("jabber", "missing request callback!\n"); |
| |
353 |
| 321 jabber_bosh_http_request_clean(request); |
354 jabber_bosh_http_request_clean(request); |
| 322 jabber_bosh_http_response_clean(response); |
355 jabber_bosh_http_response_clean(response); |
| 323 conn->current_response = NULL; |
356 conn->current_response = NULL; |
| 324 g_free(request); |
357 g_free(request); |
| 325 g_free(response); |
358 g_free(response); |