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