sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / stream / network.c
blobf9caf16720f71f578094a8daa7dcc207b46fa4c4
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 #ifdef CONFIG_LIBAVFORMAT
72 // Flash Video
73 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
74 { "video/quicktime", DEMUXER_TYPE_LAVF_PREFERRED },
75 #endif
76 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
77 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
78 // MPEG streaming
79 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
80 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
81 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
82 // AVI ??? => video/x-msvideo
83 { "video/x-msvideo", DEMUXER_TYPE_AVI },
84 // MOV => video/quicktime
85 { "video/quicktime", DEMUXER_TYPE_MOV },
86 // ASF
87 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
88 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
89 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
90 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
91 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
92 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
93 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
94 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
95 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
96 // Playlists
97 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
98 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
99 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
100 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
101 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
102 // Real Media
103 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
104 // OGG Streaming
105 { "application/x-ogg", DEMUXER_TYPE_OGG },
106 // NullSoft Streaming Video
107 { "video/nsv", DEMUXER_TYPE_NSV},
108 { "misc/ultravox", DEMUXER_TYPE_NSV},
109 { NULL, DEMUXER_TYPE_UNKNOWN},
113 streaming_ctrl_t *
114 streaming_ctrl_new(void) {
115 streaming_ctrl_t *streaming_ctrl;
116 streaming_ctrl = malloc(sizeof(streaming_ctrl_t));
117 if( streaming_ctrl==NULL ) {
118 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
119 return NULL;
121 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) );
122 return streaming_ctrl;
125 void
126 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
127 if( streaming_ctrl==NULL ) return;
128 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
129 if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer );
130 if( streaming_ctrl->data ) free( streaming_ctrl->data );
131 free( streaming_ctrl );
134 URL_t*
135 check4proxies( URL_t *url ) {
136 URL_t *url_out = NULL;
137 if( url==NULL ) return NULL;
138 url_out = url_new( url->url );
139 if( !strcasecmp(url->protocol, "http_proxy") ) {
140 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
141 return url_out;
143 // Check if the http_proxy environment variable is set.
144 if( !strcasecmp(url->protocol, "http") ) {
145 char *proxy;
146 proxy = getenv("http_proxy");
147 if( proxy!=NULL ) {
148 // We got a proxy, build the URL to use it
149 int len;
150 char *new_url;
151 URL_t *tmp_url;
152 URL_t *proxy_url = url_new( proxy );
154 if( proxy_url==NULL ) {
155 mp_msg(MSGT_NETWORK,MSGL_WARN,
156 MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout);
157 return url_out;
160 #ifdef HAVE_AF_INET6
161 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
162 mp_msg(MSGT_NETWORK,MSGL_WARN,
163 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy);
164 url_free(proxy_url);
165 return url_out;
167 #endif
169 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
170 len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port
171 new_url = malloc( len+1 );
172 if( new_url==NULL ) {
173 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
174 url_free(proxy_url);
175 return url_out;
177 sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url );
178 tmp_url = url_new( new_url );
179 if( tmp_url==NULL ) {
180 free( new_url );
181 url_free( proxy_url );
182 return url_out;
184 url_free( url_out );
185 url_out = tmp_url;
186 free( new_url );
187 url_free( proxy_url );
190 return url_out;
194 http_send_request( URL_t *url, off_t pos ) {
195 HTTP_header_t *http_hdr;
196 URL_t *server_url;
197 char str[256];
198 int fd = -1;
199 int ret;
200 int proxy = 0; // Boolean
202 http_hdr = http_new_header();
204 if( !strcasecmp(url->protocol, "http_proxy") ) {
205 proxy = 1;
206 server_url = url_new( (url->file)+1 );
207 http_set_uri( http_hdr, server_url->url );
208 } else {
209 server_url = url;
210 http_set_uri( http_hdr, server_url->file );
212 if (server_url->port && server_url->port != 80)
213 snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
214 else
215 snprintf(str, 256, "Host: %s", server_url->hostname );
216 http_set_field( http_hdr, str);
217 if (network_useragent)
219 snprintf(str, 256, "User-Agent: %s", network_useragent);
220 http_set_field(http_hdr, str);
222 else
223 http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION);
225 if( strcasecmp(url->protocol, "noicyx") )
226 http_set_field(http_hdr, "Icy-MetaData: 1");
228 if(pos>0) {
229 // Extend http_send_request with possibility to do partial content retrieval
230 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
231 http_set_field(http_hdr, str);
234 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
236 http_set_field( http_hdr, "Connection: close");
237 http_add_basic_authentication( http_hdr, url->username, url->password );
238 if( http_build_request( http_hdr )==NULL ) {
239 goto err_out;
242 if( proxy ) {
243 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
244 fd = connect2Server( url->hostname, url->port,1 );
245 url_free( server_url );
246 server_url = NULL;
247 } else {
248 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
249 fd = connect2Server( server_url->hostname, server_url->port,1 );
251 if( fd<0 ) {
252 goto err_out;
254 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
256 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
257 if( ret!=(int)http_hdr->buffer_size ) {
258 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
259 goto err_out;
262 http_free( http_hdr );
264 return fd;
265 err_out:
266 if (fd > 0) closesocket(fd);
267 http_free(http_hdr);
268 if (proxy && server_url)
269 url_free(server_url);
270 return -1;
273 HTTP_header_t *
274 http_read_response( int fd ) {
275 HTTP_header_t *http_hdr;
276 char response[BUFFER_SIZE];
277 int i;
279 http_hdr = http_new_header();
280 if( http_hdr==NULL ) {
281 return NULL;
284 do {
285 i = recv( fd, response, BUFFER_SIZE, 0 );
286 if( i<0 ) {
287 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed);
288 http_free( http_hdr );
289 return NULL;
291 if( i==0 ) {
292 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
293 http_free( http_hdr );
294 return NULL;
296 http_response_append( http_hdr, response, i );
297 } while( !http_is_header_entire( http_hdr ) );
298 http_response_parse( http_hdr );
299 return http_hdr;
303 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
304 char *aut;
306 if( *auth_retry==1 ) {
307 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
308 return -1;
310 if( *auth_retry>0 ) {
311 if( url->username ) {
312 free( url->username );
313 url->username = NULL;
315 if( url->password ) {
316 free( url->password );
317 url->password = NULL;
321 aut = http_get_field(http_hdr, "WWW-Authenticate");
322 if( aut!=NULL ) {
323 char *aut_space;
324 aut_space = strstr(aut, "realm=");
325 if( aut_space!=NULL ) aut_space += 6;
326 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequiredFor, aut_space);
327 } else {
328 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequired);
330 if( network_username ) {
331 url->username = strdup(network_username);
332 if( url->username==NULL ) {
333 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
334 return -1;
336 } else {
337 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
338 return -1;
340 if( network_password ) {
341 url->password = strdup(network_password);
342 if( url->password==NULL ) {
343 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
344 return -1;
346 } else {
347 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank);
349 (*auth_retry)++;
350 return 0;
354 http_seek( stream_t *stream, off_t pos ) {
355 HTTP_header_t *http_hdr = NULL;
356 int fd;
357 if( stream==NULL ) return 0;
359 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
360 fd = http_send_request( stream->streaming_ctrl->url, pos );
361 if( fd<0 ) return 0;
363 http_hdr = http_read_response( fd );
365 if( http_hdr==NULL ) return 0;
367 switch( http_hdr->status_code ) {
368 case 200:
369 case 206: // OK
370 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
371 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
372 if( http_hdr->body_size>0 ) {
373 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
374 http_free( http_hdr );
375 return -1;
378 break;
379 default:
380 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase );
381 close( fd );
382 fd = -1;
384 stream->fd = fd;
386 if( http_hdr ) {
387 http_free( http_hdr );
388 stream->streaming_ctrl->data = NULL;
391 stream->pos=pos;
393 return 1;
398 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
399 //printf("streaming_bufferize\n");
400 streaming_ctrl->buffer = malloc(size);
401 if( streaming_ctrl->buffer==NULL ) {
402 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
403 return -1;
405 memcpy( streaming_ctrl->buffer, buffer, size );
406 streaming_ctrl->buffer_size = size;
407 return size;
411 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
412 int len=0;
413 //printf("nop_streaming_read\n");
414 if( stream_ctrl->buffer_size!=0 ) {
415 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
416 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
417 len = (size<buffer_len)?size:buffer_len;
418 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
419 stream_ctrl->buffer_pos += len;
420 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
421 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
422 free( stream_ctrl->buffer );
423 stream_ctrl->buffer = NULL;
424 stream_ctrl->buffer_size = 0;
425 stream_ctrl->buffer_pos = 0;
426 //printf("buffer cleaned\n");
428 //printf("read %d bytes from buffer\n", len );
431 if( len<size ) {
432 int ret;
433 ret = recv( fd, buffer+len, size-len, 0 );
434 if( ret<0 ) {
435 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
437 len += ret;
438 //printf("read %d bytes from network\n", len );
441 return len;
445 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
446 return -1;
447 // To shut up gcc warning
448 fd++;
449 pos++;
450 stream_ctrl=NULL;
454 void fixup_network_stream_cache(stream_t *stream) {
455 if(stream->streaming_ctrl->buffering) {
456 if(stream_cache_size<0) {
457 // cache option not set, will use our computed value.
458 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
459 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
460 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
462 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_CacheSizeSetTo, stream_cache_size);
467 static int
468 streaming_stop( stream_t *stream ) {
469 stream->streaming_ctrl->status = streaming_stopped_e;
470 return 0;