xref: /netbsd-src/libexec/httpd/lua/glue.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
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@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 #include <lua.h>
42 #include <lauxlib.h>
43 #include <lualib.h>
44 
45 #ifndef __UNCONST
46 #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
47 #endif /* !__UNCONST */
48 
49 int luaopen_bozohttpd(lua_State *);
50 
51 #if 0
52 typedef struct strarg_t {
53 	const char	*s;	/* string */
54 	const int	 n;	/* corresponding int value */
55 } strarg_t;
56 
57 /* map a string onto an int */
58 static int
59 findtype(strarg_t *strs, const char *s)
60 {
61 	strarg_t	*sp;
62 
63 	for (sp = strs ; sp->s && strcasecmp(sp->s, s) != 0 ; sp++) {
64 	}
65 	return sp->n;
66 }
67 #endif
68 
69 /* init() */
70 static int
71 l_new(lua_State *L)
72 {
73 	bozohttpd_t	*httpd;
74 
75 	httpd = lua_newuserdata(L, sizeof(*httpd));
76 	(void) memset(httpd, 0x0, sizeof(*httpd));
77 	return 1;
78 }
79 
80 /* initialise(httpd) */
81 static int
82 l_init_httpd(lua_State *L)
83 {
84 	bozohttpd_t	*httpd;
85 
86 	httpd = lua_touserdata(L, 1);
87 	lua_pushnumber(L, bozo_init_httpd(httpd));
88 	return 1;
89 }
90 
91 /* initialise(prefs) */
92 static int
93 l_init_prefs(lua_State *L)
94 {
95 	bozohttpd_t	*httpd;
96 	bozoprefs_t	*prefs;
97 
98 	prefs = lua_newuserdata(L, sizeof(*prefs));
99 	(void) memset(prefs, 0x0, sizeof(*prefs));
100 	httpd = lua_touserdata(L, 1);
101 	(void) bozo_init_prefs(httpd, prefs);
102 	return 1;
103 }
104 
105 /* bozo_set_pref(prefs, name, value) */
106 static int
107 l_bozo_set_pref(lua_State *L)
108 {
109 	bozoprefs_t	*prefs;
110 	const char	*name;
111 	const char	*value;
112 
113 	prefs = lua_touserdata(L, 1);
114 	name = luaL_checkstring(L, 2);
115 	value = luaL_checkstring(L, 3);
116 	lua_pushnumber(L, bozo_set_pref(prefs, name, value));
117 	return 1;
118 }
119 
120 /* bozo_get_pref(prefs, name) */
121 static int
122 l_bozo_get_pref(lua_State *L)
123 {
124 	bozoprefs_t	*prefs;
125 	const char	*name;
126 
127 	prefs = lua_touserdata(L, 1);
128 	name = luaL_checkstring(L, 2);
129 	lua_pushstring(L, bozo_get_pref(prefs, name));
130 	return 1;
131 }
132 
133 /* bozo_setup(httpd, prefs, host, root) */
134 static int
135 l_bozo_setup(lua_State *L)
136 {
137 	bozohttpd_t	*httpd;
138 	bozoprefs_t	*prefs;
139 	const char	*vhost;
140 	const char	*root;
141 
142 	httpd = lua_touserdata(L, 1);
143 	prefs = lua_touserdata(L, 2);
144 	vhost = luaL_checkstring(L, 3);
145 	if (vhost && *vhost == 0x0) {
146 		vhost = NULL;
147 	}
148 	root = luaL_checkstring(L, 4);
149 	lua_pushnumber(L, bozo_setup(httpd, prefs, vhost, root));
150 	return 1;
151 }
152 
153 /* bozo_read_request(httpd) */
154 static int
155 l_bozo_read_request(lua_State *L)
156 {
157 	bozo_httpreq_t	*req;
158 	bozohttpd_t	*httpd;
159 
160 	httpd = lua_touserdata(L, 1);
161 	req = bozo_read_request(httpd);
162 	lua_pushlightuserdata(L, req);
163 	return 1;
164 }
165 
166 /* bozo_process_request(httpd, req) */
167 static int
168 l_bozo_process_request(lua_State *L)
169 {
170 	bozo_httpreq_t	*req;
171 	bozohttpd_t	*httpd;
172 
173 	httpd = lua_touserdata(L, 1);
174 	req = lua_touserdata(L, 2);
175 	bozo_process_request(httpd, req);
176 	lua_pushnumber(L, 1);
177 	return 1;
178 }
179 
180 /* bozo_clean_request(req) */
181 static int
182 l_bozo_clean_request(lua_State *L)
183 {
184 	bozo_httpreq_t	*req;
185 
186 	req = lua_touserdata(L, 1);
187 	bozo_clean_request(req);
188 	lua_pushnumber(L, 1);
189 	return 1;
190 }
191 
192 /* dynamic_mime(httpd, one, two, three, four) */
193 static int
194 l_bozo_dynamic_mime(lua_State *L)
195 {
196 	bozohttpd_t	*httpd;
197 	const char	*s[4];
198 
199 	httpd = lua_touserdata(L, 1);
200 	s[0] = luaL_checkstring(L, 2);
201 	s[1] = luaL_checkstring(L, 3);
202 	s[2] = luaL_checkstring(L, 4);
203 	s[3] = luaL_checkstring(L, 5);
204 	bozo_add_content_map_mime(httpd, s[0], s[1], s[2], s[3]);
205 	lua_pushnumber(L, 1);
206 	return 1;
207 }
208 
209 /* ssl_set_opts(httpd, one, two) */
210 static int
211 l_bozo_ssl_set_opts(lua_State *L)
212 {
213 	bozohttpd_t	*httpd;
214 	const char	*s[2];
215 
216 	httpd = lua_touserdata(L, 1);
217 	s[0] = luaL_checkstring(L, 2);
218 	s[1] = luaL_checkstring(L, 3);
219 	bozo_ssl_set_opts(httpd, s[0], s[1]);
220 	lua_pushnumber(L, 1);
221 	return 1;
222 }
223 
224 /* cgi_setbin(httpd, bin) */
225 static int
226 l_bozo_cgi_setbin(lua_State *L)
227 {
228 	bozohttpd_t	*httpd;
229 	const char	*bin;
230 
231 	httpd = lua_touserdata(L, 1);
232 	bin = luaL_checkstring(L, 2);
233 	bozo_cgi_setbin(httpd, bin);
234 	lua_pushnumber(L, 1);
235 	return 1;
236 }
237 
238 /* cgi_map(httpd, 1, 2) */
239 static int
240 l_bozo_cgi_map(lua_State *L)
241 {
242 	bozohttpd_t	*httpd;
243 	const char	*s[2];
244 
245 	httpd = lua_touserdata(L, 1);
246 	s[0] = luaL_checkstring(L, 2);
247 	s[1] = luaL_checkstring(L, 3);
248 	bozo_add_content_map_cgi(httpd, s[0], s[1]);
249 	lua_pushnumber(L, 1);
250 	return 1;
251 }
252 
253 const struct luaL_reg libluabozohttpd[] = {
254 	{ "new",		l_new },
255 	{ "init_httpd",		l_init_httpd },
256 	{ "init_prefs",		l_init_prefs },
257 
258 	{ "set_pref",		l_bozo_set_pref },
259 	{ "get_pref",		l_bozo_get_pref },
260 	{ "setup",		l_bozo_setup },
261 	{ "dynamic_mime",	l_bozo_dynamic_mime },
262 	{ "ssl_set_opts",	l_bozo_ssl_set_opts },
263 	{ "cgi_setbin",		l_bozo_cgi_setbin },
264 	{ "cgi_map",		l_bozo_cgi_map },
265 
266 	{ "read_request",	l_bozo_read_request },
267 	{ "process_request",	l_bozo_process_request },
268 	{ "clean_request",	l_bozo_clean_request },
269 
270 	{ NULL,			NULL }
271 };
272 
273 int
274 luaopen_bozohttpd(lua_State *L)
275 {
276 	luaL_openlib(L, "bozohttpd", libluabozohttpd, 0);
277 	return 1;
278 }
279