xref: /minix/libexec/httpd/lua/glue.c (revision 340f5e56)
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
l_new(lua_State * L)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
l_init_httpd(lua_State * L)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
l_init_prefs(lua_State * L)93 l_init_prefs(lua_State *L)
94 {
95 	bozoprefs_t	*prefs;
96 
97 	prefs = lua_newuserdata(L, sizeof(*prefs));
98 	(void) memset(prefs, 0x0, sizeof(*prefs));
99 	(void) bozo_init_prefs(prefs);
100 	return 1;
101 }
102 
103 /* bozo_set_pref(prefs, name, value) */
104 static int
l_bozo_set_pref(lua_State * L)105 l_bozo_set_pref(lua_State *L)
106 {
107 	bozoprefs_t	*prefs;
108 	const char	*name;
109 	const char	*value;
110 
111 	prefs = lua_touserdata(L, 1);
112 	name = luaL_checkstring(L, 2);
113 	value = luaL_checkstring(L, 3);
114 	lua_pushnumber(L, bozo_set_pref(prefs, name, value));
115 	return 1;
116 }
117 
118 /* bozo_get_pref(prefs, name) */
119 static int
l_bozo_get_pref(lua_State * L)120 l_bozo_get_pref(lua_State *L)
121 {
122 	bozoprefs_t	*prefs;
123 	const char	*name;
124 
125 	prefs = lua_touserdata(L, 1);
126 	name = luaL_checkstring(L, 2);
127 	lua_pushstring(L, bozo_get_pref(prefs, name));
128 	return 1;
129 }
130 
131 /* bozo_setup(httpd, prefs, host, root) */
132 static int
l_bozo_setup(lua_State * L)133 l_bozo_setup(lua_State *L)
134 {
135 	bozohttpd_t	*httpd;
136 	bozoprefs_t	*prefs;
137 	const char	*vhost;
138 	const char	*root;
139 
140 	httpd = lua_touserdata(L, 1);
141 	prefs = lua_touserdata(L, 2);
142 	vhost = luaL_checkstring(L, 3);
143 	if (vhost && *vhost == 0x0) {
144 		vhost = NULL;
145 	}
146 	root = luaL_checkstring(L, 4);
147 	lua_pushnumber(L, bozo_setup(httpd, prefs, vhost, root));
148 	return 1;
149 }
150 
151 /* bozo_read_request(httpd) */
152 static int
l_bozo_read_request(lua_State * L)153 l_bozo_read_request(lua_State *L)
154 {
155 	bozo_httpreq_t	*req;
156 	bozohttpd_t	*httpd;
157 
158 	httpd = lua_touserdata(L, 1);
159 	req = bozo_read_request(httpd);
160 	lua_pushlightuserdata(L, req);
161 	return 1;
162 }
163 
164 /* bozo_process_request(httpd, req) */
165 static int
l_bozo_process_request(lua_State * L)166 l_bozo_process_request(lua_State *L)
167 {
168 	bozo_httpreq_t	*req;
169 	bozohttpd_t	*httpd;
170 
171 	httpd = lua_touserdata(L, 1);
172 	req = lua_touserdata(L, 2);
173 	bozo_process_request(httpd, req);
174 	lua_pushnumber(L, 1);
175 	return 1;
176 }
177 
178 /* bozo_clean_request(req) */
179 static int
l_bozo_clean_request(lua_State * L)180 l_bozo_clean_request(lua_State *L)
181 {
182 	bozo_httpreq_t	*req;
183 
184 	req = lua_touserdata(L, 1);
185 	bozo_clean_request(req);
186 	lua_pushnumber(L, 1);
187 	return 1;
188 }
189 
190 /* dynamic_mime(httpd, one, two, three, four) */
191 static int
l_bozo_dynamic_mime(lua_State * L)192 l_bozo_dynamic_mime(lua_State *L)
193 {
194 	bozohttpd_t	*httpd;
195 	const char	*s[4];
196 
197 	httpd = lua_touserdata(L, 1);
198 	s[0] = luaL_checkstring(L, 2);
199 	s[1] = luaL_checkstring(L, 3);
200 	s[2] = luaL_checkstring(L, 4);
201 	s[3] = luaL_checkstring(L, 5);
202 	bozo_add_content_map_mime(httpd, s[0], s[1], s[2], s[3]);
203 	lua_pushnumber(L, 1);
204 	return 1;
205 }
206 
207 /* ssl_set_opts(httpd, one, two) */
208 static int
l_bozo_ssl_set_opts(lua_State * L)209 l_bozo_ssl_set_opts(lua_State *L)
210 {
211 	bozohttpd_t	*httpd;
212 	const char	*s[2];
213 
214 	httpd = lua_touserdata(L, 1);
215 	s[0] = luaL_checkstring(L, 2);
216 	s[1] = luaL_checkstring(L, 3);
217 	bozo_ssl_set_opts(httpd, s[0], s[1]);
218 	lua_pushnumber(L, 1);
219 	return 1;
220 }
221 
222 /* cgi_setbin(httpd, bin) */
223 static int
l_bozo_cgi_setbin(lua_State * L)224 l_bozo_cgi_setbin(lua_State *L)
225 {
226 	bozohttpd_t	*httpd;
227 	const char	*bin;
228 
229 	httpd = lua_touserdata(L, 1);
230 	bin = luaL_checkstring(L, 2);
231 	bozo_cgi_setbin(httpd, bin);
232 	lua_pushnumber(L, 1);
233 	return 1;
234 }
235 
236 /* cgi_map(httpd, 1, 2) */
237 static int
l_bozo_cgi_map(lua_State * L)238 l_bozo_cgi_map(lua_State *L)
239 {
240 	bozohttpd_t	*httpd;
241 	const char	*s[2];
242 
243 	httpd = lua_touserdata(L, 1);
244 	s[0] = luaL_checkstring(L, 2);
245 	s[1] = luaL_checkstring(L, 3);
246 	bozo_add_content_map_cgi(httpd, s[0], s[1]);
247 	lua_pushnumber(L, 1);
248 	return 1;
249 }
250 
251 const struct luaL_reg libluabozohttpd[] = {
252 	{ "new",		l_new },
253 	{ "init_httpd",		l_init_httpd },
254 	{ "init_prefs",		l_init_prefs },
255 
256 	{ "set_pref",		l_bozo_set_pref },
257 	{ "get_pref",		l_bozo_get_pref },
258 	{ "setup",		l_bozo_setup },
259 	{ "dynamic_mime",	l_bozo_dynamic_mime },
260 	{ "ssl_set_opts",	l_bozo_ssl_set_opts },
261 	{ "cgi_setbin",		l_bozo_cgi_setbin },
262 	{ "cgi_map",		l_bozo_cgi_map },
263 
264 	{ "read_request",	l_bozo_read_request },
265 	{ "process_request",	l_bozo_process_request },
266 	{ "clean_request",	l_bozo_clean_request },
267 
268 	{ NULL,			NULL }
269 };
270 
271 int
luaopen_bozohttpd(lua_State * L)272 luaopen_bozohttpd(lua_State *L)
273 {
274 	luaL_openlib(L, "bozohttpd", libluabozohttpd, 0);
275 	return 1;
276 }
277