00001 /***************************************************************************** 00002 * http.h: Headers for the HTTP interface 00003 ***************************************************************************** 00004 * Copyright (C) 2001-2007 the VideoLAN team 00005 * $Id$ 00006 * 00007 * Authors: Gildas Bazin <gbazin@netcourrier.com> 00008 * Laurent Aimar <fenrir@via.ecp.fr> 00009 * Christophe Massiot <massiot@via.ecp.fr> 00010 * 00011 * This program is free software; you can redistribute it and/or modify 00012 * it under the terms of the GNU General Public License as published by 00013 * the Free Software Foundation; either version 2 of the License, or 00014 * (at your option) any later version. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * along with this program; if not, write to the Free Software 00023 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. 00024 *****************************************************************************/ 00025 00026 #ifndef _HTTP_H_ 00027 #define _HTTP_H_ 00028 00029 /***************************************************************************** 00030 * Preamble 00031 *****************************************************************************/ 00032 #include <vlc_common.h> 00033 #include <stdlib.h> 00034 #include <strings.h> 00035 #include <ctype.h> 00036 #include <vlc_interface.h> 00037 #include <vlc_playlist.h> 00038 00039 #include <vlc_aout.h> 00040 #include <vlc_vout.h> /* for fullscreen */ 00041 00042 #include "vlc_httpd.h" 00043 #include "vlc_vlm.h" 00044 #include "vlc_network.h" 00045 #include "vlc_acl.h" 00046 #include "vlc_charset.h" 00047 00048 #ifdef HAVE_SYS_STAT_H 00049 # include <sys/stat.h> 00050 #endif 00051 #ifdef HAVE_ERRNO_H 00052 # include <errno.h> 00053 #endif 00054 #ifdef HAVE_FCNTL_H 00055 # include <fcntl.h> 00056 #endif 00057 00058 #ifdef HAVE_UNISTD_H 00059 # include <unistd.h> 00060 #elif defined( WIN32 ) && !defined( UNDER_CE ) 00061 # include <io.h> 00062 #endif 00063 00064 #ifdef HAVE_DIRENT_H 00065 # include <dirent.h> 00066 #endif 00067 00068 /* stat() support for large files on win32 */ 00069 #if defined( WIN32 ) && !defined( UNDER_CE ) 00070 # define stat _stati64 00071 #endif 00072 00073 /** \defgroup http_intf HTTP Interface 00074 * This is the HTTP remote control interface. It is fully customizable 00075 * by writing HTML pages using custom <vlc> tags. 00076 * 00077 * These tags use so-called macros. 00078 * 00079 * These macros can manipulate variables. For more complex operations, 00080 * a custom RPN evaluator with many built-in functions is provided. 00081 * @{ 00082 */ 00083 00084 /***************************************************************************** 00085 * Local defines 00086 *****************************************************************************/ 00087 #define MAX_DIR_SIZE 2560 00088 #define STACK_MAX 100 //< Maximum RPN stack size 00089 00090 00091 /***************************************************************************** 00092 * Utility functions 00093 *****************************************************************************/ 00094 00095 /** \defgroup http_utils Utilities 00096 * \ingroup http_intf 00097 * Utilities 00098 * @{ 00099 */ 00100 00101 /* File and directory functions */ 00102 00103 /** This function recursively parses a directory and adds all files */ 00104 int ParseDirectory( intf_thread_t *p_intf, char *psz_root, 00105 char *psz_dir ); 00106 /** This function loads a file into a buffer */ 00107 int FileLoad( FILE *f, char **pp_data, int *pi_data ); 00108 /** This function creates a suitable URL for a filename */ 00109 char *FileToUrl( char *name, bool *pb_index ); 00110 /** This function returns the real path of a file or directory */ 00111 char *RealPath( const char *psz_src ); 00112 00113 /** This command parses the "seek" command for the HTTP interface 00114 * and performs the requested action */ 00115 void HandleSeek( intf_thread_t *p_intf, char *p_value ); 00116 00117 /* URI Handling functions */ 00118 00119 /** This function extracts the value for a given argument name 00120 * from an HTTP request */ 00121 char *ExtractURIValue( char *restrict psz_uri, 00122 const char *restrict psz_name, 00123 char *restrict psz_value, size_t i_value_max ); 00124 char *ExtractURIString( char *restrict psz_uri, 00125 const char *restrict psz_name ); 00126 /** \todo Describe this function */ 00127 int TestURIParam( char *psz_uri, const char *psz_name ); 00128 00129 /** This function parses a MRL */ 00130 input_item_t *MRLParse( intf_thread_t *, char *psz, char *psz_name ); 00131 00132 /** Return the first word from a string (works in-place) */ 00133 char *FirstWord( char *psz, char *new ); 00134 00135 /**@}*/ 00136 00137 /**************************************************************************** 00138 * Variable handling functions 00139 ****************************************************************************/ 00140 00141 /** \defgroup http_vars Macro variables 00142 * \ingroup http_intf 00143 * These variables can be used in the <vlc> macros and in the RPN evaluator. 00144 * The variables make a tree: each variable can have an arbitrary 00145 * number of "children" variables. 00146 * A number of helper functions are provided to manipulate the main variable 00147 * structure 00148 * @{ 00149 */ 00150 00151 /** 00152 * \struct mvar_t 00153 * This structure defines a macro variable 00154 */ 00155 typedef struct mvar_s 00156 { 00157 char *name; ///< Variable name 00158 char *value; ///< Variable value 00159 00160 int i_field; ///< Number of children variables 00161 struct mvar_s **field; ///< Children variables array 00162 } mvar_t; 00163 00164 00165 /** This function creates a new variable */ 00166 mvar_t *mvar_New( const char *name, const char *value ); 00167 /** This function deletes a variable */ 00168 void mvar_Delete( mvar_t *v ); 00169 /** This function adds f to the children variables of v, at last position */ 00170 void mvar_AppendVar( mvar_t *v, mvar_t *f ); 00171 /** This function duplicates a variable */ 00172 mvar_t *mvar_Duplicate( const mvar_t *v ); 00173 /** This function adds f to the children variables of v, at fist position */ 00174 void mvar_PushVar( mvar_t *v, mvar_t *f ); 00175 /** This function removes f from the children variables of v */ 00176 void mvar_RemoveVar( mvar_t *v, mvar_t *f ); 00177 /** This function retrieves the child variable named "name" */ 00178 mvar_t *mvar_GetVar( mvar_t *s, const char *name ); 00179 /** This function retrieves the value of the child variable named "field" */ 00180 char *mvar_GetValue( mvar_t *v, char *field ); 00181 /** This function creates a variable with the given name and value and 00182 * adds it as first child of vars */ 00183 void mvar_PushNewVar( mvar_t *vars, const char *name, 00184 const char *value ); 00185 /** This function creates a variable with the given name and value and 00186 * adds it as last child of vars */ 00187 void mvar_AppendNewVar( mvar_t *vars, const char *name, 00188 const char *value ); 00189 /** @} */ 00190 00191 /** \defgroup http_sets Sets * 00192 * \ingroup http_intf 00193 * Sets are an application of the macro variables. There are a number of 00194 * predefined functions that will give you variables whose children represent 00195 * VLC internal data (playlist, stream info, ...) 00196 * @{ 00197 */ 00198 00199 /** This function creates a set variable which represents a series of integer 00200 * The arg parameter must be of the form "start[:stop[:step]]" */ 00201 mvar_t *mvar_IntegerSetNew( const char *name, const char *arg ); 00202 00203 /** This function creates a set variable with a list of VLC objects */ 00204 mvar_t *mvar_ObjectSetNew( intf_thread_t *p_intf, char *name, const char *arg ); 00205 00206 /** This function creates a set variable with the contents of the playlist */ 00207 mvar_t *mvar_PlaylistSetNew( intf_thread_t *p_intf, char *name, 00208 playlist_t *p_pl ); 00209 /** This function creates a set variable with the contents of the Stream 00210 * and media info box */ 00211 mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input ); 00212 /** This function creates a set variable with the input parameters */ 00213 mvar_t *mvar_InputVarSetNew( intf_thread_t *p_intf, char *name, 00214 input_thread_t *p_input, 00215 const char *psz_variable ); 00216 /** This function creates a set variable representing the files of the psz_dir 00217 * directory */ 00218 mvar_t *mvar_FileSetNew( intf_thread_t *p_intf, char *name, 00219 char *psz_dir ); 00220 /** This function creates a set variable representing the VLM streams */ 00221 mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm ); 00222 00223 /** This function converts the listing of a playlist node into a mvar set */ 00224 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl, 00225 playlist_item_t *p_node, char *name, mvar_t *s, 00226 int i_depth ); 00227 00228 /**@}*/ 00229 00230 /***************************************************************************** 00231 * RPN Evaluator 00232 *****************************************************************************/ 00233 00234 /** \defgroup http_rpn RPN Evaluator 00235 * \ingroup http_intf 00236 * @{ 00237 */ 00238 00239 /** 00240 * \struct rpn_stack_t 00241 * This structure represents a stack of RPN commands for the HTTP interface 00242 * It is attached to a request 00243 */ 00244 typedef struct 00245 { 00246 char *stack[STACK_MAX]; 00247 int i_stack; 00248 } rpn_stack_t; 00249 00250 /** This function creates the RPN evaluator stack */ 00251 void SSInit( rpn_stack_t * ); 00252 /** This function cleans the evaluator stack */ 00253 void SSClean( rpn_stack_t * ); 00254 /* Evaluate and execute the RPN Stack */ 00255 void EvaluateRPN( intf_thread_t *p_intf, mvar_t *vars, 00256 rpn_stack_t *st, char *exp ); 00257 00258 /* Push an operand on top of the RPN stack */ 00259 void SSPush ( rpn_stack_t *, const char * ); 00260 /* Remove the first operand from the RPN Stack */ 00261 char *SSPop ( rpn_stack_t * ); 00262 /* Pushes an operand at a given position in the stack */ 00263 void SSPushN ( rpn_stack_t *, int ); 00264 /* Removes an operand at the given position in the stack */ 00265 int SSPopN ( rpn_stack_t *, mvar_t * ); 00266 00267 /**@}*/ 00268 00269 00270 /**************************************************************************** 00271 * Macro handling (<vlc ... stuff) 00272 ****************************************************************************/ 00273 00274 /** \defgroup http_macros <vlc> Macros Handling 00275 * \ingroup http_intf 00276 * A macro is a code snippet in the HTML page looking like 00277 * <vlc id="macro_id" param1="value1" param2="value2"> 00278 * Macros string ids are mapped to macro types, and specific handling code 00279 * must be written for each macro type 00280 * @{ 00281 */ 00282 00283 00284 /** \struct macro_t 00285 * This structure represents a HTTP Interface macro. 00286 */ 00287 typedef struct 00288 { 00289 char *id; ///< Macro ID string 00290 char *param1; ///< First parameter 00291 char *param2; ///< Second parameter 00292 } macro_t; 00293 00294 /** This function parses a file for macros */ 00295 void Execute( httpd_file_sys_t *p_args, 00296 char *p_request, int i_request, 00297 char **pp_data, int *pi_data, 00298 char **pp_dst, 00299 char *_src, char *_end ); 00300 00301 /**@}*/ 00302 00303 /** 00304 * Core stuff 00305 */ 00306 /** \struct 00307 * This structure represent a single HTML file to be parsed by the macros 00308 * handling engine */ 00309 struct httpd_file_sys_t 00310 { 00311 intf_thread_t *p_intf; 00312 httpd_file_t *p_file; 00313 httpd_redirect_t *p_redir; 00314 httpd_redirect_t *p_redir2; 00315 00316 char *file; 00317 char *name; 00318 00319 bool b_html, b_handler; 00320 00321 /* inited for each access */ 00322 rpn_stack_t stack; 00323 mvar_t *vars; 00324 }; 00325 00326 /** \struct 00327 * Structure associating an extension to an external program 00328 */ 00329 typedef struct http_association_t 00330 { 00331 char *psz_ext; 00332 int i_argc; 00333 char **ppsz_argv; 00334 } http_association_t; 00335 00336 /** \struct 00337 * This structure represent a single CGI file to be parsed by the macros 00338 * handling engine */ 00339 struct httpd_handler_sys_t 00340 { 00341 httpd_file_sys_t file; 00342 00343 /* HACK ALERT: this is added below so that casting httpd_handler_sys_t 00344 * to httpd_file_sys_t works */ 00345 httpd_handler_t *p_handler; 00346 http_association_t *p_association; 00347 }; 00348 00349 /** \struct 00350 * Internal service structure for the HTTP interface 00351 */ 00352 struct intf_sys_t 00353 { 00354 httpd_host_t *p_httpd_host; 00355 00356 int i_files; 00357 httpd_file_sys_t **pp_files; 00358 00359 int i_handlers; 00360 http_association_t **pp_handlers; 00361 httpd_handler_t *p_art_handler; 00362 00363 playlist_t *p_playlist; 00364 input_thread_t *p_input; 00365 vlm_t *p_vlm; 00366 00367 char *psz_address; 00368 unsigned short i_port; 00369 }; 00370 00371 /** This function is the main HTTPD Callback used by the HTTP Interface */ 00372 int HttpCallback( httpd_file_sys_t *p_args, 00373 httpd_file_t *, 00374 uint8_t *p_request, 00375 uint8_t **pp_data, int *pi_data ); 00376 /** This function is the HTTPD Callback used for CGIs */ 00377 int HandlerCallback( httpd_handler_sys_t *p_args, 00378 httpd_handler_t *p_handler, char *_p_url, 00379 uint8_t *_p_request, int i_type, 00380 uint8_t *_p_in, int i_in, 00381 char *psz_remote_addr, char *psz_remote_host, 00382 uint8_t **_pp_data, int *pi_data ); 00383 /**@}*/ 00384 00385 #endif 00386
1.5.1