1 /*****************************************************************************
2 * httpd.c: HTTPd wrapper
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_httpd.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static uint8_t *vlclua_todata( lua_State *L, int narg, int *i_data );
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 * );
54 /*****************************************************************************
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 },
64 static int vlclua_httpd_tls_host_new( lua_State *L )
66 vlc_object_t *p_this = vlclua_get_this( L );
67 httpd_host_t *p_host = vlc_http_HostNew( p_this );
69 return luaL_error( L, "Failed to create HTTP host" );
71 httpd_host_t **pp_host = lua_newuserdata( L, sizeof( httpd_host_t * ) );
74 if( luaL_newmetatable( L, "httpd_host" ) )
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" );
83 lua_setmetatable( L, -2 );
87 static int vlclua_httpd_host_delete( lua_State *L )
89 httpd_host_t **pp_host = (httpd_host_t **)luaL_checkudata( L, 1, "httpd_host" );
90 httpd_HostDelete( *pp_host );
94 /*****************************************************************************
96 *****************************************************************************/
97 struct httpd_handler_sys_t
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 )
109 VLC_UNUSED(p_handler);
110 lua_State *L = p_sys->L;
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 ) )
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 );
139 /* function data outdata */
140 *pp_data = vlclua_todata( L, -1, pi_data );
146 static int vlclua_httpd_handler_new( lua_State * L )
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 */
156 httpd_handler_sys_t *p_sys = (httpd_handler_sys_t*)
157 malloc( sizeof( httpd_handler_sys_t ) );
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, NULL,
167 vlclua_httpd_handler_callback, p_sys );
171 return luaL_error( L, "Failed to create HTTPd handler." );
174 httpd_handler_t **pp_handler = lua_newuserdata( L, sizeof( httpd_handler_t * ) );
175 *pp_handler = p_handler;
177 if( luaL_newmetatable( L, "httpd_handler" ) )
179 lua_pushcfunction( L, vlclua_httpd_handler_delete );
180 lua_setfield( L, -2, "__gc" );
183 lua_setmetatable( L, -2 );
187 static int vlclua_httpd_handler_delete( lua_State *L )
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 );
196 /*****************************************************************************
198 *****************************************************************************/
199 struct httpd_file_sys_t
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 )
210 lua_State *L = p_sys->L;
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 ) )
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",
229 /* function data outdata */
230 *pp_data = vlclua_todata( L, -1, pi_data );
236 static int vlclua_httpd_file_new( lua_State *L )
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 ) );
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, NULL,
255 vlclua_httpd_file_callback, p_sys );
259 return luaL_error( L, "Failed to create HTTPd file." );
262 httpd_file_t **pp_file = lua_newuserdata( L, sizeof( httpd_file_t * ) );
265 if( luaL_newmetatable( L, "httpd_file" ) )
267 lua_pushcfunction( L, vlclua_httpd_file_delete );
268 lua_setfield( L, -2, "__gc" );
271 lua_setmetatable( L, -2 );
275 static int vlclua_httpd_file_delete( lua_State *L )
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 );
284 /*****************************************************************************
286 *****************************************************************************/
287 static int vlclua_httpd_redirect_new( lua_State *L )
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,
296 return luaL_error( L, "Failed to create HTTPd redirect." );
298 httpd_redirect_t **pp_redirect = lua_newuserdata( L, sizeof( httpd_redirect_t * ) );
299 *pp_redirect = p_redirect;
301 if( luaL_newmetatable( L, "httpd_redirect" ) )
303 lua_pushcfunction( L, vlclua_httpd_redirect_delete );
304 lua_setfield( L, -2, "__gc" );
307 lua_setmetatable( L, -2 );
311 static int vlclua_httpd_redirect_delete( lua_State *L )
313 httpd_redirect_t **pp_redirect = (httpd_redirect_t**)luaL_checkudata( L, 1, "httpd_redirect" );
314 httpd_RedirectDelete( *pp_redirect );
318 /*****************************************************************************
320 *****************************************************************************/
321 static uint8_t *vlclua_todata( lua_State *L, int narg, int *pi_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;
329 luaL_error( L, "Error while allocating buffer." );
330 return NULL; /* To please gcc even though luaL_error longjmp-ed out of here */
332 memcpy( p_data, psz_data, i_data );
336 /*****************************************************************************
338 *****************************************************************************/
339 void luaopen_httpd( lua_State *L )
341 lua_pushcfunction( L, vlclua_httpd_tls_host_new );
342 lua_setfield( L, -2, "httpd" );