1 /*****************************************************************************
2 * http.h: Headers for the HTTP interface
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
32 #include <vlc_common.h>
36 #include <vlc_interface.h>
37 #include <vlc_playlist.h>
40 #include <vlc_vout.h> /* for fullscreen */
42 #include <vlc_httpd.h>
44 #include <vlc_network.h>
46 #include <vlc_charset.h>
54 #elif defined( WIN32 ) && !defined( UNDER_CE )
62 /* stat() support for large files on win32 */
63 #if defined( WIN32 ) && !defined( UNDER_CE )
64 # define stat _stati64
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.
71 * These tags use so-called macros.
73 * These macros can manipulate variables. For more complex operations,
74 * a custom RPN evaluator with many built-in functions is provided.
78 /*****************************************************************************
80 *****************************************************************************/
81 #define MAX_DIR_SIZE 2560
82 #define STACK_MAX 100 //< Maximum RPN stack size
85 /*****************************************************************************
87 *****************************************************************************/
89 /** \defgroup http_utils Utilities
95 /* File and directory functions */
97 /** This function recursively parses a directory and adds all files */
98 int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
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 );
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 );
111 /* URI Handling functions */
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 );
123 /** This function parses a MRL */
124 input_item_t *MRLParse( intf_thread_t *, const char *psz, char *psz_name );
126 /** Return the first word from a string (works in-place) */
127 char *FirstWord( char *psz, char *new );
131 /****************************************************************************
132 * Variable handling functions
133 ****************************************************************************/
135 /** \defgroup http_vars Macro variables
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
147 * This structure defines a macro variable
149 typedef struct mvar_s
151 char *name; ///< Variable name
152 char *value; ///< Variable value
154 int i_field; ///< Number of children variables
155 struct mvar_s **field; ///< Children variables array
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,
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,
185 /** \defgroup http_sets Sets *
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, ...)
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 );
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 );
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,
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
212 mvar_t *mvar_FileSetNew( intf_thread_t *p_intf, char *name,
214 /** This function creates a set variable representing the VLM streams */
215 mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm );
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,
224 /*****************************************************************************
226 *****************************************************************************/
228 /** \defgroup http_rpn RPN Evaluator
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
240 char *stack[STACK_MAX];
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 );
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 * );
264 /****************************************************************************
265 * Macro handling (<vlc ... stuff)
266 ****************************************************************************/
268 /** \defgroup http_macros <vlc> Macros Handling
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
279 * This structure represents a HTTP Interface macro.
283 char *id; ///< Macro ID string
284 char *param1; ///< First parameter
285 char *param2; ///< Second parameter
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,
293 char *_src, char *_end );
300 /** \struct httpd_file_sys_t
301 * This structure represent a single HTML file to be parsed by the macros
303 struct httpd_file_sys_t
305 intf_thread_t *p_intf;
306 httpd_file_t *p_file;
307 httpd_redirect_t *p_redir;
308 httpd_redirect_t *p_redir2;
313 bool b_html, b_handler;
315 /* inited for each access */
320 /** \struct http_association_t
321 * Structure associating an extension to an external program
323 typedef struct http_association_t
328 } http_association_t;
330 /** \struct httpd_handler_sys_t
331 * This structure represent a single CGI file to be parsed by the macros
333 struct httpd_handler_sys_t
335 httpd_file_sys_t file;
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;
343 /** \struct intf_sys_t
344 * Internal service structure for the HTTP interface
348 httpd_host_t *p_httpd_host;
351 httpd_file_sys_t **pp_files;
354 http_association_t **pp_handlers;
355 httpd_handler_t *p_art_handler;
357 playlist_t *p_playlist;
358 input_thread_t *p_input;
362 unsigned short i_port;
365 /** This function is the main HTTPD Callback used by the HTTP Interface */
366 int HttpCallback( httpd_file_sys_t *p_args,
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 );