input: fix possible crash in printing key combo names
[mplayer.git] / stream / network.c
blobc68a8b313e8439f6117b29f61266830a46265d79
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 "mpcommon.h"
44 #include "network.h"
45 #include "tcp.h"
46 #include "http.h"
47 #include "cookies.h"
48 #include "url.h"
50 /* Variables for the command line option -user, -passwd, -bandwidth,
51 -user-agent and -nocookies */
53 char *network_username=NULL;
54 char *network_password=NULL;
55 int network_bandwidth=0;
56 int network_cookies_enabled = 0;
57 char *network_useragent=NULL;
58 char *network_referrer=NULL;
59 char **network_http_header_fields=NULL;
61 /* IPv6 options */
62 int network_ipv4_only_proxy = 0;
65 const mime_struct_t mime_type_table[] = {
66 // Flash Video
67 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
68 // do not force any demuxer in this case!
69 // we want the lavf demuxer to be tried first (happens automatically anyway),
70 // but for mov reference files to work we must also try
71 // the native demuxer if lavf fails.
72 { "video/quicktime", 0 },
73 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
74 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
75 // MPEG streaming
76 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
77 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
78 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
79 // AVI ??? => video/x-msvideo
80 { "video/x-msvideo", DEMUXER_TYPE_AVI },
81 // MOV => video/quicktime
82 { "video/quicktime", DEMUXER_TYPE_MOV },
83 // ASF
84 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
85 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
86 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
87 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
88 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
89 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
90 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
91 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
92 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
93 // Playlists
94 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
95 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
96 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
97 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
98 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
99 // Real Media
100 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
101 // OGG Streaming
102 { "application/x-ogg", DEMUXER_TYPE_OGG },
103 // NullSoft Streaming Video
104 { "video/nsv", DEMUXER_TYPE_NSV},
105 { "misc/ultravox", DEMUXER_TYPE_NSV},
106 { NULL, DEMUXER_TYPE_UNKNOWN},
110 streaming_ctrl_t *
111 streaming_ctrl_new(void) {
112 streaming_ctrl_t *streaming_ctrl = calloc(1, sizeof(*streaming_ctrl));
113 if( streaming_ctrl==NULL ) {
114 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
115 return NULL;
117 return streaming_ctrl;
120 void
121 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
122 if( streaming_ctrl==NULL ) return;
123 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
124 free(streaming_ctrl->buffer);
125 free(streaming_ctrl->data);
126 free(streaming_ctrl);
129 URL_t*
130 check4proxies( URL_t *url ) {
131 URL_t *url_out = NULL;
132 if( url==NULL ) return NULL;
133 url_out = url_new( url->url );
134 if( !strcasecmp(url->protocol, "http_proxy") ) {
135 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
136 return url_out;
138 // Check if the http_proxy environment variable is set.
139 if( !strcasecmp(url->protocol, "http") ) {
140 char *proxy;
141 proxy = getenv("http_proxy");
142 if( proxy!=NULL ) {
143 // We got a proxy, build the URL to use it
144 int len;
145 char *new_url;
146 URL_t *tmp_url;
147 URL_t *proxy_url = url_new( proxy );
149 if( proxy_url==NULL ) {
150 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
151 "Invalid proxy setting... Trying without proxy.\n");
152 return url_out;
155 #ifdef HAVE_AF_INET6
156 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
157 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
158 "Could not resolve remote hostname for AF_INET. Trying without proxy.\n");
159 url_free(proxy_url);
160 return url_out;
162 #endif
164 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
165 len = make_http_proxy_url(proxy_url, url->url, NULL, 0) + 1;
166 new_url = malloc(len);
167 if( new_url==NULL ) {
168 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
169 url_free(proxy_url);
170 return url_out;
172 make_http_proxy_url(proxy_url, url->url, new_url, len);
173 tmp_url = url_new( new_url );
174 if( tmp_url==NULL ) {
175 free( new_url );
176 url_free( proxy_url );
177 return url_out;
179 url_free( url_out );
180 url_out = tmp_url;
181 free( new_url );
182 url_free( proxy_url );
185 return url_out;
189 http_send_request( URL_t *url, off_t pos ) {
190 HTTP_header_t *http_hdr;
191 URL_t *server_url;
192 char str[256];
193 int fd = -1;
194 int ret;
195 int proxy = 0; // Boolean
197 http_hdr = http_new_header();
199 if( !strcasecmp(url->protocol, "http_proxy") ) {
200 proxy = 1;
201 server_url = url_new( (url->file)+1 );
202 if (!server_url) {
203 mp_msg(MSGT_NETWORK, MSGL_ERR, "Invalid URL '%s' to proxify\n", url->file+1);
204 goto err_out;
206 http_set_uri( http_hdr, server_url->noauth_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, sizeof(str), "Host: %s:%d", server_url->hostname, server_url->port );
213 else
214 snprintf(str, sizeof(str), "Host: %s", server_url->hostname );
215 http_set_field( http_hdr, str);
216 if (network_useragent)
217 snprintf(str, sizeof(str), "User-Agent: %s", network_useragent);
218 else
219 snprintf(str, sizeof(str), "User-Agent: %s", mplayer_version);
220 http_set_field(http_hdr, str);
222 if (network_referrer) {
223 char *referrer = NULL;
224 size_t len = strlen(network_referrer) + 10;
226 // Check len to ensure we don't do something really bad in case of an overflow
227 if (len > 10)
228 referrer = malloc(len);
230 if (referrer == NULL) {
231 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Memory allocation failed.\n");
232 } else {
233 snprintf(referrer, len, "Referer: %s", network_referrer);
234 http_set_field(http_hdr, referrer);
235 free(referrer);
239 if( strcasecmp(url->protocol, "noicyx") )
240 http_set_field(http_hdr, "Icy-MetaData: 1");
242 if(pos>0) {
243 // Extend http_send_request with possibility to do partial content retrieval
244 snprintf(str, sizeof(str), "Range: bytes=%"PRId64"-", (int64_t)pos);
245 http_set_field(http_hdr, str);
248 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
250 if (network_http_header_fields) {
251 int i=0;
252 while (network_http_header_fields[i])
253 http_set_field(http_hdr, network_http_header_fields[i++]);
256 http_set_field( http_hdr, "Connection: close");
257 if (proxy)
258 http_add_basic_proxy_authentication(http_hdr, url->username, url->password);
259 http_add_basic_authentication(http_hdr, server_url->username, server_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, DEFAULT_SEND_FLAGS );
279 if( ret!=(int)http_hdr->buffer_size ) {
280 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while sending HTTP request: Didn't send all the request.\n");
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_tmsg(MSGT_NETWORK,MSGL_ERR,"Read failed.\n");
310 http_free( http_hdr );
311 return NULL;
313 if( i==0 ) {
314 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 (i.e. EOF).\n");
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 #define MPDEMUX_NW_AuthFailed _(\
332 "Authentication failed. Please use the -user and -passwd options to provide your\n"\
333 "username/password for a list of URLs, or form an URL like:\n"\
334 "http://username:password@hostname/file\n")
337 if( *auth_retry==1 ) {
338 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
339 return -1;
341 if( *auth_retry>0 ) {
342 free(url->username);
343 url->username = NULL;
344 free(url->password);
345 url->password = NULL;
348 aut = http_get_field(http_hdr, "WWW-Authenticate");
349 if( aut!=NULL ) {
350 char *aut_space;
351 aut_space = strstr(aut, "realm=");
352 if( aut_space!=NULL ) aut_space += 6;
353 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required for %s\n", aut_space);
354 } else {
355 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required.\n");
357 if( network_username ) {
358 url->username = strdup(network_username);
359 if( url->username==NULL ) {
360 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
361 return -1;
363 } else {
364 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
365 return -1;
367 if( network_password ) {
368 url->password = strdup(network_password);
369 if( url->password==NULL ) {
370 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
371 return -1;
373 } else {
374 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"No password provided, trying blank password.\n");
376 (*auth_retry)++;
377 return 0;
381 http_seek( stream_t *stream, off_t pos ) {
382 HTTP_header_t *http_hdr = NULL;
383 int fd;
384 if( stream==NULL ) return 0;
386 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
387 fd = http_send_request( stream->streaming_ctrl->url, pos );
388 if( fd<0 ) return 0;
390 http_hdr = http_read_response( fd );
392 if( http_hdr==NULL ) return 0;
394 if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
395 http_debug_hdr( http_hdr );
397 switch( http_hdr->status_code ) {
398 case 200:
399 case 206: // OK
400 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
401 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
402 if( http_hdr->body_size>0 ) {
403 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
404 http_free( http_hdr );
405 return -1;
408 break;
409 default:
410 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returns %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
411 closesocket( fd );
412 fd = -1;
414 stream->fd = fd;
416 if( http_hdr ) {
417 http_free( http_hdr );
418 stream->streaming_ctrl->data = NULL;
421 stream->pos=pos;
423 return 1;
428 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
429 //printf("streaming_bufferize\n");
430 streaming_ctrl->buffer = malloc(size);
431 if( streaming_ctrl->buffer==NULL ) {
432 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
433 return -1;
435 memcpy( streaming_ctrl->buffer, buffer, size );
436 streaming_ctrl->buffer_size = size;
437 return size;
441 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
442 int len=0;
443 //printf("nop_streaming_read\n");
444 if( stream_ctrl->buffer_size!=0 ) {
445 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
446 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
447 len = (size<buffer_len)?size:buffer_len;
448 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
449 stream_ctrl->buffer_pos += len;
450 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
451 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
452 free( stream_ctrl->buffer );
453 stream_ctrl->buffer = NULL;
454 stream_ctrl->buffer_size = 0;
455 stream_ctrl->buffer_pos = 0;
456 //printf("buffer cleaned\n");
458 //printf("read %d bytes from buffer\n", len );
461 if( len<size ) {
462 int ret;
463 ret = recv( fd, buffer+len, size-len, 0 );
464 if( ret<0 ) {
465 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
466 ret = 0;
467 } else if (ret == 0)
468 stream_ctrl->status = streaming_stopped_e;
469 len += ret;
470 //printf("read %d bytes from network\n", len );
473 return len;
477 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
478 return -1;