comparison Plugins/Input/vorbis/http.c @ 61:fa848bd484d8 trunk

[svn] Move plugins to Plugins/
author nenolod
date Fri, 28 Oct 2005 22:58:11 -0700
parents
children 4e1d41a93cb3
comparison
equal deleted inserted replaced
60:1771f253e1b2 61:fa848bd484d8
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "libaudacious/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, err_len, port, cport;
298 gboolean redirect;
299 fd_set set;
300 struct hostent *hp;
301 struct sockaddr_in address;
302 struct timeval tv;
303
304 url = (gchar *) arg;
305 do {
306 redirect = FALSE;
307
308 g_strstrip(url);
309
310 parse_url(url, &user, &pass, &host, &port, &filename);
311
312 if ((!filename || !*filename) && url[strlen(url) - 1] != '/')
313 temp = g_strconcat(url, "/", NULL);
314 else
315 temp = g_strdup(url);
316 g_free(url);
317 url = temp;
318
319 chost = vorbis_cfg.use_proxy ? vorbis_cfg.proxy_host : host;
320 cport = vorbis_cfg.use_proxy ? vorbis_cfg.proxy_port : port;
321
322 sock = socket(AF_INET, SOCK_STREAM, 0);
323 fcntl(sock, F_SETFL, O_NONBLOCK);
324 address.sin_family = AF_INET;
325
326 status = g_strdup_printf(_("LOOKING UP %s"), chost);
327 vorbis_ip.set_info_text(status);
328 g_free(status);
329
330 if (!(hp = gethostbyname(chost))) {
331 status = g_strdup_printf(_("Couldn't look up host %s"), chost);
332 show_error_message(status);
333 g_free(status);
334
335 vorbis_ip.set_info_text(NULL);
336 eof = TRUE;
337 }
338
339 if (!eof) {
340 memcpy(&address.sin_addr.s_addr, *(hp->h_addr_list),
341 sizeof(address.sin_addr.s_addr));
342 address.sin_port = g_htons(cport);
343
344 status = g_strdup_printf(_("CONNECTING TO %s:%d"), chost, cport);
345 vorbis_ip.set_info_text(status);
346 g_free(status);
347 if (connect
348 (sock, (struct sockaddr *) &address,
349 sizeof(struct sockaddr_in)) == -1) {
350 if (errno != EINPROGRESS) {
351 status =
352 g_strdup_printf(_("Couldn't connect to host %s"),
353 chost);
354 show_error_message(status);
355 g_free(status);
356
357 vorbis_ip.set_info_text(NULL);
358 eof = TRUE;
359 }
360 }
361 while (going) {
362 tv.tv_sec = 0;
363 tv.tv_usec = 10000;
364 FD_ZERO(&set);
365 FD_SET(sock, &set);
366 if (select(sock + 1, NULL, &set, NULL, &tv) > 0) {
367 err_len = sizeof(error);
368 getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &err_len);
369 if (error) {
370 status =
371 g_strdup_printf(_
372 ("Couldn't connect to host %s"),
373 chost);
374 show_error_message(status);
375 g_free(status);
376
377 vorbis_ip.set_info_text(NULL);
378 eof = TRUE;
379
380 }
381 break;
382 }
383 }
384 if (!eof) {
385 gchar *auth = NULL, *proxy_auth = NULL;
386
387 if (user && pass)
388 auth =
389 basic_authentication_encode(user, pass,
390 "Authorization");
391
392 if (vorbis_cfg.use_proxy) {
393 file = g_strdup(url);
394 if (vorbis_cfg.proxy_use_auth && vorbis_cfg.proxy_user
395 && vorbis_cfg.proxy_pass) {
396 proxy_auth =
397 basic_authentication_encode(vorbis_cfg.
398 proxy_user,
399 vorbis_cfg.
400 proxy_pass,
401 "Proxy-Authorization");
402 }
403 }
404 else
405 file = g_strconcat("/", filename, NULL);
406 temp = g_strdup_printf("GET %s HTTP/1.0\r\n"
407 "Host: %s\r\n"
408 "User-Agent: %s/%s\r\n"
409 "%s%s\r\n",
410 file, host, PACKAGE, VERSION,
411 proxy_auth ? proxy_auth : "",
412 auth ? auth : "");
413 g_free(file);
414 if (proxy_auth)
415 g_free(proxy_auth);
416 if (auth)
417 g_free(auth);
418 write(sock, temp, strlen(temp));
419 g_free(temp);
420 vorbis_ip.set_info_text(_("CONNECTED: WAITING FOR REPLY"));
421 while (going && !eof) {
422 if (http_check_for_data()) {
423 if (vorbis_http_read_line(line, 1024)) {
424 status = strchr(line, ' ');
425 if (status) {
426 if (status[1] == '2')
427 break;
428 else if (status[1] == '3'
429 && status[2] == '0'
430 && status[3] == '2') {
431 while (going) {
432 if (http_check_for_data()) {
433 if ((cnt =
434 vorbis_http_read_line
435 (line, 1024)) != -1) {
436 if (!cnt)
437 break;
438 if (!strncmp
439 (line, "Location:", 9)) {
440 g_free(url);
441 url = g_strdup(line + 10);
442 }
443 }
444 else {
445 eof = TRUE;
446 vorbis_ip.set_info_text(NULL);
447 break;
448 }
449 }
450 }
451 redirect = TRUE;
452 break;
453 }
454 else {
455 status =
456 g_strdup_printf(_
457 ("Couldn't connect to host %s\nServer reported: %s"),
458 chost, status);
459 show_error_message(status);
460 g_free(status);
461 break;
462 }
463 }
464 }
465 else {
466 eof = TRUE;
467 vorbis_ip.set_info_text(NULL);
468 }
469 }
470 }
471
472 while (going && !redirect) {
473 if (http_check_for_data()) {
474 if ((cnt = vorbis_http_read_line(line, 1024)) != -1) {
475 if (!cnt)
476 break;
477 if (!strncmp(line, "ice-name:", 9))
478 ice_name = g_strdup(line + 9);
479
480 }
481 else {
482 eof = TRUE;
483 vorbis_ip.set_info_text(NULL);
484 break;
485 }
486 }
487 }
488 }
489 }
490
491 if (redirect) {
492 if (output_file) {
493 vfs_fclose(output_file);
494 output_file = NULL;
495 }
496 close(sock);
497 g_free(user);
498 g_free(pass);
499 g_free(host);
500 g_free(filename);
501 }
502 } while (redirect);
503
504 if (vorbis_cfg.save_http_stream) {
505 gchar *output_name;
506 file = vorbis_http_get_title(url);
507 output_name = file;
508 if (!strncasecmp(output_name, "http://", 7))
509 output_name += 7;
510 temp = strrchr(output_name, '.');
511 if (temp && !strcasecmp(temp, ".ogg"))
512 *temp = '\0';
513
514 while ((temp = strchr(output_name, '/')))
515 *temp = '_';
516 output_name =
517 g_strdup_printf("%s/%s.ogg", vorbis_cfg.save_http_path,
518 output_name);
519
520 g_free(file);
521
522 output_file = vfs_fopen(output_name, "wb");
523 g_free(output_name);
524 }
525
526 while (going) {
527
528 if (!http_used() && !vorbis_ip.output->buffer_playing())
529 prebuffering = TRUE;
530 if (http_free() > 0 && !eof) {
531 if (http_check_for_data()) {
532 cnt = min(http_free(), buffer_length - wr_index);
533 if (cnt > 1024)
534 cnt = 1024;
535 written = read(sock, buffer + wr_index, cnt);
536 if (written <= 0) {
537 eof = TRUE;
538 if (prebuffering) {
539 prebuffering = FALSE;
540
541 vorbis_ip.set_info_text(NULL);
542 }
543
544 }
545 else
546 wr_index = (wr_index + written) % buffer_length;
547 }
548
549 if (prebuffering) {
550 if (http_used() > prebuffer_length) {
551 prebuffering = FALSE;
552 vorbis_ip.set_info_text(NULL);
553 }
554 else {
555 status =
556 g_strdup_printf(_("PRE-BUFFERING: %dKB/%dKB"),
557 http_used() / 1024,
558 prebuffer_length / 1024);
559 vorbis_ip.set_info_text(status);
560 g_free(status);
561 }
562
563 }
564 }
565 else
566 xmms_usleep(10000);
567
568 }
569 if (output_file) {
570 vfs_fclose(output_file);
571 output_file = NULL;
572 }
573 close(sock);
574
575
576 g_free(user);
577 g_free(pass);
578 g_free(host);
579 g_free(filename);
580 g_free(buffer);
581 g_free(url);
582
583 return NULL;
584 }
585
586 gint
587 vorbis_http_open(const gchar * _url)
588 {
589 gchar *url;
590
591 url = g_strdup(_url);
592
593 rd_index = 0;
594 wr_index = 0;
595 buffer_length = vorbis_cfg.http_buffer_size * 1024;
596 prebuffer_length = (buffer_length * vorbis_cfg.http_prebuffer) / 100;
597 buffer_read = 0;
598 prebuffering = TRUE;
599 going = TRUE;
600 eof = FALSE;
601 buffer = g_malloc(buffer_length);
602
603 thread = g_thread_create(http_buffer_loop, url, TRUE, NULL);
604
605 return 0;
606 }
607
608 gchar *
609 vorbis_http_get_title(const gchar * url)
610 {
611 gchar *url_basename;
612
613 if (ice_name)
614 return g_strdup(ice_name);
615
616 url_basename = g_path_get_basename(url);
617
618 if (strlen(url_basename) > 0)
619 return url_basename;
620
621 g_free(url_basename);
622
623 return g_strdup(url);
624 }