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