]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
misc/picture.c: kill sign warnings
[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 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 static int  Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
59
60 #define SOUT_CFG_PREFIX "sout-http-"
61
62 #define USER_TEXT N_("Username")
63 #define USER_LONGTEXT N_("User name that will be " \
64                          "requested to access the stream." )
65 #define PASS_TEXT N_("Password")
66 #define PASS_LONGTEXT N_("Password that will be " \
67                          "requested to access the stream." )
68 #define MIME_TEXT N_("Mime")
69 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
70                         "if not specified)." )
71 #define BONJOUR_TEXT N_( "Advertise with Bonjour")
72 #define BONJOUR_LONGTEXT N_( "Advertise the stream with the Bonjour protocol." )
73
74
75 vlc_module_begin ()
76     set_description( N_("HTTP stream output") )
77     set_capability( "sout access", 0 )
78     set_shortname( "HTTP" )
79     add_shortcut( "http", "https", "mmsh" )
80     set_category( CAT_SOUT )
81     set_subcategory( SUBCAT_SOUT_ACO )
82     add_string( SOUT_CFG_PREFIX "user", "",
83                 USER_TEXT, USER_LONGTEXT, true )
84     add_password( SOUT_CFG_PREFIX "pwd", "",
85                   PASS_TEXT, PASS_LONGTEXT, true )
86     add_string( SOUT_CFG_PREFIX "mime", "",
87                 MIME_TEXT, MIME_LONGTEXT, true )
88 #if 0 //def HAVE_AVAHI_CLIENT
89     add_bool( SOUT_CFG_PREFIX "bonjour", false,
90               BONJOUR_TEXT, BONJOUR_LONGTEXT, true);
91 #endif
92     set_callbacks( Open, Close )
93 vlc_module_end ()
94
95
96 /*****************************************************************************
97  * Exported prototypes
98  *****************************************************************************/
99 static const char *const ppsz_sout_options[] = {
100     "user", "pwd", "mime", NULL
101 };
102
103 static ssize_t Write( sout_access_out_t *, block_t * );
104 static int Seek ( sout_access_out_t *, off_t  );
105 static int Control( sout_access_out_t *, int, va_list );
106
107 struct sout_access_out_sys_t
108 {
109     /* host */
110     httpd_host_t        *p_httpd_host;
111
112     /* stream */
113     httpd_stream_t      *p_httpd_stream;
114
115     /* gather header from stream */
116     int                 i_header_allocated;
117     int                 i_header_size;
118     uint8_t             *p_header;
119     bool          b_header_complete;
120
121 #if 0 //def HAVE_AVAHI_CLIENT
122     void                *p_bonjour;
123 #endif
124 };
125
126 /*****************************************************************************
127  * Open: open the file
128  *****************************************************************************/
129 static int Open( vlc_object_t *p_this )
130 {
131     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
132     sout_access_out_sys_t   *p_sys;
133
134     char                *psz_user;
135     char                *psz_pwd;
136     char                *psz_mime;
137
138     if( !( p_sys = p_access->p_sys =
139                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
140         return VLC_ENOMEM ;
141
142     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
143
144     const char *path = p_access->psz_path;
145     /* Skip everything before / - backward compatibiltiy with VLC 1.1 */
146     path += strcspn( path, "/" );
147     if( path > p_access->psz_path )
148     {
149         const char *port = strrchr( p_access->psz_path, ':' );
150         if( port != NULL && strchr( port, ']' ) != NULL )
151             port = NULL; /* IPv6 numeral */
152         if( port != p_access->psz_path )
153         {
154             int len = (port ? port : path) - p_access->psz_path;
155             /* msg_Err( p_access, "\"%.*s\" HTTP host ignored", len,
156                      p_access->psz_path );
157             msg_Info( p_access,
158                       "Pass --http-host=IP on the command line instead." ); */
159
160             char host[len + 1];
161             strncpy( host, p_access->psz_path, len );
162             host[len] = '\0';
163
164             var_Create( p_access, "http-host", VLC_VAR_STRING );
165             var_SetString( p_access, "http-host", host );
166         }
167         if( port != NULL )
168         {
169             /* int len = path - ++port;
170             msg_Err( p_access, "\"%.*s\" HTTP port ignored", len, port );
171             msg_Info( p_access, "Pass --%s-port=%.*s on the command line "
172                       "instead.", strcasecmp( p_access->psz_access, "https" )
173                       ? "http" : "https", len, port ); */
174             port++;
175
176             int bind_port = atoi( port );
177             if( bind_port > 0 )
178             {
179                 const char *var = strcasecmp( p_access->psz_access, "https" )
180                                   ? "http-port" : "https-port";
181                 var_Create( p_access, var, VLC_VAR_INTEGER );
182                 var_SetInteger( p_access, var, bind_port );
183             }
184         }
185     }
186     if( !*path )
187         path = "/";
188
189     /* TLS support */
190     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
191         p_sys->p_httpd_host = vlc_https_HostNew( VLC_OBJECT(p_access) );
192     else
193         p_sys->p_httpd_host = vlc_http_HostNew( VLC_OBJECT(p_access) );
194
195     if( p_sys->p_httpd_host == NULL )
196     {
197         msg_Err( p_access, "cannot start HTTP server" );
198         free( p_sys );
199         return VLC_EGENERIC;
200     }
201
202     psz_user = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "user" );
203     psz_pwd = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "pwd" );
204     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
205     {
206         psz_mime = strdup( "video/x-ms-asf-stream" );
207     }
208     else
209     {
210         psz_mime = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "mime" );
211     }
212
213     p_sys->p_httpd_stream =
214         httpd_StreamNew( p_sys->p_httpd_host, path, psz_mime,
215                          psz_user, psz_pwd, NULL );
216     free( psz_user );
217     free( psz_pwd );
218     free( psz_mime );
219
220     if( p_sys->p_httpd_stream == NULL )
221     {
222         msg_Err( p_access, "cannot add stream %s", path );
223         httpd_HostDelete( p_sys->p_httpd_host );
224
225         free( p_sys );
226         return VLC_EGENERIC;
227     }
228
229 #if 0 //def HAVE_AVAHI_CLIENT
230     if( var_InheritBool(p_this, SOUT_CFG_PREFIX "bonjour") )
231     {
232         char                *psz_txt, *psz_name;
233         playlist_t          *p_playlist = pl_Get( p_access );
234
235         char *psz_uri = input_item_GetURI( p_playlist->status.p_item->p_input );
236         char *psz_newuri = psz_uri;
237         psz_name = strrchr( psz_newuri, DIRECTORY_SEPARATOR );
238         if( psz_name != NULL ) psz_name++;
239         else psz_name = psz_newuri;
240
241         if( asprintf( &psz_txt, "path=%s", path ) == -1 )
242             {
243                 free( psz_uri );
244                 return VLC_ENOMEM;
245             }
246
247         p_sys->p_bonjour = bonjour_start_service( (vlc_object_t *)p_access,
248                                     strcmp( p_access->psz_access, "https" )
249                                        ? "_vlc-http._tcp" : "_vlc-https._tcp",
250                                              psz_name, i_bind_port, psz_txt );
251         free( psz_uri );
252         free( psz_txt );
253
254         if( p_sys->p_bonjour == NULL )
255             msg_Err( p_access, "unable to start requested Bonjour announce" );
256     }
257     else
258         p_sys->p_bonjour = NULL;
259 #endif
260
261     p_sys->i_header_allocated = 1024;
262     p_sys->i_header_size      = 0;
263     p_sys->p_header           = xmalloc( p_sys->i_header_allocated );
264     p_sys->b_header_complete  = false;
265
266     p_access->pf_write       = Write;
267     p_access->pf_seek        = Seek;
268     p_access->pf_control     = Control;
269
270     return VLC_SUCCESS;
271 }
272
273 /*****************************************************************************
274  * Close: close the target
275  *****************************************************************************/
276 static void Close( vlc_object_t * p_this )
277 {
278     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
279     sout_access_out_sys_t   *p_sys = p_access->p_sys;
280
281 #if 0 //def HAVE_AVAHI_CLIENT
282     if( p_sys->p_bonjour != NULL )
283         bonjour_stop_service( p_sys->p_bonjour );
284 #endif
285
286     httpd_StreamDelete( p_sys->p_httpd_stream );
287     httpd_HostDelete( p_sys->p_httpd_host );
288
289     free( p_sys->p_header );
290
291     msg_Dbg( p_access, "Close" );
292
293     free( p_sys );
294 }
295
296 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
297 {
298     (void)p_access;
299
300     switch( i_query )
301     {
302         case ACCESS_OUT_CONTROLS_PACE:
303             *va_arg( args, bool * ) = false;
304             break;
305
306         default:
307             return VLC_EGENERIC;
308     }
309     return VLC_SUCCESS;
310 }
311
312 /*****************************************************************************
313  * Write:
314  *****************************************************************************/
315 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
316 {
317     sout_access_out_sys_t *p_sys = p_access->p_sys;
318     int i_err = 0;
319     int i_len = 0;
320
321     while( p_buffer )
322     {
323         block_t *p_next;
324
325         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
326         {
327             /* gather header */
328             if( p_sys->b_header_complete )
329             {
330                 /* free previously gathered header */
331                 p_sys->i_header_size = 0;
332                 p_sys->b_header_complete = false;
333             }
334             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
335                 p_sys->i_header_allocated )
336             {
337                 p_sys->i_header_allocated =
338                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
339                 p_sys->p_header = xrealloc( p_sys->p_header,
340                                                   p_sys->i_header_allocated );
341             }
342             memcpy( &p_sys->p_header[p_sys->i_header_size],
343                     p_buffer->p_buffer,
344                     p_buffer->i_buffer );
345             p_sys->i_header_size += p_buffer->i_buffer;
346         }
347         else if( !p_sys->b_header_complete )
348         {
349             p_sys->b_header_complete = true;
350
351             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
352                                 p_sys->i_header_size );
353         }
354
355         i_len += p_buffer->i_buffer;
356         /* send data */
357         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
358                                   p_buffer->i_buffer );
359
360         p_next = p_buffer->p_next;
361         block_Release( p_buffer );
362         p_buffer = p_next;
363
364         if( i_err < 0 )
365         {
366             break;
367         }
368     }
369
370     if( i_err < 0 )
371     {
372         block_ChainRelease( p_buffer );
373     }
374
375     return( i_err < 0 ? VLC_EGENERIC : i_len );
376 }
377
378 /*****************************************************************************
379  * Seek: seek to a specific location in a file
380  *****************************************************************************/
381 static int Seek( sout_access_out_t *p_access, off_t i_pos )
382 {
383     (void)i_pos;
384     msg_Warn( p_access, "HTTP sout access cannot seek" );
385     return VLC_EGENERIC;
386 }