]> git.sesse.net Git - vlc/blob - modules/control/http/http.h
http: psz_dir is not meant to be a const
[vlc] / modules / control / http / http.h
1 /*****************************************************************************
2  * http.h: Headers for the HTTP interface
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifndef _HTTP_H_
27 #define _HTTP_H_
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include <vlc/vlc.h>
33 #include <stdlib.h>
34 #include <strings.h>
35 #include <ctype.h>
36 #include <vlc_interface.h>
37 #include <vlc_playlist.h>
38
39 #include <vlc_aout.h>
40 #include <vlc_vout.h> /* for fullscreen */
41
42 #include "vlc_httpd.h"
43 #include "vlc_vlm.h"
44 #include "vlc_network.h"
45 #include "vlc_acl.h"
46 #include "vlc_charset.h"
47
48 #ifdef HAVE_SYS_STAT_H
49 #   include <sys/stat.h>
50 #endif
51 #ifdef HAVE_ERRNO_H
52 #   include <errno.h>
53 #endif
54 #ifdef HAVE_FCNTL_H
55 #   include <fcntl.h>
56 #endif
57
58 #ifdef HAVE_UNISTD_H
59 #   include <unistd.h>
60 #elif defined( WIN32 ) && !defined( UNDER_CE )
61 #   include <io.h>
62 #endif
63
64 #ifdef HAVE_DIRENT_H
65 #   include <dirent.h>
66 #endif
67
68 /* stat() support for large files on win32 */
69 #if defined( WIN32 ) && !defined( UNDER_CE )
70 #   define stat _stati64
71 #endif
72
73 /** \defgroup http_intf HTTP Interface
74  * This is the HTTP remote control interface. It is fully customizable
75  * by writing HTML pages using custom <vlc> tags.
76  *
77  * These tags use so-called macros.
78  *
79  * These macros can manipulate variables. For more complex operations,
80  * a custom RPN evaluator with many built-in functions is provided.
81  * @{
82  */
83
84 /*****************************************************************************
85  * Local defines
86  *****************************************************************************/
87 #define MAX_DIR_SIZE 2560
88 #define STACK_MAX 100        //< Maximum RPN stack size
89
90
91 /*****************************************************************************
92  * Utility functions
93  *****************************************************************************/
94
95 /** \defgroup http_utils Utilities
96  * \ingroup http_intf
97  * Utilities
98  * @{
99  */
100
101 /* File and directory functions */
102
103 /** This function recursively parses a directory and adds all files */
104 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
105                         char *psz_dir );
106 /** This function loads a file into a buffer */
107 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data );
108 /** This function creates a suitable URL for a filename */
109 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index );
110 /** This function returns the real path of a file or directory */
111 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src );
112
113 /* Locale handling functions */
114
115 /** This fuction converts a locale string to UTF-8 */
116 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 );
117 /** This function converts an UTF-8 to locale */
118 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local );
119
120 /** This command parses the "seek" command for the HTTP interface
121  * and performs the requested action */
122 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value );
123
124 /* URI Handling functions */
125
126 /** This function extracts the value for a given argument name
127  * from an HTTP request */
128 char *E_(ExtractURIValue)( char *restrict psz_uri,
129                            const char *restrict psz_name,
130                            char *restrict psz_value, size_t i_value_max );
131 char *E_(ExtractURIString)( char *restrict psz_uri,
132                             const char *restrict psz_name );
133 /** \todo Describe this function */
134 int E_(TestURIParam)( char *psz_uri, const char *psz_name );
135
136 /** This function parses a MRL */
137 input_item_t *E_(MRLParse)( intf_thread_t *, char *psz, char *psz_name );
138
139 /** Return the first word from a string (works in-place) */
140 char *E_(FirstWord)( char *psz, char *new );
141
142 /**@}*/
143
144 /****************************************************************************
145  * Variable handling functions
146  ****************************************************************************/
147
148 /** \defgroup http_vars Macro variables
149  * \ingroup http_intf
150  * These variables can be used in the <vlc> macros and in the RPN evaluator.
151  * The variables make a tree: each variable can have an arbitrary
152  * number of "children" variables.
153  * A number of helper functions are provided to manipulate the main variable
154  * structure
155  * @{
156  */
157
158 /**
159  * \struct mvar_t
160  * This structure defines a macro variable
161  */
162 typedef struct mvar_s
163 {
164     char *name;                 ///< Variable name
165     char *value;                ///< Variable value
166
167     int           i_field;      ///< Number of children variables
168     struct mvar_s **field;      ///< Children variables array
169 } mvar_t;
170
171
172 /** This function creates a new variable */
173 mvar_t  *E_(mvar_New)( const char *name, const char *value );
174 /** This function deletes a variable */
175 void     E_(mvar_Delete)( mvar_t *v );
176 /** This function adds f to the children variables of v, at last position */
177 void     E_(mvar_AppendVar)( mvar_t *v, mvar_t *f );
178 /** This function duplicates a variable */
179 mvar_t  *E_(mvar_Duplicate)( const mvar_t *v );
180 /** This function adds f to the children variables of v, at fist position */
181 void     E_(mvar_PushVar)( mvar_t *v, mvar_t *f );
182 /** This function removes f from the children variables of v */
183 void     E_(mvar_RemoveVar)( mvar_t *v, mvar_t *f );
184 /** This function retrieves the child variable named "name" */
185 mvar_t  *E_(mvar_GetVar)( mvar_t *s, const char *name );
186 /** This function retrieves the value of the child variable named "field" */
187 char    *E_(mvar_GetValue)( mvar_t *v, char *field );
188 /** This function creates a variable with the given name and value and
189  * adds it as first child of vars */
190 void     E_(mvar_PushNewVar)( mvar_t *vars, const char *name,
191                               const char *value );
192 /** This function creates a variable with the given name and value and
193  * adds it as last child of vars */
194 void     E_(mvar_AppendNewVar)( mvar_t *vars, const char *name,
195                                 const char *value );
196 /** @} */
197
198 /** \defgroup http_sets Sets *
199  * \ingroup http_intf
200  * Sets are an application of the macro variables. There are a number of
201  * predefined functions that will give you variables whose children represent
202  * VLC internal data (playlist, stream info, ...)
203  * @{
204  */
205
206 /** This function creates a set variable which represents a series of integer
207  * The arg parameter must be of the form "start[:stop[:step]]"  */
208 mvar_t *E_(mvar_IntegerSetNew)( const char *name, const char *arg );
209
210 /** This function creates a set variable with a list of VLC objects */
211 mvar_t *E_(mvar_ObjectSetNew)( intf_thread_t *p_intf, char *name, const char *arg );
212
213 /** This function creates a set variable with the contents of the playlist */
214 mvar_t *E_(mvar_PlaylistSetNew)( intf_thread_t *p_intf, char *name,
215                                  playlist_t *p_pl );
216 /** This function creates a set variable with the contents of the Stream
217  * and media info box */
218 mvar_t *E_(mvar_InfoSetNew)( intf_thread_t *p_intf, char *name,
219                              input_thread_t *p_input );
220 /** This function creates a set variable with the input parameters */
221 mvar_t *E_(mvar_InputVarSetNew)( intf_thread_t *p_intf, char *name,
222                                  input_thread_t *p_input,
223                                  const char *psz_variable );
224 /** This function creates a set variable representing the files of the psz_dir
225  * directory */
226 mvar_t *E_(mvar_FileSetNew)( intf_thread_t *p_intf, char *name,
227                              char *psz_dir );
228 /** This function creates a set variable representing the VLM streams */
229 mvar_t *E_(mvar_VlmSetNew)( char *name, vlm_t *vlm );
230
231 /** This function converts the listing of a playlist node into a mvar set */
232 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
233                            playlist_item_t *p_node, char *name, mvar_t *s,
234                            int i_depth );
235
236 /**@}*/
237
238 /*****************************************************************************
239  * RPN Evaluator
240  *****************************************************************************/
241
242 /** \defgroup http_rpn RPN Evaluator
243  * \ingroup http_intf
244  * @{
245  */
246
247 /**
248  * \struct rpn_stack_t
249  * This structure represents a stack of RPN commands for the HTTP interface
250  * It is attached to a request
251  */
252 typedef struct
253 {
254     char *stack[STACK_MAX];
255     int  i_stack;
256 } rpn_stack_t;
257
258 /** This function creates the RPN evaluator stack */
259 void E_(SSInit)( rpn_stack_t * );
260 /** This function cleans the evaluator stack */
261 void E_(SSClean)( rpn_stack_t * );
262 /* Evaluate and execute the RPN Stack */
263 void  E_(EvaluateRPN)( intf_thread_t *p_intf, mvar_t  *vars,
264                        rpn_stack_t *st, char *exp );
265
266 /* Push an operand on top of the RPN stack */
267 void E_(SSPush)  ( rpn_stack_t *, const char * );
268 /* Remove the first operand from the RPN Stack */
269 char *E_(SSPop)  ( rpn_stack_t * );
270 /* Pushes an operand at a given position in the stack */
271 void E_(SSPushN) ( rpn_stack_t *, int );
272 /* Removes an operand at the given position in the stack */
273 int  E_(SSPopN)  ( rpn_stack_t *, mvar_t  * );
274
275 /**@}*/
276
277
278 /****************************************************************************
279  * Macro handling (<vlc ... stuff)
280  ****************************************************************************/
281
282 /** \defgroup http_macros <vlc> Macros Handling
283  * \ingroup http_intf
284  * A macro is a code snippet in the HTML page looking like
285  * <vlc id="macro_id" param1="value1" param2="value2">
286  * Macros string ids are mapped to macro types, and specific handling code
287  * must be written for each macro type
288  * @{
289  */
290
291
292 /** \struct macro_t
293  * This structure represents a HTTP Interface macro.
294  */
295 typedef struct
296 {
297     char *id;           ///< Macro ID string
298     char *param1;       ///< First parameter
299     char *param2;       ///< Second parameter
300 } macro_t;
301
302 /** This function parses a file for macros */
303 void E_(Execute)( httpd_file_sys_t *p_args,
304                   char *p_request, int i_request,
305                   char **pp_data, int *pi_data,
306                   char **pp_dst,
307                   char *_src, char *_end );
308
309 /**@}*/
310
311 /**
312  * Core stuff
313  */
314 /** \struct
315  * This structure represent a single HTML file to be parsed by the macros
316  * handling engine */
317 struct httpd_file_sys_t
318 {
319     intf_thread_t    *p_intf;
320     httpd_file_t     *p_file;
321     httpd_redirect_t *p_redir;
322     httpd_redirect_t *p_redir2;
323
324     char          *file;
325     char          *name;
326
327     vlc_bool_t    b_html, b_handler;
328
329     /* inited for each access */
330     rpn_stack_t   stack;
331     mvar_t        *vars;
332 };
333
334 /** \struct
335  * Structure associating an extension to an external program
336  */
337 typedef struct http_association_t
338 {
339     char                *psz_ext;
340     int                 i_argc;
341     char                **ppsz_argv;
342 } http_association_t;
343
344 /** \struct
345  * This structure represent a single CGI file to be parsed by the macros
346  * handling engine */
347 struct httpd_handler_sys_t
348 {
349     httpd_file_sys_t file;
350
351     /* HACK ALERT: this is added below so that casting httpd_handler_sys_t
352      * to httpd_file_sys_t works */
353     httpd_handler_t  *p_handler;
354     http_association_t *p_association;
355 };
356
357 /** \struct
358  * Internal service structure for the HTTP interface
359  */
360 struct intf_sys_t
361 {
362     httpd_host_t        *p_httpd_host;
363
364     int                 i_files;
365     httpd_file_sys_t    **pp_files;
366
367     int                 i_handlers;
368     http_association_t  **pp_handlers;
369     httpd_handler_t     *p_art_handler;
370
371     playlist_t          *p_playlist;
372     input_thread_t      *p_input;
373     vlm_t               *p_vlm;
374     char                *psz_html_type;
375     char                *psz_charset;
376     vlc_iconv_t         iconv_from_utf8, iconv_to_utf8;
377
378     char                *psz_address;
379     unsigned short      i_port;
380 };
381
382 /** This function is the main HTTPD Callback used by the HTTP Interface */
383 int E_(HttpCallback)( httpd_file_sys_t *p_args,
384                       httpd_file_t *,
385                       uint8_t *p_request,
386                       uint8_t **pp_data, int *pi_data );
387 /** This function is the HTTPD Callback used for CGIs */
388 int  E_(HandlerCallback)( httpd_handler_sys_t *p_args,
389                           httpd_handler_t *p_handler, char *_p_url,
390                           uint8_t *_p_request, int i_type,
391                           uint8_t *_p_in, int i_in,
392                           char *psz_remote_addr, char *psz_remote_host,
393                           uint8_t **_pp_data, int *pi_data );
394 /**@}*/
395
396 #endif
397