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