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