1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include "vlc_httpd.h"
35 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
37 #define DEFAULT_PORT 8080
38 #define DEFAULT_SSL_PORT 8443
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
46 #define SOUT_CFG_PREFIX "sout-http-"
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." )
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 " \
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." )
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 );
100 /*****************************************************************************
101 * Exported prototypes
102 *****************************************************************************/
103 static const char *ppsz_sout_options[] = {
104 "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
107 static int Write( sout_access_out_t *, block_t * );
108 static int Seek ( sout_access_out_t *, off_t );
110 struct sout_access_out_sys_t
113 httpd_host_t *p_httpd_host;
116 httpd_stream_t *p_httpd_stream;
118 /* gather header from stream */
119 int i_header_allocated;
122 vlc_bool_t b_header_complete;
125 /*****************************************************************************
126 * Open: open the file
127 *****************************************************************************/
128 static int Open( vlc_object_t *p_this )
130 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
131 sout_access_out_sys_t *p_sys;
134 char *psz_parser, *psz_name;
139 char *psz_user = NULL;
140 char *psz_pwd = NULL;
141 char *psz_mime = NULL;
144 if( !( p_sys = p_access->p_sys =
145 malloc( sizeof( sout_access_out_sys_t ) ) ) )
147 msg_Err( p_access, "Not enough memory" );
151 sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
153 /* p_access->psz_name host.name:port/filename */
154 psz_name = psz_parser = strdup( p_access->psz_name );
156 psz_bind_addr = psz_parser;
160 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
164 if( *psz_parser == ':' )
168 i_bind_port = atoi( psz_parser );
170 while( *psz_parser && *psz_parser != '/' )
175 if( *psz_parser == '/' )
179 psz_file_name = psz_parser;
182 if( !*psz_file_name )
184 psz_file_name = strdup( "/" );
186 else if( *psz_file_name != '/' )
188 char *p = psz_file_name;
190 psz_file_name = malloc( strlen( p ) + 2 );
191 strcpy( psz_file_name, "/" );
192 strcat( psz_file_name, p );
196 psz_file_name = strdup( psz_file_name );
200 if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
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" );
206 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
209 msg_Err( p_this, "TLS initialization error" );
210 free( psz_file_name );
216 psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
217 if ( ( psz_cert != NULL) && tls_ServerAddCA( p_tls, psz_cert ) )
219 msg_Err( p_this, "TLS CA error" );
220 tls_ServerDelete( p_tls );
221 free( psz_file_name );
227 psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
228 if ( ( psz_cert != NULL) && tls_ServerAddCRL( p_tls, psz_cert ) )
230 msg_Err( p_this, "TLS CRL error" );
231 tls_ServerDelete( p_tls );
232 free( psz_file_name );
238 if( i_bind_port <= 0 )
239 i_bind_port = DEFAULT_SSL_PORT;
244 if( i_bind_port <= 0 )
245 i_bind_port = DEFAULT_PORT;
248 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
249 psz_bind_addr, i_bind_port,
251 if( p_sys->p_httpd_host == NULL )
253 msg_Err( p_access, "cannot listen on %s:%d",
254 psz_bind_addr, i_bind_port );
257 tls_ServerDelete( p_tls );
259 free( psz_file_name );
264 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
266 psz_mime = strdup( "video/x-ms-asf-stream" );
270 var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
271 if( *val.psz_string )
272 psz_mime = val.psz_string;
274 free( val.psz_string );
277 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
278 if( *val.psz_string )
279 psz_user = val.psz_string;
281 free( val.psz_string );
283 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
284 if( *val.psz_string )
285 psz_pwd = val.psz_string;
287 free( val.psz_string );
289 p_sys->p_httpd_stream =
290 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
292 if( psz_user ) free( psz_user );
293 if( psz_pwd ) free( psz_pwd );
294 if( psz_mime ) free( psz_mime );
296 if( p_sys->p_httpd_stream == NULL )
298 msg_Err( p_access, "cannot add stream %s", psz_file_name );
299 httpd_HostDelete( p_sys->p_httpd_host );
302 free( psz_file_name );
307 free( psz_file_name );
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;
315 p_access->pf_write = Write;
316 p_access->pf_seek = Seek;
319 /* update p_sout->i_out_pace_nocontrol */
320 p_access->p_sout->i_out_pace_nocontrol++;
325 /*****************************************************************************
326 * Close: close the target
327 *****************************************************************************/
328 static void Close( vlc_object_t * p_this )
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;
333 /* update p_sout->i_out_pace_nocontrol */
334 p_access->p_sout->i_out_pace_nocontrol--;
336 httpd_StreamDelete( p_sys->p_httpd_stream );
337 httpd_HostDelete( p_sys->p_httpd_host );
339 FREE( p_sys->p_header );
341 msg_Dbg( p_access, "Close" );
346 /*****************************************************************************
348 *****************************************************************************/
349 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
351 sout_access_out_sys_t *p_sys = p_access->p_sys;
358 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
361 if( p_sys->b_header_complete )
363 /* free previously gathered header */
364 p_sys->i_header_size = 0;
365 p_sys->b_header_complete = VLC_FALSE;
367 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
368 p_sys->i_header_allocated )
370 p_sys->i_header_allocated =
371 p_buffer->i_buffer + p_sys->i_header_size + 1024;
373 realloc( p_sys->p_header, p_sys->i_header_allocated );
375 memcpy( &p_sys->p_header[p_sys->i_header_size],
377 p_buffer->i_buffer );
378 p_sys->i_header_size += p_buffer->i_buffer;
380 else if( !p_sys->b_header_complete )
382 p_sys->b_header_complete = VLC_TRUE;
384 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
385 p_sys->i_header_size );
389 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
390 p_buffer->i_buffer );
392 p_next = p_buffer->p_next;
393 block_Release( p_buffer );
404 block_ChainRelease( p_buffer );
407 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
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 )
415 msg_Warn( p_access, "HTTP sout access cannot seek" );