]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
Fix a bug with preferences
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/sout.h>
31
32 #include "vlc_httpd.h"
33 #include "vlc_tls.h"
34
35 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
36
37 #define DEFAULT_PORT        8080
38 #define DEFAULT_SSL_PORT    8443
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
45
46 #define SOUT_CFG_PREFIX "sout-http-"
47
48 #define USER_TEXT N_("Username")
49 #define USER_LONGTEXT N_("Allows you to give a user name that will be " \
50                          "requested to access the stream." )
51 #define PASS_TEXT N_("Password")
52 #define PASS_LONGTEXT N_("Allows you to give a password that will be " \
53                          "requested to access the stream." )
54 #define MIME_TEXT N_("Mime")
55 #define MIME_LONGTEXT N_("Allows you to give the mime returned by the server." )
56
57 #define CERT_TEXT N_( "Certificate file" )
58 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
59                           "be used by the HTTP/SSL stream output" )
60 #define KEY_TEXT N_( "Private key file" )
61 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
62                          " be used by the HTTP/SSL stream output. Leave " \
63                          "empty if you don't have one." )
64 #define CA_TEXT N_( "Root CA file" )
65 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
66                         "(certificate authority) file that will be used by " \
67                         "the HTTP/SSL stream output. Leave empty if you " \
68                         "don't have one." )
69 #define CRL_TEXT N_( "CRL file" )
70 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
71                          "file that will be HTTP/SSL stream output. Leave " \
72                          "empty if you don't have one." )
73
74 vlc_module_begin();
75     set_description( _("HTTP stream output") );
76     set_capability( "sout access", 0 );
77     set_shortname( N_("HTTP" ) );
78     add_shortcut( "http" );
79     add_shortcut( "https" );
80     add_shortcut( "mmsh" );
81     set_category( CAT_SOUT );
82     set_subcategory( SUBCAT_SOUT_ACO );
83     add_string( SOUT_CFG_PREFIX "user", "", NULL,
84                 USER_TEXT, USER_LONGTEXT, VLC_TRUE );
85     add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
86                 PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
87     add_string( SOUT_CFG_PREFIX "mime", "", NULL,
88                 MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
89     add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
90                 CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
91     add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
92                 KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
93     add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
94                 CA_TEXT, CA_LONGTEXT, VLC_TRUE );
95     add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
96                 CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
97     set_callbacks( Open, Close );
98 vlc_module_end();
99
100
101 /*****************************************************************************
102  * Exported prototypes
103  *****************************************************************************/
104 static const char *ppsz_sout_options[] = {
105     "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
106 };
107
108 static int Write( sout_access_out_t *, block_t * );
109 static int Seek ( sout_access_out_t *, off_t  );
110
111 struct sout_access_out_sys_t
112 {
113     /* host */
114     httpd_host_t        *p_httpd_host;
115
116     /* stream */
117     httpd_stream_t      *p_httpd_stream;
118
119     /* gather header from stream */
120     int                 i_header_allocated;
121     int                 i_header_size;
122     uint8_t             *p_header;
123     vlc_bool_t          b_header_complete;
124 };
125
126 /*****************************************************************************
127  * Open: open the file
128  *****************************************************************************/
129 static int Open( vlc_object_t *p_this )
130 {
131     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
132     sout_access_out_sys_t   *p_sys;
133     tls_server_t            *p_tls;
134
135     char                *psz_parser, *psz_name;
136
137     char                *psz_bind_addr;
138     int                 i_bind_port;
139     char                *psz_file_name;
140     char                *psz_user = NULL;
141     char                *psz_pwd = NULL;
142     char                *psz_mime = NULL;
143     vlc_value_t         val;
144
145     if( !( p_sys = p_access->p_sys =
146                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
147     {
148         msg_Err( p_access, "Not enough memory" );
149         return VLC_ENOMEM ;
150     }
151
152     sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
153
154     /* p_access->psz_name host.name:port/filename */
155     psz_name = psz_parser = strdup( p_access->psz_name );
156
157     psz_bind_addr = psz_parser;
158     i_bind_port = 0;
159     psz_file_name = "";
160
161     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
162     {
163         psz_parser++;
164     }
165     if( *psz_parser == ':' )
166     {
167         *psz_parser = '\0';
168         psz_parser++;
169         i_bind_port = atoi( psz_parser );
170
171         while( *psz_parser && *psz_parser != '/' )
172         {
173             psz_parser++;
174         }
175     }
176     if( *psz_parser == '/' )
177     {
178         *psz_parser = '\0';
179         psz_parser++;
180         psz_file_name = psz_parser;
181     }
182
183     if( !*psz_file_name )
184     {
185         psz_file_name = strdup( "/" );
186     }
187     else if( *psz_file_name != '/' )
188     {
189         char *p = psz_file_name;
190
191         psz_file_name = malloc( strlen( p ) + 2 );
192         strcpy( psz_file_name, "/" );
193         strcat( psz_file_name, p );
194     }
195     else
196     {
197         psz_file_name = strdup( psz_file_name );
198     }
199
200     /* SSL support */
201     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
202     {
203         const char *psz_cert, *psz_key;
204         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
205         psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
206
207         p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
208         if ( p_tls == NULL )
209         {
210             msg_Err( p_this, "TLS initialization error" );
211             free( psz_file_name );
212             free( psz_name );
213             free( p_sys );
214             return VLC_EGENERIC;
215         }
216
217         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
218         if ( ( psz_cert != NULL) && tls_ServerAddCA( p_tls, psz_cert ) )
219         {
220             msg_Err( p_this, "TLS CA error" );
221             tls_ServerDelete( p_tls );
222             free( psz_file_name );
223             free( psz_name );
224             free( p_sys );
225             return VLC_EGENERIC;
226         }
227
228         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
229         if ( ( psz_cert != NULL) && tls_ServerAddCRL( p_tls, psz_cert ) )
230         {
231             msg_Err( p_this, "TLS CRL error" );
232             tls_ServerDelete( p_tls );
233             free( psz_file_name );
234             free( psz_name );
235             free( p_sys );
236             return VLC_EGENERIC;
237         }
238
239         if( i_bind_port <= 0 )
240             i_bind_port = DEFAULT_SSL_PORT;
241     }
242     else
243     {
244         p_tls = NULL;
245         if( i_bind_port <= 0 )
246             i_bind_port = DEFAULT_PORT;
247     }
248
249     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
250                                             psz_bind_addr, i_bind_port,
251                                             p_tls );
252     if( p_sys->p_httpd_host == NULL )
253     {
254         msg_Err( p_access, "cannot listen on %s:%d",
255                  psz_bind_addr, i_bind_port );
256
257         if( p_tls != NULL )
258             tls_ServerDelete( p_tls );
259         free( psz_name );
260         free( psz_file_name );
261         free( p_sys );
262         return VLC_EGENERIC;
263     }
264
265     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
266     {
267         psz_mime = strdup( "video/x-ms-asf-stream" );
268     }
269     else
270     {
271         var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
272         if( *val.psz_string )
273             psz_mime = val.psz_string;
274         else
275             free( val.psz_string );
276     }
277
278     var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
279     if( *val.psz_string )
280         psz_user = val.psz_string;
281     else
282         free( val.psz_string );
283
284     var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
285     if( *val.psz_string )
286         psz_pwd = val.psz_string;
287     else
288         free( val.psz_string );
289
290     p_sys->p_httpd_stream =
291         httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
292                          psz_user, psz_pwd );
293     if( psz_user ) free( psz_user );
294     if( psz_pwd ) free( psz_pwd );
295     if( psz_mime ) free( psz_mime );
296
297     if( p_sys->p_httpd_stream == NULL )
298     {
299         msg_Err( p_access, "cannot add stream %s", psz_file_name );
300         httpd_HostDelete( p_sys->p_httpd_host );
301
302         free( psz_name );
303         free( psz_file_name );
304         free( p_sys );
305         return VLC_EGENERIC;
306     }
307
308     free( psz_file_name );
309     free( psz_name );
310
311     p_sys->i_header_allocated = 1024;
312     p_sys->i_header_size      = 0;
313     p_sys->p_header           = malloc( p_sys->i_header_allocated );
314     p_sys->b_header_complete  = VLC_FALSE;
315
316     p_access->pf_write       = Write;
317     p_access->pf_seek        = Seek;
318
319
320     /* update p_sout->i_out_pace_nocontrol */
321     p_access->p_sout->i_out_pace_nocontrol++;
322
323     return VLC_SUCCESS;
324 }
325
326 /*****************************************************************************
327  * Close: close the target
328  *****************************************************************************/
329 static void Close( vlc_object_t * p_this )
330 {
331     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
332     sout_access_out_sys_t   *p_sys = p_access->p_sys;
333
334     /* update p_sout->i_out_pace_nocontrol */
335     p_access->p_sout->i_out_pace_nocontrol--;
336
337     httpd_StreamDelete( p_sys->p_httpd_stream );
338     httpd_HostDelete( p_sys->p_httpd_host );
339
340     FREE( p_sys->p_header );
341
342     msg_Dbg( p_access, "Close" );
343
344     free( p_sys );
345 }
346
347 /*****************************************************************************
348  * Write:
349  *****************************************************************************/
350 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
351 {
352     sout_access_out_sys_t *p_sys = p_access->p_sys;
353     int i_err = 0;
354
355     while( p_buffer )
356     {
357         block_t *p_next;
358
359         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
360         {
361             /* gather header */
362             if( p_sys->b_header_complete )
363             {
364                 /* free previously gathered header */
365                 p_sys->i_header_size = 0;
366                 p_sys->b_header_complete = VLC_FALSE;
367             }
368             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
369                 p_sys->i_header_allocated )
370             {
371                 p_sys->i_header_allocated =
372                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
373                 p_sys->p_header =
374                     realloc( p_sys->p_header, p_sys->i_header_allocated );
375             }
376             memcpy( &p_sys->p_header[p_sys->i_header_size],
377                     p_buffer->p_buffer,
378                     p_buffer->i_buffer );
379             p_sys->i_header_size += p_buffer->i_buffer;
380         }
381         else if( !p_sys->b_header_complete )
382         {
383             p_sys->b_header_complete = VLC_TRUE;
384
385             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
386                                 p_sys->i_header_size );
387         }
388
389         /* send data */
390         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
391                                   p_buffer->i_buffer );
392
393         p_next = p_buffer->p_next;
394         block_Release( p_buffer );
395         p_buffer = p_next;
396
397         if( i_err < 0 )
398         {
399             break;
400         }
401     }
402
403     if( i_err < 0 )
404     {
405         block_ChainRelease( p_buffer );
406     }
407
408     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
409 }
410
411 /*****************************************************************************
412  * Seek: seek to a specific location in a file
413  *****************************************************************************/
414 static int Seek( sout_access_out_t *p_access, off_t i_pos )
415 {
416     msg_Warn( p_access, "HTTP sout access cannot seek" );
417     return VLC_EGENERIC;
418 }