Wed, 07 Aug 2013 10:38:33 +0200
HTTP: removing extra yahoo HTTP implementations - chat list browser
| libpurple/protocols/yahoo/yahoochat.c | file | annotate | diff | comparison | revisions |
--- a/libpurple/protocols/yahoo/yahoochat.c Tue Aug 06 16:47:35 2013 +0200 +++ b/libpurple/protocols/yahoo/yahoochat.c Wed Aug 07 10:38:33 2013 +0200 @@ -1175,31 +1175,19 @@ } struct yahoo_roomlist { - int fd; - int inpa; - gchar *txbuf; - gsize tx_written; - guchar *rxqueue; - int rxlen; - gboolean started; - char *path; - char *host; + PurpleHttpConnection *hc; + gchar *url; + PurpleRoomlist *list; PurpleRoomlistRoom *cat; PurpleRoomlistRoom *ucat; - GMarkupParseContext *parse; }; static void yahoo_roomlist_destroy(struct yahoo_roomlist *yrl) { - if (yrl->inpa) - purple_input_remove(yrl->inpa); - g_free(yrl->txbuf); - g_free(yrl->rxqueue); - g_free(yrl->path); - g_free(yrl->host); - if (yrl->parse) - g_markup_parse_context_free(yrl->parse); + purple_http_conn_cancel(yrl->hc); + g_free(yrl->url); + g_free(yrl); } @@ -1380,121 +1368,63 @@ purple_roomlist_unref(list); } -static void yahoo_roomlist_pending(gpointer data, gint source, PurpleInputCondition cond) +static void +yahoo_roomlist_got(PurpleHttpConnection *http_conn, + PurpleHttpResponse *response, gpointer _yrl) { - struct yahoo_roomlist *yrl = data; - PurpleRoomlist *list = yrl->list; - char buf[1024]; - int len; - guchar *start; - struct yahoo_chatxml_state *s; + struct yahoo_roomlist *yrl = _yrl; + PurpleConnection *gc; + GMarkupParseContext *parse; - len = read(yrl->fd, buf, sizeof(buf)); + yrl->hc = NULL; - if (len < 0 && errno == EAGAIN) - return; + gc = purple_account_get_connection(purple_roomlist_get_account( + yrl->list)); - if (len <= 0) { - if (yrl->parse) - g_markup_parse_context_end_parse(yrl->parse, NULL); - yahoo_roomlist_cleanup(list, yrl); + if (!purple_http_response_is_successfull(response)) { + purple_notify_error(gc, NULL, _("Unable to connect"), + _("Fetching the room list failed.")); + yahoo_roomlist_cleanup(yrl->list, yrl); return; } - yrl->rxqueue = g_realloc(yrl->rxqueue, len + yrl->rxlen); - memcpy(yrl->rxqueue + yrl->rxlen, buf, len); - yrl->rxlen += len; + parse = g_markup_parse_context_new(&parser, 0, + yahoo_chatxml_state_new(yrl->list, yrl), + (GDestroyNotify)yahoo_chatxml_state_destroy); - if (!yrl->started) { - yrl->started = TRUE; - start = (guchar *)g_strstr_len((char *)yrl->rxqueue, yrl->rxlen, "\r\n\r\n"); - if (!start || (start - yrl->rxqueue + 4) >= yrl->rxlen) - return; - start += 4; - } else { - start = yrl->rxqueue; - } - - if (yrl->parse == NULL) { - s = yahoo_chatxml_state_new(list, yrl); - yrl->parse = g_markup_parse_context_new(&parser, 0, s, - (GDestroyNotify)yahoo_chatxml_state_destroy); - } - - if (!g_markup_parse_context_parse(yrl->parse, (char *)start, (yrl->rxlen - (start - yrl->rxqueue)), NULL)) { - - yahoo_roomlist_cleanup(list, yrl); + if (!g_markup_parse_context_parse(parse, + purple_http_response_get_data(response, NULL), + purple_http_response_get_data_len(response), NULL)) + { + purple_debug_error("yahoo", "Couldn't parse the room list\n"); + yahoo_roomlist_cleanup(yrl->list, yrl); return; } - yrl->rxlen = 0; + g_markup_parse_context_free(parse); } -static void yahoo_roomlist_send_cb(gpointer data, gint source, PurpleInputCondition cond) +static void +yahoo_roomlist_make_request(struct yahoo_roomlist *yrl) { - struct yahoo_roomlist *yrl; - PurpleRoomlist *list; - int written, remaining; - - yrl = data; - list = yrl->list; - - remaining = strlen(yrl->txbuf) - yrl->tx_written; - written = write(yrl->fd, yrl->txbuf + yrl->tx_written, remaining); - - if (written < 0 && errno == EAGAIN) - written = 0; - else if (written <= 0) { - purple_input_remove(yrl->inpa); - yrl->inpa = 0; - g_free(yrl->txbuf); - yrl->txbuf = NULL; - purple_notify_error(purple_account_get_connection(purple_roomlist_get_account(list)), NULL, _("Unable to connect"), _("Fetching the room list failed.")); - yahoo_roomlist_cleanup(list, yrl); - return; - } - - if (written < remaining) { - yrl->tx_written += written; - return; - } - - g_free(yrl->txbuf); - yrl->txbuf = NULL; - - purple_input_remove(yrl->inpa); - yrl->inpa = purple_input_add(yrl->fd, PURPLE_INPUT_READ, - yahoo_roomlist_pending, yrl); - -} - -static void yahoo_roomlist_got_connected(gpointer data, gint source, const gchar *error_message) -{ - struct yahoo_roomlist *yrl = data; PurpleRoomlist *list = yrl->list; PurpleAccount *account = purple_roomlist_get_account(list); PurpleConnection *pc = purple_account_get_connection(account); YahooData *yd = purple_connection_get_protocol_data(pc); + PurpleHttpRequest *req; + PurpleHttpCookieJar *cjar; + PurpleConnection *gc; - if (source < 0) { - purple_notify_error(pc, NULL, _("Unable to connect"), _("Fetching the room list failed.")); - yahoo_roomlist_cleanup(list, yrl); - return; - } - - yrl->fd = source; + gc = purple_account_get_connection(purple_roomlist_get_account( + yrl->list)); - yrl->txbuf = g_strdup_printf( - "GET http://%s/%s HTTP/1.0\r\n" - "Host: %s\r\n" - "Cookie: Y=%s; T=%s\r\n\r\n", - yrl->host, yrl->path, yrl->host, yd->cookie_y, - yd->cookie_t); + req = purple_http_request_new(yrl->url); + cjar = purple_http_request_get_cookie_jar(req); + purple_http_cookie_jar_set(cjar, "Y", yd->cookie_y); + purple_http_cookie_jar_set(cjar, "T", yd->cookie_t); - - yrl->inpa = purple_input_add(yrl->fd, PURPLE_INPUT_WRITE, - yahoo_roomlist_send_cb, yrl); - yahoo_roomlist_send_cb(yrl, yrl->fd, PURPLE_INPUT_WRITE); + yrl->hc = purple_http_request(gc, req, yahoo_roomlist_got, yrl); + purple_http_request_unref(req); } PurpleRoomlist *yahoo_roomlist_get_list(PurpleConnection *gc) @@ -1502,7 +1432,6 @@ PurpleAccount *account; PurpleRoomlist *rl; PurpleRoomlistField *f; - PurpleHttpURL *url_p; GList *fields = NULL; struct yahoo_roomlist *yrl; const char *rll, *rlurl; @@ -1527,15 +1456,7 @@ rl = purple_roomlist_new(account); yrl->list = rl; - url_p = purple_http_url_parse(url); - g_free(url); - if (!url_p) { - purple_debug_error("yahoo", "Couldn't parse URL\n"); - return NULL; - } - yrl->host = g_strdup(purple_http_url_get_host(url_p)); - yrl->path = g_strdup(purple_http_url_get_path(url_p)); - purple_http_url_free(url_p); + yrl->url = url; f = purple_roomlist_field_new(PURPLE_ROOMLIST_FIELD_STRING, "", "room", TRUE); fields = g_list_append(fields, f); @@ -1557,19 +1478,13 @@ purple_roomlist_set_fields(rl, fields); - if (purple_proxy_connect(gc, account, yrl->host, 80, - yahoo_roomlist_got_connected, yrl) == NULL) - { - purple_notify_error(gc, NULL, _("Connection problem"), _("Unable to fetch room list.")); - yahoo_roomlist_cleanup(rl, yrl); - return NULL; - } - proto_data = purple_roomlist_get_proto_data(rl); proto_data = g_list_append(proto_data, yrl); purple_roomlist_set_proto_data(rl, proto_data); + yahoo_roomlist_make_request(yrl); purple_roomlist_set_in_progress(rl, TRUE); + return rl; } @@ -1592,7 +1507,6 @@ void yahoo_roomlist_expand_category(PurpleRoomlist *list, PurpleRoomlistRoom *category) { PurpleAccount *account; - PurpleHttpURL *url_p; struct yahoo_roomlist *yrl; char *url; char *id; @@ -1629,30 +1543,12 @@ proto_data = g_list_append(proto_data, yrl); purple_roomlist_set_proto_data(list, proto_data); - url_p = purple_http_url_parse(url); - g_free(url); - if (!url_p) { - purple_debug_error("yahoo", "Couldn't parse URL\n"); - return; - } - yrl->host = g_strdup(purple_http_url_get_host(url_p)); - yrl->path = g_strdup(purple_http_url_get_path(url_p)); - purple_http_url_free(url_p); + yrl->url = url; yrl->ucat = purple_roomlist_room_new(PURPLE_ROOMLIST_ROOMTYPE_CATEGORY, _("User Rooms"), yrl->cat); purple_roomlist_room_add(list, yrl->ucat); - if (purple_proxy_connect(purple_account_get_connection(account), - account, yrl->host, 80, - yahoo_roomlist_got_connected, yrl) == NULL) - { - purple_notify_error(purple_account_get_connection(account), - NULL, _("Connection problem"), _("Unable to fetch room list.")); - purple_roomlist_ref(list); - yahoo_roomlist_cleanup(list, yrl); - return; - } - + yahoo_roomlist_make_request(yrl); purple_roomlist_set_in_progress(list, TRUE); purple_roomlist_ref(list); }