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