]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Jon Lech Johansen <jon@nanocrew.net>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_sout.h>
31 #include <vlc_block.h>
32
33
34 #include <vlc_input.h>
35 #include <vlc_playlist.h>
36
37 #ifdef HAVE_AVAHI_CLIENT
38     #include "bonjour.h"
39
40     #if defined( WIN32 )
41         #define DIRECTORY_SEPARATOR '\\'
42     #else
43         #define DIRECTORY_SEPARATOR '/'
44     #endif
45 #endif
46
47 #include "vlc_httpd.h"
48
49 #define DEFAULT_PORT        8080
50 #define DEFAULT_SSL_PORT    8443
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 static int  Open ( vlc_object_t * );
56 static void Close( vlc_object_t * );
57
58 #define SOUT_CFG_PREFIX "sout-http-"
59
60 #define USER_TEXT N_("Username")
61 #define USER_LONGTEXT N_("User name that will be " \
62                          "requested to access the stream." )
63 #define PASS_TEXT N_("Password")
64 #define PASS_LONGTEXT N_("Password that will be " \
65                          "requested to access the stream." )
66 #define MIME_TEXT N_("Mime")
67 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
68                         "if not specified)." )
69 #define CERT_TEXT N_( "Certificate file" )
70 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
71                           "be used for HTTPS." )
72 #define KEY_TEXT N_( "Private key file" )
73 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
74                          "be used for HTTPS. Leave " \
75                          "empty if you don't have one." )
76 #define CA_TEXT N_( "Root CA file" )
77 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
78                         "(certificate authority) file that will be used for " \
79                         "HTTPS. Leave empty if you " \
80                         "don't have one." )
81 #define CRL_TEXT N_( "CRL file" )
82 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
83                          "file that will be used for SSL. Leave " \
84                          "empty if you don't have one." )
85 #define BONJOUR_TEXT N_( "Advertise with Bonjour")
86 #define BONJOUR_LONGTEXT N_( "Advertise the stream with the Bonjour protocol." )
87
88
89 vlc_module_begin();
90     set_description( _("HTTP stream output") );
91     set_capability( "sout access", 0 );
92     set_shortname( "HTTP" );
93     add_shortcut( "http" );
94     add_shortcut( "https" );
95     add_shortcut( "mmsh" );
96     set_category( CAT_SOUT );
97     set_subcategory( SUBCAT_SOUT_ACO );
98     add_string( SOUT_CFG_PREFIX "user", "", NULL,
99                 USER_TEXT, USER_LONGTEXT, VLC_TRUE );
100         change_safe();
101     add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
102                 PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
103         change_safe();
104     add_string( SOUT_CFG_PREFIX "mime", "", NULL,
105                 MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
106         change_safe();
107     add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
108                 CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
109         change_safe();
110     add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
111                 KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
112         change_safe();
113     add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
114                 CA_TEXT, CA_LONGTEXT, VLC_TRUE );
115         change_safe();
116     add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
117                 CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
118         change_safe();
119     add_bool( SOUT_CFG_PREFIX "bonjour", VLC_FALSE, NULL,
120               BONJOUR_TEXT, BONJOUR_LONGTEXT, VLC_TRUE);
121         change_safe();
122     set_callbacks( Open, Close );
123 vlc_module_end();
124
125
126 /*****************************************************************************
127  * Exported prototypes
128  *****************************************************************************/
129 static const char *ppsz_sout_options[] = {
130     "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
131 };
132
133 static ssize_t Write( sout_access_out_t *, block_t * );
134 static int Seek ( sout_access_out_t *, off_t  );
135
136 struct sout_access_out_sys_t
137 {
138     /* host */
139     httpd_host_t        *p_httpd_host;
140
141     /* stream */
142     httpd_stream_t      *p_httpd_stream;
143
144     /* gather header from stream */
145     int                 i_header_allocated;
146     int                 i_header_size;
147     uint8_t             *p_header;
148     vlc_bool_t          b_header_complete;
149
150 #ifdef HAVE_AVAHI_CLIENT
151     void                *p_bonjour;
152 #endif
153 };
154
155 /*****************************************************************************
156  * Open: open the file
157  *****************************************************************************/
158 static int Open( vlc_object_t *p_this )
159 {
160     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
161     sout_access_out_sys_t   *p_sys;
162
163     char                *psz_parser;
164
165     char                *psz_bind_addr;
166     int                 i_bind_port;
167     char                *psz_file_name;
168     char                *psz_user = NULL;
169     char                *psz_pwd = NULL;
170     char                *psz_mime = NULL;
171     const char          *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
172                         *psz_crl = NULL;
173     vlc_value_t         val;
174
175     if( !( p_sys = p_access->p_sys =
176                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
177     {
178         msg_Err( p_access, "Not enough memory" );
179         return VLC_ENOMEM ;
180     }
181
182     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
183
184     /* p_access->psz_path = "hostname:port/filename" */
185     psz_bind_addr = psz_parser = strdup( p_access->psz_path );
186
187     i_bind_port = 0;
188     psz_file_name = NULL;
189
190     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
191     {
192         psz_parser++;
193     }
194     if( *psz_parser == ':' )
195     {
196         *psz_parser = '\0';
197         psz_parser++;
198         i_bind_port = atoi( psz_parser );
199
200         while( *psz_parser && *psz_parser != '/' )
201         {
202             psz_parser++;
203         }
204     }
205     if( *psz_parser == '/' )
206     {
207         *psz_parser = '\0';
208         psz_parser++;
209         psz_file_name = psz_parser;
210     }
211
212     if( psz_file_name == NULL )
213     {
214         psz_file_name = strdup( "/" );
215     }
216     else if( *psz_file_name != '/' )
217     {
218         char *p = psz_file_name;
219
220         psz_file_name = malloc( strlen( p ) + 2 );
221         strcpy( psz_file_name, "/" );
222         strcat( psz_file_name, p );
223     }
224     else
225     {
226         psz_file_name = strdup( psz_file_name );
227     }
228
229     /* SSL support */
230     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
231     {
232         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
233         psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
234         psz_ca = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
235         psz_crl = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
236
237         if( i_bind_port <= 0 )
238             i_bind_port = DEFAULT_SSL_PORT;
239     }
240     else
241     {
242         if( i_bind_port <= 0 )
243             i_bind_port = DEFAULT_PORT;
244     }
245
246     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
247                                             psz_bind_addr, i_bind_port,
248                                             psz_cert, psz_key, psz_ca,
249                                             psz_crl );
250     if( p_sys->p_httpd_host == NULL )
251     {
252         msg_Err( p_access, "cannot listen on %s:%d",
253                  psz_bind_addr, i_bind_port );
254         free( psz_file_name );
255         free( psz_bind_addr );
256         free( p_sys );
257         return VLC_EGENERIC;
258     }
259     free( psz_bind_addr );
260
261     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
262     {
263         psz_mime = strdup( "video/x-ms-asf-stream" );
264     }
265     else
266     {
267         var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
268         if( *val.psz_string )
269             psz_mime = val.psz_string;
270         else
271             free( val.psz_string );
272     }
273
274     var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
275     if( *val.psz_string )
276         psz_user = val.psz_string;
277     else
278         free( val.psz_string );
279
280     var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
281     if( *val.psz_string )
282         psz_pwd = val.psz_string;
283     else
284         free( val.psz_string );
285
286     p_sys->p_httpd_stream =
287         httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
288                          psz_user, psz_pwd, NULL );
289     free( psz_user );
290     free( psz_pwd );
291     free( psz_mime );
292
293     if( p_sys->p_httpd_stream == NULL )
294     {
295         msg_Err( p_access, "cannot add stream %s", psz_file_name );
296         httpd_HostDelete( p_sys->p_httpd_host );
297
298         free( psz_file_name );
299         free( p_sys );
300         return VLC_EGENERIC;
301     }
302
303 #ifdef HAVE_AVAHI_CLIENT
304     if( config_GetInt(p_this, SOUT_CFG_PREFIX "bonjour") )
305     {
306         char                *psz_txt, *psz_name;
307         playlist_t          *p_playlist = pl_Yield( p_access );
308
309         char *psz_uri = input_item_GetURI( p_playlist->status.p_item->p_input );
310         char *psz_newuri = psz_uri;
311         psz_name = strrchr( psz_newuri, DIRECTORY_SEPARATOR );
312         if( psz_name != NULL ) psz_name++;
313         else psz_name = psz_newuri;
314
315         if( psz_file_name &&
316             asprintf( &psz_txt, "path=%s", psz_file_name ) == -1 )
317             {
318                 pl_Release( p_playlist );
319                 free( psz_uri );
320                 return VLC_ENOMEM;
321             }
322
323         p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
324                                     strcmp( p_access->psz_access, "https" )
325                                        ? "_vlc-http._tcp" : "_vlc-https._tcp",
326                                              psz_name, i_bind_port, psz_txt );
327         free( psz_uri );
328         free( (void *)psz_txt );
329
330         if( p_sys->p_bonjour == NULL )
331             msg_Err( p_access, "unable to start requested Bonjour announce" );
332         pl_Release( p_playlist );
333     }
334     else
335         p_sys->p_bonjour = NULL;
336 #endif
337
338     free( psz_file_name );
339
340     p_sys->i_header_allocated = 1024;
341     p_sys->i_header_size      = 0;
342     p_sys->p_header           = malloc( p_sys->i_header_allocated );
343     p_sys->b_header_complete  = VLC_FALSE;
344
345     p_access->pf_write       = Write;
346     p_access->pf_seek        = Seek;
347
348
349     /* update p_sout->i_out_pace_nocontrol */
350     p_access->p_sout->i_out_pace_nocontrol++;
351
352     return VLC_SUCCESS;
353 }
354
355 /*****************************************************************************
356  * Close: close the target
357  *****************************************************************************/
358 static void Close( vlc_object_t * p_this )
359 {
360     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
361     sout_access_out_sys_t   *p_sys = p_access->p_sys;
362
363 #ifdef HAVE_AVAHI_CLIENT
364     if( p_sys->p_bonjour != NULL )
365         bonjour_stop_service( p_sys->p_bonjour );
366 #endif
367
368     /* update p_sout->i_out_pace_nocontrol */
369     p_access->p_sout->i_out_pace_nocontrol--;
370
371     httpd_StreamDelete( p_sys->p_httpd_stream );
372     httpd_HostDelete( p_sys->p_httpd_host );
373
374     free( p_sys->p_header );
375
376     msg_Dbg( p_access, "Close" );
377
378     free( p_sys );
379 }
380
381 /*****************************************************************************
382  * Write:
383  *****************************************************************************/
384 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
385 {
386     sout_access_out_sys_t *p_sys = p_access->p_sys;
387     int i_err = 0;
388     int i_len = 0;
389
390     while( p_buffer )
391     {
392         block_t *p_next;
393
394         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
395         {
396             /* gather header */
397             if( p_sys->b_header_complete )
398             {
399                 /* free previously gathered header */
400                 p_sys->i_header_size = 0;
401                 p_sys->b_header_complete = VLC_FALSE;
402             }
403             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
404                 p_sys->i_header_allocated )
405             {
406                 p_sys->i_header_allocated =
407                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
408                 p_sys->p_header =
409                     realloc( p_sys->p_header, p_sys->i_header_allocated );
410             }
411             memcpy( &p_sys->p_header[p_sys->i_header_size],
412                     p_buffer->p_buffer,
413                     p_buffer->i_buffer );
414             p_sys->i_header_size += p_buffer->i_buffer;
415         }
416         else if( !p_sys->b_header_complete )
417         {
418             p_sys->b_header_complete = VLC_TRUE;
419
420             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
421                                 p_sys->i_header_size );
422         }
423
424         i_len += p_buffer->i_buffer;
425         /* send data */
426         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
427                                   p_buffer->i_buffer );
428
429         p_next = p_buffer->p_next;
430         block_Release( p_buffer );
431         p_buffer = p_next;
432
433         if( i_err < 0 )
434         {
435             break;
436         }
437     }
438
439     if( i_err < 0 )
440     {
441         block_ChainRelease( p_buffer );
442     }
443
444     return( i_err < 0 ? VLC_EGENERIC : i_len );
445 }
446
447 /*****************************************************************************
448  * Seek: seek to a specific location in a file
449  *****************************************************************************/
450 static int Seek( sout_access_out_t *p_access, off_t i_pos )
451 {
452     msg_Warn( p_access, "HTTP sout access cannot seek" );
453     return VLC_EGENERIC;
454 }