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