Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / stream / network.c
blob8738cad1137dd5250b56ffbb442d77aaa8a9f83f
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;
65 char *network_referrer=NULL;
67 /* IPv6 options */
68 int network_ipv4_only_proxy = 0;
71 const mime_struct_t mime_type_table[] = {
72 #ifdef CONFIG_LIBAVFORMAT
73 // Flash Video
74 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
75 // do not force any demuxer in this case!
76 // we want the lavf demuxer to be tried first (happens automatically anyway),
77 // but for mov reference files to work we must also try
78 // the native demuxer if lavf fails.
79 { "video/quicktime", 0 },
80 #endif
81 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
82 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
83 // MPEG streaming
84 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
85 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
86 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
87 // AVI ??? => video/x-msvideo
88 { "video/x-msvideo", DEMUXER_TYPE_AVI },
89 // MOV => video/quicktime
90 { "video/quicktime", DEMUXER_TYPE_MOV },
91 // ASF
92 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
93 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
94 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
95 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
96 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
97 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
98 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
99 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
100 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
101 // Playlists
102 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
103 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
104 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
105 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
106 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
107 // Real Media
108 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
109 // OGG Streaming
110 { "application/x-ogg", DEMUXER_TYPE_OGG },
111 // NullSoft Streaming Video
112 { "video/nsv", DEMUXER_TYPE_NSV},
113 { "misc/ultravox", DEMUXER_TYPE_NSV},
114 { NULL, DEMUXER_TYPE_UNKNOWN},
118 streaming_ctrl_t *
119 streaming_ctrl_new(void) {
120 streaming_ctrl_t *streaming_ctrl;
121 streaming_ctrl = malloc(sizeof(streaming_ctrl_t));
122 if( streaming_ctrl==NULL ) {
123 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
124 return NULL;
126 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) );
127 return streaming_ctrl;
130 void
131 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
132 if( streaming_ctrl==NULL ) return;
133 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
134 if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer );
135 if( streaming_ctrl->data ) free( streaming_ctrl->data );
136 free( streaming_ctrl );
139 URL_t*
140 check4proxies( URL_t *url ) {
141 URL_t *url_out = NULL;
142 if( url==NULL ) return NULL;
143 url_out = url_new( url->url );
144 if( !strcasecmp(url->protocol, "http_proxy") ) {
145 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
146 return url_out;
148 // Check if the http_proxy environment variable is set.
149 if( !strcasecmp(url->protocol, "http") ) {
150 char *proxy;
151 proxy = getenv("http_proxy");
152 if( proxy!=NULL ) {
153 // We got a proxy, build the URL to use it
154 int len;
155 char *new_url;
156 URL_t *tmp_url;
157 URL_t *proxy_url = url_new( proxy );
159 if( proxy_url==NULL ) {
160 mp_msg(MSGT_NETWORK,MSGL_WARN,
161 MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout);
162 return url_out;
165 #ifdef HAVE_AF_INET6
166 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
167 mp_msg(MSGT_NETWORK,MSGL_WARN,
168 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy);
169 url_free(proxy_url);
170 return url_out;
172 #endif
174 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
175 len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port
176 new_url = malloc( len+1 );
177 if( new_url==NULL ) {
178 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
179 url_free(proxy_url);
180 return url_out;
182 sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url );
183 tmp_url = url_new( new_url );
184 if( tmp_url==NULL ) {
185 free( new_url );
186 url_free( proxy_url );
187 return url_out;
189 url_free( url_out );
190 url_out = tmp_url;
191 free( new_url );
192 url_free( proxy_url );
195 return url_out;
199 http_send_request( URL_t *url, off_t pos ) {
200 HTTP_header_t *http_hdr;
201 URL_t *server_url;
202 char str[256];
203 int fd = -1;
204 int ret;
205 int proxy = 0; // Boolean
207 http_hdr = http_new_header();
209 if( !strcasecmp(url->protocol, "http_proxy") ) {
210 proxy = 1;
211 server_url = url_new( (url->file)+1 );
212 http_set_uri( http_hdr, server_url->url );
213 } else {
214 server_url = url;
215 http_set_uri( http_hdr, server_url->file );
217 if (server_url->port && server_url->port != 80)
218 snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
219 else
220 snprintf(str, 256, "Host: %s", server_url->hostname );
221 http_set_field( http_hdr, str);
222 if (network_useragent)
224 snprintf(str, 256, "User-Agent: %s", network_useragent);
225 http_set_field(http_hdr, str);
227 else
228 http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION);
230 if (network_referrer) {
231 char *referrer = NULL;
232 size_t len = strlen(network_referrer) + 10;
234 // Check len to ensure we don't do something really bad in case of an overflow
235 if (len > 10)
236 referrer = malloc(len);
238 if (referrer == NULL) {
239 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MemAllocFailed);
240 } else {
241 snprintf(referrer, len, "Referer: %s", network_referrer);
242 http_set_field(http_hdr, referrer);
243 free(referrer);
247 if( strcasecmp(url->protocol, "noicyx") )
248 http_set_field(http_hdr, "Icy-MetaData: 1");
250 if(pos>0) {
251 // Extend http_send_request with possibility to do partial content retrieval
252 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
253 http_set_field(http_hdr, str);
256 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
258 http_set_field( http_hdr, "Connection: close");
259 http_add_basic_authentication( http_hdr, url->username, url->password );
260 if( http_build_request( http_hdr )==NULL ) {
261 goto err_out;
264 if( proxy ) {
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 );
268 server_url = NULL;
269 } else {
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 );
273 if( fd<0 ) {
274 goto err_out;
276 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
278 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
279 if( ret!=(int)http_hdr->buffer_size ) {
280 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
281 goto err_out;
284 http_free( http_hdr );
286 return fd;
287 err_out:
288 if (fd > 0) closesocket(fd);
289 http_free(http_hdr);
290 if (proxy && server_url)
291 url_free(server_url);
292 return -1;
295 HTTP_header_t *
296 http_read_response( int fd ) {
297 HTTP_header_t *http_hdr;
298 char response[BUFFER_SIZE];
299 int i;
301 http_hdr = http_new_header();
302 if( http_hdr==NULL ) {
303 return NULL;
306 do {
307 i = recv( fd, response, BUFFER_SIZE, 0 );
308 if( i<0 ) {
309 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed);
310 http_free( http_hdr );
311 return NULL;
313 if( i==0 ) {
314 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
315 http_free( http_hdr );
316 return NULL;
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 );
322 return NULL;
324 return http_hdr;
328 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
329 char *aut;
331 if( *auth_retry==1 ) {
332 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
333 return -1;
335 if( *auth_retry>0 ) {
336 if( url->username ) {
337 free( url->username );
338 url->username = NULL;
340 if( url->password ) {
341 free( url->password );
342 url->password = NULL;
346 aut = http_get_field(http_hdr, "WWW-Authenticate");
347 if( aut!=NULL ) {
348 char *aut_space;
349 aut_space = strstr(aut, "realm=");
350 if( aut_space!=NULL ) aut_space += 6;
351 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequiredFor, aut_space);
352 } else {
353 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequired);
355 if( network_username ) {
356 url->username = strdup(network_username);
357 if( url->username==NULL ) {
358 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
359 return -1;
361 } else {
362 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
363 return -1;
365 if( network_password ) {
366 url->password = strdup(network_password);
367 if( url->password==NULL ) {
368 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
369 return -1;
371 } else {
372 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank);
374 (*auth_retry)++;
375 return 0;
379 http_seek( stream_t *stream, off_t pos ) {
380 HTTP_header_t *http_hdr = NULL;
381 int fd;
382 if( stream==NULL ) return 0;
384 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
385 fd = http_send_request( stream->streaming_ctrl->url, pos );
386 if( fd<0 ) return 0;
388 http_hdr = http_read_response( fd );
390 if( http_hdr==NULL ) return 0;
392 if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
393 http_debug_hdr( http_hdr );
395 switch( http_hdr->status_code ) {
396 case 200:
397 case 206: // OK
398 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
399 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
400 if( http_hdr->body_size>0 ) {
401 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
402 http_free( http_hdr );
403 return -1;
406 break;
407 default:
408 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase );
409 closesocket( fd );
410 fd = -1;
412 stream->fd = fd;
414 if( http_hdr ) {
415 http_free( http_hdr );
416 stream->streaming_ctrl->data = NULL;
419 stream->pos=pos;
421 return 1;
426 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
427 //printf("streaming_bufferize\n");
428 streaming_ctrl->buffer = malloc(size);
429 if( streaming_ctrl->buffer==NULL ) {
430 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
431 return -1;
433 memcpy( streaming_ctrl->buffer, buffer, size );
434 streaming_ctrl->buffer_size = size;
435 return size;
439 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
440 int len=0;
441 //printf("nop_streaming_read\n");
442 if( stream_ctrl->buffer_size!=0 ) {
443 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
444 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
445 len = (size<buffer_len)?size:buffer_len;
446 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
447 stream_ctrl->buffer_pos += len;
448 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
449 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
450 free( stream_ctrl->buffer );
451 stream_ctrl->buffer = NULL;
452 stream_ctrl->buffer_size = 0;
453 stream_ctrl->buffer_pos = 0;
454 //printf("buffer cleaned\n");
456 //printf("read %d bytes from buffer\n", len );
459 if( len<size ) {
460 int ret;
461 ret = recv( fd, buffer+len, size-len, 0 );
462 if( ret<0 ) {
463 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
465 len += ret;
466 //printf("read %d bytes from network\n", len );
469 return len;
473 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
474 return -1;
475 // To shut up gcc warning
476 fd++;
477 pos++;
478 stream_ctrl=NULL;
482 void fixup_network_stream_cache(stream_t *stream) {
483 if(stream->streaming_ctrl->buffering) {
484 if(stream_cache_size<0) {
485 // cache option not set, will use our computed value.
486 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
487 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
488 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
490 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_CacheSizeSetTo, stream_cache_size);