]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
HTTP/SSL stream output
[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 #define CERT_TEXT N_( "Certificate file" )
57 #define CERT_LONGTEXT N_( "HTTP/SSL stream output x509 PEM certificate file" )
58 #define KEY_TEXT N_( "Private key file" )
59 #define KEY_LONGTEXT N_( "HTTP/SSL stream output x509 PEM private key file" )
60 #define CA_TEXT N_( "Root CA file" )
61 #define CA_LONGTEXT N_( "HTTP/SSL stream output x509 PEM trusted root CA certificates file" )
62 #define CRL_TEXT N_( "CRL file" )
63 #define CRL_LONGTEXT N_( "HTTP/SSL stream output Certificates Revocation List file" )
64
65 vlc_module_begin();
66     set_description( _("HTTP stream output") );
67     set_capability( "sout access", 0 );
68     add_shortcut( "http" );
69     add_shortcut( "https" );
70     add_shortcut( "mmsh" );
71     add_string( SOUT_CFG_PREFIX "user", "", NULL, USER_TEXT, USER_LONGTEXT, VLC_TRUE );
72     add_string( SOUT_CFG_PREFIX "pwd", "", NULL, PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
73     add_string( SOUT_CFG_PREFIX "mime", "", NULL, MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
74     add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
75     add_string( SOUT_CFG_PREFIX "key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
76     add_string( SOUT_CFG_PREFIX "ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, VLC_TRUE );
77     add_string( SOUT_CFG_PREFIX "crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
78     set_callbacks( Open, Close );
79 vlc_module_end();
80
81
82 /*****************************************************************************
83  * Exported prototypes
84  *****************************************************************************/
85 static const char *ppsz_sout_options[] = {
86     "user", "pwd", "mime", NULL
87 };
88
89 static int Write( sout_access_out_t *, block_t * );
90 static int Seek ( sout_access_out_t *, off_t  );
91
92 struct sout_access_out_sys_t
93 {
94     /* host */
95     httpd_host_t        *p_httpd_host;
96
97     /* stream */
98     httpd_stream_t      *p_httpd_stream;
99
100     /* gather header from stream */
101     int                 i_header_allocated;
102     int                 i_header_size;
103     uint8_t             *p_header;
104     vlc_bool_t          b_header_complete;
105 };
106
107 /*****************************************************************************
108  * Open: open the file
109  *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
111 {
112     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
113     sout_access_out_sys_t   *p_sys;
114     tls_server_t            *p_tls;
115
116     char                *psz_parser, *psz_name;
117
118     char                *psz_bind_addr;
119     int                 i_bind_port;
120     char                *psz_file_name;
121     char                *psz_user = NULL;
122     char                *psz_pwd = NULL;
123     char                *psz_mime = NULL;
124     vlc_value_t         val;
125
126     if( !( p_sys = p_access->p_sys =
127                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
128     {
129         msg_Err( p_access, "Not enough memory" );
130         return VLC_ENOMEM ;
131     }
132
133     sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
134
135     /* p_access->psz_name host.name:port/filename */
136     psz_name = psz_parser = strdup( p_access->psz_name );
137
138     psz_bind_addr = psz_parser;
139     i_bind_port = 0;
140     psz_file_name = "";
141
142     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
143     {
144         psz_parser++;
145     }
146     if( *psz_parser == ':' )
147     {
148         *psz_parser = '\0';
149         psz_parser++;
150         i_bind_port = atoi( psz_parser );
151
152         while( *psz_parser && *psz_parser != '/' )
153         {
154             psz_parser++;
155         }
156     }
157     if( *psz_parser == '/' )
158     {
159         *psz_parser = '\0';
160         psz_parser++;
161         psz_file_name = psz_parser;
162     }
163
164     if( !*psz_file_name )
165     {
166         psz_file_name = strdup( "/" );
167     }
168     else if( *psz_file_name != '/' )
169     {
170         char *p = psz_file_name;
171
172         psz_file_name = malloc( strlen( p ) + 2 );
173         strcpy( psz_file_name, "/" );
174         strcat( psz_file_name, p );
175     }
176     else
177     {
178         psz_file_name = strdup( psz_file_name );
179     }
180
181     /* SSL support */
182     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
183     {
184         const char *psz_cert, *psz_key;
185         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
186         psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
187
188         p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
189         if ( p_tls == NULL )
190         {
191             msg_Err( p_this, "TLS initialization error" );
192             free( psz_file_name );
193             free( psz_name );
194             free( p_sys );
195             return VLC_EGENERIC;
196         }
197
198         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
199         if ( ( psz_cert != NULL) && tls_ServerAddCA( p_tls, psz_cert ) )
200         {
201             msg_Err( p_this, "TLS CA error" );
202             tls_ServerDelete( p_tls );
203             free( psz_file_name );
204             free( psz_name );
205             free( p_sys );
206             return VLC_EGENERIC;
207         }
208
209         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
210         if ( ( psz_cert != NULL) && tls_ServerAddCRL( p_tls, psz_cert ) )
211         {
212             msg_Err( p_this, "TLS CRL error" );
213             tls_ServerDelete( p_tls );
214             free( psz_file_name );
215             free( psz_name );
216             free( p_sys );
217             return VLC_EGENERIC;
218         }
219
220         if( i_bind_port <= 0 )
221             i_bind_port = DEFAULT_SSL_PORT;
222     }
223     else
224     {
225         p_tls = NULL;
226         if( i_bind_port <= 0 )
227             i_bind_port = DEFAULT_PORT;
228     }
229
230     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
231                                             psz_bind_addr, i_bind_port,
232                                             p_tls );
233     if( p_sys->p_httpd_host == NULL )
234     {
235         msg_Err( p_access, "cannot listen on %s:%d",
236                  psz_bind_addr, i_bind_port );
237
238         if( p_tls != NULL )
239             tls_ServerDelete( p_tls );
240         free( psz_name );
241         free( psz_file_name );
242         free( p_sys );
243         return VLC_EGENERIC;
244     }
245
246     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
247     {
248         psz_mime = strdup( "video/x-ms-asf-stream" );
249     }
250     else
251     {
252         var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
253         if( *val.psz_string )
254             psz_mime = val.psz_string;
255         else
256             free( val.psz_string );
257     }
258
259     var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
260     if( *val.psz_string )
261         psz_user = val.psz_string;
262     else
263         free( val.psz_string );
264
265     var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
266     if( *val.psz_string )
267         psz_pwd = val.psz_string;
268     else
269         free( val.psz_string );
270
271     p_sys->p_httpd_stream =
272         httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
273                          psz_user, psz_pwd );
274     if( psz_user ) free( psz_user );
275     if( psz_pwd ) free( psz_pwd );
276     if( psz_mime ) free( psz_mime );
277
278     if( p_sys->p_httpd_stream == NULL )
279     {
280         msg_Err( p_access, "cannot add stream %s", psz_file_name );
281         httpd_HostDelete( p_sys->p_httpd_host );
282
283         free( psz_name );
284         free( psz_file_name );
285         free( p_sys );
286         return VLC_EGENERIC;
287     }
288
289     free( psz_file_name );
290     free( psz_name );
291
292     p_sys->i_header_allocated = 1024;
293     p_sys->i_header_size      = 0;
294     p_sys->p_header           = malloc( p_sys->i_header_allocated );
295     p_sys->b_header_complete  = VLC_FALSE;
296
297     p_access->pf_write       = Write;
298     p_access->pf_seek        = Seek;
299
300
301     /* update p_sout->i_out_pace_nocontrol */
302     p_access->p_sout->i_out_pace_nocontrol++;
303
304     return VLC_SUCCESS;
305 }
306
307 /*****************************************************************************
308  * Close: close the target
309  *****************************************************************************/
310 static void Close( vlc_object_t * p_this )
311 {
312     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
313     sout_access_out_sys_t   *p_sys = p_access->p_sys;
314
315     /* update p_sout->i_out_pace_nocontrol */
316     p_access->p_sout->i_out_pace_nocontrol--;
317
318     httpd_StreamDelete( p_sys->p_httpd_stream );
319     httpd_HostDelete( p_sys->p_httpd_host );
320
321     FREE( p_sys->p_header );
322
323     msg_Dbg( p_access, "Close" );
324
325     free( p_sys );
326 }
327
328 /*****************************************************************************
329  * Write:
330  *****************************************************************************/
331 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
332 {
333     sout_access_out_sys_t *p_sys = p_access->p_sys;
334     int i_err = 0;
335
336     while( p_buffer )
337     {
338         block_t *p_next;
339
340         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
341         {
342             /* gather header */
343             if( p_sys->b_header_complete )
344             {
345                 /* free previously gathered header */
346                 p_sys->i_header_size = 0;
347                 p_sys->b_header_complete = VLC_FALSE;
348             }
349             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
350                 p_sys->i_header_allocated )
351             {
352                 p_sys->i_header_allocated =
353                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
354                 p_sys->p_header =
355                     realloc( p_sys->p_header, p_sys->i_header_allocated );
356             }
357             memcpy( &p_sys->p_header[p_sys->i_header_size],
358                     p_buffer->p_buffer,
359                     p_buffer->i_buffer );
360             p_sys->i_header_size += p_buffer->i_buffer;
361         }
362         else if( !p_sys->b_header_complete )
363         {
364             p_sys->b_header_complete = VLC_TRUE;
365
366             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
367                                 p_sys->i_header_size );
368         }
369
370         /* send data */
371         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
372                                   p_buffer->i_buffer );
373
374         p_next = p_buffer->p_next;
375         block_Release( p_buffer );
376         p_buffer = p_next;
377
378         if( i_err < 0 )
379         {
380             break;
381         }
382     }
383
384     if( i_err < 0 )
385     {
386         block_ChainRelease( p_buffer );
387     }
388
389     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
390 }
391
392 /*****************************************************************************
393  * Seek: seek to a specific location in a file
394  *****************************************************************************/
395 static int Seek( sout_access_out_t *p_access, off_t i_pos )
396 {
397     msg_Warn( p_access, "HTTP sout access cannot seek" );
398     return VLC_EGENERIC;
399 }