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