Comment out the correct #endif directive.
[mplayer/greg.git] / stream / network.c
blob6874d9a9eac8dddaae79581aa481372569a5440e
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 /* Variables for the command line option -user, -passwd, -bandwidth,
44 -user-agent and -nocookies */
46 char *network_username=NULL;
47 char *network_password=NULL;
48 int network_bandwidth=0;
49 int network_cookies_enabled = 0;
50 char *network_useragent=NULL;
52 /* IPv6 options */
53 int network_ipv4_only_proxy = 0;
56 const mime_struct_t mime_type_table[] = {
57 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
58 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
59 // MPEG streaming
60 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
61 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
62 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
63 // AVI ??? => video/x-msvideo
64 { "video/x-msvideo", DEMUXER_TYPE_AVI },
65 // MOV => video/quicktime
66 { "video/quicktime", DEMUXER_TYPE_MOV },
67 // ASF
68 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
69 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
70 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
71 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
72 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
73 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
74 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
75 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
76 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
77 // Playlists
78 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
79 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
80 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
81 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
82 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
83 // Real Media
84 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
85 // OGG Streaming
86 { "application/x-ogg", DEMUXER_TYPE_OGG },
87 // NullSoft Streaming Video
88 { "video/nsv", DEMUXER_TYPE_NSV},
89 { "misc/ultravox", DEMUXER_TYPE_NSV},
90 #ifdef USE_LIBAVFORMAT
91 // Flash Video
92 { "video/x-flv", DEMUXER_TYPE_LAVF},
93 #endif
94 { NULL, DEMUXER_TYPE_UNKNOWN},
98 streaming_ctrl_t *
99 streaming_ctrl_new(void) {
100 streaming_ctrl_t *streaming_ctrl;
101 streaming_ctrl = malloc(sizeof(streaming_ctrl_t));
102 if( streaming_ctrl==NULL ) {
103 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
104 return NULL;
106 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) );
107 return streaming_ctrl;
110 void
111 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
112 if( streaming_ctrl==NULL ) return;
113 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
114 if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer );
115 if( streaming_ctrl->data ) free( streaming_ctrl->data );
116 free( streaming_ctrl );
119 URL_t*
120 check4proxies( URL_t *url ) {
121 URL_t *url_out = NULL;
122 if( url==NULL ) return NULL;
123 url_out = url_new( url->url );
124 if( !strcasecmp(url->protocol, "http_proxy") ) {
125 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
126 return url_out;
128 // Check if the http_proxy environment variable is set.
129 if( !strcasecmp(url->protocol, "http") ) {
130 char *proxy;
131 proxy = getenv("http_proxy");
132 if( proxy!=NULL ) {
133 // We got a proxy, build the URL to use it
134 int len;
135 char *new_url;
136 URL_t *tmp_url;
137 URL_t *proxy_url = url_new( proxy );
139 if( proxy_url==NULL ) {
140 mp_msg(MSGT_NETWORK,MSGL_WARN,
141 MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout);
142 return url_out;
145 #ifdef HAVE_AF_INET6
146 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
147 mp_msg(MSGT_NETWORK,MSGL_WARN,
148 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy);
149 url_free(proxy_url);
150 return url_out;
152 #endif
154 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
155 len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port
156 new_url = malloc( len+1 );
157 if( new_url==NULL ) {
158 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
159 url_free(proxy_url);
160 return url_out;
162 sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url );
163 tmp_url = url_new( new_url );
164 if( tmp_url==NULL ) {
165 free( new_url );
166 url_free( proxy_url );
167 return url_out;
169 url_free( url_out );
170 url_out = tmp_url;
171 free( new_url );
172 url_free( proxy_url );
175 return url_out;
179 http_send_request( URL_t *url, off_t pos ) {
180 HTTP_header_t *http_hdr;
181 URL_t *server_url;
182 char str[256];
183 int fd = -1;
184 int ret;
185 int proxy = 0; // Boolean
187 http_hdr = http_new_header();
189 if( !strcasecmp(url->protocol, "http_proxy") ) {
190 proxy = 1;
191 server_url = url_new( (url->file)+1 );
192 http_set_uri( http_hdr, server_url->url );
193 } else {
194 server_url = url;
195 http_set_uri( http_hdr, server_url->file );
197 if (server_url->port && server_url->port != 80)
198 snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
199 else
200 snprintf(str, 256, "Host: %s", server_url->hostname );
201 http_set_field( http_hdr, str);
202 if (network_useragent)
204 snprintf(str, 256, "User-Agent: %s", network_useragent);
205 http_set_field(http_hdr, str);
207 else
208 http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION);
210 http_set_field(http_hdr, "Icy-MetaData: 1");
212 if(pos>0) {
213 // Extend http_send_request with possibility to do partial content retrieval
214 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
215 http_set_field(http_hdr, str);
218 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
220 http_set_field( http_hdr, "Connection: close");
221 http_add_basic_authentication( http_hdr, url->username, url->password );
222 if( http_build_request( http_hdr )==NULL ) {
223 goto err_out;
226 if( proxy ) {
227 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
228 fd = connect2Server( url->hostname, url->port,1 );
229 url_free( server_url );
230 server_url = NULL;
231 } else {
232 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
233 fd = connect2Server( server_url->hostname, server_url->port,1 );
235 if( fd<0 ) {
236 goto err_out;
238 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
240 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
241 if( ret!=(int)http_hdr->buffer_size ) {
242 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
243 goto err_out;
246 http_free( http_hdr );
248 return fd;
249 err_out:
250 if (fd > 0) closesocket(fd);
251 http_free(http_hdr);
252 if (proxy && server_url)
253 url_free(server_url);
254 return -1;
257 HTTP_header_t *
258 http_read_response( int fd ) {
259 HTTP_header_t *http_hdr;
260 char response[BUFFER_SIZE];
261 int i;
263 http_hdr = http_new_header();
264 if( http_hdr==NULL ) {
265 return NULL;
268 do {
269 i = recv( fd, response, BUFFER_SIZE, 0 );
270 if( i<0 ) {
271 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed);
272 http_free( http_hdr );
273 return NULL;
275 if( i==0 ) {
276 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
277 http_free( http_hdr );
278 return NULL;
280 http_response_append( http_hdr, response, i );
281 } while( !http_is_header_entire( http_hdr ) );
282 http_response_parse( http_hdr );
283 return http_hdr;
287 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
288 char *aut;
290 if( *auth_retry==1 ) {
291 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
292 return -1;
294 if( *auth_retry>0 ) {
295 if( url->username ) {
296 free( url->username );
297 url->username = NULL;
299 if( url->password ) {
300 free( url->password );
301 url->password = NULL;
305 aut = http_get_field(http_hdr, "WWW-Authenticate");
306 if( aut!=NULL ) {
307 char *aut_space;
308 aut_space = strstr(aut, "realm=");
309 if( aut_space!=NULL ) aut_space += 6;
310 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequiredFor, aut_space);
311 } else {
312 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequired);
314 if( network_username ) {
315 url->username = strdup(network_username);
316 if( url->username==NULL ) {
317 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
318 return -1;
320 } else {
321 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
322 return -1;
324 if( network_password ) {
325 url->password = strdup(network_password);
326 if( url->password==NULL ) {
327 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
328 return -1;
330 } else {
331 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank);
333 (*auth_retry)++;
334 return 0;
338 http_seek( stream_t *stream, off_t pos ) {
339 HTTP_header_t *http_hdr = NULL;
340 int fd;
341 if( stream==NULL ) return 0;
343 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
344 fd = http_send_request( stream->streaming_ctrl->url, pos );
345 if( fd<0 ) return 0;
347 http_hdr = http_read_response( fd );
349 if( http_hdr==NULL ) return 0;
351 switch( http_hdr->status_code ) {
352 case 200:
353 case 206: // OK
354 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
355 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
356 if( http_hdr->body_size>0 ) {
357 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
358 http_free( http_hdr );
359 return -1;
362 break;
363 default:
364 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase );
365 close( fd );
366 fd = -1;
368 stream->fd = fd;
370 if( http_hdr ) {
371 http_free( http_hdr );
372 stream->streaming_ctrl->data = NULL;
375 stream->pos=pos;
377 return 1;
382 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
383 //printf("streaming_bufferize\n");
384 streaming_ctrl->buffer = malloc(size);
385 if( streaming_ctrl->buffer==NULL ) {
386 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
387 return -1;
389 memcpy( streaming_ctrl->buffer, buffer, size );
390 streaming_ctrl->buffer_size = size;
391 return size;
395 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
396 int len=0;
397 //printf("nop_streaming_read\n");
398 if( stream_ctrl->buffer_size!=0 ) {
399 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
400 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
401 len = (size<buffer_len)?size:buffer_len;
402 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
403 stream_ctrl->buffer_pos += len;
404 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
405 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
406 free( stream_ctrl->buffer );
407 stream_ctrl->buffer = NULL;
408 stream_ctrl->buffer_size = 0;
409 stream_ctrl->buffer_pos = 0;
410 //printf("buffer cleaned\n");
412 //printf("read %d bytes from buffer\n", len );
415 if( len<size ) {
416 int ret;
417 ret = recv( fd, buffer+len, size-len, 0 );
418 if( ret<0 ) {
419 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
421 len += ret;
422 //printf("read %d bytes from network\n", len );
425 return len;
429 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
430 return -1;
431 // To shut up gcc warning
432 fd++;
433 pos++;
434 stream_ctrl=NULL;
438 void fixup_network_stream_cache(stream_t *stream) {
439 if(stream->streaming_ctrl->buffering) {
440 if(stream_cache_size<0) {
441 // cache option not set, will use our computed value.
442 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
443 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
444 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
446 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_CacheSizeSetTo, stream_cache_size);
452 streaming_stop( stream_t *stream ) {
453 stream->streaming_ctrl->status = streaming_stopped_e;
454 return 0;