]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
http.c: add an option to disable bonjour (closes #509).
[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 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/sout.h>
32
33 #ifdef HAVE_AVAHI_CLIENT
34     #include <vlc/intf.h>
35
36     #include "bonjour.h"
37
38     #if defined( WIN32 )
39         #define DIRECTORY_SEPARATOR '\\'
40     #else
41         #define DIRECTORY_SEPARATOR '/'
42     #endif
43 #endif
44
45 #include "vlc_httpd.h"
46
47 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
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_("Allows you to give a user name that will be " \
62                          "requested to access the stream." )
63 #define PASS_TEXT N_("Password")
64 #define PASS_LONGTEXT N_("Allows you to give a password that will be " \
65                          "requested to access the stream." )
66 #define MIME_TEXT N_("Mime")
67 #define MIME_LONGTEXT N_("Allows you to give the mime returned by the server." )
68
69 #define CERT_TEXT N_( "Certificate file" )
70 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
71                           "be used by the HTTP/SSL stream output" )
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 by the HTTP/SSL stream output. 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 by " \
79                         "the HTTP/SSL stream output. 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 HTTP/SSL stream output. 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( N_("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 int 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, *psz_name;
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 #ifdef HAVE_AVAHI_CLIENT
168     playlist_t          *p_playlist;
169     char                *psz_txt;
170 #endif
171
172     if( !( p_sys = p_access->p_sys =
173                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
174     {
175         msg_Err( p_access, "Not enough memory" );
176         return VLC_ENOMEM ;
177     }
178
179     sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
180
181     /* p_access->psz_name host.name:port/filename */
182     psz_name = psz_parser = strdup( p_access->psz_name );
183
184     psz_bind_addr = psz_parser;
185     i_bind_port = 0;
186     psz_file_name = "";
187
188     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
189     {
190         psz_parser++;
191     }
192     if( *psz_parser == ':' )
193     {
194         *psz_parser = '\0';
195         psz_parser++;
196         i_bind_port = atoi( psz_parser );
197
198         while( *psz_parser && *psz_parser != '/' )
199         {
200             psz_parser++;
201         }
202     }
203     if( *psz_parser == '/' )
204     {
205         *psz_parser = '\0';
206         psz_parser++;
207         psz_file_name = psz_parser;
208     }
209
210     if( !*psz_file_name )
211     {
212         psz_file_name = strdup( "/" );
213     }
214     else if( *psz_file_name != '/' )
215     {
216         char *p = psz_file_name;
217
218         psz_file_name = malloc( strlen( p ) + 2 );
219         strcpy( psz_file_name, "/" );
220         strcat( psz_file_name, p );
221     }
222     else
223     {
224         psz_file_name = strdup( psz_file_name );
225     }
226
227     /* SSL support */
228     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
229     {
230         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
231         psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
232         psz_ca = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
233         psz_crl = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
234
235         if( i_bind_port <= 0 )
236             i_bind_port = DEFAULT_SSL_PORT;
237     }
238     else
239     {
240         if( i_bind_port <= 0 )
241             i_bind_port = DEFAULT_PORT;
242     }
243
244     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
245                                             psz_bind_addr, i_bind_port,
246                                             psz_cert, psz_key, psz_ca,
247                                             psz_crl );
248     if( p_sys->p_httpd_host == NULL )
249     {
250         msg_Err( p_access, "cannot listen on %s:%d",
251                  psz_bind_addr, i_bind_port );
252         free( psz_name );
253         free( psz_file_name );
254         free( p_sys );
255         return VLC_EGENERIC;
256     }
257
258     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
259     {
260         psz_mime = strdup( "video/x-ms-asf-stream" );
261     }
262     else
263     {
264         var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
265         if( *val.psz_string )
266             psz_mime = val.psz_string;
267         else
268             free( val.psz_string );
269     }
270
271     var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
272     if( *val.psz_string )
273         psz_user = val.psz_string;
274     else
275         free( val.psz_string );
276
277     var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
278     if( *val.psz_string )
279         psz_pwd = val.psz_string;
280     else
281         free( val.psz_string );
282
283     p_sys->p_httpd_stream =
284         httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
285                          psz_user, psz_pwd, NULL );
286     if( psz_user ) free( psz_user );
287     if( psz_pwd ) free( psz_pwd );
288     if( psz_mime ) free( psz_mime );
289
290     if( p_sys->p_httpd_stream == NULL )
291     {
292         msg_Err( p_access, "cannot add stream %s", psz_file_name );
293         httpd_HostDelete( p_sys->p_httpd_host );
294
295         free( psz_name );
296         free( psz_file_name );
297         free( p_sys );
298         return VLC_EGENERIC;
299     }
300
301 #ifdef HAVE_AVAHI_CLIENT
302     asprintf( &psz_txt, "path=%s", psz_file_name );
303 #endif
304
305     free( psz_file_name );
306     free( psz_name );
307
308 #ifdef HAVE_AVAHI_CLIENT
309     p_playlist = (playlist_t *)vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
310                                                 FIND_ANYWHERE );
311     if( p_playlist == NULL )
312     {
313         msg_Err( p_access, "unable to find playlist" );
314         httpd_HostDelete( p_sys->p_httpd_host );
315         free( (void *)psz_txt );
316         free( (void *)p_sys );
317         return VLC_EGENERIC;
318     }
319
320     psz_name = strrchr( p_playlist->status.p_item->input.psz_uri,
321                         DIRECTORY_SEPARATOR );
322     if( psz_name != NULL ) psz_name++;
323     else psz_name = p_playlist->status.p_item->input.psz_uri;
324     
325     if( config_GetInt(p_this, SOUT_CFG_PREFIX "bonjour") )
326     {
327         p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
328                                                   "_vlc-http._tcp",
329                                                   psz_name, i_bind_port, psz_txt );
330         free( (void *)psz_txt );
331         if( p_sys->p_bonjour == NULL )
332         {
333             vlc_object_release( p_playlist );
334             httpd_HostDelete( p_sys->p_httpd_host );
335             free( (void *)p_sys );
336             return VLC_EGENERIC;
337         }
338     }
339     vlc_object_release( p_playlist );
340 #endif
341
342     p_sys->i_header_allocated = 1024;
343     p_sys->i_header_size      = 0;
344     p_sys->p_header           = malloc( p_sys->i_header_allocated );
345     p_sys->b_header_complete  = VLC_FALSE;
346
347     p_access->pf_write       = Write;
348     p_access->pf_seek        = Seek;
349
350
351     /* update p_sout->i_out_pace_nocontrol */
352     p_access->p_sout->i_out_pace_nocontrol++;
353
354     return VLC_SUCCESS;
355 }
356
357 /*****************************************************************************
358  * Close: close the target
359  *****************************************************************************/
360 static void Close( vlc_object_t * p_this )
361 {
362     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
363     sout_access_out_sys_t   *p_sys = p_access->p_sys;
364
365 #ifdef HAVE_AVAHI_CLIENT
366     if( config_GetInt(p_this, SOUT_CFG_PREFIX "bonjour") )
367         bonjour_stop_service( p_sys->p_bonjour );
368 #endif
369
370     /* update p_sout->i_out_pace_nocontrol */
371     p_access->p_sout->i_out_pace_nocontrol--;
372
373     httpd_StreamDelete( p_sys->p_httpd_stream );
374     httpd_HostDelete( p_sys->p_httpd_host );
375
376     FREE( p_sys->p_header );
377
378     msg_Dbg( p_access, "Close" );
379
380     free( p_sys );
381 }
382
383 /*****************************************************************************
384  * Write:
385  *****************************************************************************/
386 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
387 {
388     sout_access_out_sys_t *p_sys = p_access->p_sys;
389     int i_err = 0;
390
391     while( p_buffer )
392     {
393         block_t *p_next;
394
395         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
396         {
397             /* gather header */
398             if( p_sys->b_header_complete )
399             {
400                 /* free previously gathered header */
401                 p_sys->i_header_size = 0;
402                 p_sys->b_header_complete = VLC_FALSE;
403             }
404             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
405                 p_sys->i_header_allocated )
406             {
407                 p_sys->i_header_allocated =
408                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
409                 p_sys->p_header =
410                     realloc( p_sys->p_header, p_sys->i_header_allocated );
411             }
412             memcpy( &p_sys->p_header[p_sys->i_header_size],
413                     p_buffer->p_buffer,
414                     p_buffer->i_buffer );
415             p_sys->i_header_size += p_buffer->i_buffer;
416         }
417         else if( !p_sys->b_header_complete )
418         {
419             p_sys->b_header_complete = VLC_TRUE;
420
421             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
422                                 p_sys->i_header_size );
423         }
424
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 : VLC_SUCCESS );
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 }