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." )
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" )
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 );
82 /*****************************************************************************
84 *****************************************************************************/
85 static const char *ppsz_sout_options[] = {
86 "user", "pwd", "mime", NULL
89 static int Write( sout_access_out_t *, block_t * );
90 static int Seek ( sout_access_out_t *, off_t );
92 struct sout_access_out_sys_t
95 httpd_host_t *p_httpd_host;
98 httpd_stream_t *p_httpd_stream;
100 /* gather header from stream */
101 int i_header_allocated;
104 vlc_bool_t b_header_complete;
107 /*****************************************************************************
108 * Open: open the file
109 *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
112 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
113 sout_access_out_sys_t *p_sys;
116 char *psz_parser, *psz_name;
121 char *psz_user = NULL;
122 char *psz_pwd = NULL;
123 char *psz_mime = NULL;
126 if( !( p_sys = p_access->p_sys =
127 malloc( sizeof( sout_access_out_sys_t ) ) ) )
129 msg_Err( p_access, "Not enough memory" );
133 sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
135 /* p_access->psz_name host.name:port/filename */
136 psz_name = psz_parser = strdup( p_access->psz_name );
138 psz_bind_addr = psz_parser;
142 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
146 if( *psz_parser == ':' )
150 i_bind_port = atoi( psz_parser );
152 while( *psz_parser && *psz_parser != '/' )
157 if( *psz_parser == '/' )
161 psz_file_name = psz_parser;
164 if( !*psz_file_name )
166 psz_file_name = strdup( "/" );
168 else if( *psz_file_name != '/' )
170 char *p = psz_file_name;
172 psz_file_name = malloc( strlen( p ) + 2 );
173 strcpy( psz_file_name, "/" );
174 strcat( psz_file_name, p );
178 psz_file_name = strdup( psz_file_name );
182 if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
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" );
188 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
191 msg_Err( p_this, "TLS initialization error" );
192 free( psz_file_name );
198 psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
199 if ( ( psz_cert != NULL) && tls_ServerAddCA( p_tls, psz_cert ) )
201 msg_Err( p_this, "TLS CA error" );
202 tls_ServerDelete( p_tls );
203 free( psz_file_name );
209 psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
210 if ( ( psz_cert != NULL) && tls_ServerAddCRL( p_tls, psz_cert ) )
212 msg_Err( p_this, "TLS CRL error" );
213 tls_ServerDelete( p_tls );
214 free( psz_file_name );
220 if( i_bind_port <= 0 )
221 i_bind_port = DEFAULT_SSL_PORT;
226 if( i_bind_port <= 0 )
227 i_bind_port = DEFAULT_PORT;
230 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
231 psz_bind_addr, i_bind_port,
233 if( p_sys->p_httpd_host == NULL )
235 msg_Err( p_access, "cannot listen on %s:%d",
236 psz_bind_addr, i_bind_port );
239 tls_ServerDelete( p_tls );
241 free( psz_file_name );
246 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
248 psz_mime = strdup( "video/x-ms-asf-stream" );
252 var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
253 if( *val.psz_string )
254 psz_mime = val.psz_string;
256 free( val.psz_string );
259 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
260 if( *val.psz_string )
261 psz_user = val.psz_string;
263 free( val.psz_string );
265 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
266 if( *val.psz_string )
267 psz_pwd = val.psz_string;
269 free( val.psz_string );
271 p_sys->p_httpd_stream =
272 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
274 if( psz_user ) free( psz_user );
275 if( psz_pwd ) free( psz_pwd );
276 if( psz_mime ) free( psz_mime );
278 if( p_sys->p_httpd_stream == NULL )
280 msg_Err( p_access, "cannot add stream %s", psz_file_name );
281 httpd_HostDelete( p_sys->p_httpd_host );
284 free( psz_file_name );
289 free( psz_file_name );
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;
297 p_access->pf_write = Write;
298 p_access->pf_seek = Seek;
301 /* update p_sout->i_out_pace_nocontrol */
302 p_access->p_sout->i_out_pace_nocontrol++;
307 /*****************************************************************************
308 * Close: close the target
309 *****************************************************************************/
310 static void Close( vlc_object_t * p_this )
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;
315 /* update p_sout->i_out_pace_nocontrol */
316 p_access->p_sout->i_out_pace_nocontrol--;
318 httpd_StreamDelete( p_sys->p_httpd_stream );
319 httpd_HostDelete( p_sys->p_httpd_host );
321 FREE( p_sys->p_header );
323 msg_Dbg( p_access, "Close" );
328 /*****************************************************************************
330 *****************************************************************************/
331 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
333 sout_access_out_sys_t *p_sys = p_access->p_sys;
340 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
343 if( p_sys->b_header_complete )
345 /* free previously gathered header */
346 p_sys->i_header_size = 0;
347 p_sys->b_header_complete = VLC_FALSE;
349 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
350 p_sys->i_header_allocated )
352 p_sys->i_header_allocated =
353 p_buffer->i_buffer + p_sys->i_header_size + 1024;
355 realloc( p_sys->p_header, p_sys->i_header_allocated );
357 memcpy( &p_sys->p_header[p_sys->i_header_size],
359 p_buffer->i_buffer );
360 p_sys->i_header_size += p_buffer->i_buffer;
362 else if( !p_sys->b_header_complete )
364 p_sys->b_header_complete = VLC_TRUE;
366 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
367 p_sys->i_header_size );
371 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
372 p_buffer->i_buffer );
374 p_next = p_buffer->p_next;
375 block_Release( p_buffer );
386 block_ChainRelease( p_buffer );
389 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
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 )
397 msg_Warn( p_access, "HTTP sout access cannot seek" );