4 -- Copyright (c) 2009 The NetBSD Foundation, Inc.
5 -- All rights reserved.
7 -- This code is derived from software contributed to The NetBSD Foundation
8 -- by Alistair Crooks (agc@netbsd.org)
10 -- Redistribution and use in source and binary forms, with or without
11 -- modification, are permitted provided that the following conditions
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.
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.
35 opt
= OptionParser
{usage
="%prog [options] root [vhost]", version
="20091105"}
37 opt
.add_option
{"-C", "--cgimap", action
="store", dest
="cgimap", help
="--cgimap 's t'"}
38 opt
.add_option
{"-H", "--hide-dots", action
="store_true", dest
="hidedots", help
="--hide-dots"}
39 opt
.add_option
{"-I", "--portnum", action
="store", dest
="portnum", help
="--portnum number"}
40 opt
.add_option
{"-M", "--dynamicmime", action
="store", dest
="dynmime", help
="--dynamicmime 'suffix type a b'"}
41 opt
.add_option
{"-S", "--server-software", action
="store", dest
="serversw", help
="--server-software name"}
42 opt
.add_option
{"-U", "--username", action
="store", dest
="username", help
="--username name"}
43 opt
.add_option
{"-V", "--unknown-slash", action
="store_true", dest
="unknown", help
="--unknown-slash"}
44 opt
.add_option
{"-X", "--dir-index", action
="store_true", dest
="dirindex", help
="--dir-index"}
45 opt
.add_option
{"-Z", "--ssl", action
="store", dest
="ssl", help
="--ssl 'cert priv'"}
46 opt
.add_option
{"-b", "--background", action
="store", dest
="background", help
="--background count"}
47 opt
.add_option
{"-c", "--cgibin", action
="store", dest
="cgibin", help
="--cgibin bin"}
48 opt
.add_option
{"-e", "--dirtyenv", action
="store_true", dest
="dirtyenv", help
="--dirtyenv"}
49 opt
.add_option
{"-f", "--foreground", action
="store_true", dest
="foreground", help
="--foreground"}
50 opt
.add_option
{"-i", "--bindaddr", action
="store", dest
="bindaddress", help
="--bindaddr address"}
51 opt
.add_option
{"-n", "--numeric", action
="store_true", dest
="numeric", help
="--numeric"}
52 opt
.add_option
{"-p", "--public-html", action
="store", dest
="public_html", help
="--public-html dir"}
53 opt
.add_option
{"-r", "--trusted-referal", action
="store_true", dest
="trustedref", help
="trusted referal"}
54 opt
.add_option
{"-s", "--logtostderr", action
="store_true", dest
="logstderr", help
="log to stderr"}
55 opt
.add_option
{"-t", "--chroot", action
="store", dest
="chroot", help
="--chroot dir"}
56 opt
.add_option
{"-u", "--enable-users", action
="store_true", dest
="enableusers", help
="--enable-users"}
57 opt
.add_option
{"-v", "--virtbase", action
="store", dest
="virtbase", help
="virtual base location"}
58 opt
.add_option
{"-x", "--index-html", action
="store", dest
="indexhtml", help
="index.html name"}
61 local extension
= ".so"
62 f
= io
.open("libluabozohttpd.dylib", "r")
67 glupkg
= package
.loadlib("./" .. "libluabozohttpd" .. extension
, "luaopen_bozohttpd")
71 httpd
= bozohttpd
.new()
72 bozohttpd
.init_httpd(httpd
)
73 prefs
= bozohttpd
.init_prefs()
75 -- parse command line args
76 options
,args
= opt
.parse_args()
77 if options
.portnum
then
78 bozohttpd
.set_pref(prefs
, "port number", options
.portnum
)
80 if options
.background
then
81 bozohttpd
.set_pref(prefs
, "background", options
.background
)
83 if options
.numeric
then
84 bozohttpd
.set_pref(prefs
, "numeric", "true")
86 if options
.logstderr
then
87 bozohttpd
.set_pref(prefs
, "log to stderr", "true")
89 if options
.foreground
then
90 bozohttpd
.set_pref(prefs
, "foreground", "true")
92 if options
.trustedref
then
93 bozohttpd
.set_pref(prefs
, "trusted referal", "true")
95 if 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
)
100 if options
.serversw
then
101 bozohttpd
.set_pref(prefs
, "server software", options
.serversw
)
104 cert
, priv
= string.find(options
.ssl
, "(%S+)%s+(%S+)")
105 bozohttpd
.dynamic_mime(httpd
, cert
, priv
)
107 if options
.username
then
108 bozohttpd
.set_pref(prefs
, "username", options
.username
)
110 if options
.unknownslash
then
111 bozohttpd
.set_pref(prefs
, "unknown slash", "true")
113 if options
.virtbase
then
114 bozohttpd
.set_pref(prefs
, "virtual base", options
.virtbase
)
116 if options
.indexhtml
then
117 bozohttpd
.set_pref(prefs
, "index.html", options
.indexhtml
)
119 if options
.dirtyenv
then
120 bozohttpd
.set_pref(prefs
, "dirty environment", "true")
122 if options
.bindaddr
then
123 bozohttpd
.set_pref(prefs
, "bind address", options
.bindaddr
)
125 if options
.cgibin
then
126 bozohttpd
.cgi_setbin(httpd
, options
.cgibin
)
128 if options
.cgimap
then
129 name
, handler
= string.find(options
.cgimap
, "(%S+)%s+(%S+)")
130 bozohttpd
.cgi_map(httpd
, name
, handler
)
132 if options
.public_html
then
133 bozohttpd
.set_pref(prefs
, "public_html", options
.public_html
)
135 if options
.chroot
then
136 bozohttpd
.set_pref(prefs
, "chroot dir", options
.chroot
)
138 if options
.enableusers
then
139 bozohttpd
.set_pref(prefs
, "enable users", "true")
141 if options
.hidedots
then
142 bozohttpd
.set_pref(prefs
, "hide dots", "true")
144 if options
.dirindex
then
145 bozohttpd
.set_pref(prefs
, "directory indexing", "true")
149 print("At least one arg needed for root directory")
151 -- set up connections
152 local vhost
= args
[2] or ""
153 bozohttpd
.setup(httpd
, prefs
, vhost
, args
[1])
155 -- loop, serving requests
156 local numreps
= options
.background
or 0
158 req
= bozohttpd
.read_request(httpd
)
159 bozohttpd
.process_request(httpd
, req
)
160 bozohttpd
.clean_request(req
)