]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
bf7513088c879d3d903b9b5e07c8b536c73c04da
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Remi Denis-Courmont
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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 #include <vlc_httpd.h>
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
48
49 #define SOUT_CFG_PREFIX "sout-http-"
50
51 #define USER_TEXT N_("Username")
52 #define USER_LONGTEXT N_("User name that will be " \
53                          "requested to access the stream." )
54 #define PASS_TEXT N_("Password")
55 #define PASS_LONGTEXT N_("Password that will be " \
56                          "requested to access the stream." )
57 #define MIME_TEXT N_("Mime")
58 #define MIME_LONGTEXT N_("MIME returned by the server (autodetected " \
59                         "if not specified)." )
60
61
62 vlc_module_begin ()
63     set_description( N_("HTTP stream output") )
64     set_capability( "sout access", 0 )
65     set_shortname( "HTTP" )
66     add_shortcut( "http", "https", "mmsh" )
67     set_category( CAT_SOUT )
68     set_subcategory( SUBCAT_SOUT_ACO )
69     add_string( SOUT_CFG_PREFIX "user", "",
70                 USER_TEXT, USER_LONGTEXT, true )
71     add_password( SOUT_CFG_PREFIX "pwd", "",
72                   PASS_TEXT, PASS_LONGTEXT, true )
73     add_string( SOUT_CFG_PREFIX "mime", "",
74                 MIME_TEXT, MIME_LONGTEXT, true )
75     set_callbacks( Open, Close )
76 vlc_module_end ()
77
78
79 /*****************************************************************************
80  * Exported prototypes
81  *****************************************************************************/
82 static const char *const ppsz_sout_options[] = {
83     "user", "pwd", "mime", NULL
84 };
85
86 static ssize_t Write( sout_access_out_t *, block_t * );
87 static int Seek ( sout_access_out_t *, off_t  );
88 static int Control( sout_access_out_t *, int, va_list );
89
90 struct sout_access_out_sys_t
91 {
92     /* host */
93     httpd_host_t        *p_httpd_host;
94
95     /* stream */
96     httpd_stream_t      *p_httpd_stream;
97
98     /* gather header from stream */
99     int                 i_header_allocated;
100     int                 i_header_size;
101     uint8_t             *p_header;
102     bool          b_header_complete;
103 };
104
105 /*****************************************************************************
106  * Open: open the file
107  *****************************************************************************/
108 static int Open( vlc_object_t *p_this )
109 {
110     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
111     sout_access_out_sys_t   *p_sys;
112
113     char                *psz_user;
114     char                *psz_pwd;
115     char                *psz_mime;
116
117     if( !( p_sys = p_access->p_sys =
118                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
119         return VLC_ENOMEM ;
120
121     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
122
123     const char *path = p_access->psz_path;
124     path += strcspn( path, "/" );
125     if( path > p_access->psz_path )
126     {
127         const char *port = strrchr( p_access->psz_path, ':' );
128         if( port != NULL && strchr( port, ']' ) != NULL )
129             port = NULL; /* IPv6 numeral */
130         if( port != p_access->psz_path )
131         {
132             int len = (port ? port : path) - p_access->psz_path;
133             msg_Warn( p_access, "\"%.*s\" HTTP host might be ignored in "
134                       "multiple-host configurations, use at your own risks.",
135                       len, p_access->psz_path );
136             msg_Info( p_access, "Consider passing --http-host=IP on the "
137                                 "command line instead." );
138
139             char host[len + 1];
140             strncpy( host, p_access->psz_path, len );
141             host[len] = '\0';
142
143             var_Create( p_access, "http-host", VLC_VAR_STRING );
144             var_SetString( p_access, "http-host", host );
145         }
146         if( port != NULL )
147         {
148             /* int len = path - ++port;
149             msg_Info( p_access, "Consider passing --%s-port=%.*s on the "
150                                 "command line instead.",
151                       strcasecmp( p_access->psz_access, "https" )
152                       ? "http" : "https", len, port ); */
153             port++;
154
155             int bind_port = atoi( port );
156             if( bind_port > 0 )
157             {
158                 const char *var = strcasecmp( p_access->psz_access, "https" )
159                                   ? "http-port" : "https-port";
160                 var_Create( p_access, var, VLC_VAR_INTEGER );
161                 var_SetInteger( p_access, var, bind_port );
162             }
163         }
164     }
165     if( !*path )
166         path = "/";
167
168     /* TLS support */
169     if( p_access->psz_access && !strcmp( p_access->psz_access, "https" ) )
170         p_sys->p_httpd_host = vlc_https_HostNew( VLC_OBJECT(p_access) );
171     else
172         p_sys->p_httpd_host = vlc_http_HostNew( VLC_OBJECT(p_access) );
173
174     if( p_sys->p_httpd_host == NULL )
175     {
176         msg_Err( p_access, "cannot start HTTP server" );
177         free( p_sys );
178         return VLC_EGENERIC;
179     }
180
181     psz_user = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "user" );
182     psz_pwd = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "pwd" );
183     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
184     {
185         psz_mime = strdup( "video/x-ms-asf-stream" );
186     }
187     else
188     {
189         psz_mime = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "mime" );
190     }
191
192     p_sys->p_httpd_stream =
193         httpd_StreamNew( p_sys->p_httpd_host, path, psz_mime,
194                          psz_user, psz_pwd );
195     free( psz_user );
196     free( psz_pwd );
197     free( psz_mime );
198
199     if( p_sys->p_httpd_stream == NULL )
200     {
201         msg_Err( p_access, "cannot add stream %s", path );
202         httpd_HostDelete( p_sys->p_httpd_host );
203
204         free( p_sys );
205         return VLC_EGENERIC;
206     }
207
208     p_sys->i_header_allocated = 1024;
209     p_sys->i_header_size      = 0;
210     p_sys->p_header           = xmalloc( p_sys->i_header_allocated );
211     p_sys->b_header_complete  = false;
212
213     p_access->pf_write       = Write;
214     p_access->pf_seek        = Seek;
215     p_access->pf_control     = Control;
216
217     return VLC_SUCCESS;
218 }
219
220 /*****************************************************************************
221  * Close: close the target
222  *****************************************************************************/
223 static void Close( vlc_object_t * p_this )
224 {
225     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
226     sout_access_out_sys_t   *p_sys = p_access->p_sys;
227
228     httpd_StreamDelete( p_sys->p_httpd_stream );
229     httpd_HostDelete( p_sys->p_httpd_host );
230
231     free( p_sys->p_header );
232
233     msg_Dbg( p_access, "Close" );
234
235     free( p_sys );
236 }
237
238 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
239 {
240     (void)p_access;
241
242     switch( i_query )
243     {
244         case ACCESS_OUT_CONTROLS_PACE:
245             *va_arg( args, bool * ) = false;
246             break;
247
248         default:
249             return VLC_EGENERIC;
250     }
251     return VLC_SUCCESS;
252 }
253
254 /*****************************************************************************
255  * Write:
256  *****************************************************************************/
257 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
258 {
259     sout_access_out_sys_t *p_sys = p_access->p_sys;
260     int i_err = 0;
261     int i_len = 0;
262
263     while( p_buffer )
264     {
265         block_t *p_next;
266
267         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
268         {
269             /* gather header */
270             if( p_sys->b_header_complete )
271             {
272                 /* free previously gathered header */
273                 p_sys->i_header_size = 0;
274                 p_sys->b_header_complete = false;
275             }
276             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
277                 p_sys->i_header_allocated )
278             {
279                 p_sys->i_header_allocated =
280                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
281                 p_sys->p_header = xrealloc( p_sys->p_header,
282                                                   p_sys->i_header_allocated );
283             }
284             memcpy( &p_sys->p_header[p_sys->i_header_size],
285                     p_buffer->p_buffer,
286                     p_buffer->i_buffer );
287             p_sys->i_header_size += p_buffer->i_buffer;
288         }
289         else if( !p_sys->b_header_complete )
290         {
291             p_sys->b_header_complete = true;
292
293             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
294                                 p_sys->i_header_size );
295         }
296
297         i_len += p_buffer->i_buffer;
298         /* send data */
299         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer );
300
301         p_next = p_buffer->p_next;
302         block_Release( p_buffer );
303         p_buffer = p_next;
304
305         if( i_err < 0 )
306         {
307             break;
308         }
309     }
310
311     if( i_err < 0 )
312     {
313         block_ChainRelease( p_buffer );
314     }
315
316     return( i_err < 0 ? VLC_EGENERIC : i_len );
317 }
318
319 /*****************************************************************************
320  * Seek: seek to a specific location in a file
321  *****************************************************************************/
322 static int Seek( sout_access_out_t *p_access, off_t i_pos )
323 {
324     (void)i_pos;
325     msg_Warn( p_access, "HTTP sout access cannot seek" );
326     return VLC_EGENERIC;
327 }