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