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