]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
Revert the so-called whitelisting commits that are actually blacklisting
[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     add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
101                 PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
102     add_string( SOUT_CFG_PREFIX "mime", "", NULL,
103                 MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
104     add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
105                 CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
106     add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
107                 KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
108     add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
109                 CA_TEXT, CA_LONGTEXT, VLC_TRUE );
110     add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
111                 CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
112     add_bool( SOUT_CFG_PREFIX "bonjour", VLC_FALSE, NULL,
113               BONJOUR_TEXT, BONJOUR_LONGTEXT, VLC_TRUE);
114     set_callbacks( Open, Close );
115 vlc_module_end();
116
117
118 /*****************************************************************************
119  * Exported prototypes
120  *****************************************************************************/
121 static const char *ppsz_sout_options[] = {
122     "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
123 };
124
125 static ssize_t Write( sout_access_out_t *, block_t * );
126 static int Seek ( sout_access_out_t *, off_t  );
127
128 struct sout_access_out_sys_t
129 {
130     /* host */
131     httpd_host_t        *p_httpd_host;
132
133     /* stream */
134     httpd_stream_t      *p_httpd_stream;
135
136     /* gather header from stream */
137     int                 i_header_allocated;
138     int                 i_header_size;
139     uint8_t             *p_header;
140     vlc_bool_t          b_header_complete;
141
142 #ifdef HAVE_AVAHI_CLIENT
143     void                *p_bonjour;
144 #endif
145 };
146
147 /*****************************************************************************
148  * Open: open the file
149  *****************************************************************************/
150 static int Open( vlc_object_t *p_this )
151 {
152     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
153     sout_access_out_sys_t   *p_sys;
154
155     char                *psz_parser;
156
157     char                *psz_bind_addr;
158     int                 i_bind_port;
159     char                *psz_file_name;
160     char                *psz_user = NULL;
161     char                *psz_pwd = NULL;
162     char                *psz_mime = NULL;
163     const char          *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
164                         *psz_crl = NULL;
165     vlc_value_t         val;
166
167     if( !( p_sys = p_access->p_sys =
168                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
169     {
170         msg_Err( p_access, "Not enough memory" );
171         return VLC_ENOMEM ;
172     }
173
174     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
175
176     /* p_access->psz_path = "hostname:port/filename" */
177     psz_bind_addr = psz_parser = strdup( p_access->psz_path );
178
179     i_bind_port = 0;
180     psz_file_name = NULL;
181
182     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
183     {
184         psz_parser++;
185     }
186     if( *psz_parser == ':' )
187     {
188         *psz_parser = '\0';
189         psz_parser++;
190         i_bind_port = atoi( psz_parser );
191
192         while( *psz_parser && *psz_parser != '/' )
193         {
194             psz_parser++;
195         }
196     }
197     if( *psz_parser == '/' )
198     {
199         *psz_parser = '\0';
200         psz_parser++;
201         psz_file_name = psz_parser;
202     }
203
204     if( psz_file_name == NULL )
205     {
206         psz_file_name = strdup( "/" );
207     }
208     else if( *psz_file_name != '/' )
209     {
210         char *p = psz_file_name;
211
212         psz_file_name = malloc( strlen( p ) + 2 );
213         strcpy( psz_file_name, "/" );
214         strcat( psz_file_name, p );
215     }
216     else
217     {
218         psz_file_name = strdup( psz_file_name );
219     }
220
221     /* SSL support */
222     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
223     {
224         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
225         psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
226         psz_ca = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
227         psz_crl = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
228
229         if( i_bind_port <= 0 )
230             i_bind_port = DEFAULT_SSL_PORT;
231     }
232     else
233     {
234         if( i_bind_port <= 0 )
235             i_bind_port = DEFAULT_PORT;
236     }
237
238     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
239                                             psz_bind_addr, i_bind_port,
240                                             psz_cert, psz_key, psz_ca,
241                                             psz_crl );
242     if( p_sys->p_httpd_host == NULL )
243     {
244         msg_Err( p_access, "cannot listen on %s:%d",
245                  psz_bind_addr, i_bind_port );
246         free( psz_file_name );
247         free( psz_bind_addr );
248         free( p_sys );
249         return VLC_EGENERIC;
250     }
251     free( psz_bind_addr );
252
253     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
254     {
255         psz_mime = strdup( "video/x-ms-asf-stream" );
256     }
257     else
258     {
259         var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
260         if( *val.psz_string )
261             psz_mime = val.psz_string;
262         else
263             free( val.psz_string );
264     }
265
266     var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
267     if( *val.psz_string )
268         psz_user = val.psz_string;
269     else
270         free( val.psz_string );
271
272     var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
273     if( *val.psz_string )
274         psz_pwd = val.psz_string;
275     else
276         free( val.psz_string );
277
278     p_sys->p_httpd_stream =
279         httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
280                          psz_user, psz_pwd, NULL );
281     free( psz_user );
282     free( psz_pwd );
283     free( psz_mime );
284
285     if( p_sys->p_httpd_stream == NULL )
286     {
287         msg_Err( p_access, "cannot add stream %s", psz_file_name );
288         httpd_HostDelete( p_sys->p_httpd_host );
289
290         free( psz_file_name );
291         free( p_sys );
292         return VLC_EGENERIC;
293     }
294
295 #ifdef HAVE_AVAHI_CLIENT
296     if( config_GetInt(p_this, SOUT_CFG_PREFIX "bonjour") )
297     {
298         char                *psz_txt, *psz_name;
299         playlist_t          *p_playlist = pl_Yield( p_access );
300
301         char *psz_uri = input_item_GetURI( p_playlist->status.p_item->p_input );
302         char *psz_newuri = psz_uri;
303         psz_name = strrchr( psz_newuri, DIRECTORY_SEPARATOR );
304         if( psz_name != NULL ) psz_name++;
305         else psz_name = psz_newuri;
306
307         if( psz_file_name &&
308             asprintf( &psz_txt, "path=%s", psz_file_name ) == -1 )
309             {
310                 pl_Release( p_playlist );
311                 free( psz_uri );
312                 return VLC_ENOMEM;
313             }
314
315         p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
316                                     strcmp( p_access->psz_access, "https" )
317                                        ? "_vlc-http._tcp" : "_vlc-https._tcp",
318                                              psz_name, i_bind_port, psz_txt );
319         free( psz_uri );
320         free( (void *)psz_txt );
321
322         if( p_sys->p_bonjour == NULL )
323             msg_Err( p_access, "unable to start requested Bonjour announce" );
324         pl_Release( p_playlist );
325     }
326     else
327         p_sys->p_bonjour = NULL;
328 #endif
329
330     free( psz_file_name );
331
332     p_sys->i_header_allocated = 1024;
333     p_sys->i_header_size      = 0;
334     p_sys->p_header           = malloc( p_sys->i_header_allocated );
335     p_sys->b_header_complete  = VLC_FALSE;
336
337     p_access->pf_write       = Write;
338     p_access->pf_seek        = Seek;
339
340
341     /* update p_sout->i_out_pace_nocontrol */
342     p_access->p_sout->i_out_pace_nocontrol++;
343
344     return VLC_SUCCESS;
345 }
346
347 /*****************************************************************************
348  * Close: close the target
349  *****************************************************************************/
350 static void Close( vlc_object_t * p_this )
351 {
352     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
353     sout_access_out_sys_t   *p_sys = p_access->p_sys;
354
355 #ifdef HAVE_AVAHI_CLIENT
356     if( p_sys->p_bonjour != NULL )
357         bonjour_stop_service( p_sys->p_bonjour );
358 #endif
359
360     /* update p_sout->i_out_pace_nocontrol */
361     p_access->p_sout->i_out_pace_nocontrol--;
362
363     httpd_StreamDelete( p_sys->p_httpd_stream );
364     httpd_HostDelete( p_sys->p_httpd_host );
365
366     free( p_sys->p_header );
367
368     msg_Dbg( p_access, "Close" );
369
370     free( p_sys );
371 }
372
373 /*****************************************************************************
374  * Write:
375  *****************************************************************************/
376 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
377 {
378     sout_access_out_sys_t *p_sys = p_access->p_sys;
379     int i_err = 0;
380     int i_len = 0;
381
382     while( p_buffer )
383     {
384         block_t *p_next;
385
386         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
387         {
388             /* gather header */
389             if( p_sys->b_header_complete )
390             {
391                 /* free previously gathered header */
392                 p_sys->i_header_size = 0;
393                 p_sys->b_header_complete = VLC_FALSE;
394             }
395             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
396                 p_sys->i_header_allocated )
397             {
398                 p_sys->i_header_allocated =
399                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
400                 p_sys->p_header =
401                     realloc( p_sys->p_header, p_sys->i_header_allocated );
402             }
403             memcpy( &p_sys->p_header[p_sys->i_header_size],
404                     p_buffer->p_buffer,
405                     p_buffer->i_buffer );
406             p_sys->i_header_size += p_buffer->i_buffer;
407         }
408         else if( !p_sys->b_header_complete )
409         {
410             p_sys->b_header_complete = VLC_TRUE;
411
412             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
413                                 p_sys->i_header_size );
414         }
415
416         i_len += p_buffer->i_buffer;
417         /* send data */
418         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
419                                   p_buffer->i_buffer );
420
421         p_next = p_buffer->p_next;
422         block_Release( p_buffer );
423         p_buffer = p_next;
424
425         if( i_err < 0 )
426         {
427             break;
428         }
429     }
430
431     if( i_err < 0 )
432     {
433         block_ChainRelease( p_buffer );
434     }
435
436     return( i_err < 0 ? VLC_EGENERIC : i_len );
437 }
438
439 /*****************************************************************************
440  * Seek: seek to a specific location in a file
441  *****************************************************************************/
442 static int Seek( sout_access_out_t *p_access, off_t i_pos )
443 {
444     msg_Warn( p_access, "HTTP sout access cannot seek" );
445     return VLC_EGENERIC;
446 }