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
|
/* http.c - jonclegg@yahoo.com
* library routines for handling shoutcast centric http parsing
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "compat.h"
#include "http.h"
#include "util.h"
#include "debug.h"
/*********************************************************************************
* Private functions
*********************************************************************************/
static char* make_auth_header(const char *header_name,
const char *username, const char *password);
char* b64enc(const char *buf, int size);
/*
* Parse's a url as in http://host:port/path or host/path, etc..
* and now http://username:password@server:4480
*/
error_code httplib_parse_url(const char *url, URLINFO *urlinfo)
{
//
// see if we have a proto
//
char *s = strstr(url, "://");
int ret;
//
// if we have a proto, just skip it
// JCBUG -- should we care about the proto? like fail if it's not http?
//
if (s) url = s + strlen("://");
memcpy(urlinfo->path, (void *)"/\0", 2);
//
// search for a login '@' token
//
if (strchr(url, '@') != NULL)
{
ret = sscanf(url, "%[^:]:%[^@]", urlinfo->username, urlinfo->password);
if (ret < 2) return SR_ERROR_PARSE_FAILURE;
url = strchr(url, '@') + 1;
}
else
{
urlinfo->username[0] = '\0';
urlinfo->password[0] = '\0';
}
//
// search for a port seperator
//
if (strchr(url, ':') != NULL)
{
ret = sscanf(url, "%[^:]:%hu/%s", urlinfo->host,
(short unsigned int*)&urlinfo->port, urlinfo->path+1);
if (urlinfo->port < 1) return SR_ERROR_PARSE_FAILURE;
ret -= 1;
}
else
{
urlinfo->port = 80;
ret = sscanf(url, "%[^/]/%s", urlinfo->host, urlinfo->path+1);
}
if (ret < 1) return SR_ERROR_INVALID_URL;
return SR_SUCCESS;
}
error_code httplib_construct_sc_request(const char *url, const char* proxyurl, char *buffer, char *useragent)
{
int ret;
URLINFO ui;
URLINFO proxyui;
char myurl[MAX_URL_LEN];
if ((ret = httplib_parse_url(url, &ui)) != SR_SUCCESS)
return ret;
if (proxyurl)
{
sprintf(myurl, "http://%s:%d%s", ui.host, ui.port, ui.path);
if ((ret = httplib_parse_url(proxyurl, &proxyui)) != SR_SUCCESS)
return ret;
}
else
strcpy(myurl, ui.path);
snprintf(buffer, MAX_HEADER_LEN + MAX_HOST_LEN + SR_MAX_PATH,
"GET %s HTTP/1.0\r\n"
"Host: %s:%d\r\n"
"User-Agent: %s\r\n"
"Icy-MetaData:1\r\n",
myurl,
ui.host,
ui.port,
useragent[0] ? useragent : "Streamripper/1.x");
//
// http authentication (not proxy, see below for that)
//
if (ui.username[0] && ui.password[0])
{
char *authbuf = make_auth_header("Authorization",
ui.username,
ui.password);
strcat(buffer, authbuf);
free(authbuf);
}
//
// proxy auth stuff
//
if (proxyurl && proxyui.username[0] && proxyui.password[0])
{
char *authbuf = make_auth_header("Proxy-Authorization",
proxyui.username,
proxyui.password);
strcat(buffer, authbuf);
free(authbuf);
}
strcat(buffer, "\r\n");
return SR_SUCCESS;
}
// Make the 'Authorization: Basic xxxxxx\r\n' or 'Proxy-Authorization...'
// headers for the HTTP request.
static char* make_auth_header(const char *header_name,
const char *username, const char *password)
{
char *authbuf = malloc(strlen(header_name)
+ strlen(username)
+ strlen(password)
+ MAX_URI_STRING);
char *auth64;
sprintf(authbuf, "%s:%s", username, password);
auth64 = b64enc(authbuf, strlen(authbuf));
sprintf(authbuf, "%s: Basic %s\r\n", header_name, auth64);
free(auth64);
return authbuf;
}
// Here we pretend we're IE 5, hehe
error_code httplib_construct_page_request(const char *url, BOOL proxyformat, char *buffer)
{
int ret;
URLINFO ui;
char myurl[MAX_URL_LEN];
if ((ret = httplib_parse_url(url, &ui)) != SR_SUCCESS)
return ret;
if (proxyformat)
sprintf(myurl, "http://%s:%d%s", ui.host, ui.port, ui.path);
else
strcpy(myurl, ui.path);
snprintf(buffer, MAX_HEADER_LEN + MAX_HOST_LEN + SR_MAX_PATH,
"GET %s HTTP/1.0\r\n"
"Host: %s:%d\r\n"
"Accept: */*\r\n"
"Accept-Language: en-us\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n"
"Connection: Keep-Alive\r\n\r\n",
myurl,
ui.host,
ui.port);
return SR_SUCCESS;
}
/* Return 1 if a match was found, 0 if not found */
int
extract_header_value(char *header, char *dest, char *match)
{
char* start = (char *)strstr(header, match);
if (start) {
subnstr_until(start+strlen(match), "\n", dest, MAX_ICY_STRING);
return 1;
} else {
return 0;
}
}
error_code
httplib_parse_sc_header(const char *url, char *header, SR_HTTP_HEADER *info)
{
int rc;
char *start;
char versionbuf[64];
char stempbr[MAX_ICY_STRING];
URLINFO url_info;
int url_path_len;
int content_type_by_url;
if (!header || !info)
return SR_ERROR_INVALID_PARAM;
/* Parse the url here, before doing memset, because in the
recursive case, it is the location referrer */
rc = httplib_parse_url (url, &url_info);
if (rc != SR_SUCCESS) return rc;
memset(info, 0, sizeof(SR_HTTP_HEADER));
debug_printf("http header:\n%s\n", header);
// Get the ICY code.
start = (char *)strstr(header, "ICY ");
if (!start) {
start = (char *)strstr(header, "HTTP/1.");
if (!start) return SR_ERROR_NO_RESPOSE_HEADER;
}
start = strstr(start, " ") + 1;
sscanf(start, "%i", &info->icy_code);
if (info->icy_code >= 400) {
switch (info->icy_code)
{
case 400:
return SR_ERROR_HTTP_400_ERROR;
case 404:
return SR_ERROR_HTTP_404_ERROR;
case 401:
return SR_ERROR_HTTP_401_ERROR;
case 403:
return SR_ERROR_HTTP_403_ERROR;
case 407:
return SR_ERROR_HTTP_407_ERROR;
case 502:
return SR_ERROR_HTTP_502_ERROR;
default:
return SR_ERROR_NO_ICY_CODE;
}
}
// read generic headers
extract_header_value(header, info->http_location, "Location:");
extract_header_value(header, info->server, "Server:");
rc = extract_header_value(header, info->icy_name, "icy-name:");
if (rc == 0) {
/* Icecast 2.0.1 */
rc = extract_header_value(header, info->icy_name, "ice-name:");
}
info->have_icy_name = rc;
extract_header_value(header, info->icy_url, "icy-url:");
rc = extract_header_value(header, stempbr, "icy-br:");
if (rc) {
info->icy_bitrate = atoi(stempbr);
}
/* interpret the content type from http header */
rc = extract_header_value(header, stempbr, "Content-Type:");
if (rc == 0) {
rc = extract_header_value(header, stempbr, "content-type:");
}
if (rc == 0) {
info->content_type = CONTENT_TYPE_UNKNOWN;
}
else if (strstr(stempbr,"audio/mpeg")) {
info->content_type = CONTENT_TYPE_MP3;
}
else if (strstr(stempbr,"video/nsv")) {
info->content_type = CONTENT_TYPE_NSV;
}
else if (strstr(stempbr,"misc/ultravox")) {
info->content_type = CONTENT_TYPE_ULTRAVOX;
}
else if (strstr(stempbr,"application/ogg")) {
info->content_type = CONTENT_TYPE_OGG;
}
else if (strstr(stempbr,"audio/aac")) {
info->content_type = CONTENT_TYPE_AAC;
}
else {
info->content_type = CONTENT_TYPE_UNKNOWN;
}
/* Look at url for more content type hints */
url_path_len = strlen(url_info.path);
content_type_by_url = CONTENT_TYPE_UNKNOWN;
if (url_path_len >= 4) {
if (!strcmp (&url_info.path[url_path_len-4], ".aac")) {
content_type_by_url = CONTENT_TYPE_AAC;
} else if (!strcmp (&url_info.path[url_path_len-4], ".ogg")) {
content_type_by_url = CONTENT_TYPE_OGG;
} else if (!strcmp (&url_info.path[url_path_len-4], ".mp3")) {
content_type_by_url = CONTENT_TYPE_MP3;
} else if (!strcmp (&url_info.path[url_path_len-4], ".nsv")) {
content_type_by_url = CONTENT_TYPE_NSV;
}
}
//printf ("---------------\n");
//printf (header);
//printf ("---------------\n");
// Try to guess the server
// Check for Streamripper relay
if ((start = (char *)strstr(header, "[relay stream]")) != NULL) {
strcpy(info->server, "Streamripper relay server");
}
// Check for Shoutcast
else if ((start = (char *)strstr(header, "SHOUTcast")) != NULL) {
strcpy(info->server, "SHOUTcast/");
if ((start = (char *)strstr(start, "Server/")) != NULL) {
sscanf(start, "Server/%63[^<]<", versionbuf);
strcat(info->server, versionbuf);
}
}
// Check for Icecast 2
else if ((start = (char *)strstr(header, "Icecast 2")) != NULL) {
/* aac on icecast 2.0-2.1 declares content type of audio/mpeg */
if (info->content_type == CONTENT_TYPE_MP3 &&
content_type_by_url != CONTENT_TYPE_UNKNOWN) {
info->content_type = content_type_by_url;
}
}
// Check for Icecast 1
else if ((start = (char *)strstr(header, "icecast")) != NULL) {
if (!info->server[0]) {
strcpy(info->server, "icecast/");
if ((start = (char *)strstr(start, "version ")) != NULL) {
sscanf(start, "version %63[^<]<", versionbuf);
strcat(info->server, versionbuf);
}
}
// icecast 1.x headers.
extract_header_value(header, info->icy_url, "x-audiocast-server-url:");
rc = extract_header_value(header, info->icy_name, "x-audiocast-name:");
info->have_icy_name |= rc;
extract_header_value(header, info->icy_genre, "x-audiocast-genre:");
rc = extract_header_value(header, stempbr, "x-audiocast-bitrate:");
if (rc) {
info->icy_bitrate = atoi(stempbr);
}
}
// WTF is Zwitterion?
else if ((start = (char *)strstr(header, "Zwitterion v")) != NULL) {
sscanf(start, "%[^<]<", info->server);
}
/* Last chance to deduce content type */
if (info->content_type == CONTENT_TYPE_UNKNOWN) {
if (content_type_by_url == CONTENT_TYPE_UNKNOWN) {
info->content_type = CONTENT_TYPE_MP3;
} else {
info->content_type = content_type_by_url;
}
}
debug_printf ("Deduced content type: %d\n", info->content_type);
// Make sure we don't have any CRLF's at the end of our strings
trim(info->icy_url);
trim(info->icy_genre);
trim(info->icy_name);
trim(info->http_location);
trim(info->server);
//get the meta interval
start = (char*)strstr(header, "icy-metaint:");
if (start) {
sscanf(start, "icy-metaint:%i", &info->meta_interval);
if (info->meta_interval < 1) {
info->meta_interval = NO_META_INTERVAL;
#if defined (commentout)
/* GCS: I don't think we want an error here. */
return SR_ERROR_NO_META_INTERVAL;
#endif
}
} else {
info->meta_interval = NO_META_INTERVAL;
}
return SR_SUCCESS;
}
/*
* Constructs a HTTP response header from the SR_HTTP_HEADER struct, if data is null it is not
* added to the header
*/
error_code
httplib_construct_sc_response(SR_HTTP_HEADER *info, char *header, int size, int icy_meta_support)
{
char *buf = (char *)malloc(size);
char* test_header =
"HTTP/1.0 200 OK\r\n"
"Content-Type: application/ogg\r\n"
"ice-audio-info: ice-samplerate=44100\r\n"
"ice-bitrate: Quality 0\r\n"
"ice-description: This is my server desription\r\n"
"ice-genre: Rock\r\n"
"ice-name: This is my server desription\r\n"
"ice-private: 0\r\n"
"ice-public: 1\r\n"
"ice-url: http://www.oddsock.org\r\n"
"Server: Icecast 2.0.1\r\n\r\n";
if (!info || !header || size < 1)
return SR_ERROR_INVALID_PARAM;
memset(header, 0, size);
#if defined (commentout)
/* GCS - try this */
strcpy (header, test_header);
return SR_SUCCESS;
#endif
/* sprintf(buf, "HTTP/1.0 %d\r\n", info->icy_code); */
sprintf(buf, "HTTP/1.0 200 OK\r\n");
strcat(header, buf);
if (info->http_location[0])
{
sprintf(buf, "Location:%s\r\n", info->http_location);
strcat(header, buf);
}
if (info->server[0])
{
sprintf(buf, "Server:%s\r\n", info->server);
strcat(header, buf);
}
#if defined (commentout)
if (info->icy_name[0])
{
sprintf(buf, "icy-name:%s\r\n", info->icy_name);
strcat(header, buf);
}
#endif
if (info->have_icy_name) {
sprintf(buf, "icy-name:%s\r\n", info->icy_name);
strcat(header, buf);
}
if (info->icy_url[0])
{
sprintf(buf, "icy-url:%s\r\n", info->icy_url);
strcat(header, buf);
}
if (info->icy_bitrate)
{
sprintf(buf, "icy-br:%d\r\n", info->icy_bitrate);
strcat(header, buf);
}
if (info->icy_genre[0])
{
sprintf(buf, "icy-genre:%s\r\n", info->icy_genre);
strcat(header, buf);
}
if ((info->meta_interval > 0) && icy_meta_support)
{
sprintf(buf, "icy-metaint:%d\r\n", info->meta_interval);
strcat(header, buf);
}
switch (info->content_type) {
case CONTENT_TYPE_MP3:
sprintf (buf, "Content-Type: audio/mpeg\r\n");
strcat(header, buf);
break;
case CONTENT_TYPE_NSV:
sprintf (buf, "Content-Type: video/nsv\r\n");
strcat(header, buf);
break;
case CONTENT_TYPE_OGG:
sprintf (buf, "Content-Type: application/ogg\r\n");
strcat(header, buf);
break;
case CONTENT_TYPE_ULTRAVOX:
sprintf (buf, "Content-Type: misc/ultravox\r\n");
strcat(header, buf);
break;
case CONTENT_TYPE_AAC:
sprintf (buf, "Content-Type: audio/aac\r\n");
strcat(header, buf);
break;
}
free(buf);
strcat(header, "\r\n");
debug_printf ("Constructed response header:\n");
debug_printf ("%s", header);
return SR_SUCCESS;
}
//
// taken from:
// Copyright (c) 2000 Virtual Unlimited B.V.
// Author: Bob Deblier <bob@virtualunlimited.com>
// thanks bob ;)
//
#define CHARS_PER_LINE 72
char* b64enc(const char *inbuf, int size)
{
static const char* to_b64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/* encode 72 characters per line */
int div = size / 3;
int rem = size % 3;
int chars = div*4 + rem + 1;
int newlines = (chars + CHARS_PER_LINE - 1) / CHARS_PER_LINE;
const char* data = inbuf;
char* string = (char*) malloc(chars + newlines + 1 + 100);
if (string)
{
register char* buf = string;
chars = 0;
/*@+charindex@*/
while (div > 0)
{
buf[0] = to_b64[ (data[0] >> 2) & 0x3f];
buf[1] = to_b64[((data[0] << 4) & 0x30) + ((data[1] >> 4) & 0xf)];
buf[2] = to_b64[((data[1] << 2) & 0x3c) + ((data[2] >> 6) & 0x3)];
buf[3] = to_b64[ data[2] & 0x3f];
data += 3;
buf += 4;
div--;
chars += 4;
if (chars == CHARS_PER_LINE)
{
chars = 0;
*(buf++) = '\n';
}
}
switch (rem)
{
case 2:
buf[0] = to_b64[ (data[0] >> 2) & 0x3f];
buf[1] = to_b64[((data[0] << 4) & 0x30) + ((data[1] >> 4) & 0xf)];
buf[2] = to_b64[ (data[1] << 2) & 0x3c];
buf[3] = '=';
buf += 4;
chars += 4;
break;
case 1:
buf[0] = to_b64[ (data[0] >> 2) & 0x3f];
buf[1] = to_b64[ (data[0] << 4) & 0x30];
buf[2] = '=';
buf[3] = '=';
buf += 4;
chars += 4;
break;
}
/* *(buf++) = '\n'; This would result in a buffer overrun */
*buf = '\0';
}
return string;
}
|