vo_glamo: sub.h was moved to sub directory in c9026cb3210205b07e2e068467a18ee40f9259a3
[mplayer/glamo.git] / stream / network.c
blobc6776ac29af1f4f7a53623024776e0a244638772
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;
62 /* IPv6 options */
63 int network_ipv4_only_proxy = 0;
66 const mime_struct_t mime_type_table[] = {
67 #ifdef CONFIG_FFMPEG
68 // Flash Video
69 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
70 // do not force any demuxer in this case!
71 // we want the lavf demuxer to be tried first (happens automatically anyway),
72 // but for mov reference files to work we must also try
73 // the native demuxer if lavf fails.
74 { "video/quicktime", 0 },
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 = calloc(1, sizeof(*streaming_ctrl));
116 if( streaming_ctrl==NULL ) {
117 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
118 return NULL;
120 return streaming_ctrl;
123 void
124 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
125 if( streaming_ctrl==NULL ) return;
126 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
127 free(streaming_ctrl->buffer);
128 free(streaming_ctrl->data);
129 free(streaming_ctrl);
132 URL_t*
133 check4proxies( URL_t *url ) {
134 URL_t *url_out = NULL;
135 if( url==NULL ) return NULL;
136 url_out = url_new( url->url );
137 if( !strcasecmp(url->protocol, "http_proxy") ) {
138 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
139 return url_out;
141 // Check if the http_proxy environment variable is set.
142 if( !strcasecmp(url->protocol, "http") ) {
143 char *proxy;
144 proxy = getenv("http_proxy");
145 if( proxy!=NULL ) {
146 // We got a proxy, build the URL to use it
147 int len;
148 char *new_url;
149 URL_t *tmp_url;
150 URL_t *proxy_url = url_new( proxy );
152 if( proxy_url==NULL ) {
153 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
154 "Invalid proxy setting... Trying without proxy.\n");
155 return url_out;
158 #ifdef HAVE_AF_INET6
159 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
160 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
161 "Could not resolve remote hostname for AF_INET. Trying without proxy.\n");
162 url_free(proxy_url);
163 return url_out;
165 #endif
167 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
168 len = make_http_proxy_url(proxy_url, url->url, NULL, 0) + 1;
169 new_url = malloc(len);
170 if( new_url==NULL ) {
171 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
172 url_free(proxy_url);
173 return url_out;
175 make_http_proxy_url(proxy_url, url->url, new_url, len);
176 tmp_url = url_new( new_url );
177 if( tmp_url==NULL ) {
178 free( new_url );
179 url_free( proxy_url );
180 return url_out;
182 url_free( url_out );
183 url_out = tmp_url;
184 free( new_url );
185 url_free( proxy_url );
188 return url_out;
192 http_send_request( URL_t *url, off_t pos ) {
193 HTTP_header_t *http_hdr;
194 URL_t *server_url;
195 char str[256];
196 int fd = -1;
197 int ret;
198 int proxy = 0; // Boolean
200 http_hdr = http_new_header();
202 if( !strcasecmp(url->protocol, "http_proxy") ) {
203 proxy = 1;
204 server_url = url_new( (url->file)+1 );
205 if (!server_url) {
206 mp_msg(MSGT_NETWORK, MSGL_ERR, "Invalid URL '%s' to proxify\n", url->file+1);
207 goto err_out;
209 http_set_uri( http_hdr, server_url->noauth_url );
210 } else {
211 server_url = url;
212 http_set_uri( http_hdr, server_url->file );
214 if (server_url->port && server_url->port != 80)
215 snprintf(str, sizeof(str), "Host: %s:%d", server_url->hostname, server_url->port );
216 else
217 snprintf(str, sizeof(str), "Host: %s", server_url->hostname );
218 http_set_field( http_hdr, str);
219 if (network_useragent)
220 snprintf(str, sizeof(str), "User-Agent: %s", network_useragent);
221 else
222 snprintf(str, sizeof(str), "User-Agent: %s", mplayer_version);
223 http_set_field(http_hdr, str);
225 if (network_referrer) {
226 char *referrer = NULL;
227 size_t len = strlen(network_referrer) + 10;
229 // Check len to ensure we don't do something really bad in case of an overflow
230 if (len > 10)
231 referrer = malloc(len);
233 if (referrer == NULL) {
234 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Memory allocation failed.\n");
235 } else {
236 snprintf(referrer, len, "Referer: %s", network_referrer);
237 http_set_field(http_hdr, referrer);
238 free(referrer);
242 if( strcasecmp(url->protocol, "noicyx") )
243 http_set_field(http_hdr, "Icy-MetaData: 1");
245 if(pos>0) {
246 // Extend http_send_request with possibility to do partial content retrieval
247 snprintf(str, sizeof(str), "Range: bytes=%"PRId64"-", (int64_t)pos);
248 http_set_field(http_hdr, str);
251 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
253 http_set_field( http_hdr, "Connection: close");
254 if (proxy)
255 http_add_basic_proxy_authentication(http_hdr, url->username, url->password);
256 http_add_basic_authentication(http_hdr, server_url->username, server_url->password);
257 if( http_build_request( http_hdr )==NULL ) {
258 goto err_out;
261 if( proxy ) {
262 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
263 fd = connect2Server( url->hostname, url->port,1 );
264 url_free( server_url );
265 server_url = NULL;
266 } else {
267 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
268 fd = connect2Server( server_url->hostname, server_url->port,1 );
270 if( fd<0 ) {
271 goto err_out;
273 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
275 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, DEFAULT_SEND_FLAGS );
276 if( ret!=(int)http_hdr->buffer_size ) {
277 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while sending HTTP request: Didn't send all the request.\n");
278 goto err_out;
281 http_free( http_hdr );
283 return fd;
284 err_out:
285 if (fd > 0) closesocket(fd);
286 http_free(http_hdr);
287 if (proxy && server_url)
288 url_free(server_url);
289 return -1;
292 HTTP_header_t *
293 http_read_response( int fd ) {
294 HTTP_header_t *http_hdr;
295 char response[BUFFER_SIZE];
296 int i;
298 http_hdr = http_new_header();
299 if( http_hdr==NULL ) {
300 return NULL;
303 do {
304 i = recv( fd, response, BUFFER_SIZE, 0 );
305 if( i<0 ) {
306 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Read failed.\n");
307 http_free( http_hdr );
308 return NULL;
310 if( i==0 ) {
311 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 (i.e. EOF).\n");
312 http_free( http_hdr );
313 return NULL;
315 http_response_append( http_hdr, response, i );
316 } while( !http_is_header_entire( http_hdr ) );
317 if (http_response_parse( http_hdr ) < 0) {
318 http_free( http_hdr );
319 return NULL;
321 return http_hdr;
325 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
326 char *aut;
328 #define MPDEMUX_NW_AuthFailed _(\
329 "Authentication failed. Please use the -user and -passwd options to provide your\n"\
330 "username/password for a list of URLs, or form an URL like:\n"\
331 "http://username:password@hostname/file\n")
334 if( *auth_retry==1 ) {
335 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
336 return -1;
338 if( *auth_retry>0 ) {
339 free(url->username);
340 url->username = NULL;
341 free(url->password);
342 url->password = NULL;
345 aut = http_get_field(http_hdr, "WWW-Authenticate");
346 if( aut!=NULL ) {
347 char *aut_space;
348 aut_space = strstr(aut, "realm=");
349 if( aut_space!=NULL ) aut_space += 6;
350 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required for %s\n", aut_space);
351 } else {
352 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required.\n");
354 if( network_username ) {
355 url->username = strdup(network_username);
356 if( url->username==NULL ) {
357 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
358 return -1;
360 } else {
361 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
362 return -1;
364 if( network_password ) {
365 url->password = strdup(network_password);
366 if( url->password==NULL ) {
367 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
368 return -1;
370 } else {
371 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"No password provided, trying blank password.\n");
373 (*auth_retry)++;
374 return 0;
378 http_seek( stream_t *stream, off_t pos ) {
379 HTTP_header_t *http_hdr = NULL;
380 int fd;
381 if( stream==NULL ) return 0;
383 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
384 fd = http_send_request( stream->streaming_ctrl->url, pos );
385 if( fd<0 ) return 0;
387 http_hdr = http_read_response( fd );
389 if( http_hdr==NULL ) return 0;
391 if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
392 http_debug_hdr( http_hdr );
394 switch( http_hdr->status_code ) {
395 case 200:
396 case 206: // OK
397 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
398 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
399 if( http_hdr->body_size>0 ) {
400 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
401 http_free( http_hdr );
402 return -1;
405 break;
406 default:
407 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returns %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
408 closesocket( fd );
409 fd = -1;
411 stream->fd = fd;
413 if( http_hdr ) {
414 http_free( http_hdr );
415 stream->streaming_ctrl->data = NULL;
418 stream->pos=pos;
420 return 1;
425 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
426 //printf("streaming_bufferize\n");
427 streaming_ctrl->buffer = malloc(size);
428 if( streaming_ctrl->buffer==NULL ) {
429 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
430 return -1;
432 memcpy( streaming_ctrl->buffer, buffer, size );
433 streaming_ctrl->buffer_size = size;
434 return size;
438 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
439 int len=0;
440 //printf("nop_streaming_read\n");
441 if( stream_ctrl->buffer_size!=0 ) {
442 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
443 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
444 len = (size<buffer_len)?size:buffer_len;
445 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
446 stream_ctrl->buffer_pos += len;
447 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
448 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
449 free( stream_ctrl->buffer );
450 stream_ctrl->buffer = NULL;
451 stream_ctrl->buffer_size = 0;
452 stream_ctrl->buffer_pos = 0;
453 //printf("buffer cleaned\n");
455 //printf("read %d bytes from buffer\n", len );
458 if( len<size ) {
459 int ret;
460 ret = recv( fd, buffer+len, size-len, 0 );
461 if( ret<0 ) {
462 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
464 len += ret;
465 //printf("read %d bytes from network\n", len );
468 return len;
472 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
473 return -1;
477 void fixup_network_stream_cache(stream_t *stream) {
478 if(stream->streaming_ctrl->buffering) {
479 if(stream_cache_size<0) {
480 // cache option not set, will use our computed value.
481 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
482 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
483 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
485 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Cache size set to %d KBytes\n", stream_cache_size);