comparison src/vorbis/http.c @ 12:3da1b8942b8b trunk

[svn] - remove src/Input src/Output src/Effect src/General src/Visualization src/Container
author nenolod
date Mon, 18 Sep 2006 03:14:20 -0700
parents src/Input/vorbis/http.c@088092a52fea
children
comparison
equal deleted inserted replaced
11:cff1d04026ae 12:3da1b8942b8b
1 /* BMP - Cross-platform multimedia player
2 * Copyright (C) 2003-2004 BMP development team.
3 *
4 * Based on XMMS:
5 * Copyright (C) 1998-2003 XMMS development team.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <fcntl.h>
32
33 #include <unistd.h>
34 #include <errno.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <netdb.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/time.h>
41
42 #include "vorbis.h"
43 #include "http.h"
44 #include "audacious/util.h"
45 #include "audacious/plugin.h"
46
47
48 #define min(x,y) ((x)<(y)?(x):(y))
49 #define min3(x,y,z) (min(x,y)<(z)?min(x,y):(z))
50 #define min4(x,y,z,w) (min3(x,y,z)<(w)?min3(x,y,z):(w))
51
52 static gchar *ice_name = NULL;
53
54 static gboolean prebuffering, going, eof = FALSE;
55 static gint sock, rd_index, wr_index, buffer_length, prebuffer_length;
56 static guint64 buffer_read = 0;
57 static gchar *buffer;
58 static GThread *thread;
59 static GtkWidget *error_dialog = NULL;
60
61 extern vorbis_config_t vorbis_cfg;
62 extern InputPlugin vorbis_ip;
63 extern int vorbis_playing;
64
65 static VFSFile *output_file = NULL;
66
67 #define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
68
69 /* Encode the string S of length LENGTH to base64 format and place it
70 to STORE. STORE will be 0-terminated, and must point to a writable
71 buffer of at least 1+BASE64_LENGTH(length) bytes. */
72 static void
73 base64_encode(const gchar * s, gchar * store, gint length)
74 {
75 /* Conversion table. */
76 static gchar tbl[64] = {
77 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
78 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
79 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
80 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
81 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
82 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
83 'w', 'x', 'y', 'z', '0', '1', '2', '3',
84 '4', '5', '6', '7', '8', '9', '+', '/'
85 };
86 gint i;
87 guchar *p = (guchar *) store;
88
89 /* Transform the 3x8 bits to 4x6 bits, as required by base64. */
90 for (i = 0; i < length; i += 3) {
91 *p++ = tbl[s[0] >> 2];
92 *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
93 *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
94 *p++ = tbl[s[2] & 0x3f];
95 s += 3;
96 }
97 /* Pad the result if necessary... */
98 if (i == length + 1)
99 *(p - 1) = '=';
100 else if (i == length + 2)
101 *(p - 1) = *(p - 2) = '=';
102 /* ...and zero-terminate it. */
103 *p = '\0';
104 }
105
106 /* Create the authentication header contents for the `Basic' scheme.
107 This is done by encoding the string `USER:PASS' in base64 and
108 prepending `HEADER: Basic ' to it. */
109 static gchar *
110 basic_authentication_encode(const gchar * user,
111 const gchar * passwd, const gchar * header)
112 {
113 gchar *t1, *t2, *res;
114 gint len1 = strlen(user) + 1 + strlen(passwd);
115 gint len2 = BASE64_LENGTH(len1);
116
117 t1 = g_strdup_printf("%s:%s", user, passwd);
118 t2 = g_malloc0(len2 + 1);
119 base64_encode(t1, t2, len1);
120 res = g_strdup_printf("%s: Basic %s\r\n", header, t2);
121 g_free(t2);
122 g_free(t1);
123
124 return res;
125 }
126
127 static void
128 parse_url(const gchar * url, gchar ** user, gchar ** pass,
129 gchar ** host, int *port, gchar ** filename)
130 {
131 gchar *h, *p, *pt, *f, *temp, *ptr;
132
133 temp = g_strdup(url);
134 ptr = temp;
135
136 if (!strncasecmp("http://", ptr, 7))
137 ptr += 7;
138 h = strchr(ptr, '@');
139 f = strchr(ptr, '/');
140 if (h != NULL && (!f || h < f)) {
141 *h = '\0';
142 p = strchr(ptr, ':');
143 if (p != NULL && p < h) {
144 *p = '\0';
145 p++;
146 *pass = g_strdup(p);
147 }
148 else
149 *pass = NULL;
150 *user = g_strdup(ptr);
151 h++;
152 ptr = h;
153 }
154 else {
155 *user = NULL;
156 *pass = NULL;
157 h = ptr;
158 }
159 pt = strchr(ptr, ':');
160 if (pt != NULL && (f == NULL || pt < f)) {
161 *pt = '\0';
162 *port = atoi(pt + 1);
163 }
164 else {
165 if (f)
166 *f = '\0';
167 *port = 80;
168 }
169 *host = g_strdup(h);
170
171 if (f)
172 *filename = g_strdup(f + 1);
173 else
174 *filename = NULL;
175 g_free(temp);
176 }
177
178 void
179 vorbis_http_close(void)
180 {
181 going = FALSE;
182
183 g_thread_join(thread);
184 g_free(ice_name);
185 ice_name = NULL;
186 }
187
188
189 static gint
190 http_used(void)
191 {
192 if (wr_index >= rd_index)
193 return wr_index - rd_index;
194 return buffer_length - (rd_index - wr_index);
195 }
196
197 static gint
198 http_free(void)
199 {
200 if (rd_index > wr_index)
201 return (rd_index - wr_index) - 1;
202 return (buffer_length - (wr_index - rd_index)) - 1;
203 }
204
205 static void
206 http_wait_for_data(gint bytes)
207 {
208 while ((prebuffering || http_used() < bytes) && !eof && going
209 && vorbis_playing)
210 xmms_usleep(10000);
211 }
212
213 static void
214 show_error_message(gchar * error)
215 {
216 if (!error_dialog) {
217 GDK_THREADS_ENTER();
218 error_dialog = xmms_show_message(_("Error"), error, _("Ok"), FALSE,
219 NULL, NULL);
220 g_signal_connect(G_OBJECT(error_dialog),
221 "destroy",
222 G_CALLBACK(gtk_widget_destroyed), &error_dialog);
223 GDK_THREADS_LEAVE();
224 }
225 }
226
227 int
228 vorbis_http_read(gpointer data, gint length)
229 {
230 gint len, cnt, off = 0;
231
232 http_wait_for_data(length);
233
234 if (!going && !vorbis_playing)
235 return 0;
236 len = min(http_used(), length);
237
238 while (len && http_used()) {
239 cnt = min3(len, buffer_length - rd_index, http_used());
240 if (output_file)
241 vfs_fwrite(buffer + rd_index, 1, cnt, output_file);
242
243 memcpy((gchar *) data + off, buffer + rd_index, cnt);
244 rd_index = (rd_index + cnt) % buffer_length;
245 buffer_read += cnt;
246 len -= cnt;
247 off += cnt;
248 }
249 return off;
250 }
251
252 static gboolean
253 http_check_for_data(void)
254 {
255
256 fd_set set;
257 struct timeval tv;
258 gint ret;
259
260 tv.tv_sec = 0;
261 tv.tv_usec = 20000;
262 FD_ZERO(&set);
263 FD_SET(sock, &set);
264 ret = select(sock + 1, &set, NULL, NULL, &tv);
265 if (ret > 0)
266 return TRUE;
267 return FALSE;
268 }
269
270 gint
271 vorbis_http_read_line(gchar * buf, gint size)
272 {
273 gint i = 0;
274
275 while (going && i < size - 1) {
276 if (http_check_for_data()) {
277 if (read(sock, buf + i, 1) <= 0)
278 return -1;
279 if (buf[i] == '\n')
280 break;
281 if (buf[i] != '\r')
282 i++;
283 }
284 }
285 if (!going)
286 return -1;
287 buf[i] = '\0';
288 return i;
289 }
290
291 static gpointer
292 http_buffer_loop(gpointer arg)
293 {
294 gchar line[1024], *user, *pass, *host, *filename,
295 *status, *url, *temp, *file;
296 gchar *chost;
297 gint cnt, written, error, port, cport;
298 guint err_len;
299 gboolean redirect;
300 fd_set set;
301 #ifdef USE_IPV6
302 struct addrinfo hints, *res, *res0;
303 char service[6];
304 #else
305 struct hostent *hp;
306 struct sockaddr_in address;
307 #endif
308 struct timeval tv;
309
310 url = (gchar *) arg;
311 do {
312 redirect = FALSE;
313
314 g_strstrip(url);
315
316 parse_url(url, &user, &pass, &host, &port, &filename);
317
318 if ((!filename || !*filename) && url[strlen(url) - 1] != '/')
319 temp = g_strconcat(url, "/", NULL);
320 else
321 temp = g_strdup(url);
322 g_free(url);
323 url = temp;
324
325 chost = vorbis_cfg.use_proxy ? vorbis_cfg.proxy_host : host;
326 cport = vorbis_cfg.use_proxy ? vorbis_cfg.proxy_port : port;
327
328 #ifdef USE_IPV6
329 snprintf(service, 6, "%d", cport);
330 memset(&hints, 0, sizeof(hints));
331 hints.ai_socktype = SOCK_STREAM;
332 if (! getaddrinfo(chost, service, &hints, &res0)) {
333 eof = TRUE;
334 for (res = res0; res; res = res->ai_next) {
335 if ((sock = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
336 continue;
337 fcntl(sock, F_SETFL, O_NONBLOCK);
338 status = g_strdup_printf(_("CONNECTING TO %s:%d"), chost, cport);
339 vorbis_ip.set_info_text(status);
340 g_free(status);
341 ((struct sockaddr_in6 *)res->ai_addr)->sin6_port = htons(cport);
342 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
343 if (errno != EINPROGRESS) {
344 close(sock);
345 continue;
346 }
347 }
348 eof = FALSE;
349 break;
350 }
351 freeaddrinfo(res0);
352 if (eof) {
353 status = g_strdup_printf(_("Couldn't connect to host %s:%d"), chost, cport);
354 vorbis_ip.set_info_text(status);
355 g_free(status);
356 eof = TRUE;
357 break;
358 }
359 } else {
360 status = g_strdup_printf(_("Couldn't look up host %s"), chost);
361 vorbis_ip.set_info_text(status);
362 g_free(status);
363 eof = TRUE;
364 }
365 #else
366 sock = socket(AF_INET, SOCK_STREAM, 0);
367 fcntl(sock, F_SETFL, O_NONBLOCK);
368 address.sin_family = AF_INET;
369
370 status = g_strdup_printf(_("LOOKING UP %s"), chost);
371 vorbis_ip.set_info_text(status);
372 g_free(status);
373
374 if (!(hp = gethostbyname(chost))) {
375 status = g_strdup_printf(_("Couldn't look up host %s"), chost);
376 show_error_message(status);
377 g_free(status);
378
379 vorbis_ip.set_info_text(NULL);
380 eof = TRUE;
381 }
382 #endif
383
384 if (!eof) {
385 #ifndef USE_IPV6
386 memcpy(&address.sin_addr.s_addr, *(hp->h_addr_list),
387 sizeof(address.sin_addr.s_addr));
388 address.sin_port = g_htons(cport);
389
390 status = g_strdup_printf(_("CONNECTING TO %s:%d"), chost, cport);
391 vorbis_ip.set_info_text(status);
392 g_free(status);
393 if (connect
394 (sock, (struct sockaddr *) &address,
395 sizeof(struct sockaddr_in)) == -1) {
396 if (errno != EINPROGRESS) {
397 status =
398 g_strdup_printf(_("Couldn't connect to host %s"),
399 chost);
400 show_error_message(status);
401 g_free(status);
402
403 vorbis_ip.set_info_text(NULL);
404 eof = TRUE;
405 }
406 }
407 #endif
408 while (going) {
409 tv.tv_sec = 0;
410 tv.tv_usec = 10000;
411 FD_ZERO(&set);
412 FD_SET(sock, &set);
413 if (select(sock + 1, NULL, &set, NULL, &tv) > 0) {
414 err_len = sizeof(error);
415 getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &err_len);
416 if (error) {
417 status =
418 g_strdup_printf(_
419 ("Couldn't connect to host %s"),
420 chost);
421 show_error_message(status);
422 g_free(status);
423
424 vorbis_ip.set_info_text(NULL);
425 eof = TRUE;
426
427 }
428 break;
429 }
430 }
431 if (!eof) {
432 gchar *auth = NULL, *proxy_auth = NULL;
433
434 if (user && pass)
435 auth =
436 basic_authentication_encode(user, pass,
437 "Authorization");
438
439 if (vorbis_cfg.use_proxy) {
440 file = g_strdup(url);
441 if (vorbis_cfg.proxy_use_auth && vorbis_cfg.proxy_user
442 && vorbis_cfg.proxy_pass) {
443 proxy_auth =
444 basic_authentication_encode(vorbis_cfg.
445 proxy_user,
446 vorbis_cfg.
447 proxy_pass,
448 "Proxy-Authorization");
449 }
450 }
451 else
452 file = g_strconcat("/", filename, NULL);
453 temp = g_strdup_printf("GET %s HTTP/1.0\r\n"
454 "Host: %s\r\n"
455 "User-Agent: %s/%s\r\n"
456 "%s%s\r\n",
457 file, host, PACKAGE_NAME, PACKAGE_VERSION,
458 proxy_auth ? proxy_auth : "",
459 auth ? auth : "");
460 g_free(file);
461 if (proxy_auth)
462 g_free(proxy_auth);
463 if (auth)
464 g_free(auth);
465 write(sock, temp, strlen(temp));
466 g_free(temp);
467 vorbis_ip.set_info_text(_("CONNECTED: WAITING FOR REPLY"));
468 while (going && !eof) {
469 if (http_check_for_data()) {
470 if (vorbis_http_read_line(line, 1024)) {
471 status = strchr(line, ' ');
472 if (status) {
473 if (status[1] == '2')
474 break;
475 else if (status[1] == '3'
476 && status[2] == '0'
477 && status[3] == '2') {
478 while (going) {
479 if (http_check_for_data()) {
480 if ((cnt =
481 vorbis_http_read_line
482 (line, 1024)) != -1) {
483 if (!cnt)
484 break;
485 if (!strncmp
486 (line, "Location:", 9)) {
487 g_free(url);
488 url = g_strdup(line + 10);
489 }
490 }
491 else {
492 eof = TRUE;
493 vorbis_ip.set_info_text(NULL);
494 break;
495 }
496 }
497 }
498 redirect = TRUE;
499 break;
500 }
501 else {
502 status =
503 g_strdup_printf(_
504 ("Couldn't connect to host %s\nServer reported: %s"),
505 chost, status);
506 show_error_message(status);
507 g_free(status);
508 break;
509 }
510 }
511 }
512 else {
513 eof = TRUE;
514 vorbis_ip.set_info_text(NULL);
515 }
516 }
517 }
518
519 while (going && !redirect) {
520 if (http_check_for_data()) {
521 if ((cnt = vorbis_http_read_line(line, 1024)) != -1) {
522 if (!cnt)
523 break;
524 if (!strncmp(line, "ice-name:", 9))
525 ice_name = g_strdup(line + 9);
526
527 }
528 else {
529 eof = TRUE;
530 vorbis_ip.set_info_text(NULL);
531 break;
532 }
533 }
534 }
535 }
536 }
537
538 if (redirect) {
539 if (output_file) {
540 vfs_fclose(output_file);
541 output_file = NULL;
542 }
543 close(sock);
544 g_free(user);
545 g_free(pass);
546 g_free(host);
547 g_free(filename);
548 }
549 } while (redirect);
550
551 if (vorbis_cfg.save_http_stream) {
552 gchar *output_name;
553 file = vorbis_http_get_title(url);
554 output_name = file;
555 if (!strncasecmp(output_name, "http://", 7))
556 output_name += 7;
557 temp = strrchr(output_name, '.');
558 if (temp && !strcasecmp(temp, ".ogg"))
559 *temp = '\0';
560
561 while ((temp = strchr(output_name, '/')))
562 *temp = '_';
563 output_name =
564 g_strdup_printf("%s/%s.ogg", vorbis_cfg.save_http_path,
565 output_name);
566
567 g_free(file);
568
569 output_file = vfs_fopen(output_name, "wb");
570 g_free(output_name);
571 }
572
573 while (going) {
574
575 if (!http_used() && !vorbis_ip.output->buffer_playing()) {
576 prebuffering = TRUE;
577 vorbis_ip.set_status_buffering(TRUE);
578 }
579 if (http_free() > 0 && !eof) {
580 if (http_check_for_data()) {
581 cnt = min(http_free(), buffer_length - wr_index);
582 if (cnt > 1024)
583 cnt = 1024;
584 written = read(sock, buffer + wr_index, cnt);
585 if (written <= 0) {
586 eof = TRUE;
587 if (prebuffering) {
588 prebuffering = FALSE;
589 vorbis_ip.set_status_buffering(FALSE);
590
591 vorbis_ip.set_info_text(NULL);
592 }
593
594 }
595 else
596 wr_index = (wr_index + written) % buffer_length;
597 }
598
599 if (prebuffering) {
600 if (http_used() > prebuffer_length) {
601 prebuffering = FALSE;
602 vorbis_ip.set_status_buffering(FALSE);
603 vorbis_ip.set_info_text(NULL);
604 }
605 else {
606 status =
607 g_strdup_printf(_("PRE-BUFFERING: %dKB/%dKB"),
608 http_used() / 1024,
609 prebuffer_length / 1024);
610 vorbis_ip.set_info_text(status);
611 g_free(status);
612 }
613
614 }
615 }
616 else
617 xmms_usleep(10000);
618
619 }
620 if (output_file) {
621 vfs_fclose(output_file);
622 output_file = NULL;
623 }
624 close(sock);
625
626
627 g_free(user);
628 g_free(pass);
629 g_free(host);
630 g_free(filename);
631 g_free(buffer);
632 g_free(url);
633
634 return NULL;
635 }
636
637 gint
638 vorbis_http_open(const gchar * _url)
639 {
640 gchar *url;
641
642 url = g_strdup(_url);
643
644 rd_index = 0;
645 wr_index = 0;
646 buffer_length = vorbis_cfg.http_buffer_size * 1024;
647 prebuffer_length = (buffer_length * vorbis_cfg.http_prebuffer) / 100;
648 buffer_read = 0;
649 prebuffering = TRUE;
650 vorbis_ip.set_status_buffering(TRUE);
651 going = TRUE;
652 eof = FALSE;
653 buffer = g_malloc(buffer_length);
654
655 thread = g_thread_create(http_buffer_loop, url, TRUE, NULL);
656
657 return 0;
658 }
659
660 gchar *
661 vorbis_http_get_title(const gchar * url)
662 {
663 gchar *url_basename;
664
665 if (ice_name)
666 return g_strdup(ice_name);
667
668 url_basename = g_path_get_basename(url);
669
670 if (strlen(url_basename) > 0)
671 return url_basename;
672
673 g_free(url_basename);
674
675 return g_strdup(url);
676 }