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