1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
|
/*****************************************************************
* gmerlin-avdecoder - a general purpose multimedia decoding library
*
* Copyright (c) 2001 - 2012 Members of the Gmerlin project
* gmerlin-general@lists.sourceforge.net
* http://gmerlin.sourceforge.net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *****************************************************************/
#include <avdec_private.h>
#define LOG_DOMAIN "http"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <gavl/http.h>
#include <gavl/gavlsocket.h>
#include <http.h>
struct bgav_http_s
{
const bgav_options_t * opt;
gavl_dictionary_t header;
gavf_io_t * io;
// int fd;
char * keepalive_host;
int chunked; // Chunked transfer encoding
int chunk_size;
int chunk_pos;
int chunk_error;
int chunk_eof;
int64_t content_length;
int can_seek;
};
static bgav_http_t *
do_connect(bgav_http_t * ret, const char * host, int port, const bgav_options_t * opt,
const gavl_dictionary_t * request_header,
const gavl_dictionary_t * extra_header, int use_tls)
{
gavl_dictionary_t header;
int fd = -1;
const char * var;
// bgav_http_t * ret = NULL;
if(opt->dump_headers)
{
bgav_dprintf("Sending header\n");
gavl_dictionary_dump(request_header, 2);
if(extra_header)
gavl_dictionary_dump(extra_header, 2);
}
if(!ret)
{
ret = calloc(1, sizeof(*ret));
ret->opt = opt;
}
gavl_dictionary_reset(&ret->header);
if(ret->keepalive_host && strcmp(ret->keepalive_host, host))
{
gavf_io_destroy(ret->io);
ret->io = NULL;
free(ret->keepalive_host);
ret->keepalive_host = NULL;
}
if(!ret->io)
{
fd = bgav_tcp_connect(ret->opt, host, port);
if(fd == -1)
goto fail;
}
/* Wrap https */
if(use_tls)
ret->io = gavf_io_create_tls_client(fd, host, GAVF_IO_SOCKET_DO_CLOSE);
else
ret->io = gavf_io_create_socket_1(fd, ret->opt->read_timeout, GAVF_IO_SOCKET_DO_CLOSE);
gavl_dictionary_init(&header);
gavl_dictionary_copy(&header, request_header);
if(extra_header)
gavl_dictionary_merge2(&header, extra_header);
if(!gavl_http_request_write(ret->io, &header))
{
if(ret->keepalive_host) // Keepalive connection got closed by server
{
gavl_socket_close(fd);
gavf_io_destroy(ret->io);
ret->io = NULL;
fd = bgav_tcp_connect(ret->opt, host, port);
if(fd == -1)
goto fail;
if(use_tls)
ret->io = gavf_io_create_tls_client(fd, host, GAVF_IO_SOCKET_DO_CLOSE);
else
ret->io = gavf_io_create_socket_1(fd, ret->opt->read_timeout, GAVF_IO_SOCKET_DO_CLOSE);
if(!ret->io ||
!gavl_http_request_write(ret->io, &header))
goto fail;
}
else
goto fail;
}
if(!gavl_http_response_read(ret->io, &ret->header))
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Reading response failed: %s", strerror(errno));
goto fail;
}
if(opt->dump_headers)
{
bgav_dprintf("Got response\n");
gavl_dictionary_dump(&ret->header, 2);
}
if(ret->keepalive_host)
{
free(ret->keepalive_host);
ret->keepalive_host = NULL;
}
var = gavl_dictionary_get_string(&ret->header, "Connection");
if(var && !strcasecmp(var, "Keep-alive"))
ret->keepalive_host = gavl_strdup(host);
ret->content_length = 0;
var = gavl_dictionary_get_string(&ret->header, "Content-Length");
if(var)
ret->content_length = strtoll(var, NULL, 10);
var = gavl_dictionary_get_string(&ret->header, "Transfer-Encoding");
if(var && !strcasecmp(var, "chunked"))
{
ret->chunked = 1;
ret->content_length = 0;
}
return ret;
fail:
if(ret)
bgav_http_close(ret);
return NULL;
}
static char * encode_user_pass(const char * user, const char * pass)
{
int userpass_len;
int userpass_enc_len;
char * userpass;
char * ret;
userpass = bgav_sprintf("%s:%s", user, pass);
userpass_len = strlen(userpass);
userpass_enc_len = (userpass_len * 4)/3+4;
ret = malloc(userpass_enc_len);
userpass_enc_len = bgav_base64encode((uint8_t*)userpass,
userpass_len,
(uint8_t*)ret,
userpass_enc_len);
ret[userpass_enc_len] = '\0';
return ret;
}
static bgav_http_t * http_open(bgav_http_t * ret,
const char * url, const bgav_options_t * opt,
char ** redirect_url,
const gavl_dictionary_t * extra_header)
{
int port;
int status;
const char * location;
char * userpass_enc;
char * host = NULL;
char * path = NULL;
char * user = NULL;
char * pass = NULL;
char * protocol = NULL;
int default_port = 0;
const char * real_host;
int real_port;
gavl_dictionary_t request_header;
int use_tls = 0;
gavl_dictionary_init(&request_header);
port = -1;
if(!bgav_url_split(url,
&protocol,
&user, /* User */
&pass, /* Pass */
&host,
&port,
&path))
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Unvalid URL");
goto fail;
}
if(path && !strcmp(path, ";stream.nsv"))
{
free(path);
path = NULL;
}
if(!strcmp(protocol, "https"))
use_tls = 1;
if(port == -1)
{
if(use_tls)
port = 443;
else
port = 80;
default_port = 1;
}
/* Check for proxy */
if(opt->http_use_proxy)
{
real_host = opt->http_proxy_host;
real_port = opt->http_proxy_port;
}
else
{
real_host = host;
real_port = port;
}
/* Build request */
if(opt->http_use_proxy)
gavl_http_request_init(&request_header, "GET", url, "HTTP/1.1");
else
gavl_http_request_init(&request_header, "GET", ((path) ? path : "/"), "HTTP/1.1");
/* Proxy authentication */
if(opt->http_use_proxy && opt->http_proxy_auth)
{
userpass_enc = encode_user_pass(opt->http_proxy_user, opt->http_proxy_pass);
gavl_dictionary_set_string_nocopy(&request_header, "Proxy-Authorization",
gavl_sprintf("Basic %s", userpass_enc));
free(userpass_enc);
}
if(default_port)
gavl_dictionary_set_string(&request_header, "Host", host);
else
gavl_dictionary_set_string_nocopy(&request_header, "Host", bgav_sprintf("%s:%d", host, port));
ret = do_connect(ret, real_host, real_port, opt, &request_header, extra_header, use_tls);
if(!ret)
goto fail;
/* Check status code */
status = gavl_http_response_get_status_int(&ret->header);
if(status == 401)
{
/* Ok, they won't let us in, try to get a username and/or password */
bgav_http_close(ret);
ret = NULL;
if((!user || !pass) && opt->user_pass_callback)
{
if(user) { free(user); user = NULL; }
if(pass) { free(pass); pass = NULL; }
if(!opt->user_pass_callback(opt->user_pass_callback_data,
host, &user, &pass))
goto fail;
}
if(!user || !pass)
goto fail;
/* Now, user and pass should be the authentication data */
userpass_enc = encode_user_pass(user, pass);
gavl_dictionary_set_string_nocopy(&request_header, "Authorization",
gavl_sprintf("Basic %s", userpass_enc));
free(userpass_enc);
ret = do_connect(ret, real_host, real_port, opt, &request_header, extra_header, use_tls);
if(!ret)
goto fail;
/* Check status code */
status = gavl_http_response_get_status_int(&ret->header);
}
if(status >= 400) /* Error */
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "%d %s",
gavl_http_response_get_status_int(&ret->header),
gavl_http_response_get_status_str(&ret->header));
goto fail;
}
else if(status >= 300) /* Redirection */
{
/* Extract redirect URL */
if(*redirect_url)
{
free(*redirect_url);
*redirect_url = NULL;
}
location = gavl_dictionary_get_string(&ret->header, "Location");
if(location)
*redirect_url = gavl_strdup(location);
else
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
"Got redirection but no URL");
}
if(host)
free(host);
if(path)
free(path);
if(protocol)
free(protocol);
gavl_dictionary_free(&request_header);
bgav_http_close(ret);
return NULL;
}
else if(status < 200) /* Error */
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "%d %s",
gavl_http_response_get_status_int(&ret->header),
gavl_http_response_get_status_str(&ret->header));
goto fail;
}
gavl_dictionary_free(&request_header);
if(host)
free(host);
if(path)
free(path);
if(protocol)
free(protocol);
return ret;
fail:
if(*redirect_url)
{
free(*redirect_url);
*redirect_url = NULL;
}
if(host)
free(host);
if(path)
free(path);
if(protocol)
free(protocol);
gavl_dictionary_free(&request_header);
if(ret)
{
gavl_dictionary_free(&ret->header);
free(ret);
}
return NULL;
}
#define MAX_REDIRECTIONS 5
bgav_http_t * bgav_http_reopen(bgav_http_t * ret,
const char * url_orig, const bgav_options_t * opt,
char ** redirect_url,
const gavl_dictionary_t * extra_header)
{
int i;
char * url = NULL;
char * r = NULL;
if(redirect_url)
return http_open(ret, url_orig, opt, redirect_url, extra_header);
url = gavl_strdup(url_orig);
for(i = 0; i < MAX_REDIRECTIONS; i++)
{
ret = http_open(ret, url, opt, &r, extra_header);
if(ret)
break;
if(r)
{
if(url)
free(url);
url = r;
r = NULL;
}
}
if(url)
free(url);
if(r)
free(r);
return ret;
}
bgav_http_t * bgav_http_open(const char * url, const bgav_options_t * opt,
char ** redirect_url,
const gavl_dictionary_t * extra_header)
{
return bgav_http_reopen(NULL, url, opt, redirect_url, extra_header);
}
void bgav_http_close(bgav_http_t * h)
{
if(h->io)
gavf_io_destroy(h->io);
gavl_dictionary_free(&h->header);
if(h->keepalive_host)
free(h->keepalive_host);
free(h);
}
gavl_dictionary_t * bgav_http_get_header(bgav_http_t * h)
{
return &h->header;
}
static int next_chunk(bgav_http_t * h)
{
uint8_t c;
char buf[16];
int buf_len = 0;
// fprintf(stderr, "Next chunk\n");
if(h->chunk_size)
{
/* Read "\r\n" from previous chunk.
This is always done in blocking mode since we
don't expect to wait too long */
if((gavf_io_read_data(h->io, (uint8_t*)buf, 2) < 2) ||
(buf[0] != '\r') ||
(buf[1] != '\n'))
{
h->chunk_error = 1;
return 0;
}
h->chunk_size = 0;
}
/* Read first character of the chunk length */
if(gavf_io_read_data(h->io, &c, 1) < 1)
{
h->chunk_error = 1;
return 0;
}
/* Read chunk len */
buf[0] = c;
buf_len = 1;
/* Allow max 4 GB chunks */
while(buf_len < 8)
{
if(gavf_io_read_data(h->io, &c, 1) < 1)
{
h->chunk_error = 1;
return 0;
}
if(c == '\n')
break;
if(c != '\r')
{
buf[buf_len] = c;
buf_len++;
}
}
if(c != '\n')
{
h->chunk_error = 1;
return 0;
}
buf[buf_len] = '\0';
h->chunk_size = strtoul(buf, NULL, 16);
h->chunk_pos = 0;
if(!h->chunk_size)
h->chunk_eof = 1;
// fprintf(stderr, "Chunk size: %d\n", h->chunk_size);
return 1;
}
static int read_chunked(bgav_http_t * h, uint8_t * data, int len)
{
int bytes_to_read;
int bytes_read = 0;
int result;
if(h->chunk_error || h->chunk_eof)
return 0;
while(bytes_read < len)
{
if(h->chunk_pos >= h->chunk_size)
{
if(!next_chunk(h))
return bytes_read;
}
bytes_to_read = len - bytes_read;
if(bytes_to_read > h->chunk_size - h->chunk_pos)
bytes_to_read = h->chunk_size - h->chunk_pos;
result = gavf_io_read_data(h->io, data + bytes_read, bytes_to_read);
if(result <= 0)
{
h->chunk_error = 1;
return bytes_read;
}
bytes_read += result;
h->chunk_pos += result;
}
return bytes_read;
}
static int read_normal(bgav_http_t * h, uint8_t * data, int len)
{
return gavf_io_read_data(h->io, data, len);
}
int bgav_http_read(bgav_http_t * h, uint8_t * data, int len)
{
if(h->chunked)
return read_chunked(h, data, len);
else
return read_normal(h, data, len);
}
static char const * const title_vars[] =
{
"icy-name",
"ice-name",
"x-audiocast-name",
NULL
};
static char const * const genre_vars[] =
{
"x-audiocast-genre",
"icy-genre",
"ice-genre",
NULL
};
static char const * const comment_vars[] =
{
"ice-description",
"x-audiocast-description",
NULL
};
static char const * const url_vars[] =
{
"icy-url",
NULL
};
static void set_metadata_string(const gavl_dictionary_t * header,
char const * const vars[],
gavl_dictionary_t * m, const char * name)
{
const char * val;
int i = 0;
while(vars[i])
{
val = gavl_dictionary_get_string(header, vars[i]);
if(val)
{
gavl_dictionary_set_string(m, name, val);
return;
}
else
i++;
}
}
void bgav_http_set_metadata(bgav_http_t * h, gavl_dictionary_t * m)
{
int bitrate = 0;
const char * var;
gavl_dictionary_t * src;
/* Get content type */
if((src = gavl_dictionary_get_src_nc(m, GAVL_META_SRC, 0)))
{
var = gavl_dictionary_get_string_i(&h->header, "Content-Type");
if(var)
{
/* Special hack for radio-browser.info */
if(!strcasecmp(var, "application/octet-stream"))
{
if((var = gavl_dictionary_get_string_i(&h->header, "Content-Disposition")) &&
(gavl_string_starts_with(var, "attachment;")) &&
(var = strstr(var, "filename=")) &&
(var = strrchr(var, '.')))
{
if(!strcmp(var, ".pls"))
gavl_dictionary_set_string(src, GAVL_META_MIMETYPE, "audio/x-scpls");
else if(!strcmp(var, ".m3u"))
gavl_dictionary_set_string(src, GAVL_META_MIMETYPE, "audio/x-mpegurl");
}
}
else
gavl_dictionary_set_string(src, GAVL_META_MIMETYPE, var);
}
else if(gavl_dictionary_get_string_i(&h->header, "icy-notice1"))
gavl_dictionary_set_string(src, GAVL_META_MIMETYPE, "audio/mpeg");
}
/* Get Metadata */
set_metadata_string(&h->header,
title_vars, m, GAVL_META_STATION);
set_metadata_string(&h->header,
genre_vars, m, GAVL_META_GENRE);
set_metadata_string(&h->header,
comment_vars, m, GAVL_META_COMMENT);
set_metadata_string(&h->header,
url_vars, m, GAVL_META_RELURL);
/* Duration for lpcm streams from upnp servers */
if((var = gavl_dictionary_get_string(&h->header, "X-AvailableSeekRange")))
{
gavl_time_t start, end;
int len;
fprintf(stderr, "X-AvailableSeekRange: %s", var);
if((var = strstr(var, "ntp=")) &&
(var += 4) &&
(len = gavl_time_parse(var, &start)) &&
(var += len) &&
(*var == '-') &&
(var++) &&
gavl_time_parse(var, &end))
{
if(start == 0)
gavl_dictionary_set_long(m, GAVL_META_APPROX_DURATION, end);
}
}
if((var = gavl_dictionary_get_string(&h->header, "icy-br")))
bitrate = atoi(var);
else if((var = gavl_dictionary_get_string(&h->header, "ice-audio-info")))
{
var = strstr(var, "bitrate=");
if(var)
bitrate = atoi(var + 8);
}
if(bitrate)
gavl_dictionary_set_int(m, GAVL_META_BITRATE, bitrate * 1000);
}
int64_t bgav_http_total_bytes(bgav_http_t * h)
{
return h->content_length;
}
int bgav_http_can_seek(bgav_http_t * h)
{
return h->can_seek;
}
|