ao: fix crash after ao init failure (from recent 3a5fd15fa2)
[mplayer/greg.git] / stream / network.c
blobb722023061fe8af2745780c3698237247a71fe16
1 /*
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.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include <errno.h>
29 #include <ctype.h>
31 #include "config.h"
33 #include "mp_msg.h"
35 #if HAVE_WINSOCK2_H
36 #include <winsock2.h>
37 #include <ws2tcpip.h>
38 #endif
40 #include "stream.h"
41 #include "libmpdemux/demuxer.h"
42 #include "m_config.h"
43 #include "mpcommon.h"
44 #include "network.h"
45 #include "tcp.h"
46 #include "http.h"
47 #include "cookies.h"
48 #include "url.h"
50 extern int stream_cache_size;
52 /* Variables for the command line option -user, -passwd, -bandwidth,
53 -user-agent and -nocookies */
55 char *network_username=NULL;
56 char *network_password=NULL;
57 int network_bandwidth=0;
58 int network_cookies_enabled = 0;
59 char *network_useragent=NULL;
60 char *network_referrer=NULL;
61 char **network_http_header_fields=NULL;
63 /* IPv6 options */
64 int network_ipv4_only_proxy = 0;
67 const mime_struct_t mime_type_table[] = {
68 #ifdef CONFIG_FFMPEG
69 // Flash Video
70 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
71 // do not force any demuxer in this case!
72 // we want the lavf demuxer to be tried first (happens automatically anyway),
73 // but for mov reference files to work we must also try
74 // the native demuxer if lavf fails.
75 { "video/quicktime", 0 },
76 #endif
77 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
78 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
79 // MPEG streaming
80 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
81 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
82 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
83 // AVI ??? => video/x-msvideo
84 { "video/x-msvideo", DEMUXER_TYPE_AVI },
85 // MOV => video/quicktime
86 { "video/quicktime", DEMUXER_TYPE_MOV },
87 // ASF
88 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
89 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
90 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
91 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
92 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
93 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
94 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
95 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
96 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
97 // Playlists
98 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
99 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
100 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
101 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
102 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
103 // Real Media
104 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
105 // OGG Streaming
106 { "application/x-ogg", DEMUXER_TYPE_OGG },
107 // NullSoft Streaming Video
108 { "video/nsv", DEMUXER_TYPE_NSV},
109 { "misc/ultravox", DEMUXER_TYPE_NSV},
110 { NULL, DEMUXER_TYPE_UNKNOWN},
114 streaming_ctrl_t *
115 streaming_ctrl_new(void) {
116 streaming_ctrl_t *streaming_ctrl = calloc(1, sizeof(*streaming_ctrl));
117 if( streaming_ctrl==NULL ) {
118 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
119 return NULL;
121 return streaming_ctrl;
124 void
125 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
126 if( streaming_ctrl==NULL ) return;
127 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
128 free(streaming_ctrl->buffer);
129 free(streaming_ctrl->data);
130 free(streaming_ctrl);
133 URL_t*
134 check4proxies( URL_t *url ) {
135 URL_t *url_out = NULL;
136 if( url==NULL ) return NULL;
137 url_out = url_new( url->url );
138 if( !strcasecmp(url->protocol, "http_proxy") ) {
139 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
140 return url_out;
142 // Check if the http_proxy environment variable is set.
143 if( !strcasecmp(url->protocol, "http") ) {
144 char *proxy;
145 proxy = getenv("http_proxy");
146 if( proxy!=NULL ) {
147 // We got a proxy, build the URL to use it
148 int len;
149 char *new_url;
150 URL_t *tmp_url;
151 URL_t *proxy_url = url_new( proxy );
153 if( proxy_url==NULL ) {
154 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
155 "Invalid proxy setting... Trying without proxy.\n");
156 return url_out;
159 #ifdef HAVE_AF_INET6
160 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
161 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
162 "Could not resolve remote hostname for AF_INET. Trying without proxy.\n");
163 url_free(proxy_url);
164 return url_out;
166 #endif
168 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
169 len = make_http_proxy_url(proxy_url, url->url, NULL, 0) + 1;
170 new_url = malloc(len);
171 if( new_url==NULL ) {
172 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
173 url_free(proxy_url);
174 return url_out;
176 make_http_proxy_url(proxy_url, url->url, new_url, len);
177 tmp_url = url_new( new_url );
178 if( tmp_url==NULL ) {
179 free( new_url );
180 url_free( proxy_url );
181 return url_out;
183 url_free( url_out );
184 url_out = tmp_url;
185 free( new_url );
186 url_free( proxy_url );
189 return url_out;
193 http_send_request( URL_t *url, off_t pos ) {
194 HTTP_header_t *http_hdr;
195 URL_t *server_url;
196 char str[256];
197 int fd = -1;
198 int ret;
199 int proxy = 0; // Boolean
201 http_hdr = http_new_header();
203 if( !strcasecmp(url->protocol, "http_proxy") ) {
204 proxy = 1;
205 server_url = url_new( (url->file)+1 );
206 if (!server_url) {
207 mp_msg(MSGT_NETWORK, MSGL_ERR, "Invalid URL '%s' to proxify\n", url->file+1);
208 goto err_out;
210 http_set_uri( http_hdr, server_url->noauth_url );
211 } else {
212 server_url = url;
213 http_set_uri( http_hdr, server_url->file );
215 if (server_url->port && server_url->port != 80)
216 snprintf(str, sizeof(str), "Host: %s:%d", server_url->hostname, server_url->port );
217 else
218 snprintf(str, sizeof(str), "Host: %s", server_url->hostname );
219 http_set_field( http_hdr, str);
220 if (network_useragent)
221 snprintf(str, sizeof(str), "User-Agent: %s", network_useragent);
222 else
223 snprintf(str, sizeof(str), "User-Agent: %s", mplayer_version);
224 http_set_field(http_hdr, str);
226 if (network_referrer) {
227 char *referrer = NULL;
228 size_t len = strlen(network_referrer) + 10;
230 // Check len to ensure we don't do something really bad in case of an overflow
231 if (len > 10)
232 referrer = malloc(len);
234 if (referrer == NULL) {
235 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Memory allocation failed.\n");
236 } else {
237 snprintf(referrer, len, "Referer: %s", network_referrer);
238 http_set_field(http_hdr, referrer);
239 free(referrer);
243 if( strcasecmp(url->protocol, "noicyx") )
244 http_set_field(http_hdr, "Icy-MetaData: 1");
246 if(pos>0) {
247 // Extend http_send_request with possibility to do partial content retrieval
248 snprintf(str, sizeof(str), "Range: bytes=%"PRId64"-", (int64_t)pos);
249 http_set_field(http_hdr, str);
252 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
254 if (network_http_header_fields) {
255 int i=0;
256 while (network_http_header_fields[i])
257 http_set_field(http_hdr, network_http_header_fields[i++]);
260 http_set_field( http_hdr, "Connection: close");
261 if (proxy)
262 http_add_basic_proxy_authentication(http_hdr, url->username, url->password);
263 http_add_basic_authentication(http_hdr, server_url->username, server_url->password);
264 if( http_build_request( http_hdr )==NULL ) {
265 goto err_out;
268 if( proxy ) {
269 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
270 fd = connect2Server( url->hostname, url->port,1 );
271 url_free( server_url );
272 server_url = NULL;
273 } else {
274 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
275 fd = connect2Server( server_url->hostname, server_url->port,1 );
277 if( fd<0 ) {
278 goto err_out;
280 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
282 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, DEFAULT_SEND_FLAGS );
283 if( ret!=(int)http_hdr->buffer_size ) {
284 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while sending HTTP request: Didn't send all the request.\n");
285 goto err_out;
288 http_free( http_hdr );
290 return fd;
291 err_out:
292 if (fd > 0) closesocket(fd);
293 http_free(http_hdr);
294 if (proxy && server_url)
295 url_free(server_url);
296 return -1;
299 HTTP_header_t *
300 http_read_response( int fd ) {
301 HTTP_header_t *http_hdr;
302 char response[BUFFER_SIZE];
303 int i;
305 http_hdr = http_new_header();
306 if( http_hdr==NULL ) {
307 return NULL;
310 do {
311 i = recv( fd, response, BUFFER_SIZE, 0 );
312 if( i<0 ) {
313 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Read failed.\n");
314 http_free( http_hdr );
315 return NULL;
317 if( i==0 ) {
318 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 (i.e. EOF).\n");
319 http_free( http_hdr );
320 return NULL;
322 http_response_append( http_hdr, response, i );
323 } while( !http_is_header_entire( http_hdr ) );
324 if (http_response_parse( http_hdr ) < 0) {
325 http_free( http_hdr );
326 return NULL;
328 return http_hdr;
332 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
333 char *aut;
335 #define MPDEMUX_NW_AuthFailed _(\
336 "Authentication failed. Please use the -user and -passwd options to provide your\n"\
337 "username/password for a list of URLs, or form an URL like:\n"\
338 "http://username:password@hostname/file\n")
341 if( *auth_retry==1 ) {
342 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
343 return -1;
345 if( *auth_retry>0 ) {
346 free(url->username);
347 url->username = NULL;
348 free(url->password);
349 url->password = NULL;
352 aut = http_get_field(http_hdr, "WWW-Authenticate");
353 if( aut!=NULL ) {
354 char *aut_space;
355 aut_space = strstr(aut, "realm=");
356 if( aut_space!=NULL ) aut_space += 6;
357 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required for %s\n", aut_space);
358 } else {
359 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required.\n");
361 if( network_username ) {
362 url->username = strdup(network_username);
363 if( url->username==NULL ) {
364 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
365 return -1;
367 } else {
368 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
369 return -1;
371 if( network_password ) {
372 url->password = strdup(network_password);
373 if( url->password==NULL ) {
374 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
375 return -1;
377 } else {
378 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"No password provided, trying blank password.\n");
380 (*auth_retry)++;
381 return 0;
385 http_seek( stream_t *stream, off_t pos ) {
386 HTTP_header_t *http_hdr = NULL;
387 int fd;
388 if( stream==NULL ) return 0;
390 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
391 fd = http_send_request( stream->streaming_ctrl->url, pos );
392 if( fd<0 ) return 0;
394 http_hdr = http_read_response( fd );
396 if( http_hdr==NULL ) return 0;
398 if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
399 http_debug_hdr( http_hdr );
401 switch( http_hdr->status_code ) {
402 case 200:
403 case 206: // OK
404 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
405 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
406 if( http_hdr->body_size>0 ) {
407 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
408 http_free( http_hdr );
409 return -1;
412 break;
413 default:
414 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returns %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
415 closesocket( fd );
416 fd = -1;
418 stream->fd = fd;
420 if( http_hdr ) {
421 http_free( http_hdr );
422 stream->streaming_ctrl->data = NULL;
425 stream->pos=pos;
427 return 1;
432 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
433 //printf("streaming_bufferize\n");
434 streaming_ctrl->buffer = malloc(size);
435 if( streaming_ctrl->buffer==NULL ) {
436 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
437 return -1;
439 memcpy( streaming_ctrl->buffer, buffer, size );
440 streaming_ctrl->buffer_size = size;
441 return size;
445 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
446 int len=0;
447 //printf("nop_streaming_read\n");
448 if( stream_ctrl->buffer_size!=0 ) {
449 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
450 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
451 len = (size<buffer_len)?size:buffer_len;
452 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
453 stream_ctrl->buffer_pos += len;
454 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
455 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
456 free( stream_ctrl->buffer );
457 stream_ctrl->buffer = NULL;
458 stream_ctrl->buffer_size = 0;
459 stream_ctrl->buffer_pos = 0;
460 //printf("buffer cleaned\n");
462 //printf("read %d bytes from buffer\n", len );
465 if( len<size ) {
466 int ret;
467 ret = recv( fd, buffer+len, size-len, 0 );
468 if( ret<0 ) {
469 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
471 len += ret;
472 //printf("read %d bytes from network\n", len );
475 return len;
479 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
480 return -1;
484 void fixup_network_stream_cache(stream_t *stream) {
485 if(stream->streaming_ctrl->buffering) {
486 if(stream_cache_size<0) {
487 // cache option not set, will use our computed value.
488 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
489 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
490 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
492 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Cache size set to %d KBytes\n", stream_cache_size);