Mercurial > pidgin
annotate src/html.c @ 271:f7706908f8ae
[gaim-migrate @ 281]
There aren't panel support problems any more because the 100% bug got fixed.
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Fri, 26 May 2000 23:17:19 +0000 |
parents | f8a29745247c |
children | 29e1669b006b |
rev | line source |
---|---|
1 | 1 /* |
2 * gaim | |
3 * | |
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
22 #include <string.h> | |
23 #include <stdio.h> | |
24 #include <stdlib.h> | |
25 #include <sys/time.h> | |
26 #include <unistd.h> | |
27 #include <gtk/gtk.h> | |
28 #include <gdk/gdkprivate.h> | |
29 #include <gdk/gdkx.h> | |
30 #include "gaim.h" | |
31 #include <sys/types.h> | |
32 #include <sys/socket.h> | |
33 #include <netdb.h> | |
34 #include <netinet/in.h> | |
35 | |
36 gchar * strip_html(gchar * text) | |
37 { | |
38 int i, j; | |
39 int visible = 1; | |
40 gchar *text2 = g_malloc(strlen(text) + 1); | |
41 | |
42 strcpy(text2, text); | |
43 for (i = 0, j = 0;text2[i]; i++) | |
44 { | |
45 if(text2[i]=='<') | |
46 { | |
47 visible = 0; | |
48 continue; | |
49 } | |
50 else if(text2[i]=='>') | |
51 { | |
52 visible = 1; | |
53 continue; | |
54 } | |
55 if(visible) | |
56 { | |
57 text2[j++] = text2[i]; | |
58 } | |
59 } | |
60 text2[j] = '\0'; | |
61 return text2; | |
62 } | |
63 | |
64 struct g_url parse_url(char *url) | |
65 { | |
66 struct g_url test; | |
67 char scan_info[255]; | |
68 char port[5]; | |
69 int f; | |
70 | |
71 if (strstr(url, "http://")) | |
72 g_snprintf(scan_info, sizeof(scan_info), "http://%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?]"); | |
73 else | |
74 g_snprintf(scan_info, sizeof(scan_info), "%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?]"); | |
75 f = sscanf(url, scan_info, test.address, port, test.page); | |
76 if (f == 1) { | |
77 if (strstr(url, "http://")) | |
78 g_snprintf(scan_info, sizeof(scan_info), "http://%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?]"); | |
79 else | |
80 g_snprintf(scan_info, sizeof(scan_info), "%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?]"); | |
81 f = sscanf(url, scan_info, test.address, test.page); | |
82 g_snprintf(port, sizeof(test.port), "80"); | |
83 port[2] = 0; | |
84 } | |
85 if (f == 1) { | |
86 if (strstr(url, "http://")) | |
87 g_snprintf(scan_info, sizeof(scan_info), "http://%%[A-Za-z0-9.]"); | |
88 else | |
89 g_snprintf(scan_info, sizeof(scan_info), "%%[A-Za-z0-9.]"); | |
90 f = sscanf(url, scan_info, test.address); | |
91 g_snprintf(test.page, sizeof(test.page), "%c", '\0'); | |
92 } | |
93 | |
94 sscanf(port, "%d", &test.port); | |
95 return test; | |
96 } | |
97 | |
98 char *grab_url(char *url) | |
99 { | |
100 struct g_url website; | |
101 char *webdata = NULL; | |
102 int sock; | |
103 int len; | |
268
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
104 int read_rv; |
1 | 105 int datalen = 0; |
253
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
106 struct in_addr *host; |
1 | 107 char buf[256]; |
108 char data; | |
109 int startsaving = 0; | |
110 GtkWidget *pw = NULL, *pbar = NULL, *label; | |
111 | |
112 website = parse_url(url); | |
113 | |
253
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
114 /* |
1 | 115 host = gethostbyname(website.address); |
116 if (!host) { return g_strdup("g001: Error resolving host\n"); } | |
117 | |
253
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
118 if ((sock = connect_address(inet_addr(host->h_addr), website.port)) <= -1) |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
119 return g_strdup("g003: Error opening connection.\n"); |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
120 */ |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
121 host = (struct in_addr *)get_address(website.address); |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
122 if (!host) { return g_strdup("g001: Error resolving host\n"); } |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
123 if ((sock = connect_address(host->s_addr, website.port)) < 0) |
1 | 124 return g_strdup("g003: Error opening connection.\n"); |
125 | |
126 g_snprintf(buf, sizeof(buf), "GET /%s HTTP/1.0\n\n", website.page); | |
127 g_snprintf(debug_buff, sizeof(debug_buff), "Request: %s\n", buf); | |
128 debug_print(debug_buff); | |
268
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
129 write(sock, buf, strlen(buf)); |
1 | 130 |
131 webdata = NULL; | |
132 len = 0; | |
133 | |
268
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
134 /* |
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
135 * avoid fgetc(), it causes problems on solaris |
35 | 136 while ((data = fgetc(sockfile)) != EOF) { |
268
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
137 */ |
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
138 /* read_rv will be 0 on EOF and < 0 on error, so this should be fine */ |
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
139 while ((read_rv = read(sock, &data, 1)) > 0) { |
1 | 140 if (!data) |
141 continue; | |
142 | |
143 if (!startsaving && data == '<') { | |
144 #ifdef HAVE_STRSTR | |
145 char *cs = strstr(webdata, "Content-Length"); | |
146 if (cs) { | |
147 char tmpbuf[1024]; | |
148 sscanf(cs, "Content-Length: %d", &datalen); | |
149 | |
150 g_snprintf(tmpbuf, 1024, "Getting %d bytes from %s", datalen, url); | |
151 pw = gtk_dialog_new(); | |
152 | |
153 label = gtk_label_new(tmpbuf); | |
154 gtk_widget_show(label); | |
155 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pw)->vbox), | |
156 label, FALSE, FALSE, 5); | |
157 | |
158 pbar = gtk_progress_bar_new(); | |
159 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pw)->action_area), | |
160 pbar, FALSE, FALSE, 5); | |
161 gtk_widget_show(pbar); | |
162 | |
163 gtk_window_set_title(GTK_WINDOW(pw), "Getting Data"); | |
164 | |
165 gtk_widget_realize(pw); | |
166 aol_icon(pw->window); | |
167 | |
168 gtk_widget_show(pw); | |
169 } else | |
170 datalen = 0; | |
171 #else | |
172 datalen = 0; | |
173 #endif | |
174 g_free(webdata); | |
175 webdata = NULL; | |
176 len = 0; | |
177 startsaving = 1; | |
178 } | |
179 | |
180 len++; | |
181 webdata = g_realloc(webdata, len); | |
182 webdata[len - 1] = data; | |
183 | |
184 if (pbar) | |
185 gtk_progress_bar_update(GTK_PROGRESS_BAR(pbar), | |
186 ((100 * len) / datalen) / 100.0); | |
187 | |
188 while (gtk_events_pending()) | |
189 gtk_main_iteration(); | |
190 } | |
191 | |
192 webdata = g_realloc(webdata, len+1); | |
193 webdata[len] = 0; | |
194 | |
195 | |
196 g_snprintf(debug_buff, sizeof(debug_buff), "Receieved: '%s'\n", webdata); | |
197 debug_print(debug_buff); | |
198 | |
199 if (pw) | |
200 gtk_widget_destroy(pw); | |
201 | |
202 close(sock); | |
203 return webdata; | |
204 } | |
205 | |
206 char *fix_url(gchar *buf) | |
207 { | |
208 char *new,*tmp; | |
209 int size; | |
210 | |
211 size=8; | |
212 size+=strlen(quad_addr); | |
213 tmp=strchr(strchr(buf,':')+1,':'); | |
214 size+=strlen(tmp); | |
215 new=g_malloc(size); | |
216 strcpy(new,"http://"); | |
217 strcat(new,quad_addr); | |
218 strcat(new,tmp); | |
219 return(new); | |
220 } | |
221 | |
222 |