Mercurial > pidgin
comparison libpurple/protocols/jabber/bosh.c @ 25991:71835e00c0fc
Rename a few functions and drop some data members.
The connect/disconnect callbacks were mismatched and I kept confusing myself.
author | Paul Aurich <paul@darkrain42.org> |
---|---|
date | Sun, 15 Mar 2009 05:10:46 +0000 |
parents | f36a94f19db3 |
children | c3c2e8b64539 |
comparison
equal
deleted
inserted
replaced
25990:1c450685dbc6 | 25991:71835e00c0fc |
---|---|
30 | 30 |
31 #define MAX_HTTP_CONNECTIONS 2 | 31 #define MAX_HTTP_CONNECTIONS 2 |
32 | 32 |
33 typedef struct _PurpleHTTPConnection PurpleHTTPConnection; | 33 typedef struct _PurpleHTTPConnection PurpleHTTPConnection; |
34 | 34 |
35 typedef void (*PurpleHTTPConnectionConnectFunction)(PurpleHTTPConnection *conn); | |
36 typedef void (*PurpleHTTPConnectionDisconnectFunction)(PurpleHTTPConnection *conn); | |
37 typedef void (*PurpleBOSHConnectionConnectFunction)(PurpleBOSHConnection *conn); | 35 typedef void (*PurpleBOSHConnectionConnectFunction)(PurpleBOSHConnection *conn); |
38 typedef void (*PurpleBOSHConnectionReceiveFunction)(PurpleBOSHConnection *conn, xmlnode *node); | 36 typedef void (*PurpleBOSHConnectionReceiveFunction)(PurpleBOSHConnection *conn, xmlnode *node); |
39 | 37 |
40 static char *bosh_useragent = NULL; | 38 static char *bosh_useragent = NULL; |
41 | 39 |
71 }; | 69 }; |
72 | 70 |
73 struct _PurpleHTTPConnection { | 71 struct _PurpleHTTPConnection { |
74 int fd; | 72 int fd; |
75 gboolean ready; | 73 gboolean ready; |
76 char *host; | |
77 int port; | |
78 int ie_handle; | 74 int ie_handle; |
79 int requests; /* number of outstanding HTTP requests */ | 75 int requests; /* number of outstanding HTTP requests */ |
80 | 76 |
81 GString *buf; | 77 GString *buf; |
82 gboolean headers_done; | 78 gboolean headers_done; |
83 gsize handled_len; | 79 gsize handled_len; |
84 gsize body_len; | 80 gsize body_len; |
85 | 81 |
86 int pih; /* what? */ | |
87 PurpleHTTPConnectionConnectFunction connect_cb; | |
88 PurpleHTTPConnectionConnectFunction disconnect_cb; | |
89 PurpleBOSHConnection *bosh; | 82 PurpleBOSHConnection *bosh; |
90 }; | 83 }; |
91 | 84 |
92 static void jabber_bosh_connection_stream_restart(PurpleBOSHConnection *conn); | 85 static void jabber_bosh_connection_stream_restart(PurpleBOSHConnection *conn); |
93 static gboolean jabber_bosh_connection_error_check(PurpleBOSHConnection *conn, xmlnode *node); | 86 static gboolean jabber_bosh_connection_error_check(PurpleBOSHConnection *conn, xmlnode *node); |
94 static void jabber_bosh_connection_received(PurpleBOSHConnection *conn, xmlnode *node); | 87 static void jabber_bosh_connection_received(PurpleBOSHConnection *conn, xmlnode *node); |
95 static void jabber_bosh_connection_send_native(PurpleBOSHConnection *conn, PurpleBOSHPacketType, xmlnode *node); | 88 static void jabber_bosh_connection_send_native(PurpleBOSHConnection *conn, PurpleBOSHPacketType, xmlnode *node); |
96 | 89 |
97 static void jabber_bosh_http_connection_connect(PurpleHTTPConnection *conn); | 90 static void http_connection_connect(PurpleHTTPConnection *conn); |
98 static void jabber_bosh_connection_connected(PurpleHTTPConnection *conn); | 91 static void http_connection_send_request(PurpleHTTPConnection *conn, const GString *req); |
99 static void jabber_bosh_http_connection_disconnected(PurpleHTTPConnection *conn); | |
100 static void jabber_bosh_http_connection_send_request(PurpleHTTPConnection *conn, const GString *req); | |
101 | 92 |
102 void jabber_bosh_init(void) | 93 void jabber_bosh_init(void) |
103 { | 94 { |
104 GHashTable *ui_info = purple_core_get_ui_info(); | 95 GHashTable *ui_info = purple_core_get_ui_info(); |
105 const char *ui_name = NULL; | 96 const char *ui_name = NULL; |
127 static PurpleHTTPConnection* | 118 static PurpleHTTPConnection* |
128 jabber_bosh_http_connection_init(PurpleBOSHConnection *bosh) | 119 jabber_bosh_http_connection_init(PurpleBOSHConnection *bosh) |
129 { | 120 { |
130 PurpleHTTPConnection *conn = g_new0(PurpleHTTPConnection, 1); | 121 PurpleHTTPConnection *conn = g_new0(PurpleHTTPConnection, 1); |
131 conn->bosh = bosh; | 122 conn->bosh = bosh; |
132 conn->host = g_strdup(bosh->host); | |
133 conn->port = bosh->port; | |
134 conn->fd = -1; | 123 conn->fd = -1; |
135 conn->ready = FALSE; | 124 conn->ready = FALSE; |
136 | 125 |
137 return conn; | 126 return conn; |
138 } | 127 } |
139 | 128 |
140 static void | 129 static void |
141 jabber_bosh_http_connection_destroy(PurpleHTTPConnection *conn) | 130 jabber_bosh_http_connection_destroy(PurpleHTTPConnection *conn) |
142 { | 131 { |
143 g_free(conn->host); | |
144 | |
145 if (conn->buf) | 132 if (conn->buf) |
146 g_string_free(conn->buf, TRUE); | 133 g_string_free(conn->buf, TRUE); |
147 | 134 |
148 if (conn->ie_handle) | 135 if (conn->ie_handle) |
149 purple_input_remove(conn->ie_handle); | 136 purple_input_remove(conn->ie_handle); |
363 /* Second loop, look for one that's NULL and create a new connection */ | 350 /* Second loop, look for one that's NULL and create a new connection */ |
364 for (i = 0; i < MAX_HTTP_CONNECTIONS; ++i) { | 351 for (i = 0; i < MAX_HTTP_CONNECTIONS; ++i) { |
365 if (!conn->connections[i]) { | 352 if (!conn->connections[i]) { |
366 conn->connections[i] = jabber_bosh_http_connection_init(conn); | 353 conn->connections[i] = jabber_bosh_http_connection_init(conn); |
367 | 354 |
368 conn->connections[i]->connect_cb = jabber_bosh_connection_connected; | 355 http_connection_connect(conn->connections[i]); |
369 conn->connections[i]->disconnect_cb = jabber_bosh_http_connection_disconnected; | |
370 jabber_bosh_http_connection_connect(conn->connections[i]); | |
371 return NULL; | 356 return NULL; |
372 } | 357 } |
373 } | 358 } |
374 | 359 |
375 /* None available. */ | 360 /* None available. */ |
394 "xmlns='http://jabber.org/protocol/httpbind'/>", | 379 "xmlns='http://jabber.org/protocol/httpbind'/>", |
395 conn->js->user->domain, | 380 conn->js->user->domain, |
396 ++conn->rid); | 381 ++conn->rid); |
397 | 382 |
398 conn->receive_cb = boot_response_cb; | 383 conn->receive_cb = boot_response_cb; |
399 jabber_bosh_http_connection_send_request(conn->connections[0], buf); | 384 http_connection_send_request(conn->connections[0], buf); |
400 g_string_free(buf, TRUE); | 385 g_string_free(buf, TRUE); |
401 } | 386 } |
402 | 387 |
403 static void | 388 static void |
404 http_received_cb(const char *data, int len, PurpleBOSHConnection *conn) | 389 http_received_cb(const char *data, int len, PurpleBOSHConnection *conn) |
504 g_string_truncate(conn->pending, 0); | 489 g_string_truncate(conn->pending, 0); |
505 } | 490 } |
506 | 491 |
507 g_free(buf); | 492 g_free(buf); |
508 | 493 |
509 jabber_bosh_http_connection_send_request(chosen, packet); | 494 http_connection_send_request(chosen, packet); |
510 } | 495 } |
511 | 496 |
512 static void jabber_bosh_connection_connected(PurpleHTTPConnection *conn) { | 497 static void http_connection_connected(PurpleHTTPConnection *conn) |
498 { | |
513 conn->ready = TRUE; | 499 conn->ready = TRUE; |
514 | 500 |
515 if (conn->bosh->ready) { | 501 if (conn->bosh->ready) { |
516 purple_debug_info("jabber", "BOSH session already exists. Trying to reuse it.\n"); | 502 purple_debug_info("jabber", "BOSH session already exists. Trying to reuse it.\n"); |
517 if (conn->bosh->pending && conn->bosh->pending->len > 0) { | 503 if (conn->bosh->pending && conn->bosh->pending->len > 0) { |
530 void jabber_bosh_connection_refresh(PurpleBOSHConnection *conn) | 516 void jabber_bosh_connection_refresh(PurpleBOSHConnection *conn) |
531 { | 517 { |
532 jabber_bosh_connection_send(conn, NULL); | 518 jabber_bosh_connection_send(conn, NULL); |
533 } | 519 } |
534 | 520 |
535 static void jabber_bosh_http_connection_disconnected(PurpleHTTPConnection *conn) { | 521 static void http_connection_disconnected(PurpleHTTPConnection *conn) |
522 { | |
536 /* | 523 /* |
537 * Well, then. Fine! I never liked you anyway, server! I was cheating on you | 524 * Well, then. Fine! I never liked you anyway, server! I was cheating on you |
538 * with AIM! | 525 * with AIM! |
539 */ | 526 */ |
540 conn->ready = FALSE; | 527 conn->ready = FALSE; |
542 if (conn->bosh->pipelining) | 529 if (conn->bosh->pipelining) |
543 /* Hmmmm, fall back to multiple connections */ | 530 /* Hmmmm, fall back to multiple connections */ |
544 conn->bosh->pipelining = FALSE; | 531 conn->bosh->pipelining = FALSE; |
545 | 532 |
546 /* No! Please! Take me back. It was me, not you! I was weak! */ | 533 /* No! Please! Take me back. It was me, not you! I was weak! */ |
547 conn->connect_cb = jabber_bosh_connection_connected; | 534 http_connection_connect(conn); |
548 jabber_bosh_http_connection_connect(conn); | |
549 } | 535 } |
550 | 536 |
551 void jabber_bosh_connection_connect(PurpleBOSHConnection *bosh) { | 537 void jabber_bosh_connection_connect(PurpleBOSHConnection *bosh) { |
552 PurpleHTTPConnection *conn = bosh->connections[0]; | 538 PurpleHTTPConnection *conn = bosh->connections[0]; |
553 conn->connect_cb = jabber_bosh_connection_connected; | 539 http_connection_connect(conn); |
554 conn->disconnect_cb = jabber_bosh_http_connection_disconnected; | |
555 jabber_bosh_http_connection_connect(conn); | |
556 } | 540 } |
557 | 541 |
558 static void | 542 static void |
559 jabber_bosh_http_connection_process(PurpleHTTPConnection *conn) | 543 jabber_bosh_http_connection_process(PurpleHTTPConnection *conn) |
560 { | 544 { |
646 purple_debug_info("jabber", "bosh socket closed\n"); | 630 purple_debug_info("jabber", "bosh socket closed\n"); |
647 | 631 |
648 purple_input_remove(conn->ie_handle); | 632 purple_input_remove(conn->ie_handle); |
649 conn->ie_handle = 0; | 633 conn->ie_handle = 0; |
650 | 634 |
651 if (conn->disconnect_cb) | 635 http_connection_disconnected(conn); |
652 conn->disconnect_cb(conn); | |
653 | |
654 return; | 636 return; |
655 } | 637 } |
656 | 638 |
657 jabber_bosh_http_connection_process(conn); | 639 jabber_bosh_http_connection_process(conn); |
658 } | 640 } |
659 | 641 |
660 static void jabber_bosh_http_connection_callback(gpointer data, gint source, const gchar *error) | 642 static void http_connection_cb(gpointer data, gint source, const gchar *error) |
661 { | 643 { |
662 PurpleHTTPConnection *conn = data; | 644 PurpleHTTPConnection *conn = data; |
663 PurpleConnection *gc = conn->bosh->js->gc; | 645 PurpleConnection *gc = conn->bosh->js->gc; |
664 | 646 |
665 if (source < 0) { | 647 if (source < 0) { |
671 return; | 653 return; |
672 } | 654 } |
673 | 655 |
674 conn->fd = source; | 656 conn->fd = source; |
675 | 657 |
676 if (conn->connect_cb) | 658 http_connection_connected(conn); |
677 conn->connect_cb(conn); | |
678 | 659 |
679 conn->ie_handle = purple_input_add(conn->fd, PURPLE_INPUT_READ, | 660 conn->ie_handle = purple_input_add(conn->fd, PURPLE_INPUT_READ, |
680 jabber_bosh_http_connection_read, conn); | 661 jabber_bosh_http_connection_read, conn); |
681 } | 662 } |
682 | 663 |
683 static void jabber_bosh_http_connection_connect(PurpleHTTPConnection *conn) | 664 static void http_connection_connect(PurpleHTTPConnection *conn) |
684 { | 665 { |
685 PurpleConnection *gc = conn->bosh->js->gc; | 666 PurpleBOSHConnection *bosh = conn->bosh; |
667 PurpleConnection *gc = bosh->js->gc; | |
686 PurpleAccount *account = purple_connection_get_account(gc); | 668 PurpleAccount *account = purple_connection_get_account(gc); |
687 | 669 |
688 if ((purple_proxy_connect(conn, account, conn->host, conn->port, jabber_bosh_http_connection_callback, conn)) == NULL) { | 670 if ((purple_proxy_connect(conn, account, bosh->host, bosh->port, |
671 http_connection_cb, conn)) == NULL) { | |
689 purple_connection_error_reason(gc, | 672 purple_connection_error_reason(gc, |
690 PURPLE_CONNECTION_ERROR_NETWORK_ERROR, | 673 PURPLE_CONNECTION_ERROR_NETWORK_ERROR, |
691 _("Unable to create socket")); | 674 _("Unable to create socket")); |
692 } | 675 } |
693 } | 676 } |
694 | 677 |
695 static void | 678 static void |
696 jabber_bosh_http_connection_send_request(PurpleHTTPConnection *conn, | 679 http_connection_send_request(PurpleHTTPConnection *conn, const GString *req) |
697 const GString *req) | |
698 { | 680 { |
699 GString *packet = g_string_new(""); | 681 GString *packet = g_string_new(""); |
700 int ret; | 682 int ret; |
701 | 683 |
702 g_string_printf(packet, "POST %s HTTP/1.1\r\n" | 684 g_string_printf(packet, "POST %s HTTP/1.1\r\n" |
703 "Host: %s\r\n" | 685 "Host: %s\r\n" |
704 "User-Agent: %s\r\n" | 686 "User-Agent: %s\r\n" |
705 "Content-Encoding: text/xml; charset=utf-8\r\n" | 687 "Content-Encoding: text/xml; charset=utf-8\r\n" |
706 "Content-Length: %" G_GSIZE_FORMAT "\r\n\r\n", | 688 "Content-Length: %" G_GSIZE_FORMAT "\r\n\r\n", |
707 conn->bosh->path, conn->host, bosh_useragent, | 689 conn->bosh->path, conn->bosh->host, bosh_useragent, |
708 req->len); | 690 req->len); |
709 | 691 |
710 packet = g_string_append(packet, req->str); | 692 packet = g_string_append(packet, req->str); |
711 | 693 |
712 purple_debug_misc("jabber", "BOSH out: %s\n", packet->str); | 694 purple_debug_misc("jabber", "BOSH out: %s\n", packet->str); |