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