]> git.sesse.net Git - vlc/blob - modules/control/http/http.h
Split the HTTP interface and begin to document 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: http.c 12225 2005-08-18 10:01:30Z massiot $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 #ifndef _HTTP_H_
27 #define _HTTP_H_
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <vlc/vlc.h>
35 #include <vlc/intf.h>
36
37 #include <vlc/aout.h>
38 #include <vlc/vout.h> /* for fullscreen */
39
40 #include "vlc_httpd.h"
41 #include "vlc_vlm.h"
42 #include "vlc_tls.h"
43 #include "vlc_acl.h"
44 #include "charset.h"
45
46 #ifdef HAVE_SYS_STAT_H
47 #   include <sys/stat.h>
48 #endif
49 #ifdef HAVE_ERRNO_H
50 #   include <errno.h>
51 #endif
52 #ifdef HAVE_FCNTL_H
53 #   include <fcntl.h>
54 #endif
55
56 #ifdef HAVE_UNISTD_H
57 #   include <unistd.h>
58 #elif defined( WIN32 ) && !defined( UNDER_CE )
59 #   include <io.h>
60 #endif
61
62 #ifdef HAVE_DIRENT_H
63 #   include <dirent.h>
64 #endif
65
66 /* stat() support for large files on win32 */
67 #if defined( WIN32 ) && !defined( UNDER_CE )
68 #   define stat _stati64
69 #endif
70
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.
74  *
75  * These tags use so-called macros.
76  *
77  * These macros can manipulate variables. For more complex operations,
78  * a custom RPN evaluator with many built-in functions is provided.
79  * @{
80  */
81
82 /*****************************************************************************
83  * Local defines
84  *****************************************************************************/
85 #define MAX_DIR_SIZE 2560
86 #define STACK_MAX 100        //< Maximum RPN stack size
87
88
89 /*****************************************************************************
90  * Utility functions
91  *****************************************************************************/
92
93 /** \defgroup http_utils Utilities
94  * \ingroup http_intf
95  * Utilities
96  * @{
97  */
98
99 /* File and directory functions */
100
101 /** This function recursively parses a directory and adds all files */
102 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
103                            char *psz_dir );
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
109 /* Locale handling functions */
110
111 /** This fuction converts a locale string to UTF-8 */
112 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 );
113 /** This function converts an UTF-8 to locale */
114 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local );
115
116 /** This command parses the "seek" command for the HTTP interface
117  * and performs the requested action */
118 void E_(Seek)( intf_thread_t *p_intf, char *p_value );
119
120 /* URI Handling functions */
121
122 /** This function extracts the value for a given argument name
123  * from an HTTP request */
124 char *E_(uri_extract_value)( char *psz_uri, const char *psz_name,
125                              char *psz_value, int i_value_max );
126 /** \todo Describe this function */
127 int E_(uri_test_param)( char *psz_uri, const char *psz_name );
128 /** This function extracts the original value from an URL-encoded string */
129 void E_(uri_decode_url_encoded)( char *psz );
130
131 /** This function parses a MRL */
132 playlist_item_t *E_(MRLParse)( intf_thread_t *, char *psz, char *psz_name );
133
134 /** Return the first word from a string (works in-place) */
135 char *E_(FirstWord)( char *psz, char *new );
136
137 /**@}*/
138
139 /****************************************************************************
140  * Variable handling functions
141  ****************************************************************************/
142
143 /** \defgroup http_vars Macro variables
144  * \ingroup http_intf
145  * These variables are used in macros
146  * @{
147  */
148
149 /**
150  * \struct mvar_t
151  * This structure defines a macro variable as used by the HTTP interface.
152  * These variables can be used in the <vlc> macros
153  */
154 typedef struct mvar_s
155 {
156     char *name;
157     char *value;
158
159     int           i_field;
160     struct mvar_s **field;
161 } mvar_t;
162
163
164 /** This function creates a new variable */
165 mvar_t  *mvar_New( const char *name, const char *value );
166 /** This function deletes a variable */
167 void     mvar_Delete( mvar_t *v );
168 /** This function adds f to the children variables of v, at last position */
169 void     mvar_AppendVar( mvar_t *v, mvar_t *f );
170 /** This function duplicates a variable */
171 mvar_t  *mvar_Duplicate( const mvar_t *v );
172 /** This function adds f to the children variables of v, at fist position */
173 void     mvar_PushVar( mvar_t *v, mvar_t *f );
174 /** This function removes f from the children variables of v */
175 void     mvar_RemoveVar( mvar_t *v, mvar_t *f );
176 /** This function retrieves the child variable named "name" */
177 mvar_t  *mvar_GetVar( mvar_t *s, const char *name );
178 /** This function retrieves the value of the child variable named "field" */
179 char    *mvar_GetValue( mvar_t *v, char *field );
180 /** This function creates a variable with the given name and value and
181  * adds it as first child of vars */
182 void     mvar_PushNewVar( mvar_t *vars, const char *name,
183                              const char *value );
184 /** This function creates a variable with the given name and value and
185  * adds it as last child of vars */
186 void     mvar_AppendNewVar( mvar_t *vars, const char *name,
187                                const char *value );
188 /** @} */
189
190 /** \defgroup http_sets Set variables *
191  * \ingroup http_intf
192  * @{
193  */
194
195 /** This function creates a set variable which represents a series of integer
196  * The arg parameter must be of the form "start[:stop[:step]]"  */
197 mvar_t *mvar_IntegerSetNew( const char *name, const char *arg );
198
199 /** This function creates a set variable with the contents of the playlist */
200 mvar_t *mvar_PlaylistSetNew( intf_thread_t *p_intf, char *name,
201                                     playlist_t *p_pl );
202 /** This function creates a set variable with the contents of the Stream
203  * and media info box */
204 mvar_t *mvar_InfoSetNew( intf_thread_t *p_intf, char *name,
205                                 input_thread_t *p_input );
206 /** This function creates a set variable with the input parameters */
207 mvar_t *mvar_InputVarSetNew( intf_thread_t *p_intf, char *name,
208                                     input_thread_t *p_input,
209                                     const char *psz_variable );
210 /** This function creates a set variable representing the files of the psz_dir
211  * directory */
212 mvar_t *mvar_FileSetNew( intf_thread_t *p_intf, char *name,
213                                 char *psz_dir );
214 /** This function creates a set variable representing the VLM streams */
215 mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm );
216
217 /** This function converts the listing of a playlist node into a mvar set */
218 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
219                        playlist_item_t *p_node, char *name, mvar_t *s,
220                        int i_depth );
221
222 /**@}*/
223
224 /*****************************************************************************
225  * RPN Evaluator
226  *****************************************************************************/
227
228 /** \defgroup http_rpn RPN Evaluator
229  * \ingroup http_intf
230  * @{
231  */
232
233 /**
234  * \struct rpn_stack_t
235  * This structure represents a stack of RPN commands for the HTTP interface
236  * It is attached to a request
237  */
238 typedef struct
239 {
240     char *stack[STACK_MAX];
241     int  i_stack;
242 } rpn_stack_t;
243
244 /** This function creates the RPN evaluator stack */
245 void SSInit( rpn_stack_t * );
246 /** This function cleans the evaluator stack */
247 void SSClean( rpn_stack_t * );
248 /* Evaluate and execute the RPN Stack */
249 void  EvaluateRPN( intf_thread_t *p_intf, mvar_t  *vars,
250                           rpn_stack_t *st, char *exp );
251
252 /* Push an operand on top of the RPN stack */
253 void SSPush  ( rpn_stack_t *, const char * );
254 /* Remove the first operand from the RPN Stack */
255 char *SSPop  ( rpn_stack_t * );
256 /* Pushes an operand at a given position in the stack */
257 void SSPushN ( rpn_stack_t *, int );
258 /* Removes an operand at the given position in the stack */
259 int  SSPopN  ( rpn_stack_t *, mvar_t  * );
260
261 /**@}*/
262
263
264 /****************************************************************************
265  * Macro handling (<vlc ... stuff)
266  ****************************************************************************/
267
268 /** \defgroup http_macros <vlc> Macros Handling
269  * \ingroup http_intf
270  * @{
271  */
272
273
274 /** \struct macro_t
275  * This structure represents a HTTP Interface macro.
276  * A macro is a code snippet in the HTML page looking like
277  * <vlc id="macro_id" param1="value1" param2="value2"
278  */
279 typedef struct
280 {
281     char *id;
282     char *param1;
283     char *param2;
284 } macro_t;
285
286 /** This function creates a macro from a <vlc ....> tag */
287 int MacroParse( macro_t *m, char *psz_src );
288 /** This function cleans a macro */
289 void MacroClean( macro_t *m );
290
291 /** This function returns the macro type identifier from its id= string value
292  * It uses the StrToMacroTypeTab mapping array for this */
293 int StrToMacroType( char *name );
294 /** This function actually executes the macro */
295 void MacroDo( httpd_file_sys_t *p_args, macro_t *m,
296               char *p_request, int i_request, char **pp_data,
297               int *pi_data, char **pp_dst );
298 /** This function looks for macros in a string */
299 char *MacroSearch( char *src, char *end,
300                    int i_mvlc, vlc_bool_t b_after );
301
302 /** This function parses a file for macros */
303 void E_(Execute)( httpd_file_sys_t *p_args,
304                   char *p_request, int i_request,
305                   char **pp_data, int *pi_data,
306                   char **pp_dst,
307                   char *_src, char *_end );
308
309 /**@}*/
310
311 /**
312  * Core stuff
313  */
314 /** \struct
315  * This structure represent a single HTML file to be parsed by the macros
316  * handling engine */
317 struct httpd_file_sys_t
318 {
319     intf_thread_t    *p_intf;
320     httpd_file_t     *p_file;
321     httpd_redirect_t *p_redir;
322     httpd_redirect_t *p_redir2;
323
324     char          *file;
325     char          *name;
326
327     vlc_bool_t    b_html;
328
329     /* inited for each access */
330     rpn_stack_t   stack;
331     mvar_t        *vars;
332 };
333
334 /** \struct
335  * Internal service structure for the HTTP interface
336  */
337 struct intf_sys_t
338 {
339     httpd_host_t        *p_httpd_host;
340
341     int                 i_files;
342     httpd_file_sys_t    **pp_files;
343
344     playlist_t          *p_playlist;
345     input_thread_t      *p_input;
346     vlm_t               *p_vlm;
347     char                *psz_html_type;
348     vlc_iconv_t         iconv_from_utf8, iconv_to_utf8;
349 };
350
351 /** This function is the main HTTPD Callback used by the HTTP Interface */
352 int E_(HttpCallback)( httpd_file_sys_t *p_args,
353                       httpd_file_t *,
354                       uint8_t *p_request,
355                       uint8_t **pp_data, int *pi_data );
356 /**@}*/
357
358 #endif
359