1 /*****************************************************************************
2 * http.h: Headers for the HTTP interface
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
5 * $Id: http.c 12225 2005-08-18 10:01:30Z massiot $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
38 #include <vlc/vout.h> /* for fullscreen */
40 #include "vlc_httpd.h"
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
58 #elif defined( WIN32 ) && !defined( UNDER_CE )
66 /* stat() support for large files on win32 */
67 #if defined( WIN32 ) && !defined( UNDER_CE )
68 # define stat _stati64
71 /** \defgroup http_intf HTTP Interface
72 * This is the HTTP remote control interface. It is fully customizable
73 * by writing HTML pages using custom <vlc> tags.
75 * These tags use so-called macros.
77 * These macros can manipulate variables. For more complex operations,
78 * a custom RPN evaluator with many built-in functions is provided.
82 /*****************************************************************************
84 *****************************************************************************/
85 #define MAX_DIR_SIZE 2560
86 #define STACK_MAX 100 //< Maximum RPN stack size
89 /*****************************************************************************
91 *****************************************************************************/
93 /** \defgroup http_utils Utilities
99 /* File and directory functions */
101 /** This function recursively parses a directory and adds all files */
102 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
104 /** This function loads a file into a buffer */
105 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data );
106 /** This function creates a suitable URL for a filename */
107 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index );
108 /** This function returns the real path of a file or directory */
109 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src );
111 /* Locale handling functions */
113 /** This fuction converts a locale string to UTF-8 */
114 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 );
115 /** This function converts an UTF-8 to locale */
116 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local );
118 /** This command parses the "seek" command for the HTTP interface
119 * and performs the requested action */
120 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value );
122 /* URI Handling functions */
124 /** This function extracts the value for a given argument name
125 * from an HTTP request */
126 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
127 char *psz_value, int i_value_max );
128 /** \todo Describe this function */
129 int E_(TestURIParam)( char *psz_uri, const char *psz_name );
130 /** This function extracts the original value from an URL-encoded string */
131 void E_(DecodeEncodedURI)( char *psz );
133 /** This function parses a MRL */
134 playlist_item_t *E_(MRLParse)( intf_thread_t *, char *psz, char *psz_name );
136 /** Return the first word from a string (works in-place) */
137 char *E_(FirstWord)( char *psz, char *new );
141 /****************************************************************************
142 * Variable handling functions
143 ****************************************************************************/
145 /** \defgroup http_vars Macro variables
147 * These variables can be used in the <vlc> macros and in the RPN evaluator.
148 * The variables make a tree: each variable can have an arbitrary
149 * number of "children" variables.
150 * A number of helper functions are provided to manipulate the main variable
157 * This structure defines a macro variable
159 typedef struct mvar_s
161 char *name; ///< Variable name
162 char *value; ///< Variable value
164 int i_field; ///< Number of children variables
165 struct mvar_s **field; ///< Children variables array
169 /** This function creates a new variable */
170 mvar_t *E_(mvar_New)( const char *name, const char *value );
171 /** This function deletes a variable */
172 void E_(mvar_Delete)( mvar_t *v );
173 /** This function adds f to the children variables of v, at last position */
174 void E_(mvar_AppendVar)( mvar_t *v, mvar_t *f );
175 /** This function duplicates a variable */
176 mvar_t *E_(mvar_Duplicate)( const mvar_t *v );
177 /** This function adds f to the children variables of v, at fist position */
178 void E_(mvar_PushVar)( mvar_t *v, mvar_t *f );
179 /** This function removes f from the children variables of v */
180 void E_(mvar_RemoveVar)( mvar_t *v, mvar_t *f );
181 /** This function retrieves the child variable named "name" */
182 mvar_t *E_(mvar_GetVar)( mvar_t *s, const char *name );
183 /** This function retrieves the value of the child variable named "field" */
184 char *E_(mvar_GetValue)( mvar_t *v, char *field );
185 /** This function creates a variable with the given name and value and
186 * adds it as first child of vars */
187 void E_(mvar_PushNewVar)( mvar_t *vars, const char *name,
189 /** This function creates a variable with the given name and value and
190 * adds it as last child of vars */
191 void E_(mvar_AppendNewVar)( mvar_t *vars, const char *name,
195 /** \defgroup http_sets Sets *
197 * Sets are an application of the macro variables. There are a number of
198 * predefined functions that will give you variables whose children represent
199 * VLC internal data (playlist, stream info, ...)
203 /** This function creates a set variable which represents a series of integer
204 * The arg parameter must be of the form "start[:stop[:step]]" */
205 mvar_t *E_(mvar_IntegerSetNew)( const char *name, const char *arg );
207 /** This function creates a set variable with the contents of the playlist */
208 mvar_t *E_(mvar_PlaylistSetNew)( intf_thread_t *p_intf, char *name,
210 /** This function creates a set variable with the contents of the Stream
211 * and media info box */
212 mvar_t *E_(mvar_InfoSetNew)( intf_thread_t *p_intf, char *name,
213 input_thread_t *p_input );
214 /** This function creates a set variable with the input parameters */
215 mvar_t *E_(mvar_InputVarSetNew)( intf_thread_t *p_intf, char *name,
216 input_thread_t *p_input,
217 const char *psz_variable );
218 /** This function creates a set variable representing the files of the psz_dir
220 mvar_t *E_(mvar_FileSetNew)( intf_thread_t *p_intf, char *name,
222 /** This function creates a set variable representing the VLM streams */
223 mvar_t *E_(mvar_VlmSetNew)( char *name, vlm_t *vlm );
225 /** This function converts the listing of a playlist node into a mvar set */
226 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
227 playlist_item_t *p_node, char *name, mvar_t *s,
232 /*****************************************************************************
234 *****************************************************************************/
236 /** \defgroup http_rpn RPN Evaluator
242 * \struct rpn_stack_t
243 * This structure represents a stack of RPN commands for the HTTP interface
244 * It is attached to a request
248 char *stack[STACK_MAX];
252 /** This function creates the RPN evaluator stack */
253 void E_(SSInit)( rpn_stack_t * );
254 /** This function cleans the evaluator stack */
255 void E_(SSClean)( rpn_stack_t * );
256 /* Evaluate and execute the RPN Stack */
257 void E_(EvaluateRPN)( intf_thread_t *p_intf, mvar_t *vars,
258 rpn_stack_t *st, char *exp );
260 /* Push an operand on top of the RPN stack */
261 void E_(SSPush) ( rpn_stack_t *, const char * );
262 /* Remove the first operand from the RPN Stack */
263 char *E_(SSPop) ( rpn_stack_t * );
264 /* Pushes an operand at a given position in the stack */
265 void E_(SSPushN) ( rpn_stack_t *, int );
266 /* Removes an operand at the given position in the stack */
267 int E_(SSPopN) ( rpn_stack_t *, mvar_t * );
272 /****************************************************************************
273 * Macro handling (<vlc ... stuff)
274 ****************************************************************************/
276 /** \defgroup http_macros <vlc> Macros Handling
278 * A macro is a code snippet in the HTML page looking like
279 * <vlc id="macro_id" param1="value1" param2="value2">
280 * Macros string ids are mapped to macro types, and specific handling code
281 * must be written for each macro type
287 * This structure represents a HTTP Interface macro.
291 char *id; ///< Macro ID string
292 char *param1; ///< First parameter
293 char *param2; ///< Second parameter
296 /** This function creates a macro from a <vlc ....> tag */
297 int E_(MacroParse)( macro_t *m, char *psz_src );
298 /** This function cleans a macro */
299 void E_(MacroClean)( macro_t *m );
301 /** This function returns the macro type identifier from its id= string value
302 * It uses the StrToMacroTypeTab mapping array for this */
303 int E_(StrToMacroType)( char *name );
304 /** This function actually executes the macro */
305 void E_(MacroDo)( httpd_file_sys_t *p_args, macro_t *m,
306 char *p_request, int i_request, char **pp_data,
307 int *pi_data, char **pp_dst );
308 /** This function looks for macros in a string */
309 char *E_(MacroSearch)( char *src, char *end,
310 int i_mvlc, vlc_bool_t b_after );
312 /** This function parses a file for macros */
313 void E_(Execute)( httpd_file_sys_t *p_args,
314 char *p_request, int i_request,
315 char **pp_data, int *pi_data,
317 char *_src, char *_end );
325 * This structure represent a single HTML file to be parsed by the macros
327 struct httpd_file_sys_t
329 intf_thread_t *p_intf;
330 httpd_file_t *p_file;
331 httpd_redirect_t *p_redir;
332 httpd_redirect_t *p_redir2;
339 /* inited for each access */
345 * Internal service structure for the HTTP interface
349 httpd_host_t *p_httpd_host;
352 httpd_file_sys_t **pp_files;
354 playlist_t *p_playlist;
355 input_thread_t *p_input;
358 vlc_iconv_t iconv_from_utf8, iconv_to_utf8;
361 /** This function is the main HTTPD Callback used by the HTTP Interface */
362 int E_(HttpCallback)( httpd_file_sys_t *p_args,
365 uint8_t **pp_data, int *pi_data );