]> git.sesse.net Git - vlc/blob - modules/lua/libs/httpd.c
28f3abd949f6b3fd560cec2c076eed84b21a8c88
[vlc] / modules / lua / libs / httpd.c
1 /*****************************************************************************
2  * httpd.c: HTTPd wrapper
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifndef  _GNU_SOURCE
28 #   define  _GNU_SOURCE
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_httpd.h>
37
38 #include "../vlc.h"
39 #include "../libs.h"
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static uint8_t *vlclua_todata( lua_State *L, int narg, int *i_data );
45
46 static int vlclua_httpd_host_delete( lua_State * );
47 static int vlclua_httpd_handler_new( lua_State * );
48 static int vlclua_httpd_handler_delete( lua_State * );
49 static int vlclua_httpd_file_new( lua_State * );
50 static int vlclua_httpd_file_delete( lua_State * );
51 static int vlclua_httpd_redirect_new( lua_State * );
52 static int vlclua_httpd_redirect_delete( lua_State * );
53
54 /*****************************************************************************
55  * HTTPD Host
56  *****************************************************************************/
57 static const luaL_Reg vlclua_httpd_reg[] = {
58     { "handler", vlclua_httpd_handler_new },
59     { "file", vlclua_httpd_file_new },
60     { "redirect", vlclua_httpd_redirect_new },
61     { NULL, NULL }
62 };
63
64 static int vlclua_httpd_tls_host_new( lua_State *L )
65 {
66     vlc_object_t *p_this = vlclua_get_this( L );
67     httpd_host_t *p_host = vlc_http_HostNew( p_this );
68     if( !p_host )
69         return luaL_error( L, "Failed to create HTTP host" );
70
71     httpd_host_t **pp_host = lua_newuserdata( L, sizeof( httpd_host_t * ) );
72     *pp_host = p_host;
73
74     if( luaL_newmetatable( L, "httpd_host" ) )
75     {
76         lua_newtable( L );
77         luaL_register( L, NULL, vlclua_httpd_reg );
78         lua_setfield( L, -2, "__index" );
79         lua_pushcfunction( L, vlclua_httpd_host_delete );
80         lua_setfield( L, -2, "__gc" );
81     }
82
83     lua_setmetatable( L, -2 );
84     return 1;
85 }
86
87 static int vlclua_httpd_host_delete( lua_State *L )
88 {
89     httpd_host_t **pp_host = (httpd_host_t **)luaL_checkudata( L, 1, "httpd_host" );
90     httpd_HostDelete( *pp_host );
91     return 0;
92 }
93
94 /*****************************************************************************
95  * HTTPd Handler
96  *****************************************************************************/
97 struct httpd_handler_sys_t
98 {
99     lua_State *L;
100     int ref;
101 };
102
103 static int vlclua_httpd_handler_callback(
104      httpd_handler_sys_t *p_sys, httpd_handler_t *p_handler, char *psz_url,
105      uint8_t *psz_request, int i_type, uint8_t *p_in, int i_in,
106      char *psz_remote_addr, char *psz_remote_host,
107      uint8_t **pp_data, int *pi_data )
108 {
109     VLC_UNUSED(p_handler);
110     lua_State *L = p_sys->L;
111
112     /* function data */
113     lua_pushvalue( L, 1 );
114     lua_pushvalue( L, 2 );
115     /* function data function data */
116     lua_pushstring( L, psz_url );
117     /* function data function data url */
118     lua_pushstring( L, (const char *)psz_request );
119     /* function data function data url request */
120     lua_pushinteger( L, i_type ); /* Q: what does i_type stand for? */
121     /* function data function data url request type */
122     lua_pushlstring( L, (const char *)p_in, i_in ); /* Q: what do p_in contain? */
123     /* function data function data url request type in */
124     lua_pushstring( L, psz_remote_addr );
125     /* function data function data url request type in addr */
126     lua_pushstring( L, psz_remote_host );
127     /* function data function data url request type in addr host */
128     if( lua_pcall( L, 7, 1, 0 ) )
129     {
130         /* function data err */
131         vlc_object_t *p_this = vlclua_get_this( L );
132         const char *psz_err = lua_tostring( L, -1 );
133         msg_Err( p_this, "Error while running the lua HTTPd handler "
134                  "callback: %s", psz_err );
135         lua_settop( L, 2 );
136         /* function data */
137         return VLC_EGENERIC;
138     }
139     /* function data outdata */
140     *pp_data = vlclua_todata( L, -1, pi_data );
141     lua_pop( L, 1 );
142     /* function data */
143     return VLC_SUCCESS;
144 }
145
146 static int vlclua_httpd_handler_new( lua_State * L )
147 {
148     httpd_host_t **pp_host = (httpd_host_t **)luaL_checkudata( L, 1, "httpd_host" );
149     const char *psz_url = luaL_checkstring( L, 2 );
150     const char *psz_user = luaL_nilorcheckstring( L, 3 );
151     const char *psz_password = luaL_nilorcheckstring( L, 4 );
152     /* Stack item 5 is the callback function */
153     luaL_argcheck( L, lua_isfunction( L, 5 ), 5, "Should be a function" );
154     /* Stack item 6 is the callback data */
155     lua_settop( L, 6 );
156     httpd_handler_sys_t *p_sys = (httpd_handler_sys_t*)
157                                  malloc( sizeof( httpd_handler_sys_t ) );
158     if( !p_sys )
159         return luaL_error( L, "Failed to allocate private buffer." );
160     p_sys->L = lua_newthread( L );
161     p_sys->ref = luaL_ref( L, LUA_REGISTRYINDEX ); /* pops the object too */
162     /* use lua_xmove to move the lua callback function and data to
163      * the callback's stack. */
164     lua_xmove( L, p_sys->L, 2 );
165     httpd_handler_t *p_handler = httpd_HandlerNew(
166                             *pp_host, psz_url, psz_user, psz_password,
167                             vlclua_httpd_handler_callback, p_sys );
168     if( !p_handler )
169     {
170         free( p_sys );
171         return luaL_error( L, "Failed to create HTTPd handler." );
172     }
173
174     httpd_handler_t **pp_handler = lua_newuserdata( L, sizeof( httpd_handler_t * ) );
175     *pp_handler = p_handler;
176
177     if( luaL_newmetatable( L, "httpd_handler" ) )
178     {
179         lua_pushcfunction( L, vlclua_httpd_handler_delete );
180         lua_setfield( L, -2, "__gc" );
181     }
182
183     lua_setmetatable( L, -2 );
184     return 1;
185 }
186
187 static int vlclua_httpd_handler_delete( lua_State *L )
188 {
189     httpd_handler_t **pp_handler = (httpd_handler_t**)luaL_checkudata( L, 1, "httpd_handler" );
190     httpd_handler_sys_t *p_sys = httpd_HandlerDelete( *pp_handler );
191     luaL_unref( p_sys->L, LUA_REGISTRYINDEX, p_sys->ref );
192     free( p_sys );
193     return 0;
194 }
195
196 /*****************************************************************************
197  * HTTPd File
198  *****************************************************************************/
199 struct httpd_file_sys_t
200 {
201     lua_State *L;
202     int ref;
203 };
204
205 static int vlclua_httpd_file_callback(
206     httpd_file_sys_t *p_sys, httpd_file_t *p_file, uint8_t *psz_request,
207     uint8_t **pp_data, int *pi_data )
208 {
209     VLC_UNUSED(p_file);
210     lua_State *L = p_sys->L;
211
212     /* function data */
213     lua_pushvalue( L, 1 );
214     lua_pushvalue( L, 2 );
215     /* function data function data */
216     lua_pushstring( L, (const char *)psz_request );
217     /* function data function data request */
218     if( lua_pcall( L, 2, 1, 0 ) )
219     {
220         /* function data err */
221         vlc_object_t *p_this = vlclua_get_this( L );
222         const char *psz_err = lua_tostring( L, -1 );
223         msg_Err( p_this, "Error while running the lua HTTPd file callback: %s",
224                  psz_err );
225         lua_settop( L, 2 );
226         /* function data */
227         return VLC_EGENERIC;
228     }
229     /* function data outdata */
230     *pp_data = vlclua_todata( L, -1, pi_data );
231     lua_pop( L, 1 );
232     /* function data */
233     return VLC_SUCCESS;
234 }
235
236 static int vlclua_httpd_file_new( lua_State *L )
237 {
238     httpd_host_t **pp_host = (httpd_host_t **)luaL_checkudata( L, 1, "httpd_host" );
239     const char *psz_url = luaL_checkstring( L, 2 );
240     const char *psz_mime = luaL_nilorcheckstring( L, 3 );
241     const char *psz_user = luaL_nilorcheckstring( L, 4 );
242     const char *psz_password = luaL_nilorcheckstring( L, 5 );
243     /* Stack item 7 is the callback function */
244     luaL_argcheck( L, lua_isfunction( L, 6 ), 6, "Should be a function" );
245     /* Stack item 8 is the callback data */
246     httpd_file_sys_t *p_sys = (httpd_file_sys_t *)
247                               malloc( sizeof( httpd_file_sys_t ) );
248     if( !p_sys )
249         return luaL_error( L, "Failed to allocate private buffer." );
250     p_sys->L = lua_newthread( L );
251     p_sys->ref = luaL_ref( L, LUA_REGISTRYINDEX ); /* pops the object too */
252     lua_xmove( L, p_sys->L, 2 );
253     httpd_file_t *p_file = httpd_FileNew( *pp_host, psz_url, psz_mime,
254                                           psz_user, psz_password,
255                                           vlclua_httpd_file_callback, p_sys );
256     if( !p_file )
257     {
258         free( p_sys );
259         return luaL_error( L, "Failed to create HTTPd file." );
260     }
261
262     httpd_file_t **pp_file = lua_newuserdata( L, sizeof( httpd_file_t * ) );
263     *pp_file = p_file;
264
265     if( luaL_newmetatable( L, "httpd_file" ) )
266     {
267         lua_pushcfunction( L, vlclua_httpd_file_delete );
268         lua_setfield( L, -2, "__gc" );
269     }
270
271     lua_setmetatable( L, -2 );
272     return 1;
273 }
274
275 static int vlclua_httpd_file_delete( lua_State *L )
276 {
277     httpd_file_t **pp_file = (httpd_file_t**)luaL_checkudata( L, 1, "httpd_file" );
278     httpd_file_sys_t *p_sys = httpd_FileDelete( *pp_file );
279     luaL_unref( p_sys->L, LUA_REGISTRYINDEX, p_sys->ref );
280     free( p_sys );
281     return 0;
282 }
283
284 /*****************************************************************************
285  * HTTPd Redirect
286  *****************************************************************************/
287 static int vlclua_httpd_redirect_new( lua_State *L )
288 {
289     httpd_host_t **pp_host = (httpd_host_t **)luaL_checkudata( L, 1, "httpd_host" );
290     const char *psz_url_dst = luaL_checkstring( L, 2 );
291     const char *psz_url_src = luaL_checkstring( L, 3 );
292     httpd_redirect_t *p_redirect = httpd_RedirectNew( *pp_host,
293                                                       psz_url_dst,
294                                                       psz_url_src );
295     if( !p_redirect )
296         return luaL_error( L, "Failed to create HTTPd redirect." );
297
298     httpd_redirect_t **pp_redirect = lua_newuserdata( L, sizeof( httpd_redirect_t * ) );
299     *pp_redirect = p_redirect;
300
301     if( luaL_newmetatable( L, "httpd_redirect" ) )
302     {
303         lua_pushcfunction( L, vlclua_httpd_redirect_delete );
304         lua_setfield( L, -2, "__gc" );
305     }
306
307     lua_setmetatable( L, -2 );
308     return 1;
309 }
310
311 static int vlclua_httpd_redirect_delete( lua_State *L )
312 {
313     httpd_redirect_t **pp_redirect = (httpd_redirect_t**)luaL_checkudata( L, 1, "httpd_redirect" );
314     httpd_RedirectDelete( *pp_redirect );
315     return 0;
316 }
317
318 /*****************************************************************************
319  * Utils
320  *****************************************************************************/
321 static uint8_t *vlclua_todata( lua_State *L, int narg, int *pi_data )
322 {
323     size_t i_data;
324     const char *psz_data = lua_tolstring( L, narg, &i_data );
325     uint8_t *p_data = (uint8_t*)malloc( i_data * sizeof(uint8_t) );
326     *pi_data = (int)i_data;
327     if( !p_data )
328     {
329         luaL_error( L, "Error while allocating buffer." );
330         return NULL; /* To please gcc even though luaL_error longjmp-ed out of here */
331     }
332     memcpy( p_data, psz_data, i_data );
333     return p_data;
334 }
335
336 /*****************************************************************************
337  *
338  *****************************************************************************/
339 void luaopen_httpd( lua_State *L )
340 {
341     lua_pushcfunction( L, vlclua_httpd_tls_host_new );
342     lua_setfield( L, -2, "httpd" );
343 }