2 * Network layer for MPlayer
4 * Copyright (C) 2001 Bertrand Baudet <bertrand_baudet@yahoo.com>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
42 #include "libmpdemux/demuxer.h"
50 /* Variables for the command line option -user, -passwd, -bandwidth,
51 -user-agent and -nocookies */
53 char *network_username
=NULL
;
54 char *network_password
=NULL
;
55 int network_bandwidth
=0;
56 int network_cookies_enabled
= 0;
57 char *network_useragent
=NULL
;
58 char *network_referrer
=NULL
;
59 char **network_http_header_fields
=NULL
;
62 int network_ipv4_only_proxy
= 0;
65 const mime_struct_t mime_type_table
[] = {
67 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED
},
68 // do not force any demuxer in this case!
69 // we want the lavf demuxer to be tried first (happens automatically anyway),
70 // but for mov reference files to work we must also try
71 // the native demuxer if lavf fails.
72 { "video/quicktime", 0 },
73 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
74 { "audio/mpeg", DEMUXER_TYPE_AUDIO
},
76 { "video/mpeg", DEMUXER_TYPE_UNKNOWN
},
77 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN
},
78 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN
},
79 // AVI ??? => video/x-msvideo
80 { "video/x-msvideo", DEMUXER_TYPE_AVI
},
81 // MOV => video/quicktime
82 { "video/quicktime", DEMUXER_TYPE_MOV
},
84 { "audio/x-ms-wax", DEMUXER_TYPE_ASF
},
85 { "audio/x-ms-wma", DEMUXER_TYPE_ASF
},
86 { "video/x-ms-asf", DEMUXER_TYPE_ASF
},
87 { "video/x-ms-afs", DEMUXER_TYPE_ASF
},
88 { "video/x-ms-wmv", DEMUXER_TYPE_ASF
},
89 { "video/x-ms-wma", DEMUXER_TYPE_ASF
},
90 { "application/x-mms-framed", DEMUXER_TYPE_ASF
},
91 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF
},
92 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN
},
94 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST
},
95 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST
},
96 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST
},
97 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST
},
98 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST
},
100 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
102 { "application/x-ogg", DEMUXER_TYPE_OGG
},
103 // NullSoft Streaming Video
104 { "video/nsv", DEMUXER_TYPE_NSV
},
105 { "misc/ultravox", DEMUXER_TYPE_NSV
},
106 { NULL
, DEMUXER_TYPE_UNKNOWN
},
111 streaming_ctrl_new(void) {
112 streaming_ctrl_t
*streaming_ctrl
= calloc(1, sizeof(*streaming_ctrl
));
113 if( streaming_ctrl
==NULL
) {
114 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
117 return streaming_ctrl
;
121 streaming_ctrl_free( streaming_ctrl_t
*streaming_ctrl
) {
122 if( streaming_ctrl
==NULL
) return;
123 if( streaming_ctrl
->url
) url_free( streaming_ctrl
->url
);
124 free(streaming_ctrl
->buffer
);
125 free(streaming_ctrl
->data
);
126 free(streaming_ctrl
);
130 check4proxies( URL_t
*url
) {
131 URL_t
*url_out
= NULL
;
132 if( url
==NULL
) return NULL
;
133 url_out
= url_new( url
->url
);
134 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
135 mp_msg(MSGT_NETWORK
,MSGL_V
,"Using HTTP proxy: http://%s:%d\n", url
->hostname
, url
->port
);
138 // Check if the http_proxy environment variable is set.
139 if( !strcasecmp(url
->protocol
, "http") ) {
141 proxy
= getenv("http_proxy");
143 // We got a proxy, build the URL to use it
147 URL_t
*proxy_url
= url_new( proxy
);
149 if( proxy_url
==NULL
) {
150 mp_tmsg(MSGT_NETWORK
,MSGL_WARN
,
151 "Invalid proxy setting... Trying without proxy.\n");
156 if (network_ipv4_only_proxy
&& (gethostbyname(url
->hostname
)==NULL
)) {
157 mp_tmsg(MSGT_NETWORK
,MSGL_WARN
,
158 "Could not resolve remote hostname for AF_INET. Trying without proxy.\n");
164 mp_msg(MSGT_NETWORK
,MSGL_V
,"Using HTTP proxy: %s\n", proxy_url
->url
);
165 len
= make_http_proxy_url(proxy_url
, url
->url
, NULL
, 0) + 1;
166 new_url
= malloc(len
);
167 if( new_url
==NULL
) {
168 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
172 make_http_proxy_url(proxy_url
, url
->url
, new_url
, len
);
173 tmp_url
= url_new( new_url
);
174 if( tmp_url
==NULL
) {
176 url_free( proxy_url
);
182 url_free( proxy_url
);
189 http_send_request( URL_t
*url
, off_t pos
) {
190 HTTP_header_t
*http_hdr
;
195 int proxy
= 0; // Boolean
197 http_hdr
= http_new_header();
199 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
201 server_url
= url_new( (url
->file
)+1 );
203 mp_msg(MSGT_NETWORK
, MSGL_ERR
, "Invalid URL '%s' to proxify\n", url
->file
+1);
206 http_set_uri( http_hdr
, server_url
->noauth_url
);
209 http_set_uri( http_hdr
, server_url
->file
);
211 if (server_url
->port
&& server_url
->port
!= 80)
212 snprintf(str
, sizeof(str
), "Host: %s:%d", server_url
->hostname
, server_url
->port
);
214 snprintf(str
, sizeof(str
), "Host: %s", server_url
->hostname
);
215 http_set_field( http_hdr
, str
);
216 if (network_useragent
)
217 snprintf(str
, sizeof(str
), "User-Agent: %s", network_useragent
);
219 snprintf(str
, sizeof(str
), "User-Agent: %s", mplayer_version
);
220 http_set_field(http_hdr
, str
);
222 if (network_referrer
) {
223 char *referrer
= NULL
;
224 size_t len
= strlen(network_referrer
) + 10;
226 // Check len to ensure we don't do something really bad in case of an overflow
228 referrer
= malloc(len
);
230 if (referrer
== NULL
) {
231 mp_tmsg(MSGT_NETWORK
, MSGL_FATAL
, "Memory allocation failed.\n");
233 snprintf(referrer
, len
, "Referer: %s", network_referrer
);
234 http_set_field(http_hdr
, referrer
);
239 if( strcasecmp(url
->protocol
, "noicyx") )
240 http_set_field(http_hdr
, "Icy-MetaData: 1");
243 // Extend http_send_request with possibility to do partial content retrieval
244 snprintf(str
, sizeof(str
), "Range: bytes=%"PRId64
"-", (int64_t)pos
);
245 http_set_field(http_hdr
, str
);
248 if (network_cookies_enabled
) cookies_set( http_hdr
, server_url
->hostname
, server_url
->url
);
250 if (network_http_header_fields
) {
252 while (network_http_header_fields
[i
])
253 http_set_field(http_hdr
, network_http_header_fields
[i
++]);
256 http_set_field( http_hdr
, "Connection: close");
258 http_add_basic_proxy_authentication(http_hdr
, url
->username
, url
->password
);
259 http_add_basic_authentication(http_hdr
, server_url
->username
, server_url
->password
);
260 if( http_build_request( http_hdr
)==NULL
) {
265 if( url
->port
==0 ) url
->port
= 8080; // Default port for the proxy server
266 fd
= connect2Server( url
->hostname
, url
->port
,1 );
267 url_free( server_url
);
270 if( server_url
->port
==0 ) server_url
->port
= 80; // Default port for the web server
271 fd
= connect2Server( server_url
->hostname
, server_url
->port
,1 );
276 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request: [%s]\n", http_hdr
->buffer
);
278 ret
= send( fd
, http_hdr
->buffer
, http_hdr
->buffer_size
, DEFAULT_SEND_FLAGS
);
279 if( ret
!=(int)http_hdr
->buffer_size
) {
280 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error while sending HTTP request: Didn't send all the request.\n");
284 http_free( http_hdr
);
288 if (fd
> 0) closesocket(fd
);
290 if (proxy
&& server_url
)
291 url_free(server_url
);
296 http_read_response( int fd
) {
297 HTTP_header_t
*http_hdr
;
298 char response
[BUFFER_SIZE
];
301 http_hdr
= http_new_header();
302 if( http_hdr
==NULL
) {
307 i
= recv( fd
, response
, BUFFER_SIZE
, 0 );
309 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Read failed.\n");
310 http_free( http_hdr
);
314 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"http_read_response read 0 (i.e. EOF).\n");
315 http_free( http_hdr
);
318 http_response_append( http_hdr
, response
, i
);
319 } while( !http_is_header_entire( http_hdr
) );
320 if (http_response_parse( http_hdr
) < 0) {
321 http_free( http_hdr
);
328 http_authenticate(HTTP_header_t
*http_hdr
, URL_t
*url
, int *auth_retry
) {
331 #define MPDEMUX_NW_AuthFailed _(\
332 "Authentication failed. Please use the -user and -passwd options to provide your\n"\
333 "username/password for a list of URLs, or form an URL like:\n"\
334 "http://username:password@hostname/file\n")
337 if( *auth_retry
==1 ) {
338 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,MPDEMUX_NW_AuthFailed
);
341 if( *auth_retry
>0 ) {
343 url
->username
= NULL
;
345 url
->password
= NULL
;
348 aut
= http_get_field(http_hdr
, "WWW-Authenticate");
351 aut_space
= strstr(aut
, "realm=");
352 if( aut_space
!=NULL
) aut_space
+= 6;
353 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"Authentication required for %s\n", aut_space
);
355 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"Authentication required.\n");
357 if( network_username
) {
358 url
->username
= strdup(network_username
);
359 if( url
->username
==NULL
) {
360 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
364 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,MPDEMUX_NW_AuthFailed
);
367 if( network_password
) {
368 url
->password
= strdup(network_password
);
369 if( url
->password
==NULL
) {
370 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
374 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"No password provided, trying blank password.\n");
381 http_seek( stream_t
*stream
, off_t pos
) {
382 HTTP_header_t
*http_hdr
= NULL
;
384 if( stream
==NULL
) return 0;
386 if( stream
->fd
>0 ) closesocket(stream
->fd
); // need to reconnect to seek in http-stream
387 fd
= http_send_request( stream
->streaming_ctrl
->url
, pos
);
390 http_hdr
= http_read_response( fd
);
392 if( http_hdr
==NULL
) return 0;
394 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) )
395 http_debug_hdr( http_hdr
);
397 switch( http_hdr
->status_code
) {
400 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
401 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
402 if( http_hdr
->body_size
>0 ) {
403 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
404 http_free( http_hdr
);
410 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Server returns %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
417 http_free( http_hdr
);
418 stream
->streaming_ctrl
->data
= NULL
;
428 streaming_bufferize( streaming_ctrl_t
*streaming_ctrl
, char *buffer
, int size
) {
429 //printf("streaming_bufferize\n");
430 streaming_ctrl
->buffer
= malloc(size
);
431 if( streaming_ctrl
->buffer
==NULL
) {
432 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
435 memcpy( streaming_ctrl
->buffer
, buffer
, size
);
436 streaming_ctrl
->buffer_size
= size
;
441 nop_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*stream_ctrl
) {
443 //printf("nop_streaming_read\n");
444 if( stream_ctrl
->buffer_size
!=0 ) {
445 int buffer_len
= stream_ctrl
->buffer_size
-stream_ctrl
->buffer_pos
;
446 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
447 len
= (size
<buffer_len
)?size
:buffer_len
;
448 memcpy( buffer
, (stream_ctrl
->buffer
)+(stream_ctrl
->buffer_pos
), len
);
449 stream_ctrl
->buffer_pos
+= len
;
450 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
451 if( stream_ctrl
->buffer_pos
>=stream_ctrl
->buffer_size
) {
452 free( stream_ctrl
->buffer
);
453 stream_ctrl
->buffer
= NULL
;
454 stream_ctrl
->buffer_size
= 0;
455 stream_ctrl
->buffer_pos
= 0;
456 //printf("buffer cleaned\n");
458 //printf("read %d bytes from buffer\n", len );
463 ret
= recv( fd
, buffer
+len
, size
-len
, 0 );
465 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_read error : %s\n",strerror(errno
));
468 stream_ctrl
->status
= streaming_stopped_e
;
470 //printf("read %d bytes from network\n", len );
477 nop_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*stream_ctrl
) {