Mercurial > pidgin.yaz
annotate src/protocols/yahoo/yahoo_filexfer.c @ 10579:71478e6a6074
[gaim-migrate @ 11976]
Fix receiving files on Yahoo with proxies that interfere with http headers
committer: Tailor Script <tailor@pidgin.im>
author | Stu Tomlinson <stu@nosnilmot.com> |
---|---|
date | Sun, 06 Feb 2005 17:40:45 +0000 |
parents | 6c6772d3ea31 |
children | 8fbab42659c2 |
rev | line source |
---|---|
7651 | 1 /* |
2 * @file yahoo_filexfer.c Yahoo Filetransfer | |
3 * | |
8046 | 4 * Gaim is the legal property of its developers, whose names are too numerous |
5 * to list here. Please refer to the COPYRIGHT file distributed with this | |
6 * source distribution. | |
7651 | 7 * |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 */ | |
22 | |
23 #include "prpl.h" | |
24 #include "internal.h" | |
25 #include "util.h" | |
26 #include "debug.h" | |
27 #include "notify.h" | |
28 #include "proxy.h" | |
29 #include "ft.h" | |
30 #include "yahoo.h" | |
10392 | 31 #include "yahoo_packet.h" |
7651 | 32 #include "yahoo_filexfer.h" |
33 | |
34 | |
35 | |
36 struct yahoo_xfer_data { | |
37 gchar *host; | |
38 gchar *path; | |
39 int port; | |
40 GaimConnection *gc; | |
41 long expires; | |
42 gboolean started; | |
43 gchar *rxqueue; | |
44 guint rxlen; | |
45 }; | |
46 | |
47 static void yahoo_xfer_data_free(struct yahoo_xfer_data *xd) | |
48 { | |
49 if (xd->host) | |
50 g_free(xd->host); | |
51 if (xd->path) | |
52 g_free(xd->path); | |
53 g_free(xd); | |
54 } | |
55 | |
56 static void yahoo_receivefile_connected(gpointer data, gint source, GaimInputCondition condition) | |
57 { | |
58 GaimXfer *xfer; | |
59 struct yahoo_xfer_data *xd; | |
60 gchar *buf; | |
61 | |
62 gaim_debug(GAIM_DEBUG_INFO, "yahoo", | |
63 "AAA - in yahoo_receivefile_connected\n"); | |
64 if (!(xfer = data)) | |
65 return; | |
66 if (!(xd = xfer->data)) | |
67 return; | |
68 if (source < 0) { | |
69 gaim_xfer_error(GAIM_XFER_RECEIVE, xfer->who, _("Unable to connect.")); | |
7805 | 70 gaim_xfer_cancel_remote(xfer); |
7651 | 71 return; |
72 } | |
73 | |
74 xfer->fd = source; | |
75 gaim_xfer_start(xfer, source, NULL, 0); | |
76 | |
10575 | 77 buf = g_strdup_printf("GET http://%s:%d/%s HTTP/1.0\r\nHost: %s\r\n\r\n", |
78 xd->host, xd->port, xd->path, xd->host); | |
7651 | 79 write(xfer->fd, buf, strlen(buf)); |
80 g_free(buf); | |
81 | |
82 return; | |
83 } | |
84 | |
85 static void yahoo_sendfile_connected(gpointer data, gint source, GaimInputCondition condition) | |
86 { | |
87 GaimXfer *xfer; | |
88 struct yahoo_xfer_data *xd; | |
89 struct yahoo_packet *pkt; | |
90 gchar *size, *post, *buf; | |
10576 | 91 const char *host; |
10575 | 92 int content_length, port; |
7651 | 93 GaimConnection *gc; |
94 GaimAccount *account; | |
95 struct yahoo_data *yd; | |
7805 | 96 char *filename; |
7651 | 97 |
98 gaim_debug(GAIM_DEBUG_INFO, "yahoo", | |
99 "AAA - in yahoo_sendfile_connected\n"); | |
100 if (!(xfer = data)) | |
101 return; | |
102 if (!(xd = xfer->data)) | |
103 return; | |
104 | |
105 gc = xd->gc; | |
106 account = gaim_connection_get_account(gc); | |
107 yd = gc->proto_data; | |
108 | |
109 | |
110 | |
111 if (source < 0) { | |
112 gaim_xfer_error(GAIM_XFER_RECEIVE, xfer->who, _("Unable to connect.")); | |
7805 | 113 gaim_xfer_cancel_remote(xfer); |
7651 | 114 return; |
115 } | |
116 | |
117 xfer->fd = source; | |
118 gaim_xfer_start(xfer, source, NULL, 0); | |
119 | |
120 | |
121 pkt = yahoo_packet_new(YAHOO_SERVICE_FILETRANSFER, YAHOO_STATUS_AVAILABLE, yd->session_id); | |
122 | |
10111 | 123 size = g_strdup_printf("%" G_GSIZE_FORMAT, gaim_xfer_get_size(xfer)); |
7805 | 124 filename = g_path_get_basename(gaim_xfer_get_local_filename(xfer)); |
10394 | 125 |
126 yahoo_packet_hash(pkt, "sssss", 0, gaim_connection_get_display_name(gc), | |
127 5, xfer->who, 14, "", 27, filename, 28, size); | |
7651 | 128 |
129 content_length = YAHOO_PACKET_HDRLEN + yahoo_packet_length(pkt); | |
130 | |
131 buf = g_strdup_printf("Y=%s; T=%s", yd->cookie_y, yd->cookie_t); | |
132 | |
10576 | 133 host = gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST); |
10575 | 134 port = gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT); |
135 post = g_strdup_printf("POST http://%s:%d/notifyft HTTP/1.0\r\n" | |
10111 | 136 "Content-length: %" G_GSIZE_FORMAT "\r\n" |
7651 | 137 "Host: %s:%d\r\n" |
138 "Cookie: %s\r\n" | |
139 "\r\n", | |
10576 | 140 host, port, content_length + 4 + gaim_xfer_get_size(xfer), host, port, buf); |
7651 | 141 write(xfer->fd, post, strlen(post)); |
142 | |
10392 | 143 yahoo_packet_send_special(pkt, xfer->fd, 8); |
7651 | 144 yahoo_packet_free(pkt); |
145 | |
146 write(xfer->fd, "29\xc0\x80", 4); | |
147 | |
148 g_free(size); | |
149 g_free(post); | |
150 g_free(buf); | |
7805 | 151 g_free(filename); |
7651 | 152 } |
153 | |
154 static void yahoo_xfer_init(GaimXfer *xfer) | |
155 { | |
156 struct yahoo_xfer_data *xfer_data; | |
157 GaimConnection *gc; | |
158 GaimAccount *account; | |
7827 | 159 struct yahoo_data *yd; |
7651 | 160 |
161 xfer_data = xfer->data; | |
162 gc = xfer_data->gc; | |
7827 | 163 yd = gc->proto_data; |
7651 | 164 account = gaim_connection_get_account(gc); |
165 | |
166 if (gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) { | |
10361 | 167 if (0 && gaim_xfer_get_size(xfer) >= 1048577) { |
7651 | 168 gaim_notify_error(gc, NULL, _("File Transfer Aborted"), |
8282 | 169 _("Gaim cannot send files over Yahoo! that are bigger than " |
170 "One Megabyte (1,048,576 bytes).")); | |
171 gaim_xfer_cancel_local(xfer); | |
172 } else { | |
9164 | 173 if (yd->jp) { |
174 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xferjp_host", YAHOOJP_XFER_HOST), | |
175 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT), | |
176 yahoo_sendfile_connected, xfer) == -1) | |
177 { | |
178 gaim_notify_error(gc, NULL, _("File Transfer Aborted"), | |
179 _("Unable to establish file descriptor.")); | |
180 gaim_xfer_cancel_remote(xfer); | |
181 } | |
182 } else { | |
183 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST), | |
184 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT), | |
185 yahoo_sendfile_connected, xfer) == -1) | |
186 { | |
187 gaim_notify_error(gc, NULL, _("File Transfer Aborted"), | |
188 _("Unable to establish file descriptor.")); | |
189 gaim_xfer_cancel_remote(xfer); | |
190 } | |
8282 | 191 } |
7651 | 192 } |
193 } else { | |
194 xfer->fd = gaim_proxy_connect(account, xfer_data->host, xfer_data->port, | |
195 yahoo_receivefile_connected, xfer); | |
196 if (xfer->fd == -1) { | |
197 gaim_notify_error(gc, NULL, _("File Transfer Aborted"), | |
198 _("Unable to establish file descriptor.")); | |
199 gaim_xfer_cancel_remote(xfer); | |
200 } | |
201 } | |
202 } | |
203 | |
204 static void yahoo_xfer_start(GaimXfer *xfer) | |
205 { | |
206 /* We don't need to do anything here, do we? */ | |
207 } | |
208 | |
209 static void yahoo_xfer_end(GaimXfer *xfer) | |
210 { | |
211 GaimAccount *account; | |
212 struct yahoo_xfer_data *xfer_data; | |
213 | |
214 account = gaim_xfer_get_account(xfer); | |
215 xfer_data = xfer->data; | |
216 | |
217 | |
218 if (xfer_data) | |
219 yahoo_xfer_data_free(xfer_data); | |
220 xfer->data = NULL; | |
221 | |
222 } | |
223 | |
224 guint calculate_length(const gchar *l, size_t len) | |
225 { | |
226 int i; | |
227 | |
228 for (i = 0; i < len; i++) { | |
229 if (!g_ascii_isdigit(l[i])) | |
230 continue; | |
231 return strtol(l + i, NULL, 10); | |
232 } | |
233 return 0; | |
234 } | |
235 | |
236 | |
8231
f50c059b6384
[gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
8046
diff
changeset
|
237 ssize_t yahoo_xfer_read(char **buffer, GaimXfer *xfer) |
7651 | 238 { |
7710 | 239 gchar buf[4096]; |
7682 | 240 ssize_t len; |
7651 | 241 gchar *start = NULL; |
242 gchar *length; | |
243 gchar *end; | |
7710 | 244 int filelen; |
7651 | 245 struct yahoo_xfer_data *xd = xfer->data; |
246 | |
247 if (gaim_xfer_get_type(xfer) != GAIM_XFER_RECEIVE) { | |
248 return 0; | |
249 } | |
250 | |
251 len = read(xfer->fd, buf, sizeof(buf)); | |
252 | |
7682 | 253 if (len <= 0) { |
7710 | 254 if ((gaim_xfer_get_size(xfer) > 0) && |
9798 | 255 (gaim_xfer_get_bytes_sent(xfer) >= gaim_xfer_get_size(xfer))) { |
7682 | 256 gaim_xfer_set_completed(xfer, TRUE); |
9798 | 257 return 0; |
258 } else | |
259 return -1; | |
7651 | 260 } |
261 | |
262 | |
263 if (!xd->started) { | |
264 xd->rxqueue = g_realloc(xd->rxqueue, len + xd->rxlen); | |
265 memcpy(xd->rxqueue + xd->rxlen, buf, len); | |
266 xd->rxlen += len; | |
267 | |
268 length = g_strstr_len(xd->rxqueue, len, "Content-length:"); | |
10579 | 269 /* some proxies re-write this header, changing the capitalization :( |
270 * technically that's allowed since headers are case-insensitive | |
271 * [RFC 2616, section 4.2] */ | |
272 if (length == NULL) | |
273 length = g_strstr_len(xd->rxqueue, len, "Content-Length:"); | |
7651 | 274 if (length) { |
275 end = g_strstr_len(length, length - xd->rxqueue, "\r\n"); | |
276 if (!end) | |
277 return 0; | |
7710 | 278 if ((filelen = calculate_length(length, len - (length - xd->rxqueue)))) |
279 gaim_xfer_set_size(xfer, filelen); | |
7651 | 280 } |
281 start = g_strstr_len(xd->rxqueue, len, "\r\n\r\n"); | |
282 if (start) | |
283 start += 4; | |
284 if (!start || start > (xd->rxqueue + len)) | |
285 return 0; | |
286 xd->started = TRUE; | |
287 | |
288 len -= (start - xd->rxqueue); | |
289 | |
290 *buffer = g_malloc(len); | |
291 memcpy(*buffer, start, len); | |
292 g_free(xd->rxqueue); | |
293 xd->rxqueue = NULL; | |
294 xd->rxlen = 0; | |
295 } else { | |
296 *buffer = g_malloc(len); | |
297 memcpy(*buffer, buf, len); | |
298 } | |
299 | |
300 return len; | |
301 } | |
302 | |
8231
f50c059b6384
[gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
8046
diff
changeset
|
303 ssize_t yahoo_xfer_write(const char *buffer, size_t size, GaimXfer *xfer) |
7651 | 304 { |
7710 | 305 ssize_t len; |
7651 | 306 struct yahoo_xfer_data *xd = xfer->data; |
307 | |
308 if (!xd) | |
9798 | 309 return -1; |
7651 | 310 |
311 if (gaim_xfer_get_type(xfer) != GAIM_XFER_SEND) { | |
9798 | 312 return -1; |
7651 | 313 } |
314 | |
315 len = write(xfer->fd, buffer, size); | |
316 | |
7710 | 317 if (len == -1) { |
318 if (gaim_xfer_get_bytes_sent(xfer) >= gaim_xfer_get_size(xfer)) | |
319 gaim_xfer_set_completed(xfer, TRUE); | |
320 if ((errno != EAGAIN) && (errno != EINTR)) | |
9798 | 321 return -1; |
7710 | 322 return 0; |
323 } | |
324 | |
325 if ((gaim_xfer_get_bytes_sent(xfer) + len) >= gaim_xfer_get_size(xfer)) | |
7651 | 326 gaim_xfer_set_completed(xfer, TRUE); |
327 | |
328 return len; | |
329 } | |
330 | |
331 static void yahoo_xfer_cancel_send(GaimXfer *xfer) | |
332 { | |
333 GaimAccount *account; | |
334 struct yahoo_xfer_data *xfer_data; | |
335 | |
336 xfer_data = xfer->data; | |
337 account = gaim_xfer_get_account(xfer); | |
338 | |
339 if (xfer_data) | |
340 yahoo_xfer_data_free(xfer_data); | |
341 xfer->data = NULL; | |
342 } | |
343 | |
344 static void yahoo_xfer_cancel_recv(GaimXfer *xfer) | |
345 { | |
346 GaimAccount *account; | |
347 struct yahoo_xfer_data *xfer_data; | |
348 | |
349 account = gaim_xfer_get_account(xfer); | |
350 xfer_data = xfer->data; | |
351 | |
352 if (xfer_data) | |
353 yahoo_xfer_data_free(xfer_data); | |
354 xfer->data = NULL; | |
355 } | |
356 | |
357 void yahoo_process_filetransfer(GaimConnection *gc, struct yahoo_packet *pkt) | |
358 { | |
359 char *from = NULL; | |
360 char *to = NULL; | |
361 char *msg = NULL; | |
362 char *url = NULL; | |
363 long expires = 0; | |
364 GaimXfer *xfer; | |
365 struct yahoo_xfer_data *xfer_data; | |
366 | |
367 char *service = NULL; | |
368 | |
369 char *filename = NULL; | |
370 unsigned long filesize = 0L; | |
371 | |
372 GSList *l; | |
373 | |
374 for (l = pkt->hash; l; l = l->next) { | |
375 struct yahoo_pair *pair = l->data; | |
376 | |
377 if (pair->key == 4) | |
378 from = pair->value; | |
379 if (pair->key == 5) | |
380 to = pair->value; | |
381 if (pair->key == 14) | |
382 msg = pair->value; | |
383 if (pair->key == 20) | |
384 url = pair->value; | |
385 if (pair->key == 38) | |
386 expires = strtol(pair->value, NULL, 10); | |
387 | |
388 if (pair->key == 27) | |
389 filename = pair->value; | |
390 if (pair->key == 28) | |
391 filesize = atol(pair->value); | |
392 | |
393 if (pair->key == 49) | |
394 service = pair->value; | |
395 } | |
396 | |
397 if (pkt->service == YAHOO_SERVICE_P2PFILEXFER) { | |
10261 | 398 if (service && (strcmp("FILEXFER", service) != 0)) { |
7651 | 399 gaim_debug_misc("yahoo", "unhandled service 0x%02x", pkt->service); |
400 return; | |
401 } | |
402 } | |
403 | |
404 if (msg) { | |
405 char *tmp; | |
406 tmp = strchr(msg, '\006'); | |
407 if (tmp) | |
408 *tmp = '\0'; | |
409 } | |
410 | |
411 if (!url || !from) | |
412 return; | |
413 | |
414 | |
415 /* Setup the Yahoo-specific file transfer data */ | |
416 xfer_data = g_new0(struct yahoo_xfer_data, 1); | |
417 xfer_data->gc = gc; | |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9164
diff
changeset
|
418 if (!gaim_url_parse(url, &(xfer_data->host), &(xfer_data->port), &(xfer_data->path), NULL, NULL)) { |
7651 | 419 g_free(xfer_data); |
420 return; | |
421 } | |
422 | |
423 gaim_debug_misc("yahoo_filexfer", "Host is %s, port is %d, path is %s, and the full url was %s.\n", | |
424 xfer_data->host, xfer_data->port, xfer_data->path, url); | |
425 | |
426 /* Build the file transfer handle. */ | |
427 xfer = gaim_xfer_new(gc->account, GAIM_XFER_RECEIVE, from); | |
428 xfer->data = xfer_data; | |
429 | |
430 /* Set the info about the incoming file. */ | |
431 if (filename) | |
432 gaim_xfer_set_filename(xfer, filename); | |
433 else { | |
434 gchar *start, *end; | |
435 start = g_strrstr(xfer_data->path, "/"); | |
436 if (start) | |
437 start++; | |
438 end = g_strrstr(xfer_data->path, "?"); | |
439 if (start && *start && end) { | |
440 filename = g_strndup(start, end - start); | |
441 gaim_xfer_set_filename(xfer, filename); | |
442 g_free(filename); | |
443 filename = NULL; | |
444 } | |
445 } | |
446 | |
447 gaim_xfer_set_size(xfer, filesize); | |
448 | |
449 /* Setup our I/O op functions */ | |
450 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init); | |
451 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start); | |
452 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end); | |
453 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send); | |
454 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv); | |
455 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read); | |
456 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write); | |
457 | |
458 /* Now perform the request */ | |
459 gaim_xfer_request(xfer); | |
460 } | |
461 | |
462 void yahoo_send_file(GaimConnection *gc, const char *who, const char *file) | |
463 { | |
464 GaimXfer *xfer; | |
465 struct yahoo_xfer_data *xfer_data; | |
466 | |
9466 | 467 if (!who) |
7651 | 468 return; |
469 | |
470 xfer_data = g_new0(struct yahoo_xfer_data, 1); | |
471 xfer_data->gc = gc; | |
472 | |
473 | |
474 /* Build the file transfer handle. */ | |
475 xfer = gaim_xfer_new(gc->account, GAIM_XFER_SEND, who); | |
476 xfer->data = xfer_data; | |
477 | |
478 /* Setup our I/O op functions */ | |
479 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init); | |
480 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start); | |
481 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end); | |
482 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send); | |
483 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv); | |
484 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read); | |
485 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write); | |
486 | |
487 /* Now perform the request */ | |
9466 | 488 if (file) |
489 gaim_xfer_request_accepted(xfer, file); | |
490 else | |
491 gaim_xfer_request(xfer); | |
7651 | 492 } |