2001-05-29 17:03:17 +00:00
|
|
|
/*
|
|
|
|
* Network layer for MPlayer
|
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 17:03:17 +00:00
|
|
|
*/
|
|
|
|
|
2001-06-05 08:49:11 +00:00
|
|
|
//#define DUMP2FILE
|
2001-06-04 17:52:33 +00:00
|
|
|
|
2001-08-01 09:14:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-05-18 16:14:06 +00:00
|
|
|
#include <unistd.h>
|
2001-08-01 09:14:02 +00:00
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
#include <errno.h>
|
2001-05-29 17:03:17 +00:00
|
|
|
#include <ctype.h>
|
2001-05-18 16:14:06 +00:00
|
|
|
|
2001-10-30 17:03:11 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-12-10 01:40:18 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
2008-08-31 13:59:54 +00:00
|
|
|
|
2009-02-01 13:42:27 +00:00
|
|
|
#if HAVE_WINSOCK2_H
|
2008-08-31 13:59:54 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#endif
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
#include "stream.h"
|
2006-08-04 17:01:29 +00:00
|
|
|
#include "libmpdemux/demuxer.h"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "m_config.h"
|
2001-06-04 17:52:33 +00:00
|
|
|
|
2001-05-20 12:58:41 +00:00
|
|
|
#include "network.h"
|
2006-08-05 10:30:06 +00:00
|
|
|
#include "tcp.h"
|
2001-05-29 17:03:17 +00:00
|
|
|
#include "http.h"
|
2003-12-08 13:25:35 +00:00
|
|
|
#include "cookies.h"
|
2001-05-29 17:03:17 +00:00
|
|
|
#include "url.h"
|
2001-05-20 12:58:41 +00:00
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "version.h"
|
2002-06-20 08:50:40 +00:00
|
|
|
|
2002-10-23 17:21:01 +00:00
|
|
|
extern int stream_cache_size;
|
2002-01-12 21:08:12 +00:00
|
|
|
|
2003-12-08 13:25:35 +00:00
|
|
|
/* Variables for the command line option -user, -passwd, -bandwidth,
|
|
|
|
-user-agent and -nocookies */
|
|
|
|
|
2002-10-23 17:21:01 +00:00
|
|
|
char *network_username=NULL;
|
|
|
|
char *network_password=NULL;
|
|
|
|
int network_bandwidth=0;
|
2003-12-08 13:25:35 +00:00
|
|
|
int network_cookies_enabled = 0;
|
2003-10-22 17:54:32 +00:00
|
|
|
char *network_useragent=NULL;
|
2002-06-25 08:04:52 +00:00
|
|
|
|
2003-03-26 11:35:13 +00:00
|
|
|
/* IPv6 options */
|
|
|
|
int network_ipv4_only_proxy = 0;
|
|
|
|
|
2002-06-25 08:04:52 +00:00
|
|
|
|
2007-12-02 22:12:50 +00:00
|
|
|
const mime_struct_t mime_type_table[] = {
|
2010-02-05 17:35:48 +00:00
|
|
|
#ifdef CONFIG_LIBAVFORMAT
|
|
|
|
// Flash Video
|
|
|
|
{ "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
|
2010-02-20 11:48:41 +00:00
|
|
|
// do not force any demuxer in this case!
|
|
|
|
// we want the lavf demuxer to be tried first (happens automatically anyway),
|
|
|
|
// but for mov reference files to work we must also try
|
|
|
|
// the native demuxer if lavf fails.
|
|
|
|
{ "video/quicktime", 0 },
|
2010-02-05 17:35:48 +00:00
|
|
|
#endif
|
2001-11-20 22:20:20 +00:00
|
|
|
// MP3 streaming, some MP3 streaming server answer with audio/mpeg
|
2002-02-16 21:48:59 +00:00
|
|
|
{ "audio/mpeg", DEMUXER_TYPE_AUDIO },
|
2001-11-20 22:20:20 +00:00
|
|
|
// MPEG streaming
|
2004-11-21 12:16:14 +00:00
|
|
|
{ "video/mpeg", DEMUXER_TYPE_UNKNOWN },
|
|
|
|
{ "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
|
|
|
|
{ "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
|
2001-11-20 22:20:20 +00:00
|
|
|
// AVI ??? => video/x-msvideo
|
|
|
|
{ "video/x-msvideo", DEMUXER_TYPE_AVI },
|
|
|
|
// MOV => video/quicktime
|
|
|
|
{ "video/quicktime", DEMUXER_TYPE_MOV },
|
|
|
|
// ASF
|
|
|
|
{ "audio/x-ms-wax", DEMUXER_TYPE_ASF },
|
|
|
|
{ "audio/x-ms-wma", DEMUXER_TYPE_ASF },
|
|
|
|
{ "video/x-ms-asf", DEMUXER_TYPE_ASF },
|
|
|
|
{ "video/x-ms-afs", DEMUXER_TYPE_ASF },
|
|
|
|
{ "video/x-ms-wmv", DEMUXER_TYPE_ASF },
|
|
|
|
{ "video/x-ms-wma", DEMUXER_TYPE_ASF },
|
2005-12-24 18:17:39 +00:00
|
|
|
{ "application/x-mms-framed", DEMUXER_TYPE_ASF },
|
2006-02-20 21:59:28 +00:00
|
|
|
{ "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
|
2006-09-15 21:34:38 +00:00
|
|
|
{ "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
|
2002-02-21 13:14:52 +00:00
|
|
|
// Playlists
|
2002-09-13 22:39:48 +00:00
|
|
|
{ "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
|
2007-06-29 21:21:57 +00:00
|
|
|
{ "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
|
2002-02-21 13:14:52 +00:00
|
|
|
{ "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
|
|
|
|
{ "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
|
2002-05-09 14:52:23 +00:00
|
|
|
{ "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
|
|
|
|
// Real Media
|
2005-04-17 20:33:25 +00:00
|
|
|
// { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
|
2002-11-05 19:03:52 +00:00
|
|
|
// OGG Streaming
|
2004-04-12 14:19:12 +00:00
|
|
|
{ "application/x-ogg", DEMUXER_TYPE_OGG },
|
|
|
|
// NullSoft Streaming Video
|
2004-05-14 23:10:43 +00:00
|
|
|
{ "video/nsv", DEMUXER_TYPE_NSV},
|
2005-05-29 12:54:00 +00:00
|
|
|
{ "misc/ultravox", DEMUXER_TYPE_NSV},
|
|
|
|
{ NULL, DEMUXER_TYPE_UNKNOWN},
|
2001-11-20 22:20:20 +00:00
|
|
|
};
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
|
|
|
|
streaming_ctrl_t *
|
2006-02-09 14:08:03 +00:00
|
|
|
streaming_ctrl_new(void) {
|
2001-06-04 17:52:33 +00:00
|
|
|
streaming_ctrl_t *streaming_ctrl;
|
2006-07-13 16:41:13 +00:00
|
|
|
streaming_ctrl = malloc(sizeof(streaming_ctrl_t));
|
2001-06-04 17:52:33 +00:00
|
|
|
if( streaming_ctrl==NULL ) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
|
2001-06-04 17:52:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) );
|
|
|
|
return streaming_ctrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
|
|
|
|
if( streaming_ctrl==NULL ) return;
|
2002-01-19 09:04:02 +00:00
|
|
|
if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
|
|
|
|
if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer );
|
|
|
|
if( streaming_ctrl->data ) free( streaming_ctrl->data );
|
2001-06-04 17:52:33 +00:00
|
|
|
free( streaming_ctrl );
|
|
|
|
}
|
|
|
|
|
2002-01-14 06:44:30 +00:00
|
|
|
URL_t*
|
|
|
|
check4proxies( URL_t *url ) {
|
2002-02-11 05:16:09 +00:00
|
|
|
URL_t *url_out = NULL;
|
2002-01-19 09:04:02 +00:00
|
|
|
if( url==NULL ) return NULL;
|
2002-02-11 05:16:09 +00:00
|
|
|
url_out = url_new( url->url );
|
2002-01-14 06:44:30 +00:00
|
|
|
if( !strcasecmp(url->protocol, "http_proxy") ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
|
2002-02-11 05:16:09 +00:00
|
|
|
return url_out;
|
2002-01-14 06:44:30 +00:00
|
|
|
}
|
|
|
|
// Check if the http_proxy environment variable is set.
|
|
|
|
if( !strcasecmp(url->protocol, "http") ) {
|
|
|
|
char *proxy;
|
|
|
|
proxy = getenv("http_proxy");
|
|
|
|
if( proxy!=NULL ) {
|
|
|
|
// We got a proxy, build the URL to use it
|
|
|
|
int len;
|
|
|
|
char *new_url;
|
|
|
|
URL_t *tmp_url;
|
|
|
|
URL_t *proxy_url = url_new( proxy );
|
|
|
|
|
|
|
|
if( proxy_url==NULL ) {
|
2009-07-05 23:41:23 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_WARN,
|
2009-07-06 22:15:02 +00:00
|
|
|
"Invalid proxy setting... Trying without proxy.\n");
|
2002-02-11 05:16:09 +00:00
|
|
|
return url_out;
|
2002-01-14 06:44:30 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2003-03-26 11:35:13 +00:00
|
|
|
#ifdef HAVE_AF_INET6
|
|
|
|
if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
|
2009-07-05 23:41:23 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_WARN,
|
2009-07-06 22:15:02 +00:00
|
|
|
"Could not resolve remote hostname for AF_INET. Trying without proxy.\n");
|
2005-09-06 21:14:55 +00:00
|
|
|
url_free(proxy_url);
|
2003-03-26 11:35:13 +00:00
|
|
|
return url_out;
|
|
|
|
}
|
|
|
|
#endif
|
2002-01-14 06:44:30 +00:00
|
|
|
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
|
2002-01-14 06:44:30 +00:00
|
|
|
len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port
|
|
|
|
new_url = malloc( len+1 );
|
|
|
|
if( new_url==NULL ) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
|
2005-09-06 21:14:55 +00:00
|
|
|
url_free(proxy_url);
|
2002-02-11 05:16:09 +00:00
|
|
|
return url_out;
|
2002-01-14 06:44:30 +00:00
|
|
|
}
|
2002-04-30 16:58:54 +00:00
|
|
|
sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url );
|
2002-01-14 06:44:30 +00:00
|
|
|
tmp_url = url_new( new_url );
|
|
|
|
if( tmp_url==NULL ) {
|
2005-09-06 21:14:55 +00:00
|
|
|
free( new_url );
|
|
|
|
url_free( proxy_url );
|
2002-02-11 05:16:09 +00:00
|
|
|
return url_out;
|
2002-01-14 06:44:30 +00:00
|
|
|
}
|
2002-02-11 05:16:09 +00:00
|
|
|
url_free( url_out );
|
|
|
|
url_out = tmp_url;
|
2002-01-14 06:44:30 +00:00
|
|
|
free( new_url );
|
|
|
|
url_free( proxy_url );
|
|
|
|
}
|
|
|
|
}
|
2002-02-11 05:16:09 +00:00
|
|
|
return url_out;
|
2002-01-14 06:44:30 +00:00
|
|
|
}
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
int
|
2004-02-17 12:30:45 +00:00
|
|
|
http_send_request( URL_t *url, off_t pos ) {
|
2001-06-04 17:52:33 +00:00
|
|
|
HTTP_header_t *http_hdr;
|
2002-01-12 21:08:12 +00:00
|
|
|
URL_t *server_url;
|
2003-10-22 17:54:32 +00:00
|
|
|
char str[256];
|
2006-12-09 18:54:12 +00:00
|
|
|
int fd = -1;
|
2002-01-12 21:08:12 +00:00
|
|
|
int ret;
|
|
|
|
int proxy = 0; // Boolean
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
http_hdr = http_new_header();
|
2002-01-12 21:08:12 +00:00
|
|
|
|
2002-01-14 01:12:44 +00:00
|
|
|
if( !strcasecmp(url->protocol, "http_proxy") ) {
|
2002-01-12 21:08:12 +00:00
|
|
|
proxy = 1;
|
|
|
|
server_url = url_new( (url->file)+1 );
|
|
|
|
http_set_uri( http_hdr, server_url->url );
|
|
|
|
} else {
|
|
|
|
server_url = url;
|
|
|
|
http_set_uri( http_hdr, server_url->file );
|
|
|
|
}
|
2004-06-25 15:14:27 +00:00
|
|
|
if (server_url->port && server_url->port != 80)
|
|
|
|
snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
|
|
|
|
else
|
|
|
|
snprintf(str, 256, "Host: %s", server_url->hostname );
|
2001-12-18 18:45:00 +00:00
|
|
|
http_set_field( http_hdr, str);
|
2003-10-22 17:54:32 +00:00
|
|
|
if (network_useragent)
|
|
|
|
{
|
|
|
|
snprintf(str, 256, "User-Agent: %s", network_useragent);
|
|
|
|
http_set_field(http_hdr, str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION);
|
2004-02-17 12:30:45 +00:00
|
|
|
|
2008-11-02 17:15:14 +00:00
|
|
|
if( strcasecmp(url->protocol, "noicyx") )
|
2008-11-02 17:15:44 +00:00
|
|
|
http_set_field(http_hdr, "Icy-MetaData: 1");
|
2005-07-19 12:07:04 +00:00
|
|
|
|
2009-07-06 23:26:13 +00:00
|
|
|
if(pos>0) {
|
2004-02-17 12:30:45 +00:00
|
|
|
// Extend http_send_request with possibility to do partial content retrieval
|
2005-10-13 18:33:56 +00:00
|
|
|
snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
|
2004-02-17 12:30:45 +00:00
|
|
|
http_set_field(http_hdr, str);
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2003-12-08 13:25:35 +00:00
|
|
|
if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-04-29 21:34:50 +00:00
|
|
|
http_set_field( http_hdr, "Connection: close");
|
2002-06-23 09:17:52 +00:00
|
|
|
http_add_basic_authentication( http_hdr, url->username, url->password );
|
2001-06-04 17:52:33 +00:00
|
|
|
if( http_build_request( http_hdr )==NULL ) {
|
2005-09-06 21:00:57 +00:00
|
|
|
goto err_out;
|
2001-06-04 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
2002-01-12 21:08:12 +00:00
|
|
|
if( proxy ) {
|
|
|
|
if( url->port==0 ) url->port = 8080; // Default port for the proxy server
|
2003-08-15 19:13:23 +00:00
|
|
|
fd = connect2Server( url->hostname, url->port,1 );
|
2002-01-12 21:08:12 +00:00
|
|
|
url_free( server_url );
|
2006-03-08 15:07:48 +00:00
|
|
|
server_url = NULL;
|
2002-01-12 21:08:12 +00:00
|
|
|
} else {
|
|
|
|
if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
|
2003-08-15 19:13:23 +00:00
|
|
|
fd = connect2Server( server_url->hostname, server_url->port,1 );
|
2002-01-12 21:08:12 +00:00
|
|
|
}
|
2001-06-04 17:52:33 +00:00
|
|
|
if( fd<0 ) {
|
2005-09-06 21:00:57 +00:00
|
|
|
goto err_out;
|
2001-06-04 17:52:33 +00:00
|
|
|
}
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2003-05-29 19:36:58 +00:00
|
|
|
ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
|
2002-10-29 09:18:53 +00:00
|
|
|
if( ret!=(int)http_hdr->buffer_size ) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while sending HTTP request: Didn't send all the request.\n");
|
2005-09-06 21:00:57 +00:00
|
|
|
goto err_out;
|
2002-01-12 21:08:12 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
http_free( http_hdr );
|
|
|
|
|
|
|
|
return fd;
|
2005-09-06 21:00:57 +00:00
|
|
|
err_out:
|
2006-12-09 18:54:12 +00:00
|
|
|
if (fd > 0) closesocket(fd);
|
2005-09-06 21:00:57 +00:00
|
|
|
http_free(http_hdr);
|
2006-03-08 15:07:48 +00:00
|
|
|
if (proxy && server_url)
|
|
|
|
url_free(server_url);
|
2005-09-06 21:00:57 +00:00
|
|
|
return -1;
|
2001-06-04 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HTTP_header_t *
|
|
|
|
http_read_response( int fd ) {
|
|
|
|
HTTP_header_t *http_hdr;
|
|
|
|
char response[BUFFER_SIZE];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
http_hdr = http_new_header();
|
|
|
|
if( http_hdr==NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2009-07-06 23:26:13 +00:00
|
|
|
i = recv( fd, response, BUFFER_SIZE, 0 );
|
2001-06-04 17:52:33 +00:00
|
|
|
if( i<0 ) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Read failed.\n");
|
2001-12-07 07:17:05 +00:00
|
|
|
http_free( http_hdr );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if( i==0 ) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 (i.e. EOF).\n");
|
2001-12-07 07:17:05 +00:00
|
|
|
http_free( http_hdr );
|
|
|
|
return NULL;
|
2001-06-04 17:52:33 +00:00
|
|
|
}
|
|
|
|
http_response_append( http_hdr, response, i );
|
2009-07-06 23:26:13 +00:00
|
|
|
} while( !http_is_header_entire( http_hdr ) );
|
2001-06-04 17:52:33 +00:00
|
|
|
http_response_parse( http_hdr );
|
|
|
|
return http_hdr;
|
|
|
|
}
|
|
|
|
|
2002-06-24 20:12:18 +00:00
|
|
|
int
|
|
|
|
http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
|
|
|
|
char *aut;
|
2002-10-29 09:18:53 +00:00
|
|
|
|
2009-07-06 22:15:02 +00:00
|
|
|
#define MPDEMUX_NW_AuthFailed _(\
|
|
|
|
"Authentication failed. Please use the -user and -passwd options to provide your\n"\
|
|
|
|
"username/password for a list of URLs, or form an URL like:\n"\
|
|
|
|
"http://username:password@hostname/file\n")
|
|
|
|
|
|
|
|
|
2002-06-25 08:04:52 +00:00
|
|
|
if( *auth_retry==1 ) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
|
2002-06-24 20:12:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if( *auth_retry>0 ) {
|
|
|
|
if( url->username ) {
|
|
|
|
free( url->username );
|
|
|
|
url->username = NULL;
|
|
|
|
}
|
|
|
|
if( url->password ) {
|
|
|
|
free( url->password );
|
|
|
|
url->password = NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-06-25 08:04:52 +00:00
|
|
|
|
2002-06-24 20:12:18 +00:00
|
|
|
aut = http_get_field(http_hdr, "WWW-Authenticate");
|
|
|
|
if( aut!=NULL ) {
|
|
|
|
char *aut_space;
|
|
|
|
aut_space = strstr(aut, "realm=");
|
|
|
|
if( aut_space!=NULL ) aut_space += 6;
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required for %s\n", aut_space);
|
2002-06-24 20:12:18 +00:00
|
|
|
} else {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required.\n");
|
2002-06-24 20:12:18 +00:00
|
|
|
}
|
2002-10-23 17:21:01 +00:00
|
|
|
if( network_username ) {
|
|
|
|
url->username = strdup(network_username);
|
2002-06-24 20:12:18 +00:00
|
|
|
if( url->username==NULL ) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
|
2002-06-24 20:12:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
|
2002-06-24 20:12:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-10-23 17:21:01 +00:00
|
|
|
if( network_password ) {
|
|
|
|
url->password = strdup(network_password);
|
2002-06-25 08:04:52 +00:00
|
|
|
if( url->password==NULL ) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
|
2002-06-25 08:04:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_INFO,"No password provided, trying blank password.\n");
|
2002-06-25 08:04:52 +00:00
|
|
|
}
|
2002-06-24 20:12:18 +00:00
|
|
|
(*auth_retry)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-02-17 12:30:45 +00:00
|
|
|
int
|
|
|
|
http_seek( stream_t *stream, off_t pos ) {
|
|
|
|
HTTP_header_t *http_hdr = NULL;
|
|
|
|
int fd;
|
|
|
|
if( stream==NULL ) return 0;
|
|
|
|
|
|
|
|
if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
|
2009-07-06 23:26:13 +00:00
|
|
|
fd = http_send_request( stream->streaming_ctrl->url, pos );
|
2004-02-17 12:30:45 +00:00
|
|
|
if( fd<0 ) return 0;
|
|
|
|
|
|
|
|
http_hdr = http_read_response( fd );
|
|
|
|
|
|
|
|
if( http_hdr==NULL ) return 0;
|
|
|
|
|
2010-02-20 19:23:14 +00:00
|
|
|
if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
|
|
|
|
http_debug_hdr( http_hdr );
|
|
|
|
|
2004-02-17 12:30:45 +00:00
|
|
|
switch( http_hdr->status_code ) {
|
|
|
|
case 200:
|
|
|
|
case 206: // 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;
|
|
|
|
default:
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returns %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
|
2010-02-20 19:22:23 +00:00
|
|
|
closesocket( fd );
|
2004-02-17 12:30:45 +00:00
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
stream->fd = fd;
|
|
|
|
|
|
|
|
if( http_hdr ) {
|
|
|
|
http_free( http_hdr );
|
|
|
|
stream->streaming_ctrl->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->pos=pos;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
int
|
2001-11-20 22:20:20 +00:00
|
|
|
streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
|
2002-01-19 09:04:02 +00:00
|
|
|
//printf("streaming_bufferize\n");
|
2006-07-13 16:41:13 +00:00
|
|
|
streaming_ctrl->buffer = malloc(size);
|
2001-11-20 22:20:20 +00:00
|
|
|
if( streaming_ctrl->buffer==NULL ) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
|
2001-11-20 22:20:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy( streaming_ctrl->buffer, buffer, size );
|
|
|
|
streaming_ctrl->buffer_size = size;
|
2001-12-19 01:59:25 +00:00
|
|
|
return size;
|
2001-11-20 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
|
|
|
|
int len=0;
|
|
|
|
//printf("nop_streaming_read\n");
|
|
|
|
if( stream_ctrl->buffer_size!=0 ) {
|
|
|
|
int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
|
2002-01-19 09:04:02 +00:00
|
|
|
//printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
|
2001-11-20 22:20:20 +00:00
|
|
|
len = (size<buffer_len)?size:buffer_len;
|
|
|
|
memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
|
|
|
|
stream_ctrl->buffer_pos += len;
|
2002-01-19 09:04:02 +00:00
|
|
|
//printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
|
2001-11-20 22:20:20 +00:00
|
|
|
if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
|
|
|
|
free( stream_ctrl->buffer );
|
|
|
|
stream_ctrl->buffer = NULL;
|
|
|
|
stream_ctrl->buffer_size = 0;
|
|
|
|
stream_ctrl->buffer_pos = 0;
|
2002-01-19 09:04:02 +00:00
|
|
|
//printf("buffer cleaned\n");
|
2001-11-20 22:20:20 +00:00
|
|
|
}
|
2002-01-19 09:04:02 +00:00
|
|
|
//printf("read %d bytes from buffer\n", len );
|
2001-11-20 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( len<size ) {
|
2001-12-07 07:17:05 +00:00
|
|
|
int ret;
|
2003-05-29 19:36:58 +00:00
|
|
|
ret = recv( fd, buffer+len, size-len, 0 );
|
2001-12-14 20:45:30 +00:00
|
|
|
if( ret<0 ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
|
2001-12-07 07:17:05 +00:00
|
|
|
}
|
|
|
|
len += ret;
|
2001-11-20 22:20:20 +00:00
|
|
|
//printf("read %d bytes from network\n", len );
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
return len;
|
|
|
|
}
|
2001-05-29 17:03:17 +00:00
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
int
|
2001-11-20 22:20:20 +00:00
|
|
|
nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
|
|
|
|
return -1;
|
2002-10-29 09:18:53 +00:00
|
|
|
// To shut up gcc warning
|
|
|
|
fd++;
|
|
|
|
pos++;
|
|
|
|
stream_ctrl=NULL;
|
2001-11-20 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
2001-12-23 22:09:02 +00:00
|
|
|
|
2005-07-10 14:09:45 +00:00
|
|
|
void fixup_network_stream_cache(stream_t *stream) {
|
2005-05-29 12:54:00 +00:00
|
|
|
if(stream->streaming_ctrl->buffering) {
|
|
|
|
if(stream_cache_size<0) {
|
|
|
|
// cache option not set, will use our computed value.
|
|
|
|
// buffer in KBytes, *5 because the prefill is 20% of the buffer.
|
|
|
|
stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
|
|
|
|
if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
|
|
|
|
}
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Cache size set to %d KBytes\n", stream_cache_size);
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
2001-12-23 22:09:02 +00:00
|
|
|
}
|
|
|
|
|
2001-05-29 17:03:17 +00:00
|
|
|
|
|
|
|
int
|
2001-11-20 22:20:20 +00:00
|
|
|
streaming_stop( stream_t *stream ) {
|
|
|
|
stream->streaming_ctrl->status = streaming_stopped_e;
|
2001-06-04 17:52:33 +00:00
|
|
|
return 0;
|
2001-05-29 17:03:17 +00:00
|
|
|
}
|