]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
Fix warnings
[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 #include <vlc/vlc.h>
30 #include <vlc_sout.h>
31 #include <vlc_block.h>
32
33 #include <stdlib.h>
34 #include <stdio.h>
35
36 #include <vlc_input.h>
37 #include <vlc_playlist.h>
38
39 #ifdef HAVE_AVAHI_CLIENT
40     #include "bonjour.h"
41
42     #if defined( WIN32 )
43         #define DIRECTORY_SEPARATOR '\\'
44     #else
45         #define DIRECTORY_SEPARATOR '/'
46     #endif
47 #endif
48
49 #include "vlc_httpd.h"
50
51 #define DEFAULT_PORT        8080
52 #define DEFAULT_SSL_PORT    8443
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 static int  Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
59
60 #define SOUT_CFG_PREFIX "sout-http-"
61
62 #define USER_TEXT N_("Username")
63 #define USER_LONGTEXT N_("User name that will be " \
64                          "requested to access the stream." )
65 #define PASS_TEXT N_("Password")
66 #define PASS_LONGTEXT N_("Password that will be " \
67                          "requested to access the stream." )
68
69 /// \bug [String] missing closing parenthesis
70 #define MIME_TEXT N_("Mime")
71 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
72                         "if not specified." )
73
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( _("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, VLC_TRUE );
105     add_string( SOUT_CFG_PREFIX "pwd", "", NULL,
106                 PASS_TEXT, PASS_LONGTEXT, VLC_TRUE );
107     add_string( SOUT_CFG_PREFIX "mime", "", NULL,
108                 MIME_TEXT, MIME_LONGTEXT, VLC_TRUE );
109     add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
110                 CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
111     add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
112                 KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
113     add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
114                 CA_TEXT, CA_LONGTEXT, VLC_TRUE );
115     add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
116                 CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
117     add_bool( SOUT_CFG_PREFIX "bonjour", VLC_FALSE, NULL,
118               BONJOUR_TEXT, BONJOUR_LONGTEXT, VLC_TRUE);
119     set_callbacks( Open, Close );
120 vlc_module_end();
121
122
123 /*****************************************************************************
124  * Exported prototypes
125  *****************************************************************************/
126 static const char *ppsz_sout_options[] = {
127     "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
128 };
129
130 static int 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     vlc_bool_t          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     {
175         msg_Err( p_access, "Not enough memory" );
176         return VLC_ENOMEM ;
177     }
178
179     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
180
181     /* p_access->psz_name = "hostname:port/filename" */
182     psz_bind_addr = psz_parser = strdup( p_access->psz_name );
183
184     i_bind_port = 0;
185     psz_file_name = NULL;
186
187     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
188     {
189         psz_parser++;
190     }
191     if( *psz_parser == ':' )
192     {
193         *psz_parser = '\0';
194         psz_parser++;
195         i_bind_port = atoi( psz_parser );
196
197         while( *psz_parser && *psz_parser != '/' )
198         {
199             psz_parser++;
200         }
201     }
202     if( *psz_parser == '/' )
203     {
204         *psz_parser = '\0';
205         psz_parser++;
206         psz_file_name = psz_parser;
207     }
208
209     if( psz_file_name == NULL )
210     {
211         psz_file_name = strdup( "/" );
212     }
213     else if( *psz_file_name != '/' )
214     {
215         char *p = psz_file_name;
216
217         psz_file_name = malloc( strlen( p ) + 2 );
218         strcpy( psz_file_name, "/" );
219         strcat( psz_file_name, p );
220     }
221     else
222     {
223         psz_file_name = strdup( psz_file_name );
224     }
225
226     /* SSL support */
227     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
228     {
229         psz_cert = config_GetPsz( p_this, SOUT_CFG_PREFIX"cert" );
230         psz_key = config_GetPsz( p_this, SOUT_CFG_PREFIX"key" );
231         psz_ca = config_GetPsz( p_this, SOUT_CFG_PREFIX"ca" );
232         psz_crl = config_GetPsz( p_this, SOUT_CFG_PREFIX"crl" );
233
234         if( i_bind_port <= 0 )
235             i_bind_port = DEFAULT_SSL_PORT;
236     }
237     else
238     {
239         if( i_bind_port <= 0 )
240             i_bind_port = DEFAULT_PORT;
241     }
242
243     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
244                                             psz_bind_addr, i_bind_port,
245                                             psz_cert, psz_key, psz_ca,
246                                             psz_crl );
247     if( p_sys->p_httpd_host == NULL )
248     {
249         msg_Err( p_access, "cannot listen on %s:%d",
250                  psz_bind_addr, i_bind_port );
251         free( psz_file_name );
252         free( psz_bind_addr );
253         free( p_sys );
254         return VLC_EGENERIC;
255     }
256     free( psz_bind_addr );
257
258     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
259     {
260         psz_mime = strdup( "video/x-ms-asf-stream" );
261     }
262     else
263     {
264         var_Get( p_access, SOUT_CFG_PREFIX "mime", &val );
265         if( *val.psz_string )
266             psz_mime = val.psz_string;
267         else
268             free( val.psz_string );
269     }
270
271     var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
272     if( *val.psz_string )
273         psz_user = val.psz_string;
274     else
275         free( val.psz_string );
276
277     var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
278     if( *val.psz_string )
279         psz_pwd = val.psz_string;
280     else
281         free( val.psz_string );
282
283     p_sys->p_httpd_stream =
284         httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
285                          psz_user, psz_pwd, NULL );
286     if( psz_user ) free( psz_user );
287     if( psz_pwd ) free( psz_pwd );
288     if( psz_mime ) free( psz_mime );
289
290     if( p_sys->p_httpd_stream == NULL )
291     {
292         msg_Err( p_access, "cannot add stream %s", psz_file_name );
293         httpd_HostDelete( p_sys->p_httpd_host );
294
295         free( psz_file_name );
296         free( p_sys );
297         return VLC_EGENERIC;
298     }
299
300 #ifdef HAVE_AVAHI_CLIENT
301     if( config_GetInt(p_this, SOUT_CFG_PREFIX "bonjour") )
302     {
303         char                *psz_txt, *psz_name;
304         playlist_t          *p_playlist = pl_Yield( p_access );
305
306         psz_name = strrchr( p_playlist->status.p_item->p_input->psz_uri,
307                             DIRECTORY_SEPARATOR );
308         if( psz_name != NULL ) psz_name++;
309         else psz_name = p_playlist->status.p_item->p_input->psz_uri;
310
311         asprintf( &psz_txt, "path=%s", psz_file_name );
312
313         p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
314                                     strcmp( p_access->psz_access, "https" )
315                                        ? "_vlc-http._tcp" : "_vlc-https._tcp",
316                                              psz_name, i_bind_port, psz_txt );
317         free( (void *)psz_txt );
318
319         if( p_sys->p_bonjour == NULL )
320             msg_Err( p_access, "unable to start requested Bonjour announce" );
321         vlc_object_release( p_playlist );
322     }
323     else
324         p_sys->p_bonjour = NULL;
325 #endif
326
327     free( psz_file_name );
328
329     p_sys->i_header_allocated = 1024;
330     p_sys->i_header_size      = 0;
331     p_sys->p_header           = malloc( p_sys->i_header_allocated );
332     p_sys->b_header_complete  = VLC_FALSE;
333
334     p_access->pf_write       = Write;
335     p_access->pf_seek        = Seek;
336
337
338     /* update p_sout->i_out_pace_nocontrol */
339     p_access->p_sout->i_out_pace_nocontrol++;
340
341     return VLC_SUCCESS;
342 }
343
344 /*****************************************************************************
345  * Close: close the target
346  *****************************************************************************/
347 static void Close( vlc_object_t * p_this )
348 {
349     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
350     sout_access_out_sys_t   *p_sys = p_access->p_sys;
351
352 #ifdef HAVE_AVAHI_CLIENT
353     if( p_sys->p_bonjour != NULL )
354         bonjour_stop_service( p_sys->p_bonjour );
355 #endif
356
357     /* update p_sout->i_out_pace_nocontrol */
358     p_access->p_sout->i_out_pace_nocontrol--;
359
360     httpd_StreamDelete( p_sys->p_httpd_stream );
361     httpd_HostDelete( p_sys->p_httpd_host );
362
363     FREE( p_sys->p_header );
364
365     msg_Dbg( p_access, "Close" );
366
367     free( p_sys );
368 }
369
370 /*****************************************************************************
371  * Write:
372  *****************************************************************************/
373 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
374 {
375     sout_access_out_sys_t *p_sys = p_access->p_sys;
376     int i_err = 0;
377
378     while( p_buffer )
379     {
380         block_t *p_next;
381
382         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
383         {
384             /* gather header */
385             if( p_sys->b_header_complete )
386             {
387                 /* free previously gathered header */
388                 p_sys->i_header_size = 0;
389                 p_sys->b_header_complete = VLC_FALSE;
390             }
391             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
392                 p_sys->i_header_allocated )
393             {
394                 p_sys->i_header_allocated =
395                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
396                 p_sys->p_header =
397                     realloc( p_sys->p_header, p_sys->i_header_allocated );
398             }
399             memcpy( &p_sys->p_header[p_sys->i_header_size],
400                     p_buffer->p_buffer,
401                     p_buffer->i_buffer );
402             p_sys->i_header_size += p_buffer->i_buffer;
403         }
404         else if( !p_sys->b_header_complete )
405         {
406             p_sys->b_header_complete = VLC_TRUE;
407
408             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
409                                 p_sys->i_header_size );
410         }
411
412         /* send data */
413         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
414                                   p_buffer->i_buffer );
415
416         p_next = p_buffer->p_next;
417         block_Release( p_buffer );
418         p_buffer = p_next;
419
420         if( i_err < 0 )
421         {
422             break;
423         }
424     }
425
426     if( i_err < 0 )
427     {
428         block_ChainRelease( p_buffer );
429     }
430
431     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
432 }
433
434 /*****************************************************************************
435  * Seek: seek to a specific location in a file
436  *****************************************************************************/
437 static int Seek( sout_access_out_t *p_access, off_t i_pos )
438 {
439     msg_Warn( p_access, "HTTP sout access cannot seek" );
440     return VLC_EGENERIC;
441 }