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