Get rid of code I neither know nor use anymore.
[mplayer/glamo.git] / stream / network.c
blob4b8856d4b491f43b1366e844025a1824a7ec9ec0
1 /*
2 * Network layer for MPlayer
3 * by Bertrand BAUDET <bertrand_baudet@yahoo.com>
4 * (C) 2001, MPlayer team.
5 */
7 //#define DUMP2FILE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
14 #include <errno.h>
15 #include <ctype.h>
17 #include "config.h"
19 #include "mp_msg.h"
20 #include "help_mp.h"
22 #ifndef HAVE_WINSOCK2
23 #define closesocket close
24 #else
25 #include <winsock2.h>
26 #include <ws2tcpip.h>
27 #endif
29 #include "stream.h"
30 #include "libmpdemux/demuxer.h"
31 #include "m_config.h"
33 #include "network.h"
34 #include "tcp.h"
35 #include "http.h"
36 #include "cookies.h"
37 #include "url.h"
39 #include "version.h"
41 extern int stream_cache_size;
43 extern int mp_input_check_interrupt(int time);
45 /* Variables for the command line option -user, -passwd, -bandwidth,
46 -user-agent and -nocookies */
48 char *network_username=NULL;
49 char *network_password=NULL;
50 int network_bandwidth=0;
51 int network_cookies_enabled = 0;
52 char *network_useragent=NULL;
54 /* IPv6 options */
55 int network_ipv4_only_proxy = 0;
58 mime_struct_t mime_type_table[] = {
59 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
60 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
61 // MPEG streaming
62 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
63 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
64 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
65 // AVI ??? => video/x-msvideo
66 { "video/x-msvideo", DEMUXER_TYPE_AVI },
67 // MOV => video/quicktime
68 { "video/quicktime", DEMUXER_TYPE_MOV },
69 // ASF
70 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
71 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
72 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
73 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
74 { "video/x-ms-wvx", DEMUXER_TYPE_ASF },
75 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
76 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
77 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
78 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
79 { "application/octet-stream", DEMUXER_TYPE_ASF },
80 // Playlists
81 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
82 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
83 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
84 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
85 // Real Media
86 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
87 // OGG Streaming
88 { "application/x-ogg", DEMUXER_TYPE_OGG },
89 // NullSoft Streaming Video
90 { "video/nsv", DEMUXER_TYPE_NSV},
91 { "misc/ultravox", DEMUXER_TYPE_NSV},
92 { NULL, DEMUXER_TYPE_UNKNOWN},
96 streaming_ctrl_t *
97 streaming_ctrl_new(void) {
98 streaming_ctrl_t *streaming_ctrl;
99 streaming_ctrl = malloc(sizeof(streaming_ctrl_t));
100 if( streaming_ctrl==NULL ) {
101 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
102 return NULL;
104 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) );
105 return streaming_ctrl;
108 void
109 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
110 if( streaming_ctrl==NULL ) return;
111 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
112 if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer );
113 if( streaming_ctrl->data ) free( streaming_ctrl->data );
114 free( streaming_ctrl );
117 URL_t*
118 check4proxies( URL_t *url ) {
119 URL_t *url_out = NULL;
120 if( url==NULL ) return NULL;
121 url_out = url_new( url->url );
122 if( !strcasecmp(url->protocol, "http_proxy") ) {
123 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
124 return url_out;
126 // Check if the http_proxy environment variable is set.
127 if( !strcasecmp(url->protocol, "http") ) {
128 char *proxy;
129 proxy = getenv("http_proxy");
130 if( proxy!=NULL ) {
131 // We got a proxy, build the URL to use it
132 int len;
133 char *new_url;
134 URL_t *tmp_url;
135 URL_t *proxy_url = url_new( proxy );
137 if( proxy_url==NULL ) {
138 mp_msg(MSGT_NETWORK,MSGL_WARN,
139 MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout);
140 return url_out;
143 #ifdef HAVE_AF_INET6
144 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
145 mp_msg(MSGT_NETWORK,MSGL_WARN,
146 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy);
147 url_free(proxy_url);
148 return url_out;
150 #endif
152 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
153 len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port
154 new_url = malloc( len+1 );
155 if( new_url==NULL ) {
156 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
157 url_free(proxy_url);
158 return url_out;
160 sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url );
161 tmp_url = url_new( new_url );
162 if( tmp_url==NULL ) {
163 free( new_url );
164 url_free( proxy_url );
165 return url_out;
167 url_free( url_out );
168 url_out = tmp_url;
169 free( new_url );
170 url_free( proxy_url );
173 return url_out;
177 http_send_request( URL_t *url, off_t pos ) {
178 HTTP_header_t *http_hdr;
179 URL_t *server_url;
180 char str[256];
181 int fd;
182 int ret;
183 int proxy = 0; // Boolean
185 http_hdr = http_new_header();
187 if( !strcasecmp(url->protocol, "http_proxy") ) {
188 proxy = 1;
189 server_url = url_new( (url->file)+1 );
190 http_set_uri( http_hdr, server_url->url );
191 } else {
192 server_url = url;
193 http_set_uri( http_hdr, server_url->file );
195 if (server_url->port && server_url->port != 80)
196 snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
197 else
198 snprintf(str, 256, "Host: %s", server_url->hostname );
199 http_set_field( http_hdr, str);
200 if (network_useragent)
202 snprintf(str, 256, "User-Agent: %s", network_useragent);
203 http_set_field(http_hdr, str);
205 else
206 http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION);
208 http_set_field(http_hdr, "Icy-MetaData: 1");
210 if(pos>0) {
211 // Extend http_send_request with possibility to do partial content retrieval
212 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
213 http_set_field(http_hdr, str);
216 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
218 http_set_field( http_hdr, "Connection: close");
219 http_add_basic_authentication( http_hdr, url->username, url->password );
220 if( http_build_request( http_hdr )==NULL ) {
221 goto err_out;
224 if( proxy ) {
225 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
226 fd = connect2Server( url->hostname, url->port,1 );
227 url_free( server_url );
228 server_url = NULL;
229 } else {
230 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
231 fd = connect2Server( server_url->hostname, server_url->port,1 );
233 if( fd<0 ) {
234 goto err_out;
236 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
238 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
239 if( ret!=(int)http_hdr->buffer_size ) {
240 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
241 goto err_out;
244 http_free( http_hdr );
246 return fd;
247 err_out:
248 http_free(http_hdr);
249 if (proxy && server_url)
250 url_free(server_url);
251 return -1;
254 HTTP_header_t *
255 http_read_response( int fd ) {
256 HTTP_header_t *http_hdr;
257 char response[BUFFER_SIZE];
258 int i;
260 http_hdr = http_new_header();
261 if( http_hdr==NULL ) {
262 return NULL;
265 do {
266 i = recv( fd, response, BUFFER_SIZE, 0 );
267 if( i<0 ) {
268 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed);
269 http_free( http_hdr );
270 return NULL;
272 if( i==0 ) {
273 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
274 http_free( http_hdr );
275 return NULL;
277 http_response_append( http_hdr, response, i );
278 } while( !http_is_header_entire( http_hdr ) );
279 http_response_parse( http_hdr );
280 return http_hdr;
284 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
285 char *aut;
287 if( *auth_retry==1 ) {
288 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
289 return -1;
291 if( *auth_retry>0 ) {
292 if( url->username ) {
293 free( url->username );
294 url->username = NULL;
296 if( url->password ) {
297 free( url->password );
298 url->password = NULL;
302 aut = http_get_field(http_hdr, "WWW-Authenticate");
303 if( aut!=NULL ) {
304 char *aut_space;
305 aut_space = strstr(aut, "realm=");
306 if( aut_space!=NULL ) aut_space += 6;
307 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequiredFor, aut_space);
308 } else {
309 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequired);
311 if( network_username ) {
312 url->username = strdup(network_username);
313 if( url->username==NULL ) {
314 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
315 return -1;
317 } else {
318 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
319 return -1;
321 if( network_password ) {
322 url->password = strdup(network_password);
323 if( url->password==NULL ) {
324 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
325 return -1;
327 } else {
328 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank);
330 (*auth_retry)++;
331 return 0;
335 http_seek( stream_t *stream, off_t pos ) {
336 HTTP_header_t *http_hdr = NULL;
337 int fd;
338 if( stream==NULL ) return 0;
340 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
341 fd = http_send_request( stream->streaming_ctrl->url, pos );
342 if( fd<0 ) return 0;
344 http_hdr = http_read_response( fd );
346 if( http_hdr==NULL ) return 0;
348 switch( http_hdr->status_code ) {
349 case 200:
350 case 206: // OK
351 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
352 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
353 if( http_hdr->body_size>0 ) {
354 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
355 http_free( http_hdr );
356 return -1;
359 break;
360 default:
361 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase );
362 close( fd );
363 fd = -1;
365 stream->fd = fd;
367 if( http_hdr ) {
368 http_free( http_hdr );
369 stream->streaming_ctrl->data = NULL;
372 stream->pos=pos;
374 return 1;
379 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
380 //printf("streaming_bufferize\n");
381 streaming_ctrl->buffer = malloc(size);
382 if( streaming_ctrl->buffer==NULL ) {
383 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
384 return -1;
386 memcpy( streaming_ctrl->buffer, buffer, size );
387 streaming_ctrl->buffer_size = size;
388 return size;
392 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
393 int len=0;
394 //printf("nop_streaming_read\n");
395 if( stream_ctrl->buffer_size!=0 ) {
396 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
397 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
398 len = (size<buffer_len)?size:buffer_len;
399 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
400 stream_ctrl->buffer_pos += len;
401 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
402 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
403 free( stream_ctrl->buffer );
404 stream_ctrl->buffer = NULL;
405 stream_ctrl->buffer_size = 0;
406 stream_ctrl->buffer_pos = 0;
407 //printf("buffer cleaned\n");
409 //printf("read %d bytes from buffer\n", len );
412 if( len<size ) {
413 int ret;
414 ret = recv( fd, buffer+len, size-len, 0 );
415 if( ret<0 ) {
416 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
418 len += ret;
419 //printf("read %d bytes from network\n", len );
422 return len;
426 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
427 return -1;
428 // To shut up gcc warning
429 fd++;
430 pos++;
431 stream_ctrl=NULL;
435 void fixup_network_stream_cache(stream_t *stream) {
436 if(stream->streaming_ctrl->buffering) {
437 if(stream_cache_size<0) {
438 // cache option not set, will use our computed value.
439 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
440 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
441 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
443 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_CacheSizeSetTo, stream_cache_size);
449 streaming_stop( stream_t *stream ) {
450 stream->streaming_ctrl->status = streaming_stopped_e;
451 return 0;