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 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 );
98 /*****************************************************************************
100 *****************************************************************************/
101 static const char *ppsz_sout_options[] = {
102 "user", "pwd", "mime", NULL
105 static int Write( sout_access_out_t *, block_t * );
106 static int Seek ( sout_access_out_t *, off_t );
108 struct sout_access_out_sys_t
111 httpd_host_t *p_httpd_host;
114 httpd_stream_t *p_httpd_stream;
116 /* gather header from stream */
117 int i_header_allocated;
120 vlc_bool_t b_header_complete;
123 /*****************************************************************************
124 * Open: open the file
125 *****************************************************************************/
126 static int Open( vlc_object_t *p_this )
128 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
129 sout_access_out_sys_t *p_sys;
132 char *psz_parser, *psz_name;
137 char *psz_user = NULL;
138 char *psz_pwd = NULL;
139 char *psz_mime = NULL;
142 if( !( p_sys = p_access->p_sys =
143 malloc( sizeof( sout_access_out_sys_t ) ) ) )
145 msg_Err( p_access, "Not enough memory" );
149 sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
151 /* p_access->psz_name host.name:port/filename */
152 psz_name = psz_parser = strdup( p_access->psz_name );
154 psz_bind_addr = psz_parser;
158 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
162 if( *psz_parser == ':' )
166 i_bind_port = atoi( psz_parser );
168 while( *psz_parser && *psz_parser != '/' )
173 if( *psz_parser == '/' )
177 psz_file_name = psz_parser;
180 if( !*psz_file_name )
182 psz_file_name = strdup( "/" );
184 else if( *psz_file_name != '/' )
186 char *p = psz_file_name;
188 psz_file_name = malloc( strlen( p ) + 2 );
189 strcpy( psz_file_name, "/" );
190 strcat( psz_file_name, p );
194 psz_file_name = strdup( psz_file_name );
198 if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
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" );
204 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
207 msg_Err( p_this, "TLS initialization error" );
208 free( psz_file_name );
214 psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
215 if ( ( psz_cert != NULL) && tls_ServerAddCA( p_tls, psz_cert ) )
217 msg_Err( p_this, "TLS CA error" );
218 tls_ServerDelete( p_tls );
219 free( psz_file_name );
225 psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
226 if ( ( psz_cert != NULL) && tls_ServerAddCRL( p_tls, psz_cert ) )
228 msg_Err( p_this, "TLS CRL error" );
229 tls_ServerDelete( p_tls );
230 free( psz_file_name );
236 if( i_bind_port <= 0 )
237 i_bind_port = DEFAULT_SSL_PORT;
242 if( i_bind_port <= 0 )
243 i_bind_port = DEFAULT_PORT;
246 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
247 psz_bind_addr, i_bind_port,
249 if( p_sys->p_httpd_host == NULL )
251 msg_Err( p_access, "cannot listen on %s:%d",
252 psz_bind_addr, i_bind_port );
255 tls_ServerDelete( p_tls );
257 free( psz_file_name );
262 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
264 psz_mime = strdup( "video/x-ms-asf-stream" );
268 var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
269 if( *val.psz_string )
270 psz_mime = val.psz_string;
272 free( val.psz_string );
275 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
276 if( *val.psz_string )
277 psz_user = val.psz_string;
279 free( val.psz_string );
281 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
282 if( *val.psz_string )
283 psz_pwd = val.psz_string;
285 free( val.psz_string );
287 p_sys->p_httpd_stream =
288 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
290 if( psz_user ) free( psz_user );
291 if( psz_pwd ) free( psz_pwd );
292 if( psz_mime ) free( psz_mime );
294 if( p_sys->p_httpd_stream == NULL )
296 msg_Err( p_access, "cannot add stream %s", psz_file_name );
297 httpd_HostDelete( p_sys->p_httpd_host );
300 free( psz_file_name );
305 free( psz_file_name );
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;
313 p_access->pf_write = Write;
314 p_access->pf_seek = Seek;
317 /* update p_sout->i_out_pace_nocontrol */
318 p_access->p_sout->i_out_pace_nocontrol++;
323 /*****************************************************************************
324 * Close: close the target
325 *****************************************************************************/
326 static void Close( vlc_object_t * p_this )
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;
331 /* update p_sout->i_out_pace_nocontrol */
332 p_access->p_sout->i_out_pace_nocontrol--;
334 httpd_StreamDelete( p_sys->p_httpd_stream );
335 httpd_HostDelete( p_sys->p_httpd_host );
337 FREE( p_sys->p_header );
339 msg_Dbg( p_access, "Close" );
344 /*****************************************************************************
346 *****************************************************************************/
347 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
349 sout_access_out_sys_t *p_sys = p_access->p_sys;
356 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
359 if( p_sys->b_header_complete )
361 /* free previously gathered header */
362 p_sys->i_header_size = 0;
363 p_sys->b_header_complete = VLC_FALSE;
365 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
366 p_sys->i_header_allocated )
368 p_sys->i_header_allocated =
369 p_buffer->i_buffer + p_sys->i_header_size + 1024;
371 realloc( p_sys->p_header, p_sys->i_header_allocated );
373 memcpy( &p_sys->p_header[p_sys->i_header_size],
375 p_buffer->i_buffer );
376 p_sys->i_header_size += p_buffer->i_buffer;
378 else if( !p_sys->b_header_complete )
380 p_sys->b_header_complete = VLC_TRUE;
382 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
383 p_sys->i_header_size );
387 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
388 p_buffer->i_buffer );
390 p_next = p_buffer->p_next;
391 block_Release( p_buffer );
402 block_ChainRelease( p_buffer );
405 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
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 )
413 msg_Warn( p_access, "HTTP sout access cannot seek" );