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