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 33 dofile "optparse.lua" 34 35 opt = OptionParser{usage="%prog [options] root [vhost]", version="20180502"} 36 37 opt.add_option{"-C", "--cgimap", action="store", dest="cgimap", help="--cgimap 's t'"} 38 opt.add_option{"-E", "--enable-user-cgibin", action="store_true", dest="enableusercgibin", help="--enable-user-cgibin"} 39 opt.add_option{"-H", "--hide-dots", action="store_true", dest="hidedots", help="--hide-dots"} 40 opt.add_option{"-I", "--portnum", action="store", dest="portnum", help="--portnum number"} 41 opt.add_option{"-M", "--dynamicmime", action="store", dest="dynmime", help="--dynamicmime 'suffix type a b'"} 42 opt.add_option{"-S", "--server-software", action="store", dest="serversw", help="--server-software name"} 43 opt.add_option{"-U", "--username", action="store", dest="username", help="--username name"} 44 opt.add_option{"-V", "--unknown-slash", action="store_true", dest="unknown", help="--unknown-slash"} 45 opt.add_option{"-X", "--dir-index", action="store_true", dest="dirindex", help="--dir-index"} 46 opt.add_option{"-Z", "--ssl", action="store", dest="ssl", help="--ssl 'cert priv'"} 47 opt.add_option{"-b", "--background", action="store", dest="background", help="--background count"} 48 opt.add_option{"-c", "--cgibin", action="store", dest="cgibin", help="--cgibin bin"} 49 opt.add_option{"-e", "--dirtyenv", action="store_true", dest="dirtyenv", help="--dirtyenv"} 50 opt.add_option{"-f", "--foreground", action="store_true", dest="foreground", help="--foreground"} 51 opt.add_option{"-i", "--bindaddr", action="store", dest="bindaddress", help="--bindaddr address"} 52 opt.add_option{"-n", "--numeric", action="store_true", dest="numeric", help="--numeric"} 53 opt.add_option{"-p", "--public-html", action="store", dest="public_html", help="--public-html dir"} 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"} 59 60 -- caller lua script 61 local extension = ".so" 62 f = io.open("libluabozohttpd.dylib", "r") 63 if f then 64 extension = ".dylib" 65 io.close(f) 66 end 67 glupkg = package.loadlib("./" .. "libluabozohttpd" .. extension, "luaopen_bozohttpd") 68 bozohttpd = glupkg() 69 70 -- initialise 71 httpd = bozohttpd.new() 72 bozohttpd.init_httpd(httpd) 73 prefs = bozohttpd.init_prefs() 74 75 -- parse command line args 76 options,args = opt.parse_args() 77 if options.portnum then 78 bozohttpd.set_pref(httpd, prefs, "port number", options.portnum) 79 end 80 if options.background then 81 bozohttpd.set_pref(httpd, prefs, "background", options.background) 82 end 83 if options.numeric then 84 bozohttpd.set_pref(httpd, prefs, "numeric", "true") 85 end 86 if options.logstderr then 87 bozohttpd.set_pref(httpd, prefs, "log to stderr", "true") 88 end 89 if options.foreground then 90 bozohttpd.set_pref(httpd, prefs, "foreground", "true") 91 end 92 if options.trustedref then 93 bozohttpd.set_pref(httpd, prefs, "trusted referal", "true") 94 end 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) 99 end 100 if options.serversw then 101 bozohttpd.set_pref(httpd, prefs, "server software", options.serversw) 102 end 103 if options.ssl then 104 cert, priv = string.find(options.ssl, "(%S+)%s+(%S+)") 105 bozohttpd.dynamic_mime(httpd, cert, priv) 106 end 107 if options.username then 108 bozohttpd.set_pref(httpd, prefs, "username", options.username) 109 end 110 if options.unknownslash then 111 bozohttpd.set_pref(httpd, prefs, "unknown slash", "true") 112 end 113 if options.virtbase then 114 bozohttpd.set_pref(httpd, prefs, "virtual base", options.virtbase) 115 end 116 if options.indexhtml then 117 bozohttpd.set_pref(httpd, prefs, "index.html", options.indexhtml) 118 end 119 if options.dirtyenv then 120 bozohttpd.set_pref(httpd, prefs, "dirty environment", "true") 121 end 122 if options.bindaddr then 123 bozohttpd.set_pref(httpd, prefs, "bind address", options.bindaddr) 124 end 125 if options.cgibin then 126 bozohttpd.cgi_setbin(httpd, options.cgibin) 127 end 128 if options.cgimap then 129 name, handler = string.find(options.cgimap, "(%S+)%s+(%S+)") 130 bozohttpd.cgi_map(httpd, name, handler) 131 end 132 if options.public_html then 133 bozohttpd.set_pref(httpd, prefs, "public_html", options.public_html) 134 end 135 if options.chroot then 136 bozohttpd.set_pref(httpd, prefs, "chroot dir", options.chroot) 137 end 138 if options.enableusers then 139 bozohttpd.set_pref(httpd, prefs, "enable users", "true") 140 end 141 if options.hidedots then 142 bozohttpd.set_pref(httpd, prefs, "hide dots", "true") 143 end 144 if options.enableusercgibin then 145 bozohttpd.set_pref(httpd, prefs, "enable user cgibin", "true") 146 end 147 if options.dirindex then 148 bozohttpd.set_pref(httpd, prefs, "directory indexing", "true") 149 end 150 151 if #args < 1 then 152 print("At least one arg needed for root directory") 153 else 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 165 end 166