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>
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h>
57 #elif defined( WIN32 ) && !defined( UNDER_CE )
65 /* stat() support for large files on win32 */
66 #if defined( WIN32 ) && !defined( UNDER_CE )
67 # define stat _stati64
70 /** \defgroup http_intf HTTP Interface
71 * This is the HTTP remote control interface. It is fully customizable
72 * by writing HTML pages using custom <vlc> tags.
74 * These tags use so-called macros.
76 * These macros can manipulate variables. For more complex operations,
77 * a custom RPN evaluator with many built-in functions is provided.
81 /*****************************************************************************
83 *****************************************************************************/
84 #define MAX_DIR_SIZE 2560
85 #define STACK_MAX 100 //< Maximum RPN stack size
88 /*****************************************************************************
90 *****************************************************************************/
92 /** \defgroup http_utils Utilities
98 /* File and directory functions */
100 /** This function recursively parses a directory and adds all files */
101 int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
103 /** This function loads a file into a buffer */
104 int FileLoad( FILE *f, char **pp_data, int *pi_data );
105 /** This function creates a suitable URL for a filename */
106 char *FileToUrl( char *name, bool *pb_index );
107 /** This function returns the real path of a file or directory */
108 char *RealPath( const char *psz_src );
110 /** This command parses the "seek" command for the HTTP interface
111 * and performs the requested action */
112 void HandleSeek( intf_thread_t *p_intf, char *p_value );
114 /* URI Handling functions */
116 /** This function extracts the value for a given argument name
117 * from an HTTP request */
118 const char *ExtractURIValue( const char *restrict psz_uri,
119 const char *restrict psz_name,
120 char *restrict psz_value, size_t i_value_max );
121 char *ExtractURIString( const char *restrict psz_uri,
122 const char *restrict psz_name );
123 /** \todo Describe this function */
124 int TestURIParam( char *psz_uri, const char *psz_name );
126 /** This function parses a MRL */
127 input_item_t *MRLParse( intf_thread_t *, const char *psz, char *psz_name );
129 /** Return the first word from a string (works in-place) */
130 char *FirstWord( char *psz, char *new );
134 /****************************************************************************
135 * Variable handling functions
136 ****************************************************************************/
138 /** \defgroup http_vars Macro variables
140 * These variables can be used in the <vlc> macros and in the RPN evaluator.
141 * The variables make a tree: each variable can have an arbitrary
142 * number of "children" variables.
143 * A number of helper functions are provided to manipulate the main variable
150 * This structure defines a macro variable
152 typedef struct mvar_s
154 char *name; ///< Variable name
155 char *value; ///< Variable value
157 int i_field; ///< Number of children variables
158 struct mvar_s **field; ///< Children variables array
162 /** This function creates a new variable */
163 mvar_t *mvar_New( const char *name, const char *value );
164 /** This function deletes a variable */
165 void mvar_Delete( mvar_t *v );
166 /** This function adds f to the children variables of v, at last position */
167 void mvar_AppendVar( mvar_t *v, mvar_t *f );
168 /** This function duplicates a variable */
169 mvar_t *mvar_Duplicate( const mvar_t *v );
170 /** This function adds f to the children variables of v, at fist position */
171 void mvar_PushVar( mvar_t *v, mvar_t *f );
172 /** This function removes f from the children variables of v */
173 void mvar_RemoveVar( mvar_t *v, mvar_t *f );
174 /** This function retrieves the child variable named "name" */
175 mvar_t *mvar_GetVar( mvar_t *s, const char *name );
176 /** This function retrieves the value of the child variable named "field" */
177 const char *mvar_GetValue( mvar_t *v, const char *field );
178 /** This function creates a variable with the given name and value and
179 * adds it as first child of vars */
180 void mvar_PushNewVar( mvar_t *vars, const char *name,
182 /** This function creates a variable with the given name and value and
183 * adds it as last child of vars */
184 void mvar_AppendNewVar( mvar_t *vars, const char *name,
188 /** \defgroup http_sets Sets *
190 * Sets are an application of the macro variables. There are a number of
191 * predefined functions that will give you variables whose children represent
192 * VLC internal data (playlist, stream info, ...)
196 /** This function creates a set variable which represents a series of integer
197 * The arg parameter must be of the form "start[:stop[:step]]" */
198 mvar_t *mvar_IntegerSetNew( const char *name, const char *arg );
200 /** This function creates a set variable with a list of VLC objects */
201 mvar_t *mvar_ObjectSetNew( intf_thread_t *p_intf, char *name, const char *arg );
203 /** This function creates a set variable with the contents of the playlist */
204 mvar_t *mvar_PlaylistSetNew( intf_thread_t *p_intf, char *name,
206 /** This function creates a set variable with the contents of the Stream
207 * and media info box */
208 mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input );
209 /** This function creates a set variable with the input parameters */
210 mvar_t *mvar_InputVarSetNew( intf_thread_t *p_intf, char *name,
211 input_thread_t *p_input,
212 const char *psz_variable );
213 /** This function creates a set variable representing the files of the psz_dir
215 mvar_t *mvar_FileSetNew( intf_thread_t *p_intf, char *name,
217 /** This function creates a set variable representing the VLM streams */
218 mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm );
220 /** This function converts the listing of a playlist node into a mvar set */
221 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
222 playlist_item_t *p_node, char *name, mvar_t *s,
227 /*****************************************************************************
229 *****************************************************************************/
231 /** \defgroup http_rpn RPN Evaluator
237 * \struct rpn_stack_t
238 * This structure represents a stack of RPN commands for the HTTP interface
239 * It is attached to a request
243 char *stack[STACK_MAX];
247 /** This function creates the RPN evaluator stack */
248 void SSInit( rpn_stack_t * );
249 /** This function cleans the evaluator stack */
250 void SSClean( rpn_stack_t * );
251 /* Evaluate and execute the RPN Stack */
252 void EvaluateRPN( intf_thread_t *p_intf, mvar_t *vars,
253 rpn_stack_t *st, char *exp );
255 /* Push an operand on top of the RPN stack */
256 void SSPush ( rpn_stack_t *, const char * );
257 /* Remove the first operand from the RPN Stack */
258 char *SSPop ( rpn_stack_t * );
259 /* Pushes an operand at a given position in the stack */
260 void SSPushN ( rpn_stack_t *, int );
261 /* Removes an operand at the given position in the stack */
262 int SSPopN ( rpn_stack_t *, mvar_t * );
267 /****************************************************************************
268 * Macro handling (<vlc ... stuff)
269 ****************************************************************************/
271 /** \defgroup http_macros <vlc> Macros Handling
273 * A macro is a code snippet in the HTML page looking like
274 * <vlc id="macro_id" param1="value1" param2="value2">
275 * Macros string ids are mapped to macro types, and specific handling code
276 * must be written for each macro type
282 * This structure represents a HTTP Interface macro.
286 char *id; ///< Macro ID string
287 char *param1; ///< First parameter
288 char *param2; ///< Second parameter
291 /** This function parses a file for macros */
292 void Execute( httpd_file_sys_t *p_args,
293 char *p_request, int i_request,
294 char **pp_data, int *pi_data,
296 char *_src, char *_end );
303 /** \struct httpd_file_sys_t
304 * This structure represent a single HTML file to be parsed by the macros
306 struct httpd_file_sys_t
308 intf_thread_t *p_intf;
309 httpd_file_t *p_file;
310 httpd_redirect_t *p_redir;
311 httpd_redirect_t *p_redir2;
316 bool b_html, b_handler;
318 /* inited for each access */
323 /** \struct http_association_t
324 * Structure associating an extension to an external program
326 typedef struct http_association_t
331 } http_association_t;
333 /** \struct httpd_handler_sys_t
334 * This structure represent a single CGI file to be parsed by the macros
336 struct httpd_handler_sys_t
338 httpd_file_sys_t file;
340 /* HACK ALERT: this is added below so that casting httpd_handler_sys_t
341 * to httpd_file_sys_t works */
342 httpd_handler_t *p_handler;
343 http_association_t *p_association;
346 /** \struct intf_sys_t
347 * Internal service structure for the HTTP interface
351 httpd_host_t *p_httpd_host;
354 httpd_file_sys_t **pp_files;
357 http_association_t **pp_handlers;
358 httpd_handler_t *p_art_handler;
360 playlist_t *p_playlist;
361 input_thread_t *p_input;
365 unsigned short i_port;
368 /** This function is the main HTTPD Callback used by the HTTP Interface */
369 int HttpCallback( httpd_file_sys_t *p_args,
372 uint8_t **pp_data, int *pi_data );
373 /** This function is the HTTPD Callback used for CGIs */
374 int HandlerCallback( httpd_handler_sys_t *p_args,
375 httpd_handler_t *p_handler, char *_p_url,
376 uint8_t *_p_request, int i_type,
377 uint8_t *_p_in, int i_in,
378 char *psz_remote_addr, char *psz_remote_host,
379 uint8_t **_pp_data, int *pi_data );