Mercurial > pidgin.yaz
diff libpurple/util.c @ 18041:678d78b7fa34
propagate from branch 'im.pidgin.pidgin' (head a58972b72c7aa0fa0899c5a6b96e51cd6c427ab4)
to branch 'im.pidgin.pidgin.2.1.0' (head 03df10bd904eed59317242a557aed2b8430d9630)
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Mon, 04 Jun 2007 05:55:13 +0000 |
parents | c588a4a9d287 6608a7ab0fd9 |
children | 25819c54a963 |
line wrap: on
line diff
--- a/libpurple/util.c Mon Jun 04 05:47:51 2007 +0000 +++ b/libpurple/util.c Mon Jun 04 05:55:13 2007 +0000 @@ -47,6 +47,7 @@ } website; char *url; + int num_times_redirected; gboolean full; char *user_agent; gboolean http11; @@ -1285,14 +1286,17 @@ pt->dest_tag = y; \ tags = g_list_prepend(tags, pt); \ } \ - xhtml = g_string_append(xhtml, "<" y); \ - c += strlen("<" x ); \ - xhtml = g_string_append(xhtml, innards->str); \ - xhtml = g_string_append_c(xhtml, '>'); \ + if(xhtml) { \ + xhtml = g_string_append(xhtml, "<" y); \ + xhtml = g_string_append(xhtml, innards->str); \ + xhtml = g_string_append_c(xhtml, '>'); \ + } \ c = p + 1; \ } else { \ - xhtml = g_string_append(xhtml, "<"); \ - plain = g_string_append_c(plain, '<'); \ + if(xhtml) \ + xhtml = g_string_append(xhtml, "<"); \ + if(plain) \ + plain = g_string_append_c(plain, '<'); \ c++; \ } \ g_string_free(innards, TRUE); \ @@ -1301,16 +1305,19 @@ if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \ (*(c+strlen("<" x)) == '>' || \ !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \ - xhtml = g_string_append(xhtml, "<" y); \ + if(xhtml) \ + xhtml = g_string_append(xhtml, "<" y); \ c += strlen("<" x); \ if(*c != '/') { \ struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \ pt->src_tag = x; \ pt->dest_tag = y; \ tags = g_list_prepend(tags, pt); \ - xhtml = g_string_append_c(xhtml, '>'); \ + if(xhtml) \ + xhtml = g_string_append_c(xhtml, '>'); \ } else { \ - xhtml = g_string_append(xhtml, "/>");\ + if(xhtml) \ + xhtml = g_string_append(xhtml, "/>");\ } \ c = strchr(c, '>') + 1; \ continue; \ @@ -1320,11 +1327,18 @@ purple_markup_html_to_xhtml(const char *html, char **xhtml_out, char **plain_out) { - GString *xhtml = g_string_new(""); - GString *plain = g_string_new(""); + GString *xhtml = NULL; + GString *plain = NULL; GList *tags = NULL, *tag; const char *c = html; + g_return_if_fail(xhtml_out != NULL || plain_out != NULL); + + if(xhtml_out) + xhtml = g_string_new(""); + if(plain_out) + plain = g_string_new(""); + while(c && *c) { if(*c == '<') { if(*(c+1) == '/') { /* closing tag */ @@ -1340,7 +1354,8 @@ if(tag) { while(tags) { struct purple_parse_tag *pt = tags->data; - g_string_append_printf(xhtml, "</%s>", pt->dest_tag); + if(xhtml) + g_string_append_printf(xhtml, "</%s>", pt->dest_tag); if(tags == tag) break; tags = g_list_remove(tags, pt); @@ -1358,8 +1373,10 @@ if(*end == '>') { c = end+1; } else { - xhtml = g_string_append(xhtml, "<"); - plain = g_string_append_c(plain, '<'); + if(xhtml) + xhtml = g_string_append(xhtml, "<"); + if(plain) + plain = g_string_append_c(plain, '<'); c++; } } @@ -1388,7 +1405,8 @@ ALLOW_TAG("span"); ALLOW_TAG("strong"); ALLOW_TAG("ul"); - + ALLOW_TAG("img"); + /* we skip <HR> because it's not legal in XHTML-IM. However, * we still want to send something sensible, so we put a * linebreak in its place. <BR> also needs special handling @@ -1399,8 +1417,9 @@ !g_ascii_strncasecmp(c+3, "/>", 2) || !g_ascii_strncasecmp(c+3, " />", 3))) { c = strchr(c, '>') + 1; - xhtml = g_string_append(xhtml, "<br/>"); - if(*c != '\n') + if(xhtml) + xhtml = g_string_append(xhtml, "<br/>"); + if(plain && *c != '\n') plain = g_string_append_c(plain, '\n'); continue; } @@ -1444,7 +1463,8 @@ pt->dest_tag = "span"; tags = g_list_prepend(tags, pt); c = strchr(c, '>') + 1; - xhtml = g_string_append(xhtml, "<span style='font-weight: bold;'>"); + if(xhtml) + xhtml = g_string_append(xhtml, "<span style='font-weight: bold;'>"); continue; } if(!g_ascii_strncasecmp(c, "<u>", 3) || !g_ascii_strncasecmp(c, "<underline>", strlen("<underline>"))) { @@ -1453,7 +1473,8 @@ pt->dest_tag = "span"; tags = g_list_prepend(tags, pt); c = strchr(c, '>') + 1; - xhtml = g_string_append(xhtml, "<span style='text-decoration: underline;'>"); + if (xhtml) + xhtml = g_string_append(xhtml, "<span style='text-decoration: underline;'>"); continue; } if(!g_ascii_strncasecmp(c, "<s>", 3) || !g_ascii_strncasecmp(c, "<strike>", strlen("<strike>"))) { @@ -1462,7 +1483,8 @@ pt->dest_tag = "span"; tags = g_list_prepend(tags, pt); c = strchr(c, '>') + 1; - xhtml = g_string_append(xhtml, "<span style='text-decoration: line-through;'>"); + if(xhtml) + xhtml = g_string_append(xhtml, "<span style='text-decoration: line-through;'>"); continue; } if(!g_ascii_strncasecmp(c, "<sub>", 5)) { @@ -1471,7 +1493,8 @@ pt->dest_tag = "span"; tags = g_list_prepend(tags, pt); c = strchr(c, '>') + 1; - xhtml = g_string_append(xhtml, "<span style='vertical-align:sub;'>"); + if(xhtml) + xhtml = g_string_append(xhtml, "<span style='vertical-align:sub;'>"); continue; } if(!g_ascii_strncasecmp(c, "<sup>", 5)) { @@ -1480,7 +1503,8 @@ pt->dest_tag = "span"; tags = g_list_prepend(tags, pt); c = strchr(c, '>') + 1; - xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>"); + if(xhtml) + xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>"); continue; } if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) { @@ -1574,7 +1598,10 @@ pt->dest_tag = "span"; tags = g_list_prepend(tags, pt); if(style->len) - g_string_append_printf(xhtml, "<span style='%s'>", g_strstrip(style->str)); + { + if(xhtml) + g_string_append_printf(xhtml, "<span style='%s'>", g_strstrip(style->str)); + } else pt->ignore = TRUE; g_string_free(style, TRUE); @@ -1594,7 +1621,8 @@ color = g_string_append_c(color, *q); q++; } - g_string_append_printf(xhtml, "<span style='background: %s;'>", g_strstrip(color->str)); + if(xhtml) + g_string_append_printf(xhtml, "<span style='background: %s;'>", g_strstrip(color->str)); g_string_free(color, TRUE); if ((c = strchr(c, '>')) != NULL) c++; @@ -1615,14 +1643,17 @@ if(!g_ascii_strncasecmp(c, "<!--", strlen("<!--"))) { char *p = strstr(c + strlen("<!--"), "-->"); if(p) { - xhtml = g_string_append(xhtml, "<!--"); + if(xhtml) + xhtml = g_string_append(xhtml, "<!--"); c += strlen("<!--"); continue; } } - xhtml = g_string_append(xhtml, "<"); - plain = g_string_append_c(plain, '<'); + if(xhtml) + xhtml = g_string_append(xhtml, "<"); + if(plain) + plain = g_string_append_c(plain, '<'); c++; } } else if(*c == '&') { @@ -1635,29 +1666,31 @@ g_snprintf(buf, sizeof(buf), "%c", *c); pln = buf; } - xhtml = g_string_append_len(xhtml, c, len); - plain = g_string_append(plain, pln); + if(xhtml) + xhtml = g_string_append_len(xhtml, c, len); + if(plain) + plain = g_string_append(plain, pln); c += len; } else { - xhtml = g_string_append_c(xhtml, *c); - plain = g_string_append_c(plain, *c); + if(xhtml) + xhtml = g_string_append_c(xhtml, *c); + if(plain) + plain = g_string_append_c(plain, *c); c++; } } - tag = tags; - while(tag) { - struct purple_parse_tag *pt = tag->data; - if(!pt->ignore) - g_string_append_printf(xhtml, "</%s>", pt->dest_tag); - tag = tag->next; + if(xhtml) { + for (tag = tags; tag ; tag = tag->next) { + struct purple_parse_tag *pt = tag->data; + if(!pt->ignore) + g_string_append_printf(xhtml, "</%s>", pt->dest_tag); + } } g_list_free(tags); if(xhtml_out) - *xhtml_out = g_strdup(xhtml->str); + *xhtml_out = g_string_free(xhtml, FALSE); if(plain_out) - *plain_out = g_strdup(plain->str); - g_string_free(xhtml, TRUE); - g_string_free(plain, TRUE); + *plain_out = g_string_free(plain, FALSE); } /* The following are probably reasonable changes: @@ -3273,6 +3306,17 @@ g_hash_table_destroy(params); } +/* + * TODO: Should probably add a "gboolean *ret_ishttps" parameter that + * is set to TRUE if this URL is https, otherwise it is set to + * FALSE. But that change will break the API. + * + * This is important for Yahoo! web messenger login. They now + * force https login, and if you access the web messenger login + * page via http then it redirects you to the https version, but + * purple_util_fetch_url() ignores the "https" and attempts to + * fetch the URL via http again, which gets redirected again. + */ gboolean purple_url_parse(const char *url, char **ret_host, int *ret_port, char **ret_path, char **ret_user, char **ret_passwd) @@ -3293,12 +3337,16 @@ g_return_val_if_fail(url != NULL, FALSE); - if ((turl = strstr(url, "http://")) != NULL || - (turl = strstr(url, "HTTP://")) != NULL) + if ((turl = purple_strcasestr(url, "http://")) != NULL) { turl += 7; url = turl; } + else if ((turl = purple_strcasestr(url, "https://")) != NULL) + { + turl += 8; + url = turl; + } /* parse out authentication information if supplied */ /* Only care about @ char BEFORE the first / */ @@ -3378,85 +3426,92 @@ PurpleUtilFetchUrlData *gfud) { gchar *s; - - if ((s = g_strstr_len(data, data_len, "Location: ")) != NULL) + gchar *new_url, *temp_url, *end; + gboolean full; + int len; + + if ((s = g_strstr_len(data, data_len, "Location: ")) == NULL) + /* We're not being redirected */ + return FALSE; + + s += strlen("Location: "); + end = strchr(s, '\r'); + + /* Just in case :) */ + if (end == NULL) + end = strchr(s, '\n'); + + if (end == NULL) + return FALSE; + + len = end - s; + + new_url = g_malloc(len + 1); + strncpy(new_url, s, len); + new_url[len] = '\0'; + + full = gfud->full; + + if (*new_url == '/' || g_strstr_len(new_url, len, "://") == NULL) { - gchar *new_url, *temp_url, *end; - gboolean full; - int len; - - s += strlen("Location: "); - end = strchr(s, '\r'); - - /* Just in case :) */ - if (end == NULL) - end = strchr(s, '\n'); - - if (end == NULL) - return FALSE; - - len = end - s; - - new_url = g_malloc(len + 1); - strncpy(new_url, s, len); - new_url[len] = '\0'; - - full = gfud->full; - - if (*new_url == '/' || g_strstr_len(new_url, len, "://") == NULL) - { - temp_url = new_url; - - new_url = g_strdup_printf("%s:%d%s", gfud->website.address, - gfud->website.port, temp_url); - - g_free(temp_url); - - full = FALSE; - } - - purple_debug_info("util", "Redirecting to %s\n", new_url); - - /* - * Try again, with this new location. This code is somewhat - * ugly, but we need to reuse the gfud because whoever called - * us is holding a reference to it. - */ - g_free(gfud->url); - gfud->url = new_url; - gfud->full = full; - g_free(gfud->request); - gfud->request = NULL; - - purple_input_remove(gfud->inpa); - gfud->inpa = 0; - close(gfud->fd); - gfud->fd = -1; - gfud->request_written = 0; - gfud->len = 0; - gfud->data_len = 0; - - g_free(gfud->website.user); - g_free(gfud->website.passwd); - g_free(gfud->website.address); - g_free(gfud->website.page); - purple_url_parse(new_url, &gfud->website.address, &gfud->website.port, - &gfud->website.page, &gfud->website.user, &gfud->website.passwd); - - gfud->connect_data = purple_proxy_connect(NULL, NULL, - gfud->website.address, gfud->website.port, - url_fetch_connect_cb, gfud); - - if (gfud->connect_data == NULL) - { - purple_util_fetch_url_error(gfud, _("Unable to connect to %s"), - gfud->website.address); - } - + temp_url = new_url; + + new_url = g_strdup_printf("%s:%d%s", gfud->website.address, + gfud->website.port, temp_url); + + g_free(temp_url); + + full = FALSE; + } + + purple_debug_info("util", "Redirecting to %s\n", new_url); + + gfud->num_times_redirected++; + if (gfud->num_times_redirected >= 5) + { + purple_util_fetch_url_error(gfud, + _("Could not open %s: Redirected too many times"), + gfud->url); return TRUE; } - return FALSE; + /* + * Try again, with this new location. This code is somewhat + * ugly, but we need to reuse the gfud because whoever called + * us is holding a reference to it. + */ + g_free(gfud->url); + gfud->url = new_url; + gfud->full = full; + g_free(gfud->request); + gfud->request = NULL; + + purple_input_remove(gfud->inpa); + gfud->inpa = 0; + close(gfud->fd); + gfud->fd = -1; + gfud->request_written = 0; + gfud->len = 0; + gfud->data_len = 0; + + g_free(gfud->website.user); + g_free(gfud->website.passwd); + g_free(gfud->website.address); + g_free(gfud->website.page); + purple_url_parse(new_url, &gfud->website.address, &gfud->website.port, + &gfud->website.page, &gfud->website.user, &gfud->website.passwd); + + gfud->connect_data = purple_proxy_connect(NULL, NULL, + gfud->website.address, gfud->website.port, + url_fetch_connect_cb, gfud); + + if (gfud->connect_data == NULL) + { + purple_util_fetch_url_error(gfud, _("Unable to connect to %s"), + gfud->website.address); + } + + return TRUE; } static size_t