Ignore svn change r30495
[mplayer/kovensky.git] / stream / network.c
blob0e1771113fceedff96e3f96629f7045a00ec3d46
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 //#define DUMP2FILE
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include <errno.h>
31 #include <ctype.h>
33 #include "config.h"
35 #include "mp_msg.h"
36 #include "help_mp.h"
38 #if HAVE_WINSOCK2_H
39 #include <winsock2.h>
40 #include <ws2tcpip.h>
41 #endif
43 #include "stream.h"
44 #include "libmpdemux/demuxer.h"
45 #include "m_config.h"
47 #include "network.h"
48 #include "tcp.h"
49 #include "http.h"
50 #include "cookies.h"
51 #include "url.h"
53 #include "version.h"
55 extern int stream_cache_size;
57 /* Variables for the command line option -user, -passwd, -bandwidth,
58 -user-agent and -nocookies */
60 char *network_username=NULL;
61 char *network_password=NULL;
62 int network_bandwidth=0;
63 int network_cookies_enabled = 0;
64 char *network_useragent=NULL;
66 /* IPv6 options */
67 int network_ipv4_only_proxy = 0;
70 const mime_struct_t mime_type_table[] = {
71 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
72 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
73 // MPEG streaming
74 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
75 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
76 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
77 // AVI ??? => video/x-msvideo
78 { "video/x-msvideo", DEMUXER_TYPE_AVI },
79 // MOV => video/quicktime
80 { "video/quicktime", DEMUXER_TYPE_MOV },
81 // ASF
82 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
83 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
84 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
85 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
86 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
87 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
88 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
89 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
90 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
91 // Playlists
92 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
93 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
94 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
95 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
96 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
97 // Real Media
98 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
99 // OGG Streaming
100 { "application/x-ogg", DEMUXER_TYPE_OGG },
101 // NullSoft Streaming Video
102 { "video/nsv", DEMUXER_TYPE_NSV},
103 { "misc/ultravox", DEMUXER_TYPE_NSV},
104 #ifdef CONFIG_LIBAVFORMAT
105 // Flash Video
106 { "video/x-flv", DEMUXER_TYPE_LAVF},
107 #endif
108 { NULL, DEMUXER_TYPE_UNKNOWN},
112 streaming_ctrl_t *
113 streaming_ctrl_new(void) {
114 streaming_ctrl_t *streaming_ctrl;
115 streaming_ctrl = malloc(sizeof(streaming_ctrl_t));
116 if( streaming_ctrl==NULL ) {
117 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
118 return NULL;
120 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) );
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 if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer );
129 if( streaming_ctrl->data ) 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 = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port
170 new_url = malloc( len+1 );
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 sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url );
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 http_set_uri( http_hdr, server_url->url );
207 } else {
208 server_url = url;
209 http_set_uri( http_hdr, server_url->file );
211 if (server_url->port && server_url->port != 80)
212 snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
213 else
214 snprintf(str, 256, "Host: %s", server_url->hostname );
215 http_set_field( http_hdr, str);
216 if (network_useragent)
218 snprintf(str, 256, "User-Agent: %s", network_useragent);
219 http_set_field(http_hdr, str);
221 else
222 http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION);
224 if( strcasecmp(url->protocol, "noicyx") )
225 http_set_field(http_hdr, "Icy-MetaData: 1");
227 if(pos>0) {
228 // Extend http_send_request with possibility to do partial content retrieval
229 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
230 http_set_field(http_hdr, str);
233 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
235 http_set_field( http_hdr, "Connection: close");
236 http_add_basic_authentication( http_hdr, url->username, url->password );
237 if( http_build_request( http_hdr )==NULL ) {
238 goto err_out;
241 if( proxy ) {
242 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
243 fd = connect2Server( url->hostname, url->port,1 );
244 url_free( server_url );
245 server_url = NULL;
246 } else {
247 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
248 fd = connect2Server( server_url->hostname, server_url->port,1 );
250 if( fd<0 ) {
251 goto err_out;
253 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
255 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
256 if( ret!=(int)http_hdr->buffer_size ) {
257 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while sending HTTP request: Didn't send all the request.\n");
258 goto err_out;
261 http_free( http_hdr );
263 return fd;
264 err_out:
265 if (fd > 0) closesocket(fd);
266 http_free(http_hdr);
267 if (proxy && server_url)
268 url_free(server_url);
269 return -1;
272 HTTP_header_t *
273 http_read_response( int fd ) {
274 HTTP_header_t *http_hdr;
275 char response[BUFFER_SIZE];
276 int i;
278 http_hdr = http_new_header();
279 if( http_hdr==NULL ) {
280 return NULL;
283 do {
284 i = recv( fd, response, BUFFER_SIZE, 0 );
285 if( i<0 ) {
286 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Read failed.\n");
287 http_free( http_hdr );
288 return NULL;
290 if( i==0 ) {
291 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 (i.e. EOF).\n");
292 http_free( http_hdr );
293 return NULL;
295 http_response_append( http_hdr, response, i );
296 } while( !http_is_header_entire( http_hdr ) );
297 http_response_parse( http_hdr );
298 return http_hdr;
302 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
303 char *aut;
305 #define MPDEMUX_NW_AuthFailed _(\
306 "Authentication failed. Please use the -user and -passwd options to provide your\n"\
307 "username/password for a list of URLs, or form an URL like:\n"\
308 "http://username:password@hostname/file\n")
311 if( *auth_retry==1 ) {
312 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
313 return -1;
315 if( *auth_retry>0 ) {
316 if( url->username ) {
317 free( url->username );
318 url->username = NULL;
320 if( url->password ) {
321 free( url->password );
322 url->password = NULL;
326 aut = http_get_field(http_hdr, "WWW-Authenticate");
327 if( aut!=NULL ) {
328 char *aut_space;
329 aut_space = strstr(aut, "realm=");
330 if( aut_space!=NULL ) aut_space += 6;
331 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required for %s\n", aut_space);
332 } else {
333 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required.\n");
335 if( network_username ) {
336 url->username = strdup(network_username);
337 if( url->username==NULL ) {
338 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
339 return -1;
341 } else {
342 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
343 return -1;
345 if( network_password ) {
346 url->password = strdup(network_password);
347 if( url->password==NULL ) {
348 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
349 return -1;
351 } else {
352 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"No password provided, trying blank password.\n");
354 (*auth_retry)++;
355 return 0;
359 http_seek( stream_t *stream, off_t pos ) {
360 HTTP_header_t *http_hdr = NULL;
361 int fd;
362 if( stream==NULL ) return 0;
364 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
365 fd = http_send_request( stream->streaming_ctrl->url, pos );
366 if( fd<0 ) return 0;
368 http_hdr = http_read_response( fd );
370 if( http_hdr==NULL ) return 0;
372 switch( http_hdr->status_code ) {
373 case 200:
374 case 206: // OK
375 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
376 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
377 if( http_hdr->body_size>0 ) {
378 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
379 http_free( http_hdr );
380 return -1;
383 break;
384 default:
385 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returns %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
386 close( fd );
387 fd = -1;
389 stream->fd = fd;
391 if( http_hdr ) {
392 http_free( http_hdr );
393 stream->streaming_ctrl->data = NULL;
396 stream->pos=pos;
398 return 1;
403 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
404 //printf("streaming_bufferize\n");
405 streaming_ctrl->buffer = malloc(size);
406 if( streaming_ctrl->buffer==NULL ) {
407 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
408 return -1;
410 memcpy( streaming_ctrl->buffer, buffer, size );
411 streaming_ctrl->buffer_size = size;
412 return size;
416 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
417 int len=0;
418 //printf("nop_streaming_read\n");
419 if( stream_ctrl->buffer_size!=0 ) {
420 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
421 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
422 len = (size<buffer_len)?size:buffer_len;
423 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
424 stream_ctrl->buffer_pos += len;
425 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
426 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
427 free( stream_ctrl->buffer );
428 stream_ctrl->buffer = NULL;
429 stream_ctrl->buffer_size = 0;
430 stream_ctrl->buffer_pos = 0;
431 //printf("buffer cleaned\n");
433 //printf("read %d bytes from buffer\n", len );
436 if( len<size ) {
437 int ret;
438 ret = recv( fd, buffer+len, size-len, 0 );
439 if( ret<0 ) {
440 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
442 len += ret;
443 //printf("read %d bytes from network\n", len );
446 return len;
450 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
451 return -1;
452 // To shut up gcc warning
453 fd++;
454 pos++;
455 stream_ctrl=NULL;
459 void fixup_network_stream_cache(stream_t *stream) {
460 if(stream->streaming_ctrl->buffering) {
461 if(stream_cache_size<0) {
462 // cache option not set, will use our computed value.
463 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
464 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
465 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
467 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Cache size set to %d KBytes\n", stream_cache_size);
473 streaming_stop( stream_t *stream ) {
474 stream->streaming_ctrl->status = streaming_stopped_e;
475 return 0;