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