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>
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
39 #include <vlc_vout.h> /* for fullscreen */
41 #include <vlc_httpd.h>
43 #include <vlc_network.h>
48 #elif defined( WIN32 ) && !defined( UNDER_CE )
56 /* stat() support for large files on win32 */
57 #if defined( WIN32 ) && !defined( UNDER_CE )
58 # define stat _stati64
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.
65 * These tags use so-called macros.
67 * These macros can manipulate variables. For more complex operations,
68 * a custom RPN evaluator with many built-in functions is provided.
72 /*****************************************************************************
74 *****************************************************************************/
75 #define MAX_DIR_SIZE 2560
76 #define STACK_MAX 100 //< Maximum RPN stack size
79 /*****************************************************************************
81 *****************************************************************************/
83 /** \defgroup http_utils Utilities
89 /* File and directory functions */
91 /** This function recursively parses a directory and adds all files */
92 int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
94 /** This function loads a file into a buffer */
95 int FileLoad( FILE *f, char **pp_data, int *pi_data );
96 /** This function creates a suitable URL for a filename */
97 char *FileToUrl( char *name, bool *pb_index );
98 /** This function returns the real path of a file or directory */
99 char *RealPath( const char *psz_src );
101 /** This command parses the "seek" command for the HTTP interface
102 * and performs the requested action */
103 void HandleSeek( intf_thread_t *p_intf, char *p_value );
105 /* URI Handling functions */
107 /** This function extracts the value for a given argument name
108 * from an HTTP request */
109 const char *ExtractURIValue( const char *restrict psz_uri,
110 const char *restrict psz_name,
111 char *restrict psz_value, size_t i_value_max );
112 char *ExtractURIString( const char *restrict psz_uri,
113 const char *restrict psz_name );
114 /** \todo Describe this function */
115 int TestURIParam( char *psz_uri, const char *psz_name );
117 /** This function parses a MRL */
118 input_item_t *MRLParse( intf_thread_t *, const char *psz, char *psz_name );
120 /** Return the first word from a string (works in-place) */
121 char *FirstWord( char *psz, char *new );
125 /****************************************************************************
126 * Variable handling functions
127 ****************************************************************************/
129 /** \defgroup http_vars Macro variables
131 * These variables can be used in the <vlc> macros and in the RPN evaluator.
132 * The variables make a tree: each variable can have an arbitrary
133 * number of "children" variables.
134 * A number of helper functions are provided to manipulate the main variable
141 * This structure defines a macro variable
143 typedef struct mvar_s
145 char *name; ///< Variable name
146 char *value; ///< Variable value
148 int i_field; ///< Number of children variables
149 struct mvar_s **field; ///< Children variables array
153 /** This function creates a new variable */
154 mvar_t *mvar_New( const char *name, const char *value );
155 /** This function deletes a variable */
156 void mvar_Delete( mvar_t *v );
157 /** This function adds f to the children variables of v, at last position */
158 void mvar_AppendVar( mvar_t *v, mvar_t *f );
159 /** This function duplicates a variable */
160 mvar_t *mvar_Duplicate( const mvar_t *v );
161 /** This function adds f to the children variables of v, at fist position */
162 void mvar_PushVar( mvar_t *v, mvar_t *f );
163 /** This function removes f from the children variables of v */
164 void mvar_RemoveVar( mvar_t *v, mvar_t *f );
165 /** This function retrieves the child variable named "name" */
166 mvar_t *mvar_GetVar( mvar_t *s, const char *name );
167 /** This function retrieves the value of the child variable named "field" */
168 const char *mvar_GetValue( mvar_t *v, const char *field );
169 /** This function creates a variable with the given name and value and
170 * adds it as first child of vars */
171 void mvar_PushNewVar( mvar_t *vars, const char *name,
173 /** This function creates a variable with the given name and value and
174 * adds it as last child of vars */
175 void mvar_AppendNewVar( mvar_t *vars, const char *name,
179 /** \defgroup http_sets Sets *
181 * Sets are an application of the macro variables. There are a number of
182 * predefined functions that will give you variables whose children represent
183 * VLC internal data (playlist, stream info, ...)
187 /** This function creates a set variable which represents a series of integer
188 * The arg parameter must be of the form "start[:stop[:step]]" */
189 mvar_t *mvar_IntegerSetNew( const char *name, const char *arg );
191 /** This function creates a set variable with a list of VLC objects */
192 mvar_t *mvar_ObjectSetNew( intf_thread_t *p_intf, char *name, const char *arg );
194 /** This function creates a set variable with the contents of the playlist */
195 mvar_t *mvar_PlaylistSetNew( intf_thread_t *p_intf, char *name,
197 /** This function creates a set variable with the contents of the Stream
198 * and media info box */
199 mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input );
200 /** This function creates a set variable with the input parameters */
201 mvar_t *mvar_InputVarSetNew( intf_thread_t *p_intf, char *name,
202 input_thread_t *p_input,
203 const char *psz_variable );
204 /** This function creates a set variable representing the files of the psz_dir
206 mvar_t *mvar_FileSetNew( intf_thread_t *p_intf, char *name,
208 /** This function creates a set variable representing the VLM streams */
209 mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm );
211 /** This function converts the listing of a playlist node into a mvar set */
212 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
213 playlist_item_t *p_node, char *name, mvar_t *s,
218 /*****************************************************************************
220 *****************************************************************************/
222 /** \defgroup http_rpn RPN Evaluator
228 * \struct rpn_stack_t
229 * This structure represents a stack of RPN commands for the HTTP interface
230 * It is attached to a request
234 char *stack[STACK_MAX];
238 /** This function creates the RPN evaluator stack */
239 void SSInit( rpn_stack_t * );
240 /** This function cleans the evaluator stack */
241 void SSClean( rpn_stack_t * );
242 /* Evaluate and execute the RPN Stack */
243 void EvaluateRPN( intf_thread_t *p_intf, mvar_t *vars,
244 rpn_stack_t *st, char *exp );
246 /* Push an operand on top of the RPN stack */
247 void SSPush ( rpn_stack_t *, const char * );
248 /* Remove the first operand from the RPN Stack */
249 char *SSPop ( rpn_stack_t * );
250 /* Pushes an operand at a given position in the stack */
251 void SSPushN ( rpn_stack_t *, int );
252 /* Removes an operand at the given position in the stack */
253 int SSPopN ( rpn_stack_t *, mvar_t * );
258 /****************************************************************************
259 * Macro handling (<vlc ... stuff)
260 ****************************************************************************/
262 /** \defgroup http_macros <vlc> Macros Handling
264 * A macro is a code snippet in the HTML page looking like
265 * <vlc id="macro_id" param1="value1" param2="value2">
266 * Macros string ids are mapped to macro types, and specific handling code
267 * must be written for each macro type
273 * This structure represents a HTTP Interface macro.
277 char *id; ///< Macro ID string
278 char *param1; ///< First parameter
279 char *param2; ///< Second parameter
282 /** This function parses a file for macros */
283 void Execute( httpd_file_sys_t *p_args,
284 char *p_request, int i_request,
285 char **pp_data, int *pi_data,
287 char *_src, char *_end );
294 /** \struct httpd_file_sys_t
295 * This structure represent a single HTML file to be parsed by the macros
297 struct httpd_file_sys_t
299 intf_thread_t *p_intf;
300 httpd_file_t *p_file;
301 httpd_redirect_t *p_redir;
302 httpd_redirect_t *p_redir2;
307 bool b_html, b_handler;
309 /* inited for each access */
314 /** \struct http_association_t
315 * Structure associating an extension to an external program
317 typedef struct http_association_t
322 } http_association_t;
324 /** \struct httpd_handler_sys_t
325 * This structure represent a single CGI file to be parsed by the macros
327 struct httpd_handler_sys_t
329 httpd_file_sys_t file;
331 /* HACK ALERT: this is added below so that casting httpd_handler_sys_t
332 * to httpd_file_sys_t works */
333 httpd_handler_t *p_handler;
334 http_association_t *p_association;
337 /** \struct intf_sys_t
338 * Internal service structure for the HTTP interface
342 httpd_host_t *p_httpd_host;
345 httpd_file_sys_t **pp_files;
348 http_association_t **pp_handlers;
349 httpd_handler_t *p_art_handler;
351 playlist_t *p_playlist;
352 input_thread_t *p_input;
356 unsigned short i_port;
359 /** This function is the main HTTPD Callback used by the HTTP Interface */
360 int HttpCallback( httpd_file_sys_t *p_args,
363 uint8_t **pp_data, int *pi_data );
364 /** This function is the HTTPD Callback used for CGIs */
365 int HandlerCallback( httpd_handler_sys_t *p_args,
366 httpd_handler_t *p_handler, char *_p_url,
367 uint8_t *_p_request, int i_type,
368 uint8_t *_p_in, int i_in,
369 char *psz_remote_addr, char *psz_remote_host,
370 uint8_t **_pp_data, int *pi_data );