]> git.sesse.net Git - vlc/blob - modules/access/http.c
* modules/access/*: strings review + coding style fixes.
[vlc] / modules / access / http.c
1 /*****************************************************************************
2  * http.c: HTTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id: http.c,v 1.57 2004/01/25 17:31:22 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "vlc_playlist.h"
34 #include "network.h"
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 #define PROXY_TEXT N_("HTTP proxy")
43 #define PROXY_LONGTEXT N_( \
44     "You can specify an HTTP proxy to use. It must be of the form " \
45     "http://myproxy.mydomain:myport/. If none is specified, the HTTP_PROXY " \
46     "environment variable will be tried." )
47
48 #define CACHING_TEXT N_("Caching value in ms")
49 #define CACHING_LONGTEXT N_( \
50     "Allows you to modify the default caching value for http streams. This " \
51     "value should be set in millisecond units." )
52
53 #define USER_TEXT N_("HTTP user name")
54 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
55     "be used for the connection (Basic authentification only).")
56
57 #define PASS_TEXT N_("HTTP password")
58 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
59     "used for the connection.")
60
61 #define AGENT_TEXT N_("HTTP user agent")
62 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
63     "used for the connection.")
64
65 vlc_module_begin();
66     set_description( _("HTTP input") );
67     set_capability( "access", 0 );
68
69     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
70                 VLC_FALSE );
71     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
72                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
73     add_string( "http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_FALSE );
74     add_string( "http-pwd", NULL , NULL, PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
75     add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
76                 AGENT_LONGTEXT, VLC_FALSE );
77
78     add_shortcut( "http" );
79     add_shortcut( "http4" );
80     add_shortcut( "http6" );
81     set_callbacks( Open, Close );
82 vlc_module_end();
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 struct access_sys_t
88 {
89     int fd;
90
91     /* From uri */
92     vlc_url_t url;
93     char    *psz_user;
94     char    *psz_passwd;
95     char    *psz_user_agent;
96
97     /* Proxy */
98     vlc_bool_t b_proxy;
99     vlc_url_t  proxy;
100
101     /* */
102     int        i_code;
103     char      *psz_protocol;
104     int        i_version;
105
106     char       *psz_mime;
107     char       *psz_location;
108
109     int64_t    i_tell;
110     int64_t    i_size;
111 };
112
113 static void    Seek( input_thread_t *, off_t );
114 static ssize_t Read( input_thread_t *, byte_t *, size_t );
115
116 static void    ParseURL( access_sys_t *, char *psz_url );
117 static int     Connect( input_thread_t *, vlc_bool_t *, off_t *, off_t );
118
119 static char *b64_encode( unsigned char *src );
120
121 /*****************************************************************************
122  * Open:
123  *****************************************************************************/
124 static int  Open ( vlc_object_t *p_this )
125 {
126     input_thread_t *p_input = (input_thread_t*)p_this;
127     access_sys_t   *p_sys;
128     vlc_value_t    val;
129
130     /* Create private struct */
131     p_sys = p_input->p_access_data = malloc( sizeof( access_sys_t ) );
132     memset( p_sys, 0, sizeof( access_sys_t ) );
133     p_sys->fd = -1;
134     p_sys->b_proxy = VLC_FALSE;
135     p_sys->i_version = 1;
136     p_sys->psz_mime = NULL;
137     p_sys->psz_location = NULL;
138     p_sys->psz_user_agent = NULL;
139
140     /* First set ipv4/ipv6 */
141     var_Create( p_input, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
142     var_Create( p_input, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
143
144     if( *p_input->psz_access )
145     {
146         /* Find out which shortcut was used */
147         if( !strncmp( p_input->psz_access, "http4", 6 ) )
148         {
149             val.b_bool = VLC_TRUE;
150             var_Set( p_input, "ipv4", val );
151
152             val.b_bool = VLC_FALSE;
153             var_Set( p_input, "ipv6", val );
154         }
155         else if( !strncmp( p_input->psz_access, "http6", 6 ) )
156         {
157             val.b_bool = VLC_TRUE;
158             var_Set( p_input, "ipv6", val );
159
160             val.b_bool = VLC_FALSE;
161             var_Set( p_input, "ipv4", val );
162         }
163     }
164
165     /* Parse URI */
166     ParseURL( p_sys, p_input->psz_name );
167     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
168     {
169         msg_Warn( p_input, "invalid host" );
170         goto error;
171     }
172     if( p_sys->url.i_port <= 0 )
173     {
174         p_sys->url.i_port = 80;
175     }
176     if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
177     {
178         var_Create( p_input, "http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
179         var_Get( p_input, "http-user", &val );
180         p_sys->psz_user = val.psz_string;
181
182         var_Create( p_input, "http-pwd", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
183         var_Get( p_input, "http-pwd", &val );
184         p_sys->psz_passwd = val.psz_string;
185     }
186
187     /* Do user agent */
188     var_Create( p_input, "http-user-agent", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
189     var_Get( p_input, "http-user-agent", &val );
190     p_sys->psz_user_agent = val.psz_string;
191
192     /* Check proxy */
193     var_Create( p_input, "http-proxy", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
194     var_Get( p_input, "http-proxy", &val );
195     if( val.psz_string && *val.psz_string )
196     {
197         p_sys->b_proxy = VLC_TRUE;
198         vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
199     }
200     else
201     {
202         char *psz_proxy = getenv( "http_proxy" );
203         if( psz_proxy && *psz_proxy )
204         {
205             p_sys->b_proxy = VLC_TRUE;
206             vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
207         }
208         if( psz_proxy )
209         {
210             free( psz_proxy );
211         }
212     }
213     if( val.psz_string )
214     {
215         free( val.psz_string );
216     }
217
218     if( p_sys->b_proxy )
219     {
220         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
221         {
222             msg_Warn( p_input, "invalid proxy host" );
223             goto error;
224         }
225         if( p_sys->proxy.i_port <= 0 )
226         {
227             p_sys->proxy.i_port = 80;
228         }
229     }
230
231     msg_Dbg( p_input, "http: server='%s' port=%d file='%s",
232              p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
233     if( p_sys->b_proxy )
234     {
235         msg_Dbg( p_input, "      proxy %s:%d", p_sys->proxy.psz_host,
236                  p_sys->proxy.i_port );
237     }
238     if( p_sys->psz_user && *p_sys->psz_user )
239     {
240         msg_Dbg( p_input, "      user='%s', pwd='%s'",
241                  p_sys->psz_user, p_sys->psz_passwd );
242     }
243
244     /* Connect */
245     if( Connect( p_input, &p_input->stream.b_seekable,
246                  &p_input->stream.p_selected_area->i_size, 0 ) )
247     {
248         /* Retry with http 1.0 */
249         p_sys->i_version = 0;
250
251         if( p_input->b_die ||
252             Connect( p_input, &p_input->stream.b_seekable,
253                      &p_input->stream.p_selected_area->i_size, 0 ) )
254         {
255             goto error;
256         }
257     }
258
259     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
260           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
261         p_sys->psz_location && *p_sys->psz_location )
262     {
263         playlist_t * p_playlist;
264
265         msg_Dbg( p_input, "redirection to %s", p_sys->psz_location );
266
267         p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
268         if( !p_playlist )
269         {
270             msg_Err( p_input, "redirection failed: can't find playlist" );
271             goto error;
272         }
273         p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
274         playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
275                       PLAYLIST_INSERT | PLAYLIST_GO,
276                       p_playlist->i_index + 1 );
277         vlc_object_release( p_playlist );
278
279         p_sys->i_size = 0;  /* Force to stop reading */
280     }
281
282     /* Finish to set up p_input */
283     p_input->pf_read = Read;
284     p_input->pf_set_program = input_SetProgram;
285     p_input->pf_set_area = NULL;
286     p_input->pf_seek = Seek;
287
288     vlc_mutex_lock( &p_input->stream.stream_lock );
289     p_input->stream.b_pace_control = VLC_TRUE;
290     p_input->stream.p_selected_area->i_tell = 0;
291     p_input->stream.i_method = INPUT_METHOD_NETWORK;
292     vlc_mutex_unlock( &p_input->stream.stream_lock );
293     p_input->i_mtu = 0;
294     if( !strcmp( p_sys->psz_protocol, "ICY" ) &&
295         ( !p_input->psz_demux || !*p_input->psz_demux ) )
296     {
297         if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
298         {
299             p_input->psz_demux = strdup( "nsv" );
300         }
301         else
302         {
303             p_input->psz_demux = strdup( "mp3" );
304         }
305         msg_Info( p_input, "ICY server found, %s demuxer selected",
306                   p_input->psz_demux );
307     }
308
309     /* Update default_pts to a suitable value for http access */
310     var_Create( p_input, "http-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
311     var_Get( p_input, "http-caching", &val );
312     p_input->i_pts_delay = val.i_int * 1000;
313
314     return VLC_SUCCESS;
315
316 error:
317     vlc_UrlClean( &p_sys->url );
318     vlc_UrlClean( &p_sys->proxy );
319     if( p_sys->psz_mime ) free( p_sys->psz_mime );
320     if( p_sys->psz_location ) free( p_sys->psz_location );
321     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
322     if( p_sys->psz_user ) free( p_sys->psz_user );
323     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
324
325     if( p_sys->fd > 0 )
326     {
327         net_Close( p_sys->fd );
328     }
329     free( p_sys );
330     return VLC_EGENERIC;
331 }
332
333 /*****************************************************************************
334  * Close:
335  *****************************************************************************/
336 static void Close( vlc_object_t *p_this )
337 {
338     input_thread_t *p_input = (input_thread_t*)p_this;
339     access_sys_t   *p_sys   = p_input->p_access_data;
340
341     vlc_UrlClean( &p_sys->url );
342     vlc_UrlClean( &p_sys->proxy );
343
344     if( p_sys->psz_user ) free( p_sys->psz_user );
345     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
346
347     if( p_sys->psz_mime ) free( p_sys->psz_mime );
348     if( p_sys->psz_location ) free( p_sys->psz_location );
349
350     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
351
352     if( p_sys->fd > 0 )
353     {
354         net_Close( p_sys->fd );
355     }
356     free( p_sys );
357 }
358
359 /*****************************************************************************
360  * Seek: close and re-open a connection at the right place
361  *****************************************************************************/
362 static void Seek( input_thread_t * p_input, off_t i_pos )
363 {
364     access_sys_t   *p_sys   = p_input->p_access_data;
365
366     msg_Dbg( p_input, "trying to seek to "I64Fd, i_pos );
367
368     net_Close( p_sys->fd ); p_sys->fd = -1;
369
370     if( Connect( p_input, &p_input->stream.b_seekable,
371                  &p_input->stream.p_selected_area->i_size, i_pos ) )
372     {
373         msg_Err( p_input, "seek failed" );
374     }
375
376     vlc_mutex_lock( &p_input->stream.stream_lock );
377     p_input->stream.p_selected_area->i_tell = i_pos;
378     vlc_mutex_unlock( &p_input->stream.stream_lock );
379 }
380
381 /*****************************************************************************
382  * Read: Read up to i_len bytes from the http connection and place in
383  * p_buffer. Return the actual number of bytes read
384  *****************************************************************************/
385 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
386 {
387     access_sys_t   *p_sys   = p_input->p_access_data;
388     int            i_read;
389
390     if( p_sys->fd < 0 )
391     {
392         return -1;
393     }
394     if( p_sys->i_size > 0 && i_len + p_sys->i_tell > p_sys->i_size )
395     {
396         if( ( i_len = p_sys->i_size - p_sys->i_tell ) == 0 )
397         {
398             return 0;
399         }
400     }
401
402     i_read = net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE );
403     if( i_read > 0 )
404     {
405         p_sys->i_tell += i_read;
406     }
407     return i_read;
408 }
409
410 /*****************************************************************************
411  * ParseURL: extract user:password
412  *****************************************************************************/
413 static void ParseURL( access_sys_t *p_sys, char *psz_url )
414 {
415     char *psz_dup = strdup( psz_url );
416     char *p = psz_dup;
417     char *psz;
418
419     /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
420     while( *p == '/' )
421     {
422         p++;
423     }
424     psz = p;
425
426     /* Parse auth */
427     if( ( p = strchr( psz, '@' ) ) )
428     {
429         char *comma;
430
431         *p++ = '\0';
432         comma = strchr( psz, ':' );
433
434         /* Retreive user:password */
435         if( comma )
436         {
437             *comma++ = '\0';
438
439             p_sys->psz_user = strdup( psz );
440             p_sys->psz_passwd = strdup( comma );
441         }
442         else
443         {
444             p_sys->psz_user = strdup( psz );
445         }
446     }
447     else
448     {
449         p = psz;
450     }
451
452     /* Parse uri */
453     vlc_UrlParse( &p_sys->url, p, 0 );
454
455     free( psz_dup );
456 }
457
458 /*****************************************************************************
459  * Connect:
460  *****************************************************************************/
461 static int Connect( input_thread_t *p_input, vlc_bool_t *pb_seekable,
462                     off_t *pi_size, off_t i_tell )
463 {
464     access_sys_t   *p_sys   = p_input->p_access_data;
465     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
466     char           *psz;
467
468     /* Clean info */
469     if( p_sys->psz_location ) free( p_sys->psz_location );
470     if( p_sys->psz_mime ) free( p_sys->psz_mime );
471
472     p_sys->psz_location = NULL;
473     p_sys->psz_mime = NULL;
474     p_sys->i_size = -1;
475     p_sys->i_tell = i_tell;
476
477
478     /* Open connection */
479     p_sys->fd = net_OpenTCP( p_input, srv.psz_host, srv.i_port );
480     if( p_sys->fd < 0 )
481     {
482         msg_Err( p_input, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
483         return VLC_EGENERIC;
484     }
485
486     if( p_sys->b_proxy )
487     {
488         net_Printf( VLC_OBJECT(p_input), p_sys->fd,
489                     "GET http://%s:%d/%s HTTP/1.%d\r\n",
490                     p_sys->url.psz_host, p_sys->url.i_port,
491                     p_sys->url.psz_path, p_sys->i_version );
492     }
493     else
494     {
495         char *psz_path = p_sys->url.psz_path;
496         if( !psz_path || !*psz_path )
497         {
498             psz_path = "/";
499         }
500         net_Printf( VLC_OBJECT(p_input), p_sys->fd,
501                     "GET %s HTTP/1.%d\r\nHost: %s\r\n",
502                     psz_path, p_sys->i_version, p_sys->url.psz_host );
503     }
504     /* User Agent */
505     net_Printf( VLC_OBJECT(p_input), p_sys->fd, "User-Agent: %s\r\n",
506                 p_sys->psz_user_agent );
507     /* Offset */
508     if( p_sys->i_version == 1 )
509     {
510         net_Printf( VLC_OBJECT(p_input), p_sys->fd,
511                     "Range: bytes="I64Fd"-\r\n", i_tell );
512     }
513     /* Authentification */
514     if( p_sys->psz_user && *p_sys->psz_user )
515     {
516         char *buf;
517         char *b64;
518
519         asprintf( &buf, "%s:%s", p_sys->psz_user,
520                    p_sys->psz_passwd ? p_sys->psz_passwd : "" );
521
522         b64 = b64_encode( buf );
523
524         net_Printf( VLC_OBJECT(p_input), p_sys->fd,
525                     "Authorization: Basic %s", b64 );
526         free( b64 );
527     }
528     net_Printf( VLC_OBJECT(p_input), p_sys->fd, "Connection: Close\r\n" );
529
530     if( net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" ) < 0 )
531     {
532         msg_Err( p_input, "failed to send request" );
533         net_Close( p_sys->fd ); p_sys->fd = -1;
534         return VLC_EGENERIC;
535     }
536
537     /* Set values */
538     *pb_seekable = p_sys->i_version == 1 ? VLC_TRUE : VLC_FALSE;
539     *pi_size = 0;
540
541     /* Read Answer */
542     if( ( psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd ) ) == NULL )
543     {
544         msg_Err( p_input, "failed to read answer" );
545         goto error;
546     }
547     if( !strncmp( psz, "HTTP/1.", 7 ) )
548     {
549         p_sys->psz_protocol = "HTTP";
550         p_sys->i_code = atoi( &psz[9] );
551     }
552     else if( !strncmp( psz, "ICY", 3 ) )
553     {
554         p_sys->psz_protocol = "ICY";
555         p_sys->i_code = atoi( &psz[4] );
556     }
557     else
558     {
559         msg_Err( p_input, "invalid HTTP reply '%s'", psz );
560         free( psz );
561         goto error;
562     }
563     msg_Dbg( p_input, "protocol '%s' answer code %d",
564              p_sys->psz_protocol, p_sys->i_code );
565     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
566     {
567         *pb_seekable = VLC_FALSE;
568     }
569     if( p_sys->i_code != 206 )
570     {
571         *pb_seekable = VLC_FALSE;
572     }
573     if( p_sys->i_code >= 400 )
574     {
575         msg_Err( p_input, "error: %s", psz );
576         free( psz );
577         goto error;
578     }
579     free( psz );
580
581     for( ;; )
582     {
583         char *psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd );
584         char *p;
585
586         if( psz == NULL )
587         {
588             msg_Err( p_input, "failed to read answer" );
589             goto error;
590         }
591
592         /* msg_Dbg( p_input, "Line=%s", psz ); */
593         if( *psz == '\0' )
594         {
595             free( psz );
596             break;
597         }
598
599
600         if( ( p = strchr( psz, ':' ) ) == NULL )
601         {
602             msg_Err( p_input, "malformed header line: %s", psz );
603             free( psz );
604             goto error;
605         }
606         *p++ = '\0';
607
608         if( !strcasecmp( psz, "Content-Length" ) )
609         {
610             *pi_size = p_sys->i_size = i_tell + atoll( p );
611             msg_Dbg( p_input, "stream size="I64Fd, p_sys->i_size );
612         }
613         else if( !strcasecmp( psz, "Location" ) )
614         {
615             if( p_sys->psz_location ) free( p_sys->psz_location );
616             p_sys->psz_location = strdup( p );
617         }
618         else if( !strcasecmp( psz, "Content-Type" ) )
619         {
620             if( p_sys->psz_mime ) free( p_sys->psz_mime );
621             p_sys->psz_mime = strdup( p );
622         }
623
624         free( psz );
625     }
626     return VLC_SUCCESS;
627
628 error:
629     net_Close( p_sys->fd ); p_sys->fd = -1;
630     return VLC_EGENERIC;
631 }
632
633 /*****************************************************************************
634  * b64_encode:
635  *****************************************************************************/
636 static char *b64_encode( unsigned char *src )
637 {
638     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
639
640     char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
641     char *ret = dst;
642     unsigned i_bits = 0;
643     unsigned i_shift = 0;
644
645     for( ;; )
646     {
647         if( *src )
648         {
649             i_bits = ( i_bits << 8 )|( *src++ );
650             i_shift += 8;
651         }
652         else if( i_shift > 0 )
653         {
654            i_bits <<= 6 - i_shift;
655            i_shift = 6;
656         }
657         else
658         {
659             *dst++ = '=';
660             break;
661         }
662
663         while( i_shift >= 6 )
664         {
665             i_shift -= 6;
666             *dst++ = b64[(i_bits >> i_shift)&0x3f];
667         }
668     }
669
670     *dst++ = '\0';
671
672     return ret;
673 }