]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
Access strings (Refs:#438)
[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 FREE( p ) if( p ) { free( p); (p) = NULL; }
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 #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         playlist_t          *p_playlist;
300         char                *psz_txt, *psz_name;
301
302         p_playlist = (playlist_t *)vlc_object_find( p_access,
303                                                     VLC_OBJECT_PLAYLIST,
304                                                     FIND_ANYWHERE );
305         if( p_playlist == NULL )
306         {
307             msg_Err( p_access, "unable to find playlist" );
308             httpd_StreamDelete( p_sys->p_httpd_stream );
309             httpd_HostDelete( p_sys->p_httpd_host );
310             free( (void *)p_sys );
311             return VLC_EGENERIC;
312         }
313
314         psz_name = strrchr( p_playlist->status.p_item->input.psz_uri,
315                             DIRECTORY_SEPARATOR );
316         if( psz_name != NULL ) psz_name++;
317         else psz_name = p_playlist->status.p_item->input.psz_uri;
318
319         asprintf( &psz_txt, "path=%s", psz_file_name );
320
321         p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
322                                     strcmp( p_access->psz_access, "https" )
323                                        ? "_vlc-http._tcp" : "_vlc-https._tcp",
324                                                   psz_name, i_bind_port, psz_txt );
325         free( (void *)psz_txt );
326
327         if( p_sys->p_bonjour == NULL )
328         {
329             vlc_object_release( p_playlist );
330             httpd_StreamDelete( p_sys->p_httpd_stream );
331             httpd_HostDelete( p_sys->p_httpd_host );
332             free( (void *)p_sys );
333             return VLC_EGENERIC;
334         }
335         vlc_object_release( p_playlist );
336     }
337     else
338         p_sys->p_bonjour = NULL;
339 #endif
340
341     free( psz_file_name );
342
343     p_sys->i_header_allocated = 1024;
344     p_sys->i_header_size      = 0;
345     p_sys->p_header           = malloc( p_sys->i_header_allocated );
346     p_sys->b_header_complete  = VLC_FALSE;
347
348     p_access->pf_write       = Write;
349     p_access->pf_seek        = Seek;
350
351
352     /* update p_sout->i_out_pace_nocontrol */
353     p_access->p_sout->i_out_pace_nocontrol++;
354
355     return VLC_SUCCESS;
356 }
357
358 /*****************************************************************************
359  * Close: close the target
360  *****************************************************************************/
361 static void Close( vlc_object_t * p_this )
362 {
363     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
364     sout_access_out_sys_t   *p_sys = p_access->p_sys;
365
366 #ifdef HAVE_AVAHI_CLIENT
367     if( p_sys->p_bonjour != NULL )
368         bonjour_stop_service( p_sys->p_bonjour );
369 #endif
370
371     /* update p_sout->i_out_pace_nocontrol */
372     p_access->p_sout->i_out_pace_nocontrol--;
373
374     httpd_StreamDelete( p_sys->p_httpd_stream );
375     httpd_HostDelete( p_sys->p_httpd_host );
376
377     FREE( p_sys->p_header );
378
379     msg_Dbg( p_access, "Close" );
380
381     free( p_sys );
382 }
383
384 /*****************************************************************************
385  * Write:
386  *****************************************************************************/
387 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
388 {
389     sout_access_out_sys_t *p_sys = p_access->p_sys;
390     int i_err = 0;
391
392     while( p_buffer )
393     {
394         block_t *p_next;
395
396         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
397         {
398             /* gather header */
399             if( p_sys->b_header_complete )
400             {
401                 /* free previously gathered header */
402                 p_sys->i_header_size = 0;
403                 p_sys->b_header_complete = VLC_FALSE;
404             }
405             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
406                 p_sys->i_header_allocated )
407             {
408                 p_sys->i_header_allocated =
409                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
410                 p_sys->p_header =
411                     realloc( p_sys->p_header, p_sys->i_header_allocated );
412             }
413             memcpy( &p_sys->p_header[p_sys->i_header_size],
414                     p_buffer->p_buffer,
415                     p_buffer->i_buffer );
416             p_sys->i_header_size += p_buffer->i_buffer;
417         }
418         else if( !p_sys->b_header_complete )
419         {
420             p_sys->b_header_complete = VLC_TRUE;
421
422             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
423                                 p_sys->i_header_size );
424         }
425
426         /* send data */
427         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
428                                   p_buffer->i_buffer );
429
430         p_next = p_buffer->p_next;
431         block_Release( p_buffer );
432         p_buffer = p_next;
433
434         if( i_err < 0 )
435         {
436             break;
437         }
438     }
439
440     if( i_err < 0 )
441     {
442         block_ChainRelease( p_buffer );
443     }
444
445     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
446 }
447
448 /*****************************************************************************
449  * Seek: seek to a specific location in a file
450  *****************************************************************************/
451 static int Seek( sout_access_out_t *p_access, off_t i_pos )
452 {
453     msg_Warn( p_access, "HTTP sout access cannot seek" );
454     return VLC_EGENERIC;
455 }