1#! /usr/bin/env lua
2
3--
4-- Copyright (c) 2009 The NetBSD Foundation, Inc.
5-- All rights reserved.
6--
7-- This code is derived from software contributed to The NetBSD Foundation
8-- by Alistair Crooks (agc@netbsd.org)
9--
10-- Redistribution and use in source and binary forms, with or without
11-- modification, are permitted provided that the following conditions
12-- are met:
13-- 1. Redistributions of source code must retain the above copyright
14--    notice, this list of conditions and the following disclaimer.
15-- 2. Redistributions in binary form must reproduce the above copyright
16--    notice, this list of conditions and the following disclaimer in the
17--    documentation and/or other materials provided with the distribution.
18--
19-- THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20-- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22-- PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23-- BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29-- POSSIBILITY OF SUCH DAMAGE.
30--
31
32-- command line args
33dofile "optparse.lua"
34
35opt = OptionParser{usage="%prog [options] root [vhost]", version="20180502"}                           
36
37opt.add_option{"-C", "--cgimap", action="store", dest="cgimap", help="--cgimap 's t'"}
38opt.add_option{"-E", "--enable-user-cgibin", action="store_true", dest="enableusercgibin", help="--enable-user-cgibin"}
39opt.add_option{"-H", "--hide-dots", action="store_true", dest="hidedots", help="--hide-dots"}
40opt.add_option{"-I", "--portnum", action="store", dest="portnum", help="--portnum number"}
41opt.add_option{"-M", "--dynamicmime", action="store", dest="dynmime", help="--dynamicmime 'suffix type a b'"}
42opt.add_option{"-S", "--server-software", action="store", dest="serversw", help="--server-software name"}
43opt.add_option{"-U", "--username", action="store", dest="username", help="--username name"}
44opt.add_option{"-V", "--unknown-slash", action="store_true", dest="unknown", help="--unknown-slash"}
45opt.add_option{"-X", "--dir-index", action="store_true", dest="dirindex", help="--dir-index"}
46opt.add_option{"-Z", "--ssl", action="store", dest="ssl", help="--ssl 'cert priv'"}
47opt.add_option{"-b", "--background", action="store", dest="background", help="--background count"}
48opt.add_option{"-c", "--cgibin", action="store", dest="cgibin", help="--cgibin bin"}
49opt.add_option{"-e", "--dirtyenv", action="store_true", dest="dirtyenv", help="--dirtyenv"}
50opt.add_option{"-f", "--foreground", action="store_true", dest="foreground", help="--foreground"}
51opt.add_option{"-i", "--bindaddr", action="store", dest="bindaddress", help="--bindaddr address"}
52opt.add_option{"-n", "--numeric", action="store_true", dest="numeric", help="--numeric"}
53opt.add_option{"-p", "--public-html", action="store", dest="public_html", help="--public-html dir"}
54opt.add_option{"-s", "--logtostderr", action="store_true", dest="logstderr", help="log to stderr"}
55opt.add_option{"-t", "--chroot", action="store", dest="chroot", help="--chroot dir"}
56opt.add_option{"-u", "--enable-users", action="store_true", dest="enableusers", help="--enable-users"}
57opt.add_option{"-v", "--virtbase", action="store", dest="virtbase", help="virtual base location"}
58opt.add_option{"-x", "--index-html", action="store", dest="indexhtml", help="index.html name"}
59
60-- caller lua script
61local extension = ".so"
62f = io.open("libluabozohttpd.dylib", "r")
63if f then
64	extension = ".dylib"
65	io.close(f)
66end
67glupkg = package.loadlib("./" .. "libluabozohttpd" .. extension, "luaopen_bozohttpd")
68bozohttpd = glupkg()
69
70-- initialise
71httpd = bozohttpd.new()
72bozohttpd.init_httpd(httpd)
73prefs = bozohttpd.init_prefs()
74
75-- parse command line args
76options,args = opt.parse_args()
77if options.portnum then
78        bozohttpd.set_pref(httpd, prefs, "port number", options.portnum)
79end
80if options.background then
81        bozohttpd.set_pref(httpd, prefs, "background", options.background)
82end
83if options.numeric then
84        bozohttpd.set_pref(httpd, prefs, "numeric", "true")
85end
86if options.logstderr then
87        bozohttpd.set_pref(httpd, prefs, "log to stderr", "true")
88end
89if options.foreground then
90        bozohttpd.set_pref(httpd, prefs, "foreground", "true")
91end
92if options.trustedref then
93        bozohttpd.set_pref(httpd, prefs, "trusted referal", "true")
94end
95if options.dynmime then
96	suffix, type, s1, s2 = string.find(options.dynmime,
97					"(%S+)%s+(%S+)%s+(%S+)%s+(%S+)")
98        bozohttpd.dynamic_mime(httpd, suffix, type, s1, s2)
99end
100if options.serversw then
101        bozohttpd.set_pref(httpd, prefs, "server software", options.serversw)
102end
103if options.ssl then
104	cert, priv = string.find(options.ssl, "(%S+)%s+(%S+)")
105        bozohttpd.dynamic_mime(httpd, cert, priv)
106end
107if options.username then
108        bozohttpd.set_pref(httpd, prefs, "username", options.username)
109end
110if options.unknownslash then
111        bozohttpd.set_pref(httpd, prefs, "unknown slash", "true")
112end
113if options.virtbase then
114        bozohttpd.set_pref(httpd, prefs, "virtual base", options.virtbase)
115end
116if options.indexhtml then
117        bozohttpd.set_pref(httpd, prefs, "index.html", options.indexhtml)
118end
119if options.dirtyenv then
120        bozohttpd.set_pref(httpd, prefs, "dirty environment", "true")
121end
122if options.bindaddr then
123        bozohttpd.set_pref(httpd, prefs, "bind address", options.bindaddr)
124end
125if options.cgibin then
126        bozohttpd.cgi_setbin(httpd, options.cgibin)
127end
128if options.cgimap then
129	name, handler = string.find(options.cgimap, "(%S+)%s+(%S+)")
130        bozohttpd.cgi_map(httpd, name, handler)
131end
132if options.public_html then
133        bozohttpd.set_pref(httpd, prefs, "public_html", options.public_html)
134end
135if options.chroot then
136        bozohttpd.set_pref(httpd, prefs, "chroot dir", options.chroot)
137end
138if options.enableusers then
139        bozohttpd.set_pref(httpd, prefs, "enable users", "true")
140end
141if options.hidedots then
142        bozohttpd.set_pref(httpd, prefs, "hide dots", "true")
143end
144if options.enableusercgibin then
145        bozohttpd.set_pref(httpd, prefs, "enable user cgibin", "true")
146end
147if options.dirindex then
148        bozohttpd.set_pref(httpd, prefs, "directory indexing", "true")
149end
150
151if #args < 1 then
152	print("At least one arg needed for root directory")
153else
154	-- set up connections
155	local vhost = args[2] or ""
156	bozohttpd.setup(httpd, prefs, vhost, args[1])
157
158	-- loop, serving requests
159	local numreps = options.background or 0
160	repeat
161		req = bozohttpd.read_request(httpd)
162		bozohttpd.process_request(req)
163		bozohttpd.clean_request(req)
164	until numreps == 0
165end
166