Mercurial > pidgin.yaz
diff src/html.c @ 7094:2343c3aa1dec
[gaim-migrate @ 7659]
grab_url() and parse_url() are gone, replaced with gaim_url_fetch() and
gaim_url_parse(). They were also moved to util.[ch].
committer: Tailor Script <tailor@pidgin.im>
| author | Christian Hammond <chipx86@chipx86.com> |
|---|---|
| date | Wed, 01 Oct 2003 03:01:25 +0000 |
| parents | 67c4e9d39242 |
| children |
line wrap: on
line diff
--- a/src/html.c Wed Oct 01 02:06:12 2003 +0000 +++ b/src/html.c Wed Oct 01 03:01:25 2003 +0000 @@ -71,281 +71,6 @@ return text2; } -struct g_url *parse_url(char *url) -{ - struct g_url *test = g_new0(struct g_url, 1); - char scan_info[255]; - char port[5]; - int f; - char* turl; - /* hyphen at end includes it in control set */ - char addr_ctrl[] = "A-Za-z0-9.-"; - char port_ctrl[] = "0-9"; - char page_ctrl[] = "A-Za-z0-9.~_/:*!@&%%?=+^-"; - - if((turl=strstr(url, "http://")) || (turl=strstr(url, "HTTP://"))) - url=turl+=7; - - snprintf(scan_info, sizeof(scan_info), - "%%[%s]:%%[%s]/%%[%s]", - addr_ctrl, port_ctrl, page_ctrl); - - f = sscanf(url, scan_info, test->address, port, test->page); - if (f == 1) { - snprintf(scan_info, sizeof(scan_info), - "%%[%s]/%%[%s]", - addr_ctrl, page_ctrl); - f = sscanf(url, scan_info, test->address, test->page); - snprintf(port, sizeof(port), "80"); - } - if (f == 1) - test->page[0] = '\0'; - - sscanf(port, "%d", &test->port); - return test; -} - -struct grab_url_data { - void (* callback)(gpointer, char *, unsigned long); - gpointer data; - struct g_url *website; - char *url; - gboolean full; - char *user_agent; - int http11; - - int inpa; - - gboolean sentreq; - gboolean newline; - gboolean startsaving; - char *webdata; - unsigned long len; - unsigned long data_len; -}; - -static gboolean -parse_redirect(const char *data, size_t data_len, gint sock, - struct grab_url_data *gunk) -{ - gchar *s; - - if ((s = g_strstr_len(data, data_len, "Location: ")) != 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'); - - len = end - s; - - new_url = g_malloc(len + 1); - strncpy(new_url, s, len); - new_url[len] = '\0'; - - full = gunk->full; - - if (*new_url == '/' || g_strstr_len(new_url, len, "://") == NULL) { - temp_url = new_url; - - new_url = g_strdup_printf("%s:%d%s", gunk->website->address, - gunk->website->port, temp_url); - - g_free(temp_url); - - full = FALSE; - } - - /* Close the existing stuff. */ - gaim_input_remove(gunk->inpa); - close(sock); - - gaim_debug(GAIM_DEBUG_INFO, "grab_url", - "Redirecting to %s\n", new_url); - - /* Try again, with this new location. */ - grab_url(new_url, full, gunk->callback, gunk->data, gunk->user_agent, gunk->http11); - - /* Free up. */ - g_free(new_url); - g_free(gunk->webdata); - g_free(gunk->website); - g_free(gunk->url); - g_free(gunk->user_agent); - g_free(gunk); - - return TRUE; - } - - return FALSE; -} - -static size_t -parse_content_len(const char *data, size_t data_len) -{ - size_t content_len = 0; - - sscanf(data, "Content-Length: %d", &content_len); - - return content_len; -} - -static void grab_url_callback(gpointer dat, gint sock, GaimInputCondition cond) -{ - struct grab_url_data *gunk = dat; - char data; - - if (sock == -1) { - gunk->callback(gunk->data, NULL, 0); - g_free(gunk->website); - g_free(gunk->url); - g_free(gunk->user_agent); - g_free(gunk); - return; - } - - if (!gunk->sentreq) { - char buf[1024]; - - if(gunk->user_agent) { - if(gunk->http11) - g_snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.1\r\nUser-Agent: \"%s\"\r\nHost: %s\r\n\r\n", gunk->full ? "" : "/", - gunk->full ? gunk->url : gunk->website->page, gunk->user_agent, gunk->website->address); - else - g_snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\r\nUser-Agent: \"%s\"\r\n\r\n", gunk->full ? "" : "/", - gunk->full ? gunk->url : gunk->website->page, gunk->user_agent); - } - else { - if(gunk->http11) - g_snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.1\r\nHost: %s\r\n\r\n", gunk->full ? "" : "/", - gunk->full ? gunk->url : gunk->website->page, gunk->website->address); - else - g_snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\r\n\r\n", gunk->full ? "" : "/", - gunk->full ? gunk->url : gunk->website->page); - - } - gaim_debug(GAIM_DEBUG_MISC, "grab_url_callback", - "Request: %s\n", buf); - - write(sock, buf, strlen(buf)); - fcntl(sock, F_SETFL, O_NONBLOCK); - gunk->sentreq = TRUE; - gunk->inpa = gaim_input_add(sock, GAIM_INPUT_READ, grab_url_callback, dat); - gunk->data_len = 4096; - gunk->webdata = g_malloc(gunk->data_len); - return; - } - - if (read(sock, &data, 1) > 0 || errno == EWOULDBLOCK) { - if (errno == EWOULDBLOCK) { - errno = 0; - return; - } - - gunk->len++; - - if (gunk->len == gunk->data_len + 1) { - gunk->data_len += (gunk->data_len) / 2; - - gunk->webdata = g_realloc(gunk->webdata, gunk->data_len); - } - - gunk->webdata[gunk->len - 1] = data; - - if (!gunk->startsaving) { - if (data == '\r') - return; - if (data == '\n') { - if (gunk->newline) { - size_t content_len; - gunk->startsaving = TRUE; - - /* See if we can find a redirect. */ - if (parse_redirect(gunk->webdata, gunk->len, sock, gunk)) - return; - - /* No redirect. See if we can find a content length. */ - content_len = parse_content_len(gunk->webdata, gunk->len); - - if (content_len == 0) { - /* We'll stick with an initial 8192 */ - content_len = 8192; - } - - /* Out with the old... */ - gunk->len = 0; - g_free(gunk->webdata); - gunk->webdata = NULL; - - /* In with the new. */ - gunk->data_len = content_len; - gunk->webdata = g_malloc(gunk->data_len); - } - else - gunk->newline = TRUE; - return; - } - gunk->newline = FALSE; - } - } else if (errno != ETIMEDOUT) { - gunk->webdata = g_realloc(gunk->webdata, gunk->len + 1); - gunk->webdata[gunk->len] = 0; - - gaim_debug(GAIM_DEBUG_MISC, "grab_url_callback", - "Received: '%s'\n", gunk->webdata); - - gaim_input_remove(gunk->inpa); - close(sock); - gunk->callback(gunk->data, gunk->webdata, gunk->len); - if (gunk->webdata) - g_free(gunk->webdata); - g_free(gunk->website); - g_free(gunk->url); - g_free(gunk->user_agent); - g_free(gunk); - } else { - gaim_input_remove(gunk->inpa); - close(sock); - gunk->callback(gunk->data, NULL, 0); - if (gunk->webdata) - g_free(gunk->webdata); - g_free(gunk->website); - g_free(gunk->url); - g_free(gunk->user_agent); - g_free(gunk); - } -} - -void grab_url(char *url, gboolean full, void callback(gpointer, char *, unsigned long), - gpointer data, char *user_agent, int http11) -{ - int sock; - struct grab_url_data *gunk = g_new0(struct grab_url_data, 1); - - gunk->callback = callback; - gunk->data = data; - gunk->url = g_strdup(url); - gunk->user_agent = (user_agent) ? g_strdup(user_agent) : NULL; - gunk->http11 = http11; - gunk->website = parse_url(url); - gunk->full = full; - - if ((sock = gaim_proxy_connect(NULL, gunk->website->address, - gunk->website->port, grab_url_callback, - gunk)) < 0) { - g_free(gunk->website); - g_free(gunk->url); - g_free(gunk->user_agent); - g_free(gunk); - callback(data, g_strdup(_("g003: Error opening connection.\n")), 0); - } -} - struct gaim_parse_tag { char *src_tag; char *dest_tag; @@ -655,7 +380,7 @@ g_string_free(plain, TRUE); } -int info_extract_field(char *original, char *add_to, char *start_tok, +int info_extract_field(const char *original, char *add_to, char *start_tok, int skip, char *end_tok, char check_value, char *no_value_tok, char *display_name, int islink, char *link_prefix) {
