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