2009-05-13 02:58:57 +00:00
|
|
|
/*
|
2001-05-29 16:58:52 +00:00
|
|
|
* HTTP Helper
|
2010-01-30 22:26:47 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2001 Bertrand Baudet <bertrand_baudet@yahoo.com>
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer 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.
|
|
|
|
*
|
|
|
|
* MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2001-05-29 16:58:52 +00:00
|
|
|
*/
|
|
|
|
|
2005-06-02 18:57:58 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-05-25 13:58:32 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-05-29 12:54:00 +00:00
|
|
|
#include <unistd.h>
|
2001-05-25 13:58:32 +00:00
|
|
|
|
2009-02-01 13:42:27 +00:00
|
|
|
#if !HAVE_WINSOCK2_H
|
2008-08-31 13:59:54 +00:00
|
|
|
#else
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "http.h"
|
2002-02-23 07:31:45 +00:00
|
|
|
#include "url.h"
|
2002-04-30 16:58:54 +00:00
|
|
|
#include "mp_msg.h"
|
2001-05-25 13:58:32 +00:00
|
|
|
|
2005-05-29 12:54:00 +00:00
|
|
|
#include "stream.h"
|
2006-08-04 17:01:29 +00:00
|
|
|
#include "libmpdemux/demuxer.h"
|
2005-05-29 12:54:00 +00:00
|
|
|
#include "network.h"
|
|
|
|
#include "help_mp.h"
|
|
|
|
|
|
|
|
|
2007-12-02 22:12:50 +00:00
|
|
|
extern const mime_struct_t mime_type_table[];
|
2005-05-29 12:54:00 +00:00
|
|
|
extern int stream_cache_size;
|
|
|
|
extern int network_bandwidth;
|
|
|
|
|
2008-12-03 23:01:03 +00:00
|
|
|
int http_seek(stream_t *stream, off_t pos);
|
2005-05-29 12:54:00 +00:00
|
|
|
|
2005-07-19 12:07:04 +00:00
|
|
|
typedef struct {
|
|
|
|
unsigned metaint;
|
|
|
|
unsigned metapos;
|
|
|
|
int is_ultravox;
|
|
|
|
} scast_data_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief first read any data from sc->buffer then from fd
|
|
|
|
* \param fd file descriptor to read data from
|
|
|
|
* \param buffer buffer to read into
|
|
|
|
* \param len how many bytes to read
|
|
|
|
* \param sc streaming control containing buffer to read from first
|
|
|
|
* \return len unless there is a read error or eof
|
|
|
|
*/
|
|
|
|
static unsigned my_read(int fd, char *buffer, int len, streaming_ctrl_t *sc) {
|
|
|
|
unsigned pos = 0;
|
|
|
|
unsigned cp_len = sc->buffer_size - sc->buffer_pos;
|
|
|
|
if (cp_len > len)
|
|
|
|
cp_len = len;
|
|
|
|
memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len);
|
|
|
|
sc->buffer_pos += cp_len;
|
|
|
|
pos += cp_len;
|
|
|
|
while (pos < len) {
|
2005-07-23 15:29:51 +00:00
|
|
|
int ret = recv(fd, &buffer[pos], len - pos, 0);
|
2005-07-19 12:07:04 +00:00
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
pos += ret;
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2005-07-20 17:14:45 +00:00
|
|
|
/**
|
|
|
|
* \brief read and process (i.e. discard *g*) a block of ultravox metadata
|
|
|
|
* \param fd file descriptor to read from
|
|
|
|
* \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
|
|
|
|
* \return number of real data before next metadata block starts or 0 on error
|
|
|
|
*/
|
2005-07-19 12:07:04 +00:00
|
|
|
static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) {
|
|
|
|
unsigned metaint;
|
2005-07-23 15:29:51 +00:00
|
|
|
unsigned char info[6] = {0, 0, 0, 0, 0, 0};
|
|
|
|
int info_read;
|
2005-07-19 12:07:04 +00:00
|
|
|
do {
|
2005-07-23 15:29:51 +00:00
|
|
|
info_read = my_read(fd, info, 1, sc);
|
2005-07-19 12:07:04 +00:00
|
|
|
if (info[0] == 0x00)
|
2005-07-23 15:29:51 +00:00
|
|
|
info_read = my_read(fd, info, 6, sc);
|
2005-07-19 12:07:04 +00:00
|
|
|
else
|
2005-07-23 15:29:51 +00:00
|
|
|
info_read += my_read(fd, &info[1], 5, sc);
|
|
|
|
if (info_read != 6) // read error or eof
|
|
|
|
return 0;
|
2005-07-20 17:12:33 +00:00
|
|
|
// sync byte and reserved flags
|
|
|
|
if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) {
|
2005-07-19 12:07:04 +00:00
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2005-07-20 17:12:33 +00:00
|
|
|
if (info[1] & 0x01)
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n");
|
2005-07-19 12:07:04 +00:00
|
|
|
metaint = info[4] << 8 | info[5];
|
2005-07-20 17:12:33 +00:00
|
|
|
if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense
|
2005-07-19 12:07:04 +00:00
|
|
|
char *metabuf = malloc(metaint);
|
|
|
|
my_read(fd, metabuf, metaint, sc);
|
|
|
|
free(metabuf);
|
|
|
|
}
|
2005-07-20 17:12:33 +00:00
|
|
|
} while ((info[3] & 0xf) < 0x07);
|
2005-07-19 12:07:04 +00:00
|
|
|
return metaint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief read one scast meta data entry and print it
|
2005-07-20 17:14:45 +00:00
|
|
|
* \param fd file descriptor to read from
|
|
|
|
* \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
|
2005-07-19 12:07:04 +00:00
|
|
|
*/
|
|
|
|
static void scast_meta_read(int fd, streaming_ctrl_t *sc) {
|
|
|
|
unsigned char tmp = 0;
|
|
|
|
unsigned metalen;
|
|
|
|
my_read(fd, &tmp, 1, sc);
|
|
|
|
metalen = tmp * 16;
|
|
|
|
if (metalen > 0) {
|
2006-07-02 08:17:07 +00:00
|
|
|
char *info = malloc(metalen + 1);
|
2005-07-19 12:07:04 +00:00
|
|
|
unsigned nlen = my_read(fd, info, metalen, sc);
|
|
|
|
info[nlen] = 0;
|
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info);
|
|
|
|
free(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-20 17:14:45 +00:00
|
|
|
/**
|
|
|
|
* \brief read data from scast/ultravox stream without any metadata
|
|
|
|
* \param fd file descriptor to read from
|
|
|
|
* \param buffer buffer to read data into
|
|
|
|
* \param size number of bytes to read
|
|
|
|
* \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
|
|
|
|
*/
|
2005-07-19 12:07:04 +00:00
|
|
|
static int scast_streaming_read(int fd, char *buffer, int size,
|
|
|
|
streaming_ctrl_t *sc) {
|
|
|
|
scast_data_t *sd = (scast_data_t *)sc->data;
|
|
|
|
unsigned block, ret;
|
|
|
|
unsigned done = 0;
|
|
|
|
|
|
|
|
// first read remaining data up to next metadata
|
|
|
|
block = sd->metaint - sd->metapos;
|
|
|
|
if (block > size)
|
|
|
|
block = size;
|
|
|
|
ret = my_read(fd, buffer, block, sc);
|
|
|
|
sd->metapos += ret;
|
|
|
|
done += ret;
|
|
|
|
if (ret != block) // read problems or eof
|
|
|
|
size = done;
|
|
|
|
|
|
|
|
while (done < size) { // now comes the metadata
|
|
|
|
if (sd->is_ultravox)
|
2005-07-23 15:29:51 +00:00
|
|
|
{
|
2005-07-19 12:07:04 +00:00
|
|
|
sd->metaint = uvox_meta_read(fd, sc);
|
2005-07-23 15:29:51 +00:00
|
|
|
if (!sd->metaint)
|
|
|
|
size = done;
|
|
|
|
}
|
2005-07-19 12:07:04 +00:00
|
|
|
else
|
|
|
|
scast_meta_read(fd, sc); // read and display metadata
|
|
|
|
sd->metapos = 0;
|
|
|
|
block = size - done;
|
|
|
|
if (block > sd->metaint)
|
|
|
|
block = sd->metaint;
|
|
|
|
ret = my_read(fd, &buffer[done], block, sc);
|
|
|
|
sd->metapos += ret;
|
|
|
|
done += ret;
|
|
|
|
if (ret != block) // read problems or eof
|
|
|
|
size = done;
|
|
|
|
}
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int scast_streaming_start(stream_t *stream) {
|
|
|
|
int metaint;
|
|
|
|
scast_data_t *scast_data;
|
|
|
|
HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
|
2005-07-23 15:29:51 +00:00
|
|
|
int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
|
2005-07-19 12:07:04 +00:00
|
|
|
if (!stream || stream->fd < 0 || !http_hdr)
|
|
|
|
return -1;
|
|
|
|
if (is_ultravox)
|
|
|
|
metaint = 0;
|
|
|
|
else {
|
|
|
|
metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt"));
|
|
|
|
if (metaint <= 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
stream->streaming_ctrl->buffer = malloc(http_hdr->body_size);
|
|
|
|
stream->streaming_ctrl->buffer_size = http_hdr->body_size;
|
|
|
|
stream->streaming_ctrl->buffer_pos = 0;
|
|
|
|
memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size);
|
|
|
|
scast_data = malloc(sizeof(scast_data_t));
|
|
|
|
scast_data->metaint = metaint;
|
|
|
|
scast_data->metapos = 0;
|
|
|
|
scast_data->is_ultravox = is_ultravox;
|
|
|
|
http_free(http_hdr);
|
|
|
|
stream->streaming_ctrl->data = scast_data;
|
|
|
|
stream->streaming_ctrl->streaming_read = scast_streaming_read;
|
|
|
|
stream->streaming_ctrl->streaming_seek = NULL;
|
|
|
|
stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes
|
|
|
|
stream->streaming_ctrl->buffering = 1;
|
|
|
|
stream->streaming_ctrl->status = streaming_playing_e;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-29 12:54:00 +00:00
|
|
|
static int nop_streaming_start( stream_t *stream ) {
|
|
|
|
HTTP_header_t *http_hdr = NULL;
|
|
|
|
char *next_url=NULL;
|
|
|
|
URL_t *rd_url=NULL;
|
|
|
|
int fd,ret;
|
|
|
|
if( stream==NULL ) return -1;
|
|
|
|
|
|
|
|
fd = stream->fd;
|
|
|
|
if( fd<0 ) {
|
2009-05-13 02:58:57 +00:00
|
|
|
fd = http_send_request( stream->streaming_ctrl->url, 0 );
|
2005-05-29 12:54:00 +00:00
|
|
|
if( fd<0 ) return -1;
|
|
|
|
http_hdr = http_read_response( fd );
|
|
|
|
if( http_hdr==NULL ) return -1;
|
|
|
|
|
|
|
|
switch( http_hdr->status_code ) {
|
|
|
|
case 200: // OK
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
|
|
|
|
if( http_hdr->body_size>0 ) {
|
|
|
|
if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
|
|
|
|
http_free( http_hdr );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Redirect
|
|
|
|
case 301: // Permanently
|
|
|
|
case 302: // Temporarily
|
2006-08-20 11:47:33 +00:00
|
|
|
case 303: // See Other
|
2005-05-29 12:54:00 +00:00
|
|
|
ret=-1;
|
|
|
|
next_url = http_get_field( http_hdr, "Location" );
|
|
|
|
|
|
|
|
if (next_url != NULL)
|
|
|
|
rd_url=url_new(next_url);
|
|
|
|
|
|
|
|
if (next_url != NULL && rd_url != NULL) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url);
|
|
|
|
stream->streaming_ctrl->url=check4proxies(rd_url);
|
2009-05-13 02:58:57 +00:00
|
|
|
ret=nop_streaming_start(stream); //recursively get streaming started
|
2005-05-29 12:54:00 +00:00
|
|
|
} else {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n");
|
|
|
|
closesocket( fd );
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
break;
|
|
|
|
case 401: //Authorization required
|
|
|
|
case 403: //Forbidden
|
|
|
|
case 404: //Not found
|
|
|
|
case 500: //Server Error
|
|
|
|
default:
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
|
|
|
|
closesocket( fd );
|
|
|
|
fd = -1;
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stream->fd = fd;
|
|
|
|
} else {
|
|
|
|
http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data;
|
|
|
|
if( http_hdr->body_size>0 ) {
|
|
|
|
if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
|
|
|
|
http_free( http_hdr );
|
|
|
|
stream->streaming_ctrl->data = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( http_hdr ) {
|
|
|
|
http_free( http_hdr );
|
|
|
|
stream->streaming_ctrl->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->streaming_ctrl->streaming_read = nop_streaming_read;
|
|
|
|
stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
|
|
|
|
stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes
|
|
|
|
stream->streaming_ctrl->buffering = 1;
|
|
|
|
stream->streaming_ctrl->status = streaming_playing_e;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-05-25 13:58:32 +00:00
|
|
|
HTTP_header_t *
|
2006-02-09 14:08:03 +00:00
|
|
|
http_new_header(void) {
|
2001-05-25 13:58:32 +00:00
|
|
|
HTTP_header_t *http_hdr;
|
|
|
|
|
2006-07-02 08:17:07 +00:00
|
|
|
http_hdr = malloc(sizeof(HTTP_header_t));
|
2001-05-25 13:58:32 +00:00
|
|
|
if( http_hdr==NULL ) return NULL;
|
|
|
|
memset( http_hdr, 0, sizeof(HTTP_header_t) );
|
|
|
|
|
|
|
|
return http_hdr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
http_free( HTTP_header_t *http_hdr ) {
|
2001-11-20 22:14:16 +00:00
|
|
|
HTTP_field_t *field, *field2free;
|
2001-05-25 13:58:32 +00:00
|
|
|
if( http_hdr==NULL ) return;
|
|
|
|
if( http_hdr->protocol!=NULL ) free( http_hdr->protocol );
|
|
|
|
if( http_hdr->uri!=NULL ) free( http_hdr->uri );
|
|
|
|
if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase );
|
|
|
|
if( http_hdr->field_search!=NULL ) free( http_hdr->field_search );
|
2001-05-29 16:58:52 +00:00
|
|
|
if( http_hdr->method!=NULL ) free( http_hdr->method );
|
|
|
|
if( http_hdr->buffer!=NULL ) free( http_hdr->buffer );
|
2001-11-20 22:14:16 +00:00
|
|
|
field = http_hdr->first_field;
|
|
|
|
while( field!=NULL ) {
|
|
|
|
field2free = field;
|
2005-01-11 17:49:59 +00:00
|
|
|
if (field->field_name)
|
|
|
|
free(field->field_name);
|
2001-11-20 22:14:16 +00:00
|
|
|
field = field->next;
|
|
|
|
free( field2free );
|
|
|
|
}
|
2001-05-25 13:58:32 +00:00
|
|
|
free( http_hdr );
|
2001-11-20 22:14:16 +00:00
|
|
|
http_hdr = NULL;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
|
|
|
|
2001-05-29 16:58:52 +00:00
|
|
|
int
|
|
|
|
http_response_append( HTTP_header_t *http_hdr, char *response, int length ) {
|
2001-06-05 08:46:31 +00:00
|
|
|
if( http_hdr==NULL || response==NULL || length<0 ) return -1;
|
2002-09-07 08:15:57 +00:00
|
|
|
|
2006-06-04 22:41:27 +00:00
|
|
|
if( (unsigned)length > SIZE_MAX - http_hdr->buffer_size - 1) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Bad size in memory (re)allocation\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2002-09-06 00:03:16 +00:00
|
|
|
http_hdr->buffer = (char*)realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 );
|
2002-09-07 08:15:57 +00:00
|
|
|
if( http_hdr->buffer==NULL ) {
|
2002-09-06 00:03:16 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n");
|
2001-05-29 16:58:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-09-06 00:03:16 +00:00
|
|
|
memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length );
|
|
|
|
http_hdr->buffer_size += length;
|
|
|
|
http_hdr->buffer[http_hdr->buffer_size]=0; // close the string!
|
2001-05-29 16:58:52 +00:00
|
|
|
return http_hdr->buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-10-26 18:26:06 +00:00
|
|
|
http_is_header_entire( HTTP_header_t *http_hdr ) {
|
2001-05-29 16:58:52 +00:00
|
|
|
if( http_hdr==NULL ) return -1;
|
2002-09-06 00:03:16 +00:00
|
|
|
if( http_hdr->buffer==NULL ) return 0; // empty
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-12-27 01:53:25 +00:00
|
|
|
if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL &&
|
|
|
|
strstr(http_hdr->buffer, "\n\n")==NULL ) return 0;
|
|
|
|
return 1;
|
2001-05-29 16:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
http_response_parse( HTTP_header_t *http_hdr ) {
|
2001-05-25 13:58:32 +00:00
|
|
|
char *hdr_ptr, *ptr;
|
|
|
|
char *field=NULL;
|
2006-06-04 22:41:27 +00:00
|
|
|
int pos_hdr_sep, hdr_sep_len;
|
|
|
|
size_t len;
|
2001-05-29 16:58:52 +00:00
|
|
|
if( http_hdr==NULL ) return -1;
|
|
|
|
if( http_hdr->is_parsed ) return 0;
|
2001-05-25 13:58:32 +00:00
|
|
|
|
|
|
|
// Get the protocol
|
2001-05-29 16:58:52 +00:00
|
|
|
hdr_ptr = strstr( http_hdr->buffer, " " );
|
2001-05-25 13:58:32 +00:00
|
|
|
if( hdr_ptr==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n");
|
2001-05-29 16:58:52 +00:00
|
|
|
return -1;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
2001-05-29 16:58:52 +00:00
|
|
|
len = hdr_ptr-http_hdr->buffer;
|
2006-07-02 08:17:07 +00:00
|
|
|
http_hdr->protocol = malloc(len+1);
|
2001-05-25 13:58:32 +00:00
|
|
|
if( http_hdr->protocol==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
|
2001-05-29 16:58:52 +00:00
|
|
|
return -1;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
2001-05-29 16:58:52 +00:00
|
|
|
strncpy( http_hdr->protocol, http_hdr->buffer, len );
|
|
|
|
http_hdr->protocol[len]='\0';
|
2001-05-25 13:58:32 +00:00
|
|
|
if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) {
|
|
|
|
if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n");
|
2001-05-29 16:58:52 +00:00
|
|
|
return -1;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the status code
|
|
|
|
if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n");
|
2001-05-29 16:58:52 +00:00
|
|
|
return -1;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
|
|
|
hdr_ptr += 4;
|
|
|
|
|
|
|
|
// Get the reason phrase
|
2001-12-16 07:40:52 +00:00
|
|
|
ptr = strstr( hdr_ptr, "\n" );
|
2001-05-25 13:58:32 +00:00
|
|
|
if( hdr_ptr==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n");
|
2001-05-29 16:58:52 +00:00
|
|
|
return -1;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
|
|
|
len = ptr-hdr_ptr;
|
2006-07-02 08:17:07 +00:00
|
|
|
http_hdr->reason_phrase = malloc(len+1);
|
2001-05-25 13:58:32 +00:00
|
|
|
if( http_hdr->reason_phrase==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
|
2001-05-29 16:58:52 +00:00
|
|
|
return -1;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
|
|
|
strncpy( http_hdr->reason_phrase, hdr_ptr, len );
|
2002-01-23 08:46:22 +00:00
|
|
|
if( http_hdr->reason_phrase[len-1]=='\r' ) {
|
|
|
|
len--;
|
|
|
|
}
|
2001-05-25 13:58:32 +00:00
|
|
|
http_hdr->reason_phrase[len]='\0';
|
|
|
|
|
|
|
|
// Set the position of the header separator: \r\n\r\n
|
2002-11-13 09:02:55 +00:00
|
|
|
hdr_sep_len = 4;
|
2001-05-29 16:58:52 +00:00
|
|
|
ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
|
2001-05-25 13:58:32 +00:00
|
|
|
if( ptr==NULL ) {
|
2001-12-16 07:40:52 +00:00
|
|
|
ptr = strstr( http_hdr->buffer, "\n\n" );
|
|
|
|
if( ptr==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n");
|
2001-12-16 07:40:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-11-13 09:02:55 +00:00
|
|
|
hdr_sep_len = 2;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
2001-05-29 16:58:52 +00:00
|
|
|
pos_hdr_sep = ptr-http_hdr->buffer;
|
2001-05-25 13:58:32 +00:00
|
|
|
|
2001-12-16 07:40:52 +00:00
|
|
|
// Point to the first line after the method line.
|
|
|
|
hdr_ptr = strstr( http_hdr->buffer, "\n" )+1;
|
2001-05-25 13:58:32 +00:00
|
|
|
do {
|
2001-12-16 07:40:52 +00:00
|
|
|
ptr = hdr_ptr;
|
|
|
|
while( *ptr!='\r' && *ptr!='\n' ) ptr++;
|
2001-05-25 13:58:32 +00:00
|
|
|
len = ptr-hdr_ptr;
|
2002-02-23 07:31:45 +00:00
|
|
|
if( len==0 ) break;
|
2001-05-25 13:58:32 +00:00
|
|
|
field = (char*)realloc(field, len+1);
|
|
|
|
if( field==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
|
2001-05-29 16:58:52 +00:00
|
|
|
return -1;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
|
|
|
strncpy( field, hdr_ptr, len );
|
|
|
|
field[len]='\0';
|
|
|
|
http_set_field( http_hdr, field );
|
2001-12-16 07:40:52 +00:00
|
|
|
hdr_ptr = ptr+((*ptr=='\r')?2:1);
|
2001-05-29 16:58:52 +00:00
|
|
|
} while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) );
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-05-25 13:58:32 +00:00
|
|
|
if( field!=NULL ) free( field );
|
|
|
|
|
2002-11-13 09:02:55 +00:00
|
|
|
if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) {
|
2001-05-25 13:58:32 +00:00
|
|
|
// Response has data!
|
2002-11-13 09:02:55 +00:00
|
|
|
http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len;
|
|
|
|
http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len);
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
|
|
|
|
2001-05-29 16:58:52 +00:00
|
|
|
http_hdr->is_parsed = 1;
|
|
|
|
return 0;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2001-05-29 16:58:52 +00:00
|
|
|
http_build_request( HTTP_header_t *http_hdr ) {
|
2001-12-14 23:50:57 +00:00
|
|
|
char *ptr, *uri=NULL;
|
2001-05-29 16:58:52 +00:00
|
|
|
int len;
|
2001-11-20 22:14:16 +00:00
|
|
|
HTTP_field_t *field;
|
2001-05-25 13:58:32 +00:00
|
|
|
if( http_hdr==NULL ) return NULL;
|
|
|
|
|
|
|
|
if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
|
|
|
|
if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/");
|
2001-12-14 23:50:57 +00:00
|
|
|
else {
|
2006-07-02 08:17:07 +00:00
|
|
|
uri = malloc(strlen(http_hdr->uri) + 1);
|
2001-12-14 23:50:57 +00:00
|
|
|
if( uri==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
|
2001-12-14 23:50:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-05-01 23:35:25 +00:00
|
|
|
strcpy(uri,http_hdr->uri);
|
2001-12-14 23:50:57 +00:00
|
|
|
}
|
2001-05-25 13:58:32 +00:00
|
|
|
|
2001-12-14 23:50:57 +00:00
|
|
|
//**** Compute the request length
|
|
|
|
// Add the Method line
|
|
|
|
len = strlen(http_hdr->method)+strlen(uri)+12;
|
|
|
|
// Add the fields
|
2009-05-13 02:58:57 +00:00
|
|
|
field = http_hdr->first_field;
|
2001-11-20 22:14:16 +00:00
|
|
|
while( field!=NULL ) {
|
|
|
|
len += strlen(field->field_name)+2;
|
|
|
|
field = field->next;
|
|
|
|
}
|
2001-12-14 23:50:57 +00:00
|
|
|
// Add the CRLF
|
|
|
|
len += 2;
|
|
|
|
// Add the body
|
2001-05-29 16:58:52 +00:00
|
|
|
if( http_hdr->body!=NULL ) {
|
|
|
|
len += http_hdr->body_size;
|
|
|
|
}
|
2001-12-14 23:50:57 +00:00
|
|
|
// Free the buffer if it was previously used
|
2001-05-29 16:58:52 +00:00
|
|
|
if( http_hdr->buffer!=NULL ) {
|
|
|
|
free( http_hdr->buffer );
|
|
|
|
http_hdr->buffer = NULL;
|
|
|
|
}
|
2006-07-02 08:17:07 +00:00
|
|
|
http_hdr->buffer = malloc(len+1);
|
2001-05-29 16:58:52 +00:00
|
|
|
if( http_hdr->buffer==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
|
2001-05-29 16:58:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
http_hdr->buffer_size = len;
|
|
|
|
|
2001-12-14 23:50:57 +00:00
|
|
|
//*** Building the request
|
2001-05-29 16:58:52 +00:00
|
|
|
ptr = http_hdr->buffer;
|
2001-12-14 23:50:57 +00:00
|
|
|
// Add the method line
|
|
|
|
ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version );
|
2001-11-20 22:14:16 +00:00
|
|
|
field = http_hdr->first_field;
|
2001-12-14 23:50:57 +00:00
|
|
|
// Add the field
|
2001-11-20 22:14:16 +00:00
|
|
|
while( field!=NULL ) {
|
|
|
|
ptr += sprintf( ptr, "%s\r\n", field->field_name );
|
|
|
|
field = field->next;
|
|
|
|
}
|
2001-05-25 13:58:32 +00:00
|
|
|
ptr += sprintf( ptr, "\r\n" );
|
2001-12-14 23:50:57 +00:00
|
|
|
// Add the body
|
2001-05-25 13:58:32 +00:00
|
|
|
if( http_hdr->body!=NULL ) {
|
|
|
|
memcpy( ptr, http_hdr->body, http_hdr->body_size );
|
|
|
|
}
|
2001-12-14 23:50:57 +00:00
|
|
|
|
|
|
|
if( uri ) free( uri );
|
2009-05-13 02:58:57 +00:00
|
|
|
return http_hdr->buffer;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
http_get_field( HTTP_header_t *http_hdr, const char *field_name ) {
|
|
|
|
if( http_hdr==NULL || field_name==NULL ) return NULL;
|
2001-11-20 22:14:16 +00:00
|
|
|
http_hdr->field_search_pos = http_hdr->first_field;
|
|
|
|
http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 );
|
2001-05-25 13:58:32 +00:00
|
|
|
if( http_hdr->field_search==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
|
2001-05-25 13:58:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
strcpy( http_hdr->field_search, field_name );
|
|
|
|
return http_get_next_field( http_hdr );
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
http_get_next_field( HTTP_header_t *http_hdr ) {
|
|
|
|
char *ptr;
|
2001-11-20 22:14:16 +00:00
|
|
|
HTTP_field_t *field;
|
2001-05-25 13:58:32 +00:00
|
|
|
if( http_hdr==NULL ) return NULL;
|
|
|
|
|
2001-11-20 22:14:16 +00:00
|
|
|
field = http_hdr->field_search_pos;
|
2009-05-13 02:58:57 +00:00
|
|
|
while( field!=NULL ) {
|
2001-11-20 22:14:16 +00:00
|
|
|
ptr = strstr( field->field_name, ":" );
|
2001-05-25 13:58:32 +00:00
|
|
|
if( ptr==NULL ) return NULL;
|
2001-11-20 22:14:16 +00:00
|
|
|
if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) {
|
2001-05-25 13:58:32 +00:00
|
|
|
ptr++; // Skip the column
|
|
|
|
while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some
|
2001-11-20 22:14:16 +00:00
|
|
|
http_hdr->field_search_pos = field->next;
|
2001-05-25 13:58:32 +00:00
|
|
|
return ptr; // return the value without the field name
|
|
|
|
}
|
2001-11-20 22:14:16 +00:00
|
|
|
field = field->next;
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-11-20 22:14:16 +00:00
|
|
|
http_set_field( HTTP_header_t *http_hdr, const char *field_name ) {
|
|
|
|
HTTP_field_t *new_field;
|
|
|
|
if( http_hdr==NULL || field_name==NULL ) return;
|
2001-05-25 13:58:32 +00:00
|
|
|
|
2006-07-02 08:17:07 +00:00
|
|
|
new_field = malloc(sizeof(HTTP_field_t));
|
2001-11-20 22:14:16 +00:00
|
|
|
if( new_field==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
|
2001-05-25 13:58:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-11-20 22:14:16 +00:00
|
|
|
new_field->next = NULL;
|
2006-07-02 08:17:07 +00:00
|
|
|
new_field->field_name = malloc(strlen(field_name)+1);
|
2001-11-20 22:14:16 +00:00
|
|
|
if( new_field->field_name==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
|
2008-10-31 12:56:25 +00:00
|
|
|
free(new_field);
|
2001-11-20 22:14:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
strcpy( new_field->field_name, field_name );
|
|
|
|
|
|
|
|
if( http_hdr->last_field==NULL ) {
|
|
|
|
http_hdr->first_field = new_field;
|
|
|
|
} else {
|
|
|
|
http_hdr->last_field->next = new_field;
|
|
|
|
}
|
|
|
|
http_hdr->last_field = new_field;
|
2001-05-25 13:58:32 +00:00
|
|
|
http_hdr->field_nb++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
http_set_method( HTTP_header_t *http_hdr, const char *method ) {
|
|
|
|
if( http_hdr==NULL || method==NULL ) return;
|
|
|
|
|
2006-07-02 08:17:07 +00:00
|
|
|
http_hdr->method = malloc(strlen(method)+1);
|
2001-05-25 13:58:32 +00:00
|
|
|
if( http_hdr->method==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
|
2001-05-25 13:58:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
strcpy( http_hdr->method, method );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
http_set_uri( HTTP_header_t *http_hdr, const char *uri ) {
|
|
|
|
if( http_hdr==NULL || uri==NULL ) return;
|
|
|
|
|
2006-07-02 08:17:07 +00:00
|
|
|
http_hdr->uri = malloc(strlen(uri)+1);
|
2001-05-25 13:58:32 +00:00
|
|
|
if( http_hdr->uri==NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
|
2001-05-25 13:58:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
strcpy( http_hdr->uri, uri );
|
|
|
|
}
|
|
|
|
|
2002-06-23 09:16:08 +00:00
|
|
|
int
|
|
|
|
http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
|
2006-04-14 13:37:38 +00:00
|
|
|
char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL;
|
2002-06-23 09:16:08 +00:00
|
|
|
int encoded_len, pass_len=0, out_len;
|
2006-04-14 13:37:38 +00:00
|
|
|
int res = -1;
|
2002-06-23 09:16:08 +00:00
|
|
|
if( http_hdr==NULL || username==NULL ) return -1;
|
|
|
|
|
|
|
|
if( password!=NULL ) {
|
|
|
|
pass_len = strlen(password);
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2006-07-02 08:17:07 +00:00
|
|
|
usr_pass = malloc(strlen(username)+pass_len+2);
|
2002-06-23 09:16:08 +00:00
|
|
|
if( usr_pass==NULL ) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
|
2006-04-14 13:37:38 +00:00
|
|
|
goto out;
|
2002-06-23 09:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password );
|
|
|
|
|
|
|
|
// Base 64 encode with at least 33% more data than the original size
|
|
|
|
encoded_len = strlen(usr_pass)*2;
|
2006-07-02 08:17:07 +00:00
|
|
|
b64_usr_pass = malloc(encoded_len);
|
2002-06-23 09:16:08 +00:00
|
|
|
if( b64_usr_pass==NULL ) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
|
2006-04-14 13:37:38 +00:00
|
|
|
goto out;
|
2002-06-23 09:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len);
|
|
|
|
if( out_len<0 ) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n");
|
2006-04-14 13:37:38 +00:00
|
|
|
goto out;
|
2002-06-23 09:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b64_usr_pass[out_len]='\0';
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2006-07-02 08:17:07 +00:00
|
|
|
auth = malloc(encoded_len+22);
|
2002-06-23 09:16:08 +00:00
|
|
|
if( auth==NULL ) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
|
2006-04-14 13:37:38 +00:00
|
|
|
goto out;
|
2002-06-23 09:16:08 +00:00
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2002-06-23 09:16:08 +00:00
|
|
|
sprintf( auth, "Authorization: Basic %s", b64_usr_pass);
|
|
|
|
http_set_field( http_hdr, auth );
|
2006-04-14 13:37:38 +00:00
|
|
|
res = 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2006-04-14 13:37:38 +00:00
|
|
|
out:
|
2002-06-23 09:16:08 +00:00
|
|
|
free( usr_pass );
|
|
|
|
free( b64_usr_pass );
|
|
|
|
free( auth );
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2006-04-14 13:37:38 +00:00
|
|
|
return res;
|
2002-06-23 09:16:08 +00:00
|
|
|
}
|
|
|
|
|
2001-05-25 13:58:32 +00:00
|
|
|
void
|
|
|
|
http_debug_hdr( HTTP_header_t *http_hdr ) {
|
2001-11-20 22:14:16 +00:00
|
|
|
HTTP_field_t *field;
|
|
|
|
int i = 0;
|
2001-05-29 16:58:52 +00:00
|
|
|
if( http_hdr==NULL ) return;
|
2001-05-25 13:58:32 +00:00
|
|
|
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n");
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol );
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version );
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri );
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method );
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code );
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase );
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size );
|
2001-05-25 13:58:32 +00:00
|
|
|
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n");
|
2001-11-20 22:14:16 +00:00
|
|
|
field = http_hdr->first_field;
|
|
|
|
while( field!=NULL ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name );
|
2001-11-20 22:14:16 +00:00
|
|
|
field = field->next;
|
|
|
|
}
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n");
|
2001-05-25 13:58:32 +00:00
|
|
|
}
|
2002-06-23 09:16:08 +00:00
|
|
|
|
2009-05-13 02:58:57 +00:00
|
|
|
int
|
2002-06-23 09:16:08 +00:00
|
|
|
base64_encode(const void *enc, int encLen, char *out, int outMax) {
|
2006-03-08 15:14:08 +00:00
|
|
|
static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
2002-06-23 09:16:08 +00:00
|
|
|
|
|
|
|
unsigned char *encBuf;
|
|
|
|
int outLen;
|
|
|
|
unsigned int bits;
|
|
|
|
unsigned int shift;
|
|
|
|
|
|
|
|
encBuf = (unsigned char*)enc;
|
|
|
|
outLen = 0;
|
|
|
|
bits = 0;
|
|
|
|
shift = 0;
|
2006-03-08 15:14:08 +00:00
|
|
|
outMax &= ~3;
|
2002-06-23 09:16:08 +00:00
|
|
|
|
2007-01-28 19:25:03 +00:00
|
|
|
while(1) {
|
2002-06-23 09:16:08 +00:00
|
|
|
if( encLen>0 ) {
|
|
|
|
// Shift in byte
|
|
|
|
bits <<= 8;
|
|
|
|
bits |= *encBuf;
|
|
|
|
shift += 8;
|
|
|
|
// Next byte
|
|
|
|
encBuf++;
|
|
|
|
encLen--;
|
|
|
|
} else if( shift>0 ) {
|
|
|
|
// Pad last bits to 6 bits - will end next loop
|
|
|
|
bits <<= 6 - shift;
|
|
|
|
shift = 6;
|
|
|
|
} else {
|
2006-03-08 15:14:08 +00:00
|
|
|
// As per RFC 2045, section 6.8,
|
|
|
|
// pad output as necessary: 0 to 2 '=' chars.
|
|
|
|
while( outLen & 3 ){
|
|
|
|
*out++ = '=';
|
|
|
|
outLen++;
|
|
|
|
}
|
2002-06-23 09:16:08 +00:00
|
|
|
|
|
|
|
return outLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode 6 bit segments
|
|
|
|
while( shift>=6 ) {
|
2007-01-28 19:25:03 +00:00
|
|
|
if (outLen >= outMax)
|
|
|
|
return -1;
|
2002-06-23 09:16:08 +00:00
|
|
|
shift -= 6;
|
|
|
|
*out = b64[ (bits >> shift) & 0x3F ];
|
|
|
|
out++;
|
|
|
|
outLen++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-15 20:31:10 +00:00
|
|
|
static void print_icy_metadata(HTTP_header_t *http_hdr) {
|
|
|
|
const char *field_data;
|
|
|
|
// note: I skip icy-notice1 and 2, as they contain html <BR>
|
|
|
|
// and are IMHO useless info ::atmos
|
|
|
|
if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL )
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data);
|
|
|
|
if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL )
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data);
|
|
|
|
if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL )
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data);
|
|
|
|
// XXX: does this really mean public server? ::atmos
|
|
|
|
if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL )
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no");
|
|
|
|
if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL )
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data);
|
|
|
|
}
|
|
|
|
|
2006-12-10 19:47:24 +00:00
|
|
|
//! If this function succeeds you must closesocket stream->fd
|
2005-05-29 12:54:00 +00:00
|
|
|
static int http_streaming_start(stream_t *stream, int* file_format) {
|
2006-12-09 19:33:28 +00:00
|
|
|
HTTP_header_t *http_hdr = NULL;
|
2005-05-29 12:54:00 +00:00
|
|
|
unsigned int i;
|
2006-12-09 19:33:28 +00:00
|
|
|
int fd = stream->fd;
|
2007-11-26 00:41:21 +00:00
|
|
|
int res = STREAM_UNSUPPORTED;
|
2005-05-29 12:54:00 +00:00
|
|
|
int redirect = 0;
|
|
|
|
int auth_retry=0;
|
|
|
|
int seekable=0;
|
|
|
|
char *content_type;
|
2010-01-23 11:14:18 +00:00
|
|
|
const char *content_length;
|
2005-05-29 12:54:00 +00:00
|
|
|
char *next_url;
|
|
|
|
URL_t *url = stream->streaming_ctrl->url;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2006-12-09 19:34:46 +00:00
|
|
|
redirect = 0;
|
2006-12-09 19:33:28 +00:00
|
|
|
if (fd > 0) closesocket(fd);
|
2005-05-29 12:54:00 +00:00
|
|
|
fd = http_send_request( url, 0 );
|
|
|
|
if( fd<0 ) {
|
2006-12-09 19:33:28 +00:00
|
|
|
goto err_out;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
|
2006-12-09 19:33:28 +00:00
|
|
|
http_free(http_hdr);
|
2005-05-29 12:54:00 +00:00
|
|
|
http_hdr = http_read_response( fd );
|
|
|
|
if( http_hdr==NULL ) {
|
2006-12-09 19:33:28 +00:00
|
|
|
goto err_out;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
|
2006-03-24 08:12:03 +00:00
|
|
|
if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
|
2005-05-29 12:54:00 +00:00
|
|
|
http_debug_hdr( http_hdr );
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-29 12:54:00 +00:00
|
|
|
// Check if we can make partial content requests and thus seek in http-streams
|
|
|
|
if( http_hdr!=NULL && http_hdr->status_code==200 ) {
|
2009-12-31 16:47:53 +00:00
|
|
|
const char *accept_ranges = http_get_field(http_hdr,"Accept-Ranges");
|
2009-12-31 16:46:44 +00:00
|
|
|
const char *server = http_get_field(http_hdr, "Server");
|
2009-12-31 16:47:53 +00:00
|
|
|
if (accept_ranges)
|
2005-05-29 12:54:00 +00:00
|
|
|
seekable = strncmp(accept_ranges,"bytes",5)==0;
|
2009-12-31 16:46:44 +00:00
|
|
|
else if (server && strcmp(server, "gvs 1.0") == 0)
|
2009-12-30 20:50:03 +00:00
|
|
|
seekable = 1; // HACK for youtube incorrectly claiming not to support seeking
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
|
2008-02-15 20:32:45 +00:00
|
|
|
print_icy_metadata(http_hdr);
|
|
|
|
|
2005-05-29 12:54:00 +00:00
|
|
|
// Check if the response is an ICY status_code reason_phrase
|
2008-02-15 20:20:16 +00:00
|
|
|
if( !strcasecmp(http_hdr->protocol, "ICY") ||
|
|
|
|
http_get_field(http_hdr, "Icy-MetaInt") ) {
|
2005-05-29 12:54:00 +00:00
|
|
|
switch( http_hdr->status_code ) {
|
|
|
|
case 200: { // OK
|
2008-02-15 20:23:59 +00:00
|
|
|
char *field_data;
|
2009-05-13 02:58:57 +00:00
|
|
|
// If content-type == video/nsv we most likely have a winamp video stream
|
|
|
|
// otherwise it should be mp3. if there are more types consider adding mime type
|
2005-05-29 12:54:00 +00:00
|
|
|
// handling like later
|
|
|
|
if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox")))
|
|
|
|
*file_format = DEMUXER_TYPE_NSV;
|
2005-11-08 22:07:53 +00:00
|
|
|
else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac")))
|
2005-11-05 15:32:50 +00:00
|
|
|
*file_format = DEMUXER_TYPE_AAC;
|
2005-05-29 12:54:00 +00:00
|
|
|
else
|
|
|
|
*file_format = DEMUXER_TYPE_AUDIO;
|
2007-11-26 00:41:21 +00:00
|
|
|
res = STREAM_ERROR;
|
2006-12-09 19:33:28 +00:00
|
|
|
goto out;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
case 400: // Server Full
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n");
|
2006-12-09 19:33:28 +00:00
|
|
|
goto err_out;
|
2005-05-29 12:54:00 +00:00
|
|
|
case 401: // Service Unavailable
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n");
|
2006-12-09 19:33:28 +00:00
|
|
|
goto err_out;
|
2005-05-29 12:54:00 +00:00
|
|
|
case 403: // Service Forbidden
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n");
|
2006-12-09 19:33:28 +00:00
|
|
|
goto err_out;
|
2005-05-29 12:54:00 +00:00
|
|
|
case 404: // Resource Not Found
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n");
|
2006-12-09 19:33:28 +00:00
|
|
|
goto err_out;
|
2005-05-29 12:54:00 +00:00
|
|
|
default:
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
|
2006-12-09 19:33:28 +00:00
|
|
|
goto err_out;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:58:57 +00:00
|
|
|
// Assume standard http if not ICY
|
2005-05-29 12:54:00 +00:00
|
|
|
switch( http_hdr->status_code ) {
|
|
|
|
case 200: // OK
|
2010-01-23 11:14:18 +00:00
|
|
|
content_length = http_get_field(http_hdr, "Content-Length");
|
|
|
|
if (content_length) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", content_length);
|
|
|
|
stream->end_pos = atoll(content_length);
|
|
|
|
}
|
2005-05-29 12:54:00 +00:00
|
|
|
// Look if we can use the Content-Type
|
|
|
|
content_type = http_get_field( http_hdr, "Content-Type" );
|
|
|
|
if( content_type!=NULL ) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type );
|
|
|
|
// Check in the mime type table for a demuxer type
|
|
|
|
i = 0;
|
|
|
|
while(mime_type_table[i].mime_type != NULL) {
|
|
|
|
if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) {
|
|
|
|
*file_format = mime_type_table[i].demuxer_type;
|
2006-12-09 19:33:28 +00:00
|
|
|
res = seekable;
|
|
|
|
goto out;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not found in the mime type table, don't fail,
|
|
|
|
// we should try raw HTTP
|
2006-12-09 19:33:28 +00:00
|
|
|
res = seekable;
|
|
|
|
goto out;
|
2005-05-29 12:54:00 +00:00
|
|
|
// Redirect
|
|
|
|
case 301: // Permanently
|
|
|
|
case 302: // Temporarily
|
2006-08-20 11:47:33 +00:00
|
|
|
case 303: // See Other
|
2005-05-29 12:54:00 +00:00
|
|
|
// TODO: RFC 2616, recommand to detect infinite redirection loops
|
|
|
|
next_url = http_get_field( http_hdr, "Location" );
|
|
|
|
if( next_url!=NULL ) {
|
2007-12-02 18:24:01 +00:00
|
|
|
int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
|
2006-11-08 22:42:31 +00:00
|
|
|
stream->streaming_ctrl->url = url_redirect( &url, next_url );
|
2007-11-26 00:41:21 +00:00
|
|
|
if (!strcasecmp(url->protocol, "mms")) {
|
|
|
|
res = STREAM_REDIRECTED;
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
if (strcasecmp(url->protocol, "http")) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Unsupported http %d redirect to %s protocol\n", http_hdr->status_code, url->protocol);
|
|
|
|
goto err_out;
|
|
|
|
}
|
2007-12-02 18:24:01 +00:00
|
|
|
if (is_ultravox) {
|
|
|
|
free(url->protocol);
|
|
|
|
url->protocol = strdup("unsv");
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
redirect = 1;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 401: // Authentication required
|
2006-12-10 19:38:41 +00:00
|
|
|
if( http_authenticate(http_hdr, url, &auth_retry)<0 )
|
2006-12-09 19:33:28 +00:00
|
|
|
goto err_out;
|
2005-05-29 12:54:00 +00:00
|
|
|
redirect = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
|
2006-12-09 19:33:28 +00:00
|
|
|
goto err_out;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
} while( redirect );
|
|
|
|
|
2006-12-09 19:33:28 +00:00
|
|
|
err_out:
|
|
|
|
if (fd > 0) closesocket( fd );
|
2006-12-10 19:35:39 +00:00
|
|
|
fd = -1;
|
2006-12-09 19:33:28 +00:00
|
|
|
http_free( http_hdr );
|
2006-12-30 13:23:39 +00:00
|
|
|
http_hdr = NULL;
|
2006-12-09 19:50:08 +00:00
|
|
|
out:
|
2006-12-30 13:23:39 +00:00
|
|
|
stream->streaming_ctrl->data = (void*)http_hdr;
|
2006-12-10 19:35:39 +00:00
|
|
|
stream->fd = fd;
|
2006-12-09 19:33:28 +00:00
|
|
|
return res;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fixup_open(stream_t *stream,int seekable) {
|
2005-07-19 12:07:04 +00:00
|
|
|
HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
|
2005-07-24 12:22:30 +00:00
|
|
|
int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt");
|
2005-07-23 15:29:51 +00:00
|
|
|
int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
|
2005-05-29 12:54:00 +00:00
|
|
|
|
|
|
|
stream->type = STREAMTYPE_STREAM;
|
2005-07-19 12:07:04 +00:00
|
|
|
if(!is_icy && !is_ultravox && seekable)
|
2005-05-29 12:54:00 +00:00
|
|
|
{
|
2009-11-22 15:18:21 +00:00
|
|
|
stream->flags |= MP_STREAM_SEEK;
|
2005-05-29 12:54:00 +00:00
|
|
|
stream->seek = http_seek;
|
|
|
|
}
|
|
|
|
stream->streaming_ctrl->bandwidth = network_bandwidth;
|
2005-07-19 12:07:04 +00:00
|
|
|
if ((!is_icy && !is_ultravox) || scast_streaming_start(stream))
|
2005-05-29 12:54:00 +00:00
|
|
|
if(nop_streaming_start( stream )) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n");
|
2006-12-10 19:47:24 +00:00
|
|
|
if (stream->fd >= 0)
|
|
|
|
closesocket(stream->fd);
|
|
|
|
stream->fd = -1;
|
2005-05-29 12:54:00 +00:00
|
|
|
streaming_ctrl_free(stream->streaming_ctrl);
|
|
|
|
stream->streaming_ctrl = NULL;
|
2007-08-28 22:38:45 +00:00
|
|
|
return STREAM_UNSUPPORTED;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fixup_network_stream_cache(stream);
|
|
|
|
return STREAM_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) {
|
|
|
|
int seekable=0;
|
|
|
|
URL_t *url;
|
|
|
|
|
|
|
|
stream->streaming_ctrl = streaming_ctrl_new();
|
|
|
|
if( stream->streaming_ctrl==NULL ) {
|
|
|
|
return STREAM_ERROR;
|
|
|
|
}
|
|
|
|
stream->streaming_ctrl->bandwidth = network_bandwidth;
|
|
|
|
url = url_new(stream->url);
|
|
|
|
stream->streaming_ctrl->url = check4proxies(url);
|
2005-09-06 21:00:57 +00:00
|
|
|
url_free(url);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2006-10-12 22:24:19 +00:00
|
|
|
mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(1), URL: %s\n", stream->url);
|
2005-05-29 12:54:00 +00:00
|
|
|
seekable = http_streaming_start(stream, file_format);
|
|
|
|
if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) {
|
2006-12-10 19:47:24 +00:00
|
|
|
if (stream->fd >= 0)
|
|
|
|
closesocket(stream->fd);
|
|
|
|
stream->fd = -1;
|
2007-11-26 00:41:21 +00:00
|
|
|
if (seekable == STREAM_REDIRECTED)
|
|
|
|
return seekable;
|
2005-05-29 12:54:00 +00:00
|
|
|
streaming_ctrl_free(stream->streaming_ctrl);
|
|
|
|
stream->streaming_ctrl = NULL;
|
2007-08-28 22:38:45 +00:00
|
|
|
return STREAM_UNSUPPORTED;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fixup_open(stream, seekable);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) {
|
|
|
|
int seekable=0;
|
|
|
|
URL_t *url;
|
|
|
|
|
|
|
|
stream->streaming_ctrl = streaming_ctrl_new();
|
|
|
|
if( stream->streaming_ctrl==NULL ) {
|
|
|
|
return STREAM_ERROR;
|
|
|
|
}
|
|
|
|
stream->streaming_ctrl->bandwidth = network_bandwidth;
|
|
|
|
url = url_new(stream->url);
|
|
|
|
stream->streaming_ctrl->url = check4proxies(url);
|
2005-09-28 15:43:24 +00:00
|
|
|
url_free(url);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2006-10-12 22:24:19 +00:00
|
|
|
mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url);
|
2005-05-29 12:54:00 +00:00
|
|
|
seekable = http_streaming_start(stream, file_format);
|
|
|
|
if(seekable < 0) {
|
2006-12-10 19:47:24 +00:00
|
|
|
if (stream->fd >= 0)
|
|
|
|
closesocket(stream->fd);
|
|
|
|
stream->fd = -1;
|
2005-05-29 12:54:00 +00:00
|
|
|
streaming_ctrl_free(stream->streaming_ctrl);
|
|
|
|
stream->streaming_ctrl = NULL;
|
2007-08-28 22:38:45 +00:00
|
|
|
return STREAM_UNSUPPORTED;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fixup_open(stream, seekable);
|
|
|
|
}
|
|
|
|
|
2002-06-23 09:16:08 +00:00
|
|
|
|
2007-12-02 13:22:53 +00:00
|
|
|
const stream_info_t stream_info_http1 = {
|
2005-05-29 12:54:00 +00:00
|
|
|
"http streaming",
|
|
|
|
"null",
|
|
|
|
"Bertrand, Albeau, Reimar Doeffinger, Arpi?",
|
|
|
|
"plain http",
|
|
|
|
open_s1,
|
2008-11-02 17:15:14 +00:00
|
|
|
{"http", "http_proxy", "unsv", "icyx", "noicyx", NULL},
|
2005-05-29 12:54:00 +00:00
|
|
|
NULL,
|
|
|
|
0 // Urls are an option string
|
|
|
|
};
|
|
|
|
|
2007-12-02 13:22:53 +00:00
|
|
|
const stream_info_t stream_info_http2 = {
|
2005-05-29 12:54:00 +00:00
|
|
|
"http streaming",
|
|
|
|
"null",
|
|
|
|
"Bertrand, Albeu, Arpi? who?",
|
2005-11-06 16:10:39 +00:00
|
|
|
"plain http, also used as fallback for many other protocols",
|
2005-05-29 12:54:00 +00:00
|
|
|
open_s2,
|
|
|
|
{"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback
|
|
|
|
NULL,
|
|
|
|
0 // Urls are an option string
|
|
|
|
};
|