]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
Make Zorglub less unhappy
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2003 the VideoLAN team
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
34 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
35
36 #define DEFAULT_PORT        8080
37 #define DEFAULT_SSL_PORT    8443
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
44
45 #define SOUT_CFG_PREFIX "sout-http-"
46
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." )
55
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 " \
67                         "don't have one." )
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." )
72
73 vlc_module_begin();
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 );
97 vlc_module_end();
98
99
100 /*****************************************************************************
101  * Exported prototypes
102  *****************************************************************************/
103 static const char *ppsz_sout_options[] = {
104     "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
105 };
106
107 static int Write( sout_access_out_t *, block_t * );
108 static int Seek ( sout_access_out_t *, off_t  );
109
110 struct sout_access_out_sys_t
111 {
112     /* host */
113     httpd_host_t        *p_httpd_host;
114
115     /* stream */
116     httpd_stream_t      *p_httpd_stream;
117
118     /* gather header from stream */
119     int                 i_header_allocated;
120     int                 i_header_size;
121     uint8_t             *p_header;
122     vlc_bool_t          b_header_complete;
123 };
124
125 /*****************************************************************************
126  * Open: open the file
127  *****************************************************************************/
128 static int Open( vlc_object_t *p_this )
129 {
130     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
131     sout_access_out_sys_t   *p_sys;
132
133     char                *psz_parser, *psz_name;
134
135     char                *psz_bind_addr;
136     int                 i_bind_port;
137     char                *psz_file_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,
142                         *psz_crl = NULL;
143     vlc_value_t         val;
144
145     if( !( p_sys = p_access->p_sys =
146                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
147     {
148         msg_Err( p_access, "Not enough memory" );
149         return VLC_ENOMEM ;
150     }
151
152     sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
153
154     /* p_access->psz_name host.name:port/filename */
155     psz_name = psz_parser = strdup( p_access->psz_name );
156
157     psz_bind_addr = psz_parser;
158     i_bind_port = 0;
159     psz_file_name = "";
160
161     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
162     {
163         psz_parser++;
164     }
165     if( *psz_parser == ':' )
166     {
167         *psz_parser = '\0';
168         psz_parser++;
169         i_bind_port = atoi( psz_parser );
170
171         while( *psz_parser && *psz_parser != '/' )
172         {
173             psz_parser++;
174         }
175     }
176     if( *psz_parser == '/' )
177     {
178         *psz_parser = '\0';
179         psz_parser++;
180         psz_file_name = psz_parser;
181     }
182
183     if( !*psz_file_name )
184     {
185         psz_file_name = strdup( "/" );
186     }
187     else if( *psz_file_name != '/' )
188     {
189         char *p = psz_file_name;
190
191         psz_file_name = malloc( strlen( p ) + 2 );
192         strcpy( psz_file_name, "/" );
193         strcat( psz_file_name, p );
194     }
195     else
196     {
197         psz_file_name = strdup( psz_file_name );
198     }
199
200     /* SSL support */
201     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
202     {
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" );
207
208         if( i_bind_port <= 0 )
209             i_bind_port = DEFAULT_SSL_PORT;
210     }
211     else
212     {
213         if( i_bind_port <= 0 )
214             i_bind_port = DEFAULT_PORT;
215     }
216
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,
220                                             psz_crl );
221     if( p_sys->p_httpd_host == NULL )
222     {
223         msg_Err( p_access, "cannot listen on %s:%d",
224                  psz_bind_addr, i_bind_port );
225         free( psz_name );
226         free( psz_file_name );
227         free( p_sys );
228         return VLC_EGENERIC;
229     }
230
231     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
232     {
233         psz_mime = strdup( "video/x-ms-asf-stream" );
234     }
235     else
236     {
237         var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
238         if( *val.psz_string )
239             psz_mime = val.psz_string;
240         else
241             free( val.psz_string );
242     }
243
244     var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
245     if( *val.psz_string )
246         psz_user = val.psz_string;
247     else
248         free( val.psz_string );
249
250     var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
251     if( *val.psz_string )
252         psz_pwd = val.psz_string;
253     else
254         free( val.psz_string );
255
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 );
262
263     if( p_sys->p_httpd_stream == NULL )
264     {
265         msg_Err( p_access, "cannot add stream %s", psz_file_name );
266         httpd_HostDelete( p_sys->p_httpd_host );
267
268         free( psz_name );
269         free( psz_file_name );
270         free( p_sys );
271         return VLC_EGENERIC;
272     }
273
274     free( psz_file_name );
275     free( psz_name );
276
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;
281
282     p_access->pf_write       = Write;
283     p_access->pf_seek        = Seek;
284
285
286     /* update p_sout->i_out_pace_nocontrol */
287     p_access->p_sout->i_out_pace_nocontrol++;
288
289     return VLC_SUCCESS;
290 }
291
292 /*****************************************************************************
293  * Close: close the target
294  *****************************************************************************/
295 static void Close( vlc_object_t * p_this )
296 {
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;
299
300     /* update p_sout->i_out_pace_nocontrol */
301     p_access->p_sout->i_out_pace_nocontrol--;
302
303     httpd_StreamDelete( p_sys->p_httpd_stream );
304     httpd_HostDelete( p_sys->p_httpd_host );
305
306     FREE( p_sys->p_header );
307
308     msg_Dbg( p_access, "Close" );
309
310     free( p_sys );
311 }
312
313 /*****************************************************************************
314  * Write:
315  *****************************************************************************/
316 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
317 {
318     sout_access_out_sys_t *p_sys = p_access->p_sys;
319     int i_err = 0;
320
321     while( p_buffer )
322     {
323         block_t *p_next;
324
325         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
326         {
327             /* gather header */
328             if( p_sys->b_header_complete )
329             {
330                 /* free previously gathered header */
331                 p_sys->i_header_size = 0;
332                 p_sys->b_header_complete = VLC_FALSE;
333             }
334             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
335                 p_sys->i_header_allocated )
336             {
337                 p_sys->i_header_allocated =
338                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
339                 p_sys->p_header =
340                     realloc( p_sys->p_header, p_sys->i_header_allocated );
341             }
342             memcpy( &p_sys->p_header[p_sys->i_header_size],
343                     p_buffer->p_buffer,
344                     p_buffer->i_buffer );
345             p_sys->i_header_size += p_buffer->i_buffer;
346         }
347         else if( !p_sys->b_header_complete )
348         {
349             p_sys->b_header_complete = VLC_TRUE;
350
351             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
352                                 p_sys->i_header_size );
353         }
354
355         /* send data */
356         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
357                                   p_buffer->i_buffer );
358
359         p_next = p_buffer->p_next;
360         block_Release( p_buffer );
361         p_buffer = p_next;
362
363         if( i_err < 0 )
364         {
365             break;
366         }
367     }
368
369     if( i_err < 0 )
370     {
371         block_ChainRelease( p_buffer );
372     }
373
374     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
375 }
376
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 )
381 {
382     msg_Warn( p_access, "HTTP sout access cannot seek" );
383     return VLC_EGENERIC;
384 }