Give better name to Inverse_Table_6_9
[mplayer/glamo.git] / stream / network.c
blob19a87ea2a98e0b29eb1d6c5d96d479effbc5fdf1
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 #if HAVE_WINSOCK2_H
23 #include <winsock2.h>
24 #include <ws2tcpip.h>
25 #endif
27 #include "stream.h"
28 #include "libmpdemux/demuxer.h"
29 #include "m_config.h"
31 #include "network.h"
32 #include "tcp.h"
33 #include "http.h"
34 #include "cookies.h"
35 #include "url.h"
37 #include "version.h"
39 extern int stream_cache_size;
41 /* Variables for the command line option -user, -passwd, -bandwidth,
42 -user-agent and -nocookies */
44 char *network_username=NULL;
45 char *network_password=NULL;
46 int network_bandwidth=0;
47 int network_cookies_enabled = 0;
48 char *network_useragent=NULL;
50 /* IPv6 options */
51 int network_ipv4_only_proxy = 0;
54 const mime_struct_t mime_type_table[] = {
55 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
56 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
57 // MPEG streaming
58 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
59 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
60 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
61 // AVI ??? => video/x-msvideo
62 { "video/x-msvideo", DEMUXER_TYPE_AVI },
63 // MOV => video/quicktime
64 { "video/quicktime", DEMUXER_TYPE_MOV },
65 // ASF
66 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
67 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
68 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
69 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
70 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
71 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
72 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
73 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
74 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
75 // Playlists
76 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
77 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
78 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
79 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
80 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
81 // Real Media
82 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
83 // OGG Streaming
84 { "application/x-ogg", DEMUXER_TYPE_OGG },
85 // NullSoft Streaming Video
86 { "video/nsv", DEMUXER_TYPE_NSV},
87 { "misc/ultravox", DEMUXER_TYPE_NSV},
88 #ifdef CONFIG_LIBAVFORMAT
89 // Flash Video
90 { "video/x-flv", DEMUXER_TYPE_LAVF},
91 #endif
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 = -1;
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 if( strcasecmp(url->protocol, "noicyx") )
209 http_set_field(http_hdr, "Icy-MetaData: 1");
211 if(pos>0) {
212 // Extend http_send_request with possibility to do partial content retrieval
213 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
214 http_set_field(http_hdr, str);
217 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
219 http_set_field( http_hdr, "Connection: close");
220 http_add_basic_authentication( http_hdr, url->username, url->password );
221 if( http_build_request( http_hdr )==NULL ) {
222 goto err_out;
225 if( proxy ) {
226 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
227 fd = connect2Server( url->hostname, url->port,1 );
228 url_free( server_url );
229 server_url = NULL;
230 } else {
231 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
232 fd = connect2Server( server_url->hostname, server_url->port,1 );
234 if( fd<0 ) {
235 goto err_out;
237 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
239 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
240 if( ret!=(int)http_hdr->buffer_size ) {
241 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
242 goto err_out;
245 http_free( http_hdr );
247 return fd;
248 err_out:
249 if (fd > 0) closesocket(fd);
250 http_free(http_hdr);
251 if (proxy && server_url)
252 url_free(server_url);
253 return -1;
256 HTTP_header_t *
257 http_read_response( int fd ) {
258 HTTP_header_t *http_hdr;
259 char response[BUFFER_SIZE];
260 int i;
262 http_hdr = http_new_header();
263 if( http_hdr==NULL ) {
264 return NULL;
267 do {
268 i = recv( fd, response, BUFFER_SIZE, 0 );
269 if( i<0 ) {
270 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed);
271 http_free( http_hdr );
272 return NULL;
274 if( i==0 ) {
275 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
276 http_free( http_hdr );
277 return NULL;
279 http_response_append( http_hdr, response, i );
280 } while( !http_is_header_entire( http_hdr ) );
281 http_response_parse( http_hdr );
282 return http_hdr;
286 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
287 char *aut;
289 if( *auth_retry==1 ) {
290 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
291 return -1;
293 if( *auth_retry>0 ) {
294 if( url->username ) {
295 free( url->username );
296 url->username = NULL;
298 if( url->password ) {
299 free( url->password );
300 url->password = NULL;
304 aut = http_get_field(http_hdr, "WWW-Authenticate");
305 if( aut!=NULL ) {
306 char *aut_space;
307 aut_space = strstr(aut, "realm=");
308 if( aut_space!=NULL ) aut_space += 6;
309 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequiredFor, aut_space);
310 } else {
311 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequired);
313 if( network_username ) {
314 url->username = strdup(network_username);
315 if( url->username==NULL ) {
316 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
317 return -1;
319 } else {
320 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
321 return -1;
323 if( network_password ) {
324 url->password = strdup(network_password);
325 if( url->password==NULL ) {
326 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
327 return -1;
329 } else {
330 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank);
332 (*auth_retry)++;
333 return 0;
337 http_seek( stream_t *stream, off_t pos ) {
338 HTTP_header_t *http_hdr = NULL;
339 int fd;
340 if( stream==NULL ) return 0;
342 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
343 fd = http_send_request( stream->streaming_ctrl->url, pos );
344 if( fd<0 ) return 0;
346 http_hdr = http_read_response( fd );
348 if( http_hdr==NULL ) return 0;
350 switch( http_hdr->status_code ) {
351 case 200:
352 case 206: // OK
353 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
354 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
355 if( http_hdr->body_size>0 ) {
356 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
357 http_free( http_hdr );
358 return -1;
361 break;
362 default:
363 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase );
364 close( fd );
365 fd = -1;
367 stream->fd = fd;
369 if( http_hdr ) {
370 http_free( http_hdr );
371 stream->streaming_ctrl->data = NULL;
374 stream->pos=pos;
376 return 1;
381 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
382 //printf("streaming_bufferize\n");
383 streaming_ctrl->buffer = malloc(size);
384 if( streaming_ctrl->buffer==NULL ) {
385 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
386 return -1;
388 memcpy( streaming_ctrl->buffer, buffer, size );
389 streaming_ctrl->buffer_size = size;
390 return size;
394 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
395 int len=0;
396 //printf("nop_streaming_read\n");
397 if( stream_ctrl->buffer_size!=0 ) {
398 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
399 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
400 len = (size<buffer_len)?size:buffer_len;
401 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
402 stream_ctrl->buffer_pos += len;
403 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
404 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
405 free( stream_ctrl->buffer );
406 stream_ctrl->buffer = NULL;
407 stream_ctrl->buffer_size = 0;
408 stream_ctrl->buffer_pos = 0;
409 //printf("buffer cleaned\n");
411 //printf("read %d bytes from buffer\n", len );
414 if( len<size ) {
415 int ret;
416 ret = recv( fd, buffer+len, size-len, 0 );
417 if( ret<0 ) {
418 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
420 len += ret;
421 //printf("read %d bytes from network\n", len );
424 return len;
428 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
429 return -1;
430 // To shut up gcc warning
431 fd++;
432 pos++;
433 stream_ctrl=NULL;
437 void fixup_network_stream_cache(stream_t *stream) {
438 if(stream->streaming_ctrl->buffering) {
439 if(stream_cache_size<0) {
440 // cache option not set, will use our computed value.
441 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
442 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
443 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
445 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_CacheSizeSetTo, stream_cache_size);
451 streaming_stop( stream_t *stream ) {
452 stream->streaming_ctrl->status = streaming_stopped_e;
453 return 0;