1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2003 the VideoLAN team
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"
34 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
36 #define DEFAULT_PORT 8080
37 #define DEFAULT_SSL_PORT 8443
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
45 #define SOUT_CFG_PREFIX "sout-http-"
47 #define USER_TEXT N_("Username")
48 #define USER_LONGTEXT N_("Allows you to give a user name that will be " \
49 "requested to access the stream." )
50 #define PASS_TEXT N_("Password")
51 #define PASS_LONGTEXT N_("Allows you to give a password that will be " \
52 "requested to access the stream." )
53 #define MIME_TEXT N_("Mime")
54 #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_( "Path to the x509 PEM certificate file that will "\
58 "be used by the HTTP/SSL stream output" )
59 #define KEY_TEXT N_( "Private key file" )
60 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
61 " be used by the HTTP/SSL stream output. Leave " \
62 "empty if you don't have one." )
63 #define CA_TEXT N_( "Root CA file" )
64 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
65 "(certificate authority) file that will be used by " \
66 "the HTTP/SSL stream output. Leave empty if you " \
68 #define CRL_TEXT N_( "CRL file" )
69 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
70 "file that will be HTTP/SSL stream output. Leave " \
71 "empty if you don't have one." )
74 set_description( _("HTTP stream output") );
75 set_capability( "sout access", 0 );
76 set_shortname( N_("HTTP" ) );
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;
133 char *psz_parser, *psz_name;
138 char *psz_user = NULL;
139 char *psz_pwd = NULL;
140 char *psz_mime = NULL;
141 const char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
145 if( !( p_sys = p_access->p_sys =
146 malloc( sizeof( sout_access_out_sys_t ) ) ) )
148 msg_Err( p_access, "Not enough memory" );
152 sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
154 /* p_access->psz_name host.name:port/filename */
155 psz_name = psz_parser = strdup( p_access->psz_name );
157 psz_bind_addr = psz_parser;
161 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
165 if( *psz_parser == ':' )
169 i_bind_port = atoi( psz_parser );
171 while( *psz_parser && *psz_parser != '/' )
176 if( *psz_parser == '/' )
180 psz_file_name = psz_parser;
183 if( !*psz_file_name )
185 psz_file_name = strdup( "/" );
187 else if( *psz_file_name != '/' )
189 char *p = psz_file_name;
191 psz_file_name = malloc( strlen( p ) + 2 );
192 strcpy( psz_file_name, "/" );
193 strcat( psz_file_name, p );
197 psz_file_name = strdup( psz_file_name );
201 if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
203 psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
204 psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
205 psz_ca = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
206 psz_crl = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
208 if( i_bind_port <= 0 )
209 i_bind_port = DEFAULT_SSL_PORT;
213 if( i_bind_port <= 0 )
214 i_bind_port = DEFAULT_PORT;
217 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
218 psz_bind_addr, i_bind_port,
219 psz_cert, psz_key, psz_ca,
221 if( p_sys->p_httpd_host == NULL )
223 msg_Err( p_access, "cannot listen on %s:%d",
224 psz_bind_addr, i_bind_port );
226 free( psz_file_name );
231 if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
233 psz_mime = strdup( "video/x-ms-asf-stream" );
237 var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
238 if( *val.psz_string )
239 psz_mime = val.psz_string;
241 free( val.psz_string );
244 var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
245 if( *val.psz_string )
246 psz_user = val.psz_string;
248 free( val.psz_string );
250 var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
251 if( *val.psz_string )
252 psz_pwd = val.psz_string;
254 free( val.psz_string );
256 p_sys->p_httpd_stream =
257 httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
258 psz_user, psz_pwd, NULL, 0 );
259 if( psz_user ) free( psz_user );
260 if( psz_pwd ) free( psz_pwd );
261 if( psz_mime ) free( psz_mime );
263 if( p_sys->p_httpd_stream == NULL )
265 msg_Err( p_access, "cannot add stream %s", psz_file_name );
266 httpd_HostDelete( p_sys->p_httpd_host );
269 free( psz_file_name );
274 free( psz_file_name );
277 p_sys->i_header_allocated = 1024;
278 p_sys->i_header_size = 0;
279 p_sys->p_header = malloc( p_sys->i_header_allocated );
280 p_sys->b_header_complete = VLC_FALSE;
282 p_access->pf_write = Write;
283 p_access->pf_seek = Seek;
286 /* update p_sout->i_out_pace_nocontrol */
287 p_access->p_sout->i_out_pace_nocontrol++;
292 /*****************************************************************************
293 * Close: close the target
294 *****************************************************************************/
295 static void Close( vlc_object_t * p_this )
297 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
298 sout_access_out_sys_t *p_sys = p_access->p_sys;
300 /* update p_sout->i_out_pace_nocontrol */
301 p_access->p_sout->i_out_pace_nocontrol--;
303 httpd_StreamDelete( p_sys->p_httpd_stream );
304 httpd_HostDelete( p_sys->p_httpd_host );
306 FREE( p_sys->p_header );
308 msg_Dbg( p_access, "Close" );
313 /*****************************************************************************
315 *****************************************************************************/
316 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
318 sout_access_out_sys_t *p_sys = p_access->p_sys;
325 if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
328 if( p_sys->b_header_complete )
330 /* free previously gathered header */
331 p_sys->i_header_size = 0;
332 p_sys->b_header_complete = VLC_FALSE;
334 if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
335 p_sys->i_header_allocated )
337 p_sys->i_header_allocated =
338 p_buffer->i_buffer + p_sys->i_header_size + 1024;
340 realloc( p_sys->p_header, p_sys->i_header_allocated );
342 memcpy( &p_sys->p_header[p_sys->i_header_size],
344 p_buffer->i_buffer );
345 p_sys->i_header_size += p_buffer->i_buffer;
347 else if( !p_sys->b_header_complete )
349 p_sys->b_header_complete = VLC_TRUE;
351 httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
352 p_sys->i_header_size );
356 i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
357 p_buffer->i_buffer );
359 p_next = p_buffer->p_next;
360 block_Release( p_buffer );
371 block_ChainRelease( p_buffer );
374 return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
377 /*****************************************************************************
378 * Seek: seek to a specific location in a file
379 *****************************************************************************/
380 static int Seek( sout_access_out_t *p_access, off_t i_pos )
382 msg_Warn( p_access, "HTTP sout access cannot seek" );