]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
http output: kill config_Get (commented out anyway)
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2009 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_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_block.h>
37
38
39 #include <vlc_input.h>
40 #include <vlc_playlist.h>
41
42 #if 0 //def HAVE_AVAHI_CLIENT
43     #include "bonjour.h"
44
45     #if defined( WIN32 )
46         #define DIRECTORY_SEPARATOR '\\'
47     #else
48         #define DIRECTORY_SEPARATOR '/'
49     #endif
50 #endif
51
52 #include <vlc_httpd.h>
53
54 #define DEFAULT_PORT        8080
55 #define DEFAULT_SSL_PORT    8443
56
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60 static int  Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
62
63 #define SOUT_CFG_PREFIX "sout-http-"
64
65 #define USER_TEXT N_("Username")
66 #define USER_LONGTEXT N_("User name that will be " \
67                          "requested to access the stream." )
68 #define PASS_TEXT N_("Password")
69 #define PASS_LONGTEXT N_("Password that will be " \
70                          "requested to access the stream." )
71 #define MIME_TEXT N_("Mime")
72 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
73                         "if not specified)." )
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( N_("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, true )
105     add_password( SOUT_CFG_PREFIX "pwd", "", NULL,
106                   PASS_TEXT, PASS_LONGTEXT, true )
107     add_string( SOUT_CFG_PREFIX "mime", "", NULL,
108                 MIME_TEXT, MIME_LONGTEXT, true )
109     add_string( SOUT_CFG_PREFIX "cert", "vlc.pem", NULL,
110                 CERT_TEXT, CERT_LONGTEXT, true )
111     add_string( SOUT_CFG_PREFIX "key", NULL, NULL,
112                 KEY_TEXT, KEY_LONGTEXT, true )
113     add_string( SOUT_CFG_PREFIX "ca", NULL, NULL,
114                 CA_TEXT, CA_LONGTEXT, true )
115     add_string( SOUT_CFG_PREFIX "crl", NULL, NULL,
116                 CRL_TEXT, CRL_LONGTEXT, true )
117 #if 0 //def HAVE_AVAHI_CLIENT
118     add_bool( SOUT_CFG_PREFIX "bonjour", false, NULL,
119               BONJOUR_TEXT, BONJOUR_LONGTEXT, true);
120 #endif
121     set_callbacks( Open, Close )
122 vlc_module_end ()
123
124
125 /*****************************************************************************
126  * Exported prototypes
127  *****************************************************************************/
128 static const char *const ppsz_sout_options[] = {
129     "user", "pwd", "mime", "cert", "key", "ca", "crl", NULL
130 };
131
132 static ssize_t Write( sout_access_out_t *, block_t * );
133 static int Seek ( sout_access_out_t *, off_t  );
134 static int Control( sout_access_out_t *, int, va_list );
135
136 struct sout_access_out_sys_t
137 {
138     /* host */
139     httpd_host_t        *p_httpd_host;
140
141     /* stream */
142     httpd_stream_t      *p_httpd_stream;
143
144     /* gather header from stream */
145     int                 i_header_allocated;
146     int                 i_header_size;
147     uint8_t             *p_header;
148     bool          b_header_complete;
149
150 #if 0 //def HAVE_AVAHI_CLIENT
151     void                *p_bonjour;
152 #endif
153 };
154
155 /*****************************************************************************
156  * Open: open the file
157  *****************************************************************************/
158 static int Open( vlc_object_t *p_this )
159 {
160     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
161     sout_access_out_sys_t   *p_sys;
162
163     char                *psz_parser;
164
165     char                *psz_bind_addr;
166     int                 i_bind_port;
167     char                *psz_file_name;
168     char                *psz_user;
169     char                *psz_pwd;
170     char                *psz_mime;
171     char                *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
172                         *psz_crl = NULL;
173
174     if( !( p_sys = p_access->p_sys =
175                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
176         return VLC_ENOMEM ;
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 = strdup( p_access->psz_path );
182
183     i_bind_port = 0;
184
185     psz_parser = strchr( psz_bind_addr, '/' );
186     if( psz_parser )
187     {
188         psz_file_name = strdup( psz_parser );
189         *psz_parser = '\0';
190     }
191     else
192         psz_file_name = strdup( "/" );
193
194     if( psz_bind_addr[0] == '[' )
195     {
196         psz_bind_addr++;
197         psz_parser = strstr( psz_bind_addr, "]:" );
198         if( psz_parser )
199         {
200             *psz_parser = '\0';
201             i_bind_port = atoi( psz_parser + 2 );
202         }
203         psz_parser = psz_bind_addr - 1;
204     }
205     else
206     {
207         psz_parser = strrchr( psz_bind_addr, ':' );
208         if( psz_parser )
209         {
210             *psz_parser = '\0';
211             i_bind_port = atoi( psz_parser + 1 );
212         }
213         psz_parser = psz_bind_addr;
214     }
215
216     /* SSL support */
217     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
218     {
219         psz_cert = var_CreateGetNonEmptyString( p_this, SOUT_CFG_PREFIX"cert" );
220         psz_key = var_CreateGetNonEmptyString( p_this, SOUT_CFG_PREFIX"key" );
221         psz_ca = var_CreateGetNonEmptyString( p_this, SOUT_CFG_PREFIX"ca" );
222         psz_crl = var_CreateGetNonEmptyString( p_this, SOUT_CFG_PREFIX"crl" );
223
224         if( i_bind_port <= 0 )
225             i_bind_port = DEFAULT_SSL_PORT;
226     }
227     else
228     {
229         if( i_bind_port <= 0 )
230             i_bind_port = DEFAULT_PORT;
231     }
232
233     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_access),
234                                             psz_bind_addr, i_bind_port,
235                                             psz_cert, psz_key, psz_ca,
236                                             psz_crl );
237     free( psz_cert );
238     free( psz_key );
239     free( psz_ca );
240     free( psz_crl );
241
242     if( p_sys->p_httpd_host == NULL )
243     {
244         msg_Err( p_access, "cannot listen on %s port %d",
245                  psz_bind_addr, i_bind_port );
246         free( psz_file_name );
247         free( psz_parser );
248         free( p_sys );
249         return VLC_EGENERIC;
250     }
251     free( psz_parser );
252
253     psz_user = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "user" );
254     psz_pwd = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "pwd" );
255     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
256     {
257         psz_mime = strdup( "video/x-ms-asf-stream" );
258     }
259     else
260     {
261         psz_mime = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "mime" );
262     }
263
264     p_sys->p_httpd_stream =
265         httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
266                          psz_user, psz_pwd, NULL );
267     free( psz_user );
268     free( psz_pwd );
269     free( psz_mime );
270
271     if( p_sys->p_httpd_stream == NULL )
272     {
273         msg_Err( p_access, "cannot add stream %s", psz_file_name );
274         httpd_HostDelete( p_sys->p_httpd_host );
275
276         free( psz_file_name );
277         free( p_sys );
278         return VLC_EGENERIC;
279     }
280
281 #if 0 //def HAVE_AVAHI_CLIENT
282     if( var_InheritInteger(p_this, SOUT_CFG_PREFIX "bonjour") )
283     {
284         char                *psz_txt, *psz_name;
285         playlist_t          *p_playlist = pl_Hold( p_access );
286
287         char *psz_uri = input_item_GetURI( p_playlist->status.p_item->p_input );
288         char *psz_newuri = psz_uri;
289         psz_name = strrchr( psz_newuri, DIRECTORY_SEPARATOR );
290         if( psz_name != NULL ) psz_name++;
291         else psz_name = psz_newuri;
292
293         if( psz_file_name &&
294             asprintf( &psz_txt, "path=%s", psz_file_name ) == -1 )
295             {
296                 pl_Release( p_access );
297                 free( psz_uri );
298                 return VLC_ENOMEM;
299             }
300
301         p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
302                                     strcmp( p_access->psz_access, "https" )
303                                        ? "_vlc-http._tcp" : "_vlc-https._tcp",
304                                              psz_name, i_bind_port, psz_txt );
305         free( psz_uri );
306         free( psz_txt );
307
308         if( p_sys->p_bonjour == NULL )
309             msg_Err( p_access, "unable to start requested Bonjour announce" );
310         pl_Release( p_access );
311     }
312     else
313         p_sys->p_bonjour = NULL;
314 #endif
315
316     free( psz_file_name );
317
318     p_sys->i_header_allocated = 1024;
319     p_sys->i_header_size      = 0;
320     p_sys->p_header           = xmalloc( p_sys->i_header_allocated );
321     p_sys->b_header_complete  = false;
322
323     p_access->pf_write       = Write;
324     p_access->pf_seek        = Seek;
325     p_access->pf_control     = Control;
326
327     return VLC_SUCCESS;
328 }
329
330 /*****************************************************************************
331  * Close: close the target
332  *****************************************************************************/
333 static void Close( vlc_object_t * p_this )
334 {
335     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
336     sout_access_out_sys_t   *p_sys = p_access->p_sys;
337
338 #if 0 //def HAVE_AVAHI_CLIENT
339     if( p_sys->p_bonjour != NULL )
340         bonjour_stop_service( p_sys->p_bonjour );
341 #endif
342
343     httpd_StreamDelete( p_sys->p_httpd_stream );
344     httpd_HostDelete( p_sys->p_httpd_host );
345
346     free( p_sys->p_header );
347
348     msg_Dbg( p_access, "Close" );
349
350     free( p_sys );
351 }
352
353 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
354 {
355     (void)p_access;
356
357     switch( i_query )
358     {
359         case ACCESS_OUT_CONTROLS_PACE:
360             *va_arg( args, bool * ) = false;
361             break;
362
363         default:
364             return VLC_EGENERIC;
365     }
366     return VLC_SUCCESS;
367 }
368
369 /*****************************************************************************
370  * Write:
371  *****************************************************************************/
372 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
373 {
374     sout_access_out_sys_t *p_sys = p_access->p_sys;
375     int i_err = 0;
376     int i_len = 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 = 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 = xrealloc( p_sys->p_header,
397                                                   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 = true;
407
408             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
409                                 p_sys->i_header_size );
410         }
411
412         i_len += p_buffer->i_buffer;
413         /* send data */
414         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
415                                   p_buffer->i_buffer );
416
417         p_next = p_buffer->p_next;
418         block_Release( p_buffer );
419         p_buffer = p_next;
420
421         if( i_err < 0 )
422         {
423             break;
424         }
425     }
426
427     if( i_err < 0 )
428     {
429         block_ChainRelease( p_buffer );
430     }
431
432     return( i_err < 0 ? VLC_EGENERIC : i_len );
433 }
434
435 /*****************************************************************************
436  * Seek: seek to a specific location in a file
437  *****************************************************************************/
438 static int Seek( sout_access_out_t *p_access, off_t i_pos )
439 {
440     (void)i_pos;
441     msg_Warn( p_access, "HTTP sout access cannot seek" );
442     return VLC_EGENERIC;
443 }