Home | History | Annotate | Line # | Download | only in lua
glue.c revision 1.3
      1 /*-
      2  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Alistair Crooks (agc (at) netbsd.org)
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 #include <sys/types.h>
     30 #include <sys/param.h>
     31 #include <sys/stat.h>
     32 
     33 #include <bozohttpd.h>
     34 #include <inttypes.h>
     35 #include <string.h>
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 #include <unistd.h>
     39 
     40 #define LUA_LIB
     41 #define LUA_USE_APICHECK
     42 #include <lua.h>
     43 #include <lauxlib.h>
     44 #include <lualib.h>
     45 
     46 #ifndef __UNCONST
     47 #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
     48 #endif /* !__UNCONST */
     49 
     50 int luaopen_bozohttpd(lua_State *);
     51 
     52 /* init() */
     53 static int
     54 l_new(lua_State *L)
     55 {
     56 	bozohttpd_t	*httpd;
     57 
     58 	httpd = lua_newuserdata(L, sizeof(*httpd));
     59 	(void) memset(httpd, 0x0, sizeof(*httpd));
     60 	return 1;
     61 }
     62 
     63 /* initialise(httpd) */
     64 static int
     65 l_init_httpd(lua_State *L)
     66 {
     67 	bozohttpd_t	*httpd;
     68 
     69 	httpd = lua_touserdata(L, 1);
     70 	lua_pushnumber(L, bozo_init_httpd(httpd));
     71 	return 1;
     72 }
     73 
     74 /* initialise(prefs) */
     75 static int
     76 l_init_prefs(lua_State *L)
     77 {
     78 	bozohttpd_t	*httpd;
     79 	bozoprefs_t	*prefs;
     80 
     81 	prefs = lua_newuserdata(L, sizeof(*prefs));
     82 	(void) memset(prefs, 0x0, sizeof(*prefs));
     83 	httpd = lua_touserdata(L, 1);
     84 	(void) bozo_init_prefs(httpd, prefs);
     85 	return 1;
     86 }
     87 
     88 /* bozo_set_pref(httpd, prefs, name, value) */
     89 static int
     90 l_bozo_set_pref(lua_State *L)
     91 {
     92 	bozohttpd_t	*httpd;
     93 	bozoprefs_t	*prefs;
     94 	const char	*name;
     95 	const char	*value;
     96 
     97 	httpd = lua_touserdata(L, 1);
     98 	prefs = lua_touserdata(L, 2);
     99 	name = luaL_checkstring(L, 3);
    100 	value = luaL_checkstring(L, 4);
    101 	lua_pushnumber(L, bozo_set_pref(httpd, prefs, name, value));
    102 	return 1;
    103 }
    104 
    105 /* bozo_get_pref(prefs, name) */
    106 static int
    107 l_bozo_get_pref(lua_State *L)
    108 {
    109 	bozoprefs_t	*prefs;
    110 	const char	*name;
    111 
    112 	prefs = lua_touserdata(L, 1);
    113 	name = luaL_checkstring(L, 2);
    114 	lua_pushstring(L, bozo_get_pref(prefs, name));
    115 	return 1;
    116 }
    117 
    118 /* bozo_setup(httpd, prefs, host, root) */
    119 static int
    120 l_bozo_setup(lua_State *L)
    121 {
    122 	bozohttpd_t	*httpd;
    123 	bozoprefs_t	*prefs;
    124 	const char	*vhost;
    125 	const char	*root;
    126 
    127 	httpd = lua_touserdata(L, 1);
    128 	prefs = lua_touserdata(L, 2);
    129 	vhost = luaL_checkstring(L, 3);
    130 	if (vhost && *vhost == 0x0) {
    131 		vhost = NULL;
    132 	}
    133 	root = luaL_checkstring(L, 4);
    134 	lua_pushnumber(L, bozo_setup(httpd, prefs, vhost, root));
    135 	return 1;
    136 }
    137 
    138 /* bozo_read_request(httpd) */
    139 static int
    140 l_bozo_read_request(lua_State *L)
    141 {
    142 	bozo_httpreq_t	*req;
    143 	bozohttpd_t	*httpd;
    144 
    145 	httpd = lua_touserdata(L, 1);
    146 	req = bozo_read_request(httpd);
    147 	lua_pushlightuserdata(L, req);
    148 	return 1;
    149 }
    150 
    151 /* bozo_process_request(req) */
    152 static int
    153 l_bozo_process_request(lua_State *L)
    154 {
    155 	bozo_httpreq_t	*req;
    156 
    157 	req = lua_touserdata(L, 1);
    158 	bozo_process_request(req);
    159 	lua_pushnumber(L, 1);
    160 	return 1;
    161 }
    162 
    163 /* bozo_clean_request(req) */
    164 static int
    165 l_bozo_clean_request(lua_State *L)
    166 {
    167 	bozo_httpreq_t	*req;
    168 
    169 	req = lua_touserdata(L, 1);
    170 	bozo_clean_request(req);
    171 	lua_pushnumber(L, 1);
    172 	return 1;
    173 }
    174 
    175 /* dynamic_mime(httpd, one, two, three, four) */
    176 static int
    177 l_bozo_dynamic_mime(lua_State *L)
    178 {
    179 	bozohttpd_t	*httpd;
    180 	const char	*s[4];
    181 
    182 	httpd = lua_touserdata(L, 1);
    183 	s[0] = luaL_checkstring(L, 2);
    184 	s[1] = luaL_checkstring(L, 3);
    185 	s[2] = luaL_checkstring(L, 4);
    186 	s[3] = luaL_checkstring(L, 5);
    187 	bozo_add_content_map_mime(httpd, s[0], s[1], s[2], s[3]);
    188 	lua_pushnumber(L, 1);
    189 	return 1;
    190 }
    191 
    192 /* ssl_set_opts(httpd, one, two) */
    193 static int
    194 l_bozo_ssl_set_opts(lua_State *L)
    195 {
    196 	bozohttpd_t	*httpd;
    197 	const char	*s[2];
    198 
    199 	httpd = lua_touserdata(L, 1);
    200 	s[0] = luaL_checkstring(L, 2);
    201 	s[1] = luaL_checkstring(L, 3);
    202 	bozo_ssl_set_opts(httpd, s[0], s[1]);
    203 	lua_pushnumber(L, 1);
    204 	return 1;
    205 }
    206 
    207 /* cgi_setbin(httpd, bin) */
    208 static int
    209 l_bozo_cgi_setbin(lua_State *L)
    210 {
    211 	bozohttpd_t	*httpd;
    212 	const char	*bin;
    213 
    214 	httpd = lua_touserdata(L, 1);
    215 	bin = luaL_checkstring(L, 2);
    216 	bozo_cgi_setbin(httpd, bin);
    217 	lua_pushnumber(L, 1);
    218 	return 1;
    219 }
    220 
    221 /* cgi_map(httpd, 1, 2) */
    222 static int
    223 l_bozo_cgi_map(lua_State *L)
    224 {
    225 	bozohttpd_t	*httpd;
    226 	const char	*s[2];
    227 
    228 	httpd = lua_touserdata(L, 1);
    229 	s[0] = luaL_checkstring(L, 2);
    230 	s[1] = luaL_checkstring(L, 3);
    231 	bozo_add_content_map_cgi(httpd, s[0], s[1]);
    232 	lua_pushnumber(L, 1);
    233 	return 1;
    234 }
    235 
    236 const struct luaL_Reg libluabozohttpd[] = {
    237 	{ "new",		l_new },
    238 	{ "init_httpd",		l_init_httpd },
    239 	{ "init_prefs",		l_init_prefs },
    240 
    241 	{ "set_pref",		l_bozo_set_pref },
    242 	{ "get_pref",		l_bozo_get_pref },
    243 	{ "setup",		l_bozo_setup },
    244 	{ "dynamic_mime",	l_bozo_dynamic_mime },
    245 	{ "ssl_set_opts",	l_bozo_ssl_set_opts },
    246 	{ "cgi_setbin",		l_bozo_cgi_setbin },
    247 	{ "cgi_map",		l_bozo_cgi_map },
    248 
    249 	{ "read_request",	l_bozo_read_request },
    250 	{ "process_request",	l_bozo_process_request },
    251 	{ "clean_request",	l_bozo_clean_request },
    252 
    253 	{ NULL,			NULL }
    254 };
    255 
    256 int
    257 luaopen_bozohttpd(lua_State *L)
    258 {
    259 #if LUA_VERSION_NUM >= 502
    260         luaL_newlib(L, libluabozohttpd);
    261 #else
    262         luaL_register(L, "bozohttpd", libluabozohttpd);
    263 #endif
    264 	return 1;
    265 }
    266