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