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