Mercurial > pidgin
annotate src/html.c @ 287:8bbc269d5b8d
[gaim-migrate @ 297]
YTay
committer: Tailor Script <tailor@pidgin.im>
author | Rob Flynn <gaim@robflynn.com> |
---|---|
date | Wed, 31 May 2000 18:06:09 +0000 |
parents | 29e1669b006b |
children | b402a23f35df |
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> | |
278
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
35 #include <fcntl.h> |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
36 #include <errno.h> |
1 | 37 |
38 gchar * strip_html(gchar * text) | |
39 { | |
40 int i, j; | |
41 int visible = 1; | |
42 gchar *text2 = g_malloc(strlen(text) + 1); | |
43 | |
44 strcpy(text2, text); | |
45 for (i = 0, j = 0;text2[i]; i++) | |
46 { | |
47 if(text2[i]=='<') | |
48 { | |
49 visible = 0; | |
50 continue; | |
51 } | |
52 else if(text2[i]=='>') | |
53 { | |
54 visible = 1; | |
55 continue; | |
56 } | |
57 if(visible) | |
58 { | |
59 text2[j++] = text2[i]; | |
60 } | |
61 } | |
62 text2[j] = '\0'; | |
63 return text2; | |
64 } | |
65 | |
66 struct g_url parse_url(char *url) | |
67 { | |
68 struct g_url test; | |
69 char scan_info[255]; | |
70 char port[5]; | |
71 int f; | |
72 | |
73 if (strstr(url, "http://")) | |
74 g_snprintf(scan_info, sizeof(scan_info), "http://%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?]"); | |
75 else | |
76 g_snprintf(scan_info, sizeof(scan_info), "%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?]"); | |
77 f = sscanf(url, scan_info, test.address, port, test.page); | |
78 if (f == 1) { | |
79 if (strstr(url, "http://")) | |
80 g_snprintf(scan_info, sizeof(scan_info), "http://%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?]"); | |
81 else | |
82 g_snprintf(scan_info, sizeof(scan_info), "%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?]"); | |
83 f = sscanf(url, scan_info, test.address, test.page); | |
84 g_snprintf(port, sizeof(test.port), "80"); | |
85 port[2] = 0; | |
86 } | |
87 if (f == 1) { | |
88 if (strstr(url, "http://")) | |
89 g_snprintf(scan_info, sizeof(scan_info), "http://%%[A-Za-z0-9.]"); | |
90 else | |
91 g_snprintf(scan_info, sizeof(scan_info), "%%[A-Za-z0-9.]"); | |
92 f = sscanf(url, scan_info, test.address); | |
93 g_snprintf(test.page, sizeof(test.page), "%c", '\0'); | |
94 } | |
95 | |
96 sscanf(port, "%d", &test.port); | |
97 return test; | |
98 } | |
99 | |
100 char *grab_url(char *url) | |
101 { | |
102 struct g_url website; | |
103 char *webdata = NULL; | |
104 int sock; | |
105 int len; | |
268
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
106 int read_rv; |
1 | 107 int datalen = 0; |
253
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
108 struct in_addr *host; |
1 | 109 char buf[256]; |
110 char data; | |
111 int startsaving = 0; | |
112 GtkWidget *pw = NULL, *pbar = NULL, *label; | |
113 | |
114 website = parse_url(url); | |
115 | |
253
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
116 /* |
1 | 117 host = gethostbyname(website.address); |
118 if (!host) { return g_strdup("g001: Error resolving host\n"); } | |
119 | |
253
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
120 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
|
121 return g_strdup("g003: Error opening connection.\n"); |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
122 */ |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
123 host = (struct in_addr *)get_address(website.address); |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
124 if (!host) { return g_strdup("g001: Error resolving host\n"); } |
5b28ef2b550e
[gaim-migrate @ 263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
35
diff
changeset
|
125 if ((sock = connect_address(host->s_addr, website.port)) < 0) |
1 | 126 return g_strdup("g003: Error opening connection.\n"); |
127 | |
128 g_snprintf(buf, sizeof(buf), "GET /%s HTTP/1.0\n\n", website.page); | |
129 g_snprintf(debug_buff, sizeof(debug_buff), "Request: %s\n", buf); | |
130 debug_print(debug_buff); | |
268
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
131 write(sock, buf, strlen(buf)); |
278
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
132 fcntl(sock, F_SETFL, O_NONBLOCK); |
1 | 133 |
134 webdata = NULL; | |
135 len = 0; | |
136 | |
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 * avoid fgetc(), it causes problems on solaris |
35 | 139 while ((data = fgetc(sockfile)) != EOF) { |
268
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
140 */ |
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
141 /* read_rv will be 0 on EOF and < 0 on error, so this should be fine */ |
278
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
142 while ((read_rv = read(sock, &data, 1)) > 0 || errno == EWOULDBLOCK) { |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
143 if (errno == EWOULDBLOCK) { |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
144 errno = 0; |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
145 continue; |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
146 } |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
147 |
1 | 148 if (!data) |
149 continue; | |
150 | |
151 if (!startsaving && data == '<') { | |
152 #ifdef HAVE_STRSTR | |
153 char *cs = strstr(webdata, "Content-Length"); | |
154 if (cs) { | |
155 char tmpbuf[1024]; | |
156 sscanf(cs, "Content-Length: %d", &datalen); | |
157 | |
158 g_snprintf(tmpbuf, 1024, "Getting %d bytes from %s", datalen, url); | |
159 pw = gtk_dialog_new(); | |
160 | |
161 label = gtk_label_new(tmpbuf); | |
162 gtk_widget_show(label); | |
163 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pw)->vbox), | |
164 label, FALSE, FALSE, 5); | |
165 | |
166 pbar = gtk_progress_bar_new(); | |
167 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pw)->action_area), | |
168 pbar, FALSE, FALSE, 5); | |
169 gtk_widget_show(pbar); | |
170 | |
171 gtk_window_set_title(GTK_WINDOW(pw), "Getting Data"); | |
172 | |
173 gtk_widget_realize(pw); | |
174 aol_icon(pw->window); | |
175 | |
176 gtk_widget_show(pw); | |
177 } else | |
178 datalen = 0; | |
179 #else | |
180 datalen = 0; | |
181 #endif | |
182 g_free(webdata); | |
183 webdata = NULL; | |
184 len = 0; | |
185 startsaving = 1; | |
186 } | |
187 | |
188 len++; | |
189 webdata = g_realloc(webdata, len); | |
190 webdata[len - 1] = data; | |
191 | |
192 if (pbar) | |
193 gtk_progress_bar_update(GTK_PROGRESS_BAR(pbar), | |
194 ((100 * len) / datalen) / 100.0); | |
195 | |
196 while (gtk_events_pending()) | |
197 gtk_main_iteration(); | |
198 } | |
199 | |
200 webdata = g_realloc(webdata, len+1); | |
201 webdata[len] = 0; | |
202 | |
203 | |
204 g_snprintf(debug_buff, sizeof(debug_buff), "Receieved: '%s'\n", webdata); | |
205 debug_print(debug_buff); | |
206 | |
207 if (pw) | |
208 gtk_widget_destroy(pw); | |
209 | |
210 close(sock); | |
211 return webdata; | |
212 } | |
213 | |
214 char *fix_url(gchar *buf) | |
215 { | |
216 char *new,*tmp; | |
217 int size; | |
218 | |
219 size=8; | |
220 size+=strlen(quad_addr); | |
221 tmp=strchr(strchr(buf,':')+1,':'); | |
222 size+=strlen(tmp); | |
223 new=g_malloc(size); | |
224 strcpy(new,"http://"); | |
225 strcat(new,quad_addr); | |
226 strcat(new,tmp); | |
227 return(new); | |
228 } | |
229 | |
230 |