]> git.sesse.net Git - vlc/blob - modules/control/http/http.h
try fixing charset problems. I'll launch a win32 nightly, try it out and change it...
[vlc] / modules / control / http / http.h
1 /*****************************************************************************
2  * http.h: Headers for the HTTP interface
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 #ifndef _HTTP_H_
27 #define _HTTP_H_
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <ctype.h>
35 #include <vlc/vlc.h>
36 #include <vlc/intf.h>
37
38 #include <vlc/aout.h>
39 #include <vlc/vout.h> /* for fullscreen */
40
41 #include "vlc_httpd.h"
42 #include "vlc_vlm.h"
43 #include "vlc_tls.h"
44 #include "vlc_acl.h"
45 #include "charset.h"
46
47 #ifdef HAVE_SYS_STAT_H
48 #   include <sys/stat.h>
49 #endif
50 #ifdef HAVE_ERRNO_H
51 #   include <errno.h>
52 #endif
53 #ifdef HAVE_FCNTL_H
54 #   include <fcntl.h>
55 #endif
56
57 #ifdef HAVE_UNISTD_H
58 #   include <unistd.h>
59 #elif defined( WIN32 ) && !defined( UNDER_CE )
60 #   include <io.h>
61 #endif
62
63 #ifdef HAVE_DIRENT_H
64 #   include <dirent.h>
65 #endif
66
67 /* stat() support for large files on win32 */
68 #if defined( WIN32 ) && !defined( UNDER_CE )
69 #   define stat _stati64
70 #endif
71
72 /** \defgroup http_intf HTTP Interface
73  * This is the HTTP remote control interface. It is fully customizable
74  * by writing HTML pages using custom <vlc> tags.
75  *
76  * These tags use so-called macros.
77  *
78  * These macros can manipulate variables. For more complex operations,
79  * a custom RPN evaluator with many built-in functions is provided.
80  * @{
81  */
82
83 /*****************************************************************************
84  * Local defines
85  *****************************************************************************/
86 #define MAX_DIR_SIZE 2560
87 #define STACK_MAX 100        //< Maximum RPN stack size
88
89
90 /*****************************************************************************
91  * Utility functions
92  *****************************************************************************/
93
94 /** \defgroup http_utils Utilities
95  * \ingroup http_intf
96  * Utilities
97  * @{
98  */
99
100 /* File and directory functions */
101
102 /** This function recursively parses a directory and adds all files */
103 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
104                         char *psz_dir );
105 /** This function loads a file into a buffer */
106 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data );
107 /** This function creates a suitable URL for a filename */
108 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index );
109 /** This function returns the real path of a file or directory */
110 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src );
111
112 /* Locale handling functions */
113
114 /** This fuction converts a locale string to UTF-8 */
115 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 );
116 /** This function converts an UTF-8 to locale */
117 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local );
118
119 /** This command parses the "seek" command for the HTTP interface
120  * and performs the requested action */
121 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value );
122
123 /* URI Handling functions */
124
125 /** This function extracts the value for a given argument name
126  * from an HTTP request */
127 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
128                              char *psz_value, int i_value_max );
129 /** \todo Describe this function */
130 int E_(TestURIParam)( char *psz_uri, const char *psz_name );
131 /** This function extracts the original value from an URL-encoded string */
132 void E_(DecodeEncodedURI)( char *psz );
133
134 /** This function parses a MRL */
135 playlist_item_t *E_(MRLParse)( intf_thread_t *, char *psz, char *psz_name );
136
137 /** Return the first word from a string (works in-place) */
138 char *E_(FirstWord)( char *psz, char *new );
139
140 /**@}*/
141
142 /****************************************************************************
143  * Variable handling functions
144  ****************************************************************************/
145
146 /** \defgroup http_vars Macro variables
147  * \ingroup http_intf
148  * These variables can be used in the <vlc> macros and in the RPN evaluator.
149  * The variables make a tree: each variable can have an arbitrary
150  * number of "children" variables.
151  * A number of helper functions are provided to manipulate the main variable
152  * structure
153  * @{
154  */
155
156 /**
157  * \struct mvar_t
158  * This structure defines a macro variable
159  */
160 typedef struct mvar_s
161 {
162     char *name;                 ///< Variable name
163     char *value;                ///< Variable value
164
165     int           i_field;      ///< Number of children variables
166     struct mvar_s **field;      ///< Children variables array
167 } mvar_t;
168
169
170 /** This function creates a new variable */
171 mvar_t  *E_(mvar_New)( const char *name, const char *value );
172 /** This function deletes a variable */
173 void     E_(mvar_Delete)( mvar_t *v );
174 /** This function adds f to the children variables of v, at last position */
175 void     E_(mvar_AppendVar)( mvar_t *v, mvar_t *f );
176 /** This function duplicates a variable */
177 mvar_t  *E_(mvar_Duplicate)( const mvar_t *v );
178 /** This function adds f to the children variables of v, at fist position */
179 void     E_(mvar_PushVar)( mvar_t *v, mvar_t *f );
180 /** This function removes f from the children variables of v */
181 void     E_(mvar_RemoveVar)( mvar_t *v, mvar_t *f );
182 /** This function retrieves the child variable named "name" */
183 mvar_t  *E_(mvar_GetVar)( mvar_t *s, const char *name );
184 /** This function retrieves the value of the child variable named "field" */
185 char    *E_(mvar_GetValue)( mvar_t *v, char *field );
186 /** This function creates a variable with the given name and value and
187  * adds it as first child of vars */
188 void     E_(mvar_PushNewVar)( mvar_t *vars, const char *name,
189                               const char *value );
190 /** This function creates a variable with the given name and value and
191  * adds it as last child of vars */
192 void     E_(mvar_AppendNewVar)( mvar_t *vars, const char *name,
193                                 const char *value );
194 /** @} */
195
196 /** \defgroup http_sets Sets *
197  * \ingroup http_intf
198  * Sets are an application of the macro variables. There are a number of
199  * predefined functions that will give you variables whose children represent
200  * VLC internal data (playlist, stream info, ...)
201  * @{
202  */
203
204 /** This function creates a set variable which represents a series of integer
205  * The arg parameter must be of the form "start[:stop[:step]]"  */
206 mvar_t *E_(mvar_IntegerSetNew)( const char *name, const char *arg );
207
208 /** This function creates a set variable with a list of VLC objects */
209 mvar_t *E_(mvar_ObjectSetNew)( intf_thread_t *p_intf, char *name, char *arg );
210
211 /** This function creates a set variable with the contents of the playlist */
212 mvar_t *E_(mvar_PlaylistSetNew)( intf_thread_t *p_intf, char *name,
213                                  playlist_t *p_pl );
214 /** This function creates a set variable with the contents of the Stream
215  * and media info box */
216 mvar_t *E_(mvar_InfoSetNew)( intf_thread_t *p_intf, char *name,
217                              input_thread_t *p_input );
218 /** This function creates a set variable with the input parameters */
219 mvar_t *E_(mvar_InputVarSetNew)( intf_thread_t *p_intf, char *name,
220                                  input_thread_t *p_input,
221                                  const char *psz_variable );
222 /** This function creates a set variable representing the files of the psz_dir
223  * directory */
224 mvar_t *E_(mvar_FileSetNew)( intf_thread_t *p_intf, char *name,
225                              char *psz_dir );
226 /** This function creates a set variable representing the VLM streams */
227 mvar_t *E_(mvar_VlmSetNew)( char *name, vlm_t *vlm );
228
229 /** This function converts the listing of a playlist node into a mvar set */
230 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
231                            playlist_item_t *p_node, char *name, mvar_t *s,
232                            int i_depth );
233
234 /**@}*/
235
236 /*****************************************************************************
237  * RPN Evaluator
238  *****************************************************************************/
239
240 /** \defgroup http_rpn RPN Evaluator
241  * \ingroup http_intf
242  * @{
243  */
244
245 /**
246  * \struct rpn_stack_t
247  * This structure represents a stack of RPN commands for the HTTP interface
248  * It is attached to a request
249  */
250 typedef struct
251 {
252     char *stack[STACK_MAX];
253     int  i_stack;
254 } rpn_stack_t;
255
256 /** This function creates the RPN evaluator stack */
257 void E_(SSInit)( rpn_stack_t * );
258 /** This function cleans the evaluator stack */
259 void E_(SSClean)( rpn_stack_t * );
260 /* Evaluate and execute the RPN Stack */
261 void  E_(EvaluateRPN)( intf_thread_t *p_intf, mvar_t  *vars,
262                        rpn_stack_t *st, char *exp );
263
264 /* Push an operand on top of the RPN stack */
265 void E_(SSPush)  ( rpn_stack_t *, const char * );
266 /* Remove the first operand from the RPN Stack */
267 char *E_(SSPop)  ( rpn_stack_t * );
268 /* Pushes an operand at a given position in the stack */
269 void E_(SSPushN) ( rpn_stack_t *, int );
270 /* Removes an operand at the given position in the stack */
271 int  E_(SSPopN)  ( rpn_stack_t *, mvar_t  * );
272
273 /**@}*/
274
275
276 /****************************************************************************
277  * Macro handling (<vlc ... stuff)
278  ****************************************************************************/
279
280 /** \defgroup http_macros <vlc> Macros Handling
281  * \ingroup http_intf
282  * A macro is a code snippet in the HTML page looking like
283  * <vlc id="macro_id" param1="value1" param2="value2">
284  * Macros string ids are mapped to macro types, and specific handling code
285  * must be written for each macro type
286  * @{
287  */
288
289
290 /** \struct macro_t
291  * This structure represents a HTTP Interface macro.
292  */
293 typedef struct
294 {
295     char *id;           ///< Macro ID string
296     char *param1;       ///< First parameter
297     char *param2;       ///< Second parameter
298 } macro_t;
299
300 /** This function creates a macro from a <vlc ....> tag */
301 int E_(MacroParse)( macro_t *m, char *psz_src );
302 /** This function cleans a macro */
303 void E_(MacroClean)( macro_t *m );
304
305 /** This function returns the macro type identifier from its id= string value
306  * It uses the StrToMacroTypeTab mapping array for this */
307 int E_(StrToMacroType)( char *name );
308 /** This function actually executes the macro */
309 void E_(MacroDo)( httpd_file_sys_t *p_args, macro_t *m,
310                   char *p_request, int i_request, char **pp_data,
311                   int *pi_data, char **pp_dst );
312 /** This function looks for macros in a string */
313 char *E_(MacroSearch)( char *src, char *end,
314                    int i_mvlc, vlc_bool_t b_after );
315
316 /** This function parses a file for macros */
317 void E_(Execute)( httpd_file_sys_t *p_args,
318                   char *p_request, int i_request,
319                   char **pp_data, int *pi_data,
320                   char **pp_dst,
321                   char *_src, char *_end );
322
323 /**@}*/
324
325 /**
326  * Core stuff
327  */
328 /** \struct
329  * This structure represent a single HTML file to be parsed by the macros
330  * handling engine */
331 struct httpd_file_sys_t
332 {
333     intf_thread_t    *p_intf;
334     httpd_file_t     *p_file;
335     httpd_redirect_t *p_redir;
336     httpd_redirect_t *p_redir2;
337
338     char          *file;
339     char          *name;
340
341     vlc_bool_t    b_html, b_handler;
342
343     /* inited for each access */
344     rpn_stack_t   stack;
345     mvar_t        *vars;
346 };
347
348 /** \struct
349  * Structure associating an extension to an external program
350  */
351 typedef struct http_association_t
352 {
353     char                *psz_ext;
354     int                 i_argc;
355     char                **ppsz_argv;
356 } http_association_t;
357
358 /** \struct
359  * This structure represent a single CGI file to be parsed by the macros
360  * handling engine */
361 struct httpd_handler_sys_t
362 {
363     httpd_file_sys_t file;
364
365     /* HACK ALERT: this is added below so that casting httpd_handler_sys_t
366      * to httpd_file_sys_t works */
367     httpd_handler_t  *p_handler;
368     http_association_t *p_association;
369 };
370
371 /** \struct
372  * Internal service structure for the HTTP interface
373  */
374 struct intf_sys_t
375 {
376     httpd_host_t        *p_httpd_host;
377
378     int                 i_files;
379     httpd_file_sys_t    **pp_files;
380
381     int                 i_handlers;
382     http_association_t  **pp_handlers;
383
384     playlist_t          *p_playlist;
385     input_thread_t      *p_input;
386     vlm_t               *p_vlm;
387     char                *psz_html_type;
388     char                *psz_charset;
389     vlc_iconv_t         iconv_from_utf8, iconv_to_utf8;
390
391     char                *psz_address;
392     unsigned short      i_port;
393 };
394
395 /** This function is the main HTTPD Callback used by the HTTP Interface */
396 int E_(HttpCallback)( httpd_file_sys_t *p_args,
397                       httpd_file_t *,
398                       uint8_t *p_request,
399                       uint8_t **pp_data, int *pi_data );
400 /** This function is the HTTPD Callback used for CGIs */
401 int  E_(HandlerCallback)( httpd_handler_sys_t *p_args,
402                           httpd_handler_t *p_handler, char *_p_url,
403                           uint8_t *_p_request, int i_type,
404                           uint8_t *_p_in, int i_in,
405                           char *psz_remote_addr, char *psz_remote_host,
406                           uint8_t **_pp_data, int *pi_data );
407 /**@}*/
408
409 #endif
410