]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
A bit of headers cleanup
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Jon Lech Johansen <jon@nanocrew.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc_sout.h>
32 #include <vlc_block.h>
33
34 #include <vlc_input.h>
35 #include <vlc_playlist.h>
36
37 #ifdef HAVE_AVAHI_CLIENT
38     #include "bonjour.h"
39
40     #if defined( WIN32 )
41         #define DIRECTORY_SEPARATOR '\\'
42     #else
43         #define DIRECTORY_SEPARATOR '/'
44     #endif
45 #endif
46
47 #include "vlc_httpd.h"
48
49 #define DEFAULT_PORT        8080
50 #define DEFAULT_SSL_PORT    8443
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 static int  Open ( vlc_object_t * );
56 static void Close( vlc_object_t * );
57
58 #define SOUT_CFG_PREFIX "sout-http-"
59
60 #define USER_TEXT N_("Username")
61 #define USER_LONGTEXT N_("User name that will be " \
62                          "requested to access the stream." )
63 #define PASS_TEXT N_("Password")
64 #define PASS_LONGTEXT N_("Password that will be " \
65                          "requested to access the stream." )
66
67 /// \bug [String] missing closing parenthesis
68 #define MIME_TEXT N_("Mime")
69 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
70                         "if not specified." )
71
72 #define CERT_TEXT N_( "Certificate file" )
73 #define CERT_LONGTEXT N_( "Path to the x509 PEM certificate file that will "\
74                           "be used for HTTPS." )
75 #define KEY_TEXT N_( "Private key file" )
76 #define KEY_LONGTEXT N_( "Path to the x509 PEM private key file that will " \
77                          "be used for HTTPS. Leave " \
78                          "empty if you don't have one." )
79 #define CA_TEXT N_( "Root CA file" )
80 #define CA_LONGTEXT N_( "Path to the x509 PEM trusted root CA certificates " \
81                         "(certificate authority) file that will be used for " \
82                         "HTTPS. Leave empty if you " \
83                         "don't have one." )
84 #define CRL_TEXT N_( "CRL file" )
85 #define CRL_LONGTEXT N_( "Path to the x509 PEM Certificates Revocation List " \
86                          "file that will be used for SSL. Leave " \
87                          "empty if you don't have one." )
88 #define BONJOUR_TEXT N_( "Advertise with Bonjour")
89 #define BONJOUR_LONGTEXT N_( "Advertise the stream with the Bonjour protocol." )
90
91
92 vlc_module_begin();
93     set_description( _("HTTP stream output") );
94     set_capability( "sout access", 0 );
95     set_shortname( "HTTP" );
96     add_shortcut( "http" );
97     add_shortcut( "https" );
98     add_shortcut( "mmsh" );
99     set_category( CAT_SOUT );
100     set_subcategory( SUBCAT_SOUT_ACO );
101     add_string( SOUT_CFG_PREFIX "user", "", NULL,
102                 USER_TEXT, USER_LONGTEXT, VLC_TRUE );
103     add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
104                 PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
105     add_string( SOUT_CFG_PREFIX "mime", "", NULL,
106                 MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
107     add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
108                 CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
109     add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
110                 KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
111     add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
112                 CA_TEXT, CA_LONGTEXT, VLC_TRUE );
113     add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
114                 CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
115     add_bool( SOUT_CFG_PREFIX "bonjour", VLC_FALSE, NULL,
116               BONJOUR_TEXT, BONJOUR_LONGTEXT, VLC_TRUE);
117     set_callbacks( Open, Close );
118 vlc_module_end();
119
120
121 /*****************************************************************************
122  * Exported prototypes
123  *****************************************************************************/
124 static const char *ppsz_sout_options[] = {
125     "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
126 };
127
128 static int Write( sout_access_out_t *, block_t * );
129 static int Seek ( sout_access_out_t *, off_t  );
130
131 struct sout_access_out_sys_t
132 {
133     /* host */
134     httpd_host_t        *p_httpd_host;
135
136     /* stream */
137     httpd_stream_t      *p_httpd_stream;
138
139     /* gather header from stream */
140     int                 i_header_allocated;
141     int                 i_header_size;
142     uint8_t             *p_header;
143     vlc_bool_t          b_header_complete;
144
145 #ifdef HAVE_AVAHI_CLIENT
146     void                *p_bonjour;
147 #endif
148 };
149
150 /*****************************************************************************
151  * Open: open the file
152  *****************************************************************************/
153 static int Open( vlc_object_t *p_this )
154 {
155     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
156     sout_access_out_sys_t   *p_sys;
157
158     char                *psz_parser;
159
160     char                *psz_bind_addr;
161     int                 i_bind_port;
162     char                *psz_file_name;
163     char                *psz_user = NULL;
164     char                *psz_pwd = NULL;
165     char                *psz_mime = NULL;
166     const char          *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
167                         *psz_crl = NULL;
168     vlc_value_t         val;
169
170     if( !( p_sys = p_access->p_sys =
171                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
172     {
173         msg_Err( p_access, "Not enough memory" );
174         return VLC_ENOMEM ;
175     }
176
177     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
178
179     /* p_access->psz_name = "hostname:port/filename" */
180     psz_bind_addr = psz_parser = strdup( p_access->psz_name );
181
182     i_bind_port = 0;
183     psz_file_name = "";
184
185     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
186     {
187         psz_parser++;
188     }
189     if( *psz_parser == ':' )
190     {
191         *psz_parser = '\0';
192         psz_parser++;
193         i_bind_port = atoi( psz_parser );
194
195         while( *psz_parser && *psz_parser != '/' )
196         {
197             psz_parser++;
198         }
199     }
200     if( *psz_parser == '/' )
201     {
202         *psz_parser = '\0';
203         psz_parser++;
204         psz_file_name = psz_parser;
205     }
206
207     if( !*psz_file_name )
208     {
209         psz_file_name = strdup( "/" );
210     }
211     else if( *psz_file_name != '/' )
212     {
213         char *p = psz_file_name;
214
215         psz_file_name = malloc( strlen( p ) + 2 );
216         strcpy( psz_file_name, "/" );
217         strcat( psz_file_name, p );
218     }
219     else
220     {
221         psz_file_name = strdup( psz_file_name );
222     }
223
224     /* SSL support */
225     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
226     {
227         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
228         psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
229         psz_ca = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
230         psz_crl = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
231
232         if( i_bind_port <= 0 )
233             i_bind_port = DEFAULT_SSL_PORT;
234     }
235     else
236     {
237         if( i_bind_port <= 0 )
238             i_bind_port = DEFAULT_PORT;
239     }
240
241     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
242                                             psz_bind_addr, i_bind_port,
243                                             psz_cert, psz_key, psz_ca,
244                                             psz_crl );
245     if( p_sys->p_httpd_host == NULL )
246     {
247         msg_Err( p_access, "cannot listen on %s:%d",
248                  psz_bind_addr, i_bind_port );
249         free( psz_file_name );
250         free( psz_bind_addr );
251         free( p_sys );
252         return VLC_EGENERIC;
253     }
254     free( psz_bind_addr );
255
256     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
257     {
258         psz_mime = strdup( "video/x-ms-asf-stream" );
259     }
260     else
261     {
262         var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
263         if( *val.psz_string )
264             psz_mime = val.psz_string;
265         else
266             free( val.psz_string );
267     }
268
269     var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
270     if( *val.psz_string )
271         psz_user = val.psz_string;
272     else
273         free( val.psz_string );
274
275     var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
276     if( *val.psz_string )
277         psz_pwd = val.psz_string;
278     else
279         free( val.psz_string );
280
281     p_sys->p_httpd_stream =
282         httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
283                          psz_user, psz_pwd, NULL );
284     if( psz_user ) free( psz_user );
285     if( psz_pwd ) free( psz_pwd );
286     if( psz_mime ) free( psz_mime );
287
288     if( p_sys->p_httpd_stream == NULL )
289     {
290         msg_Err( p_access, "cannot add stream %s", psz_file_name );
291         httpd_HostDelete( p_sys->p_httpd_host );
292
293         free( psz_file_name );
294         free( p_sys );
295         return VLC_EGENERIC;
296     }
297
298 #ifdef HAVE_AVAHI_CLIENT
299     if( config_GetInt(p_this, SOUT_CFG_PREFIX "bonjour") )
300     {
301         char                *psz_txt, *psz_name;
302         playlist_t          *p_playlist = pl_Yield( p_access );
303
304         psz_name = strrchr( p_playlist->status.p_item->p_input->psz_uri,
305                             DIRECTORY_SEPARATOR );
306         if( psz_name != NULL ) psz_name++;
307         else psz_name = p_playlist->status.p_item->p_input->psz_uri;
308
309         asprintf( &psz_txt, "path=%s", psz_file_name );
310
311         p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
312                                     strcmp( p_access->psz_access, "https" )
313                                        ? "_vlc-http._tcp" : "_vlc-https._tcp",
314                                              psz_name, i_bind_port, psz_txt );
315         free( (void *)psz_txt );
316
317         if( p_sys->p_bonjour == NULL )
318             msg_Err( p_access, "unable to start requested Bonjour announce" );
319         vlc_object_release( p_playlist );
320     }
321     else
322         p_sys->p_bonjour = NULL;
323 #endif
324
325     free( psz_file_name );
326
327     p_sys->i_header_allocated = 1024;
328     p_sys->i_header_size      = 0;
329     p_sys->p_header           = malloc( p_sys->i_header_allocated );
330     p_sys->b_header_complete  = VLC_FALSE;
331
332     p_access->pf_write       = Write;
333     p_access->pf_seek        = Seek;
334
335
336     /* update p_sout->i_out_pace_nocontrol */
337     p_access->p_sout->i_out_pace_nocontrol++;
338
339     return VLC_SUCCESS;
340 }
341
342 /*****************************************************************************
343  * Close: close the target
344  *****************************************************************************/
345 static void Close( vlc_object_t * p_this )
346 {
347     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
348     sout_access_out_sys_t   *p_sys = p_access->p_sys;
349
350 #ifdef HAVE_AVAHI_CLIENT
351     if( p_sys->p_bonjour != NULL )
352         bonjour_stop_service( p_sys->p_bonjour );
353 #endif
354
355     /* update p_sout->i_out_pace_nocontrol */
356     p_access->p_sout->i_out_pace_nocontrol--;
357
358     httpd_StreamDelete( p_sys->p_httpd_stream );
359     httpd_HostDelete( p_sys->p_httpd_host );
360
361     FREE( p_sys->p_header );
362
363     msg_Dbg( p_access, "Close" );
364
365     free( p_sys );
366 }
367
368 /*****************************************************************************
369  * Write:
370  *****************************************************************************/
371 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
372 {
373     sout_access_out_sys_t *p_sys = p_access->p_sys;
374     int i_err = 0;
375
376     while( p_buffer )
377     {
378         block_t *p_next;
379
380         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
381         {
382             /* gather header */
383             if( p_sys->b_header_complete )
384             {
385                 /* free previously gathered header */
386                 p_sys->i_header_size = 0;
387                 p_sys->b_header_complete = VLC_FALSE;
388             }
389             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
390                 p_sys->i_header_allocated )
391             {
392                 p_sys->i_header_allocated =
393                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
394                 p_sys->p_header =
395                     realloc( p_sys->p_header, p_sys->i_header_allocated );
396             }
397             memcpy( &p_sys->p_header[p_sys->i_header_size],
398                     p_buffer->p_buffer,
399                     p_buffer->i_buffer );
400             p_sys->i_header_size += p_buffer->i_buffer;
401         }
402         else if( !p_sys->b_header_complete )
403         {
404             p_sys->b_header_complete = VLC_TRUE;
405
406             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
407                                 p_sys->i_header_size );
408         }
409
410         /* send data */
411         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
412                                   p_buffer->i_buffer );
413
414         p_next = p_buffer->p_next;
415         block_Release( p_buffer );
416         p_buffer = p_next;
417
418         if( i_err < 0 )
419         {
420             break;
421         }
422     }
423
424     if( i_err < 0 )
425     {
426         block_ChainRelease( p_buffer );
427     }
428
429     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
430 }
431
432 /*****************************************************************************
433  * Seek: seek to a specific location in a file
434  *****************************************************************************/
435 static int Seek( sout_access_out_t *p_access, off_t i_pos )
436 {
437     msg_Warn( p_access, "HTTP sout access cannot seek" );
438     return VLC_EGENERIC;
439 }