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