input: fix memory leak with full key buffer
[mplayer.git] / stream / network.c
blobd48ab81695f271de82fdb1aef0020f9459e02ba8
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"
32 #include "options.h"
34 #include "mp_msg.h"
36 #if HAVE_WINSOCK2_H
37 #include <winsock2.h>
38 #include <ws2tcpip.h>
39 #endif
41 #include "stream.h"
42 #include "libmpdemux/demuxer.h"
43 #include "m_config.h"
44 #include "mpcommon.h"
45 #include "network.h"
46 #include "tcp.h"
47 #include "http.h"
48 #include "cookies.h"
49 #include "url.h"
51 /* Variables for the command line option -user, -passwd, -bandwidth,
52 -user-agent and -nocookies */
54 char *network_username=NULL;
55 char *network_password=NULL;
56 int network_bandwidth=0;
57 int network_cookies_enabled = 0;
58 char *network_useragent=NULL;
59 char *network_referrer=NULL;
60 char **network_http_header_fields=NULL;
62 /* IPv6 options */
63 int network_ipv4_only_proxy = 0;
66 const mime_struct_t mime_type_table[] = {
67 // Flash Video
68 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
69 // do not force any demuxer in this case!
70 // we want the lavf demuxer to be tried first (happens automatically anyway),
71 // but for mov reference files to work we must also try
72 // the native demuxer if lavf fails.
73 { "video/quicktime", 0 },
74 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
75 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
76 // MPEG streaming
77 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
78 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
79 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
80 // AVI ??? => video/x-msvideo
81 { "video/x-msvideo", DEMUXER_TYPE_AVI },
82 // MOV => video/quicktime
83 { "video/quicktime", DEMUXER_TYPE_MOV },
84 // ASF
85 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
86 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
87 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
88 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
89 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
90 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
91 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
92 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
93 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
94 // Playlists
95 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
96 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
97 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
98 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
99 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
100 // Real Media
101 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
102 // OGG Streaming
103 { "application/x-ogg", DEMUXER_TYPE_OGG },
104 // NullSoft Streaming Video
105 { "video/nsv", DEMUXER_TYPE_NSV},
106 { "misc/ultravox", DEMUXER_TYPE_NSV},
107 { NULL, DEMUXER_TYPE_UNKNOWN},
111 streaming_ctrl_t *
112 streaming_ctrl_new(void) {
113 streaming_ctrl_t *streaming_ctrl = calloc(1, sizeof(*streaming_ctrl));
114 if( streaming_ctrl==NULL ) {
115 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
116 return NULL;
118 return streaming_ctrl;
121 void
122 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
123 if( streaming_ctrl==NULL ) return;
124 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
125 free(streaming_ctrl->buffer);
126 free(streaming_ctrl->data);
127 free(streaming_ctrl);
130 URL_t*
131 check4proxies( URL_t *url ) {
132 URL_t *url_out = NULL;
133 if( url==NULL ) return NULL;
134 url_out = url_new( url->url );
135 if( !strcasecmp(url->protocol, "http_proxy") ) {
136 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
137 return url_out;
139 // Check if the http_proxy environment variable is set.
140 if( !strcasecmp(url->protocol, "http") ) {
141 char *proxy;
142 proxy = getenv("http_proxy");
143 if( proxy!=NULL ) {
144 // We got a proxy, build the URL to use it
145 int len;
146 char *new_url;
147 URL_t *tmp_url;
148 URL_t *proxy_url = url_new( proxy );
150 if( proxy_url==NULL ) {
151 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
152 "Invalid proxy setting... Trying without proxy.\n");
153 return url_out;
156 #ifdef HAVE_AF_INET6
157 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
158 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
159 "Could not resolve remote hostname for AF_INET. Trying without proxy.\n");
160 url_free(proxy_url);
161 return url_out;
163 #endif
165 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
166 len = make_http_proxy_url(proxy_url, url->url, NULL, 0) + 1;
167 new_url = malloc(len);
168 if( new_url==NULL ) {
169 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
170 url_free(proxy_url);
171 return url_out;
173 make_http_proxy_url(proxy_url, url->url, new_url, len);
174 tmp_url = url_new( new_url );
175 if( tmp_url==NULL ) {
176 free( new_url );
177 url_free( proxy_url );
178 return url_out;
180 url_free( url_out );
181 url_out = tmp_url;
182 free( new_url );
183 url_free( proxy_url );
186 return url_out;
190 http_send_request( URL_t *url, off_t pos ) {
191 HTTP_header_t *http_hdr;
192 URL_t *server_url;
193 char str[256];
194 int fd = -1;
195 int ret;
196 int proxy = 0; // Boolean
198 http_hdr = http_new_header();
200 if( !strcasecmp(url->protocol, "http_proxy") ) {
201 proxy = 1;
202 server_url = url_new( (url->file)+1 );
203 if (!server_url) {
204 mp_msg(MSGT_NETWORK, MSGL_ERR, "Invalid URL '%s' to proxify\n", url->file+1);
205 goto err_out;
207 http_set_uri( http_hdr, server_url->noauth_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, sizeof(str), "Host: %s:%d", server_url->hostname, server_url->port );
214 else
215 snprintf(str, sizeof(str), "Host: %s", server_url->hostname );
216 http_set_field( http_hdr, str);
217 if (network_useragent)
218 snprintf(str, sizeof(str), "User-Agent: %s", network_useragent);
219 else
220 snprintf(str, sizeof(str), "User-Agent: %s", mplayer_version);
221 http_set_field(http_hdr, str);
223 if (network_referrer) {
224 char *referrer = NULL;
225 size_t len = strlen(network_referrer) + 10;
227 // Check len to ensure we don't do something really bad in case of an overflow
228 if (len > 10)
229 referrer = malloc(len);
231 if (referrer == NULL) {
232 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Memory allocation failed.\n");
233 } else {
234 snprintf(referrer, len, "Referer: %s", network_referrer);
235 http_set_field(http_hdr, referrer);
236 free(referrer);
240 if( strcasecmp(url->protocol, "noicyx") )
241 http_set_field(http_hdr, "Icy-MetaData: 1");
243 if(pos>0) {
244 // Extend http_send_request with possibility to do partial content retrieval
245 snprintf(str, sizeof(str), "Range: bytes=%"PRId64"-", (int64_t)pos);
246 http_set_field(http_hdr, str);
249 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
251 if (network_http_header_fields) {
252 int i=0;
253 while (network_http_header_fields[i])
254 http_set_field(http_hdr, network_http_header_fields[i++]);
257 http_set_field( http_hdr, "Connection: close");
258 if (proxy)
259 http_add_basic_proxy_authentication(http_hdr, url->username, url->password);
260 http_add_basic_authentication(http_hdr, server_url->username, server_url->password);
261 if( http_build_request( http_hdr )==NULL ) {
262 goto err_out;
265 if( proxy ) {
266 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
267 fd = connect2Server( url->hostname, url->port,1 );
268 url_free( server_url );
269 server_url = NULL;
270 } else {
271 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
272 fd = connect2Server( server_url->hostname, server_url->port,1 );
274 if( fd<0 ) {
275 goto err_out;
277 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
279 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, DEFAULT_SEND_FLAGS );
280 if( ret!=(int)http_hdr->buffer_size ) {
281 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while sending HTTP request: Didn't send all the request.\n");
282 goto err_out;
285 http_free( http_hdr );
287 return fd;
288 err_out:
289 if (fd > 0) closesocket(fd);
290 http_free(http_hdr);
291 if (proxy && server_url)
292 url_free(server_url);
293 return -1;
296 HTTP_header_t *
297 http_read_response( int fd ) {
298 HTTP_header_t *http_hdr;
299 char response[BUFFER_SIZE];
300 int i;
302 http_hdr = http_new_header();
303 if( http_hdr==NULL ) {
304 return NULL;
307 do {
308 i = recv( fd, response, BUFFER_SIZE, 0 );
309 if( i<0 ) {
310 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Read failed.\n");
311 http_free( http_hdr );
312 return NULL;
314 if( i==0 ) {
315 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 (i.e. EOF).\n");
316 http_free( http_hdr );
317 return NULL;
319 http_response_append( http_hdr, response, i );
320 } while( !http_is_header_entire( http_hdr ) );
321 if (http_response_parse( http_hdr ) < 0) {
322 http_free( http_hdr );
323 return NULL;
325 return http_hdr;
329 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
330 char *aut;
332 #define MPDEMUX_NW_AuthFailed _(\
333 "Authentication failed. Please use the -user and -passwd options to provide your\n"\
334 "username/password for a list of URLs, or form an URL like:\n"\
335 "http://username:password@hostname/file\n")
338 if( *auth_retry==1 ) {
339 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
340 return -1;
342 if( *auth_retry>0 ) {
343 free(url->username);
344 url->username = NULL;
345 free(url->password);
346 url->password = NULL;
349 aut = http_get_field(http_hdr, "WWW-Authenticate");
350 if( aut!=NULL ) {
351 char *aut_space;
352 aut_space = strstr(aut, "realm=");
353 if( aut_space!=NULL ) aut_space += 6;
354 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required for %s\n", aut_space);
355 } else {
356 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required.\n");
358 if( network_username ) {
359 url->username = strdup(network_username);
360 if( url->username==NULL ) {
361 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
362 return -1;
364 } else {
365 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
366 return -1;
368 if( network_password ) {
369 url->password = strdup(network_password);
370 if( url->password==NULL ) {
371 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
372 return -1;
374 } else {
375 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"No password provided, trying blank password.\n");
377 (*auth_retry)++;
378 return 0;
382 http_seek( stream_t *stream, off_t pos ) {
383 HTTP_header_t *http_hdr = NULL;
384 int fd;
385 if( stream==NULL ) return 0;
387 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
388 fd = http_send_request( stream->streaming_ctrl->url, pos );
389 if( fd<0 ) return 0;
391 http_hdr = http_read_response( fd );
393 if( http_hdr==NULL ) return 0;
395 if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
396 http_debug_hdr( http_hdr );
398 switch( http_hdr->status_code ) {
399 case 200:
400 case 206: // OK
401 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
402 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
403 if( http_hdr->body_size>0 ) {
404 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
405 http_free( http_hdr );
406 return -1;
409 break;
410 default:
411 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returns %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
412 closesocket( fd );
413 fd = -1;
415 stream->fd = fd;
417 if( http_hdr ) {
418 http_free( http_hdr );
419 stream->streaming_ctrl->data = NULL;
422 stream->pos=pos;
424 return 1;
429 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
430 //printf("streaming_bufferize\n");
431 streaming_ctrl->buffer = malloc(size);
432 if( streaming_ctrl->buffer==NULL ) {
433 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
434 return -1;
436 memcpy( streaming_ctrl->buffer, buffer, size );
437 streaming_ctrl->buffer_size = size;
438 return size;
442 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
443 int len=0;
444 //printf("nop_streaming_read\n");
445 if( stream_ctrl->buffer_size!=0 ) {
446 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
447 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
448 len = (size<buffer_len)?size:buffer_len;
449 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
450 stream_ctrl->buffer_pos += len;
451 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
452 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
453 free( stream_ctrl->buffer );
454 stream_ctrl->buffer = NULL;
455 stream_ctrl->buffer_size = 0;
456 stream_ctrl->buffer_pos = 0;
457 //printf("buffer cleaned\n");
459 //printf("read %d bytes from buffer\n", len );
462 if( len<size ) {
463 int ret;
464 ret = recv( fd, buffer+len, size-len, 0 );
465 if( ret<0 ) {
466 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
467 ret = 0;
468 } else if (ret == 0)
469 stream_ctrl->status = streaming_stopped_e;
470 len += ret;
471 //printf("read %d bytes from network\n", len );
474 return len;
478 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
479 return -1;
483 void fixup_network_stream_cache(stream_t *stream) {
484 struct MPOpts *opts = stream->opts;
485 if(stream->streaming_ctrl->buffering) {
486 if(opts->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 opts->stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
490 if( opts->stream_cache_size<64 ) opts->stream_cache_size = 64; // 16KBytes min buffer
492 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Cache size set to %d KBytes\n", opts->stream_cache_size);