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