Support OnHttpRequest Lua callback

If your redbean `/.init.lua` file defines a global callable named
`OnHttpRequest` then redbean will delegate all serving control to
your function. You may then restore the default serving paths, by
calling the new `Route()`, `RouteHost()`, and `RoutePath()` APIs.

Closes #150
This commit is contained in:
Justine Tunney 2021-04-24 17:09:01 -07:00
parent dc6d11a031
commit 472b95fea3
13 changed files with 671 additions and 568 deletions

View file

@ -53,10 +53,8 @@ static int __fmt_stoa_bing(out_f out, void *a, uint64_t w) {
static int __fmt_stoa_quoted(out_f out, void *a, uint64_t w) {
char buf[8];
if (w <= 0x7F) {
if (w < 0x20 || w == 0x7F) {
if (isascii(w)) {
w = cescapec(w);
}
} else {
w = tpenc(w);
}

17
tool/net/demo/.init.lua Normal file
View file

@ -0,0 +1,17 @@
-- /.init.lua is loaded at startup in redbean's main process
HidePath('/usr/share/zoneinfo/')
function OnHttpRequest()
if HasParam('magic') then
Write('<p>\r\n')
Write('OnHttpRequest() has intercepted your request<br>\r\n')
Write('because you specified the magic parameter\r\n')
Write('<pre>\r\n')
Write(EscapeHtml(LoadAsset('/.init.lua')))
Write('</pre>\r\n')
else
Route()
end
SetHeader('Server', 'redbean!')
end

1
tool/net/demo/hello.lua Normal file
View file

@ -0,0 +1 @@
Write('hello world\r\n')

View file

@ -33,10 +33,10 @@ local function main()
SetHeader('Expires', FormatHttpDateTime(GetDate()))
SetHeader('Cache-Control', 'no-cache, must-revalidate, max-age=0')
-- Roundtripping information can make it safer.
Write('<p>Thank you for visiting ')
Write(EscapeHtml(EncodeUrl(ParseUrl(GetUrl()))))
Write('\r\n')
-- GetUrl() is the resolved Request-URI (TODO: Maybe change API to return a URL object?)
Write('<p>Thank you for visiting <code>')
Write(GetUrl()) -- redbean encoded this value so it doesn't need html entity escaping
Write('</code>\r\n')
-- GetParam(NAME) is the fastest easiest way to get URL and FORM params
-- If you want the RequestURL query params specifically in full do this
@ -55,6 +55,12 @@ local function main()
end
end
Write('</dl>\r\n')
Write("<p>Whatever you do, don't click on ")
Write('<a href="')
Write(EscapeHtml(EscapePath(GetPath()) .. '?magic'))
Write('">')
Write(EscapeHtml(VisualizeControlCodes(GetPath())))
Write('?magic</a>\r\n')
else
Write('<p>\r\n')
Write('<em>none</em><br>\r\n')

View file

@ -72,13 +72,12 @@ o/$(MODE)/tool/net/redbean.com.dbg: \
o/$(MODE)/tool/net/redbean.com: \
o/$(MODE)/tool/net/redbean.com.dbg \
tool/net/net.mk \
tool/net/favicon.ico \
tool/net/redbean.png \
tool/net/.init.lua \
tool/net/.reload.lua
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ zip -qj $@ o/$(MODE)/tool/net/.ape tool/net/.init.lua tool/net/.reload.lua tool/net/favicon.ico tool/net/redbean.png
@$(COMPILE) -AZIP -T$@ zip -qj $@ o/$(MODE)/tool/net/.ape tool/net/.init.lua tool/net/favicon.ico tool/net/redbean.png
o/$(MODE)/tool/net/redbean-demo.com.dbg: \
o/$(MODE)/tool/net/redbean.com.dbg
@ -87,17 +86,18 @@ o/$(MODE)/tool/net/redbean-demo.com.dbg: \
o/$(MODE)/tool/net/redbean-demo.com: \
o/$(MODE)/tool/net/redbean-demo.com.dbg \
tool/net/net.mk \
tool/net/.init.lua \
tool/net/.reload.lua \
tool/net/404.html \
tool/net/favicon.ico \
tool/net/redbean.png \
tool/net/index.html \
tool/net/redbean.css \
tool/net/redbean.lua \
tool/net/redbean-form.lua \
tool/net/redbean-xhr.lua \
tool/net/seekable.txt \
tool/net/demo/.init.lua \
tool/net/demo/.reload.lua \
tool/net/demo/404.html \
tool/net/demo/hello.lua \
tool/net/demo/index.html \
tool/net/demo/redbean.css \
tool/net/demo/redbean.lua \
tool/net/demo/redbean-form.lua \
tool/net/demo/redbean-xhr.lua \
tool/net/demo/seekable.txt \
tool/net/redbean.c \
net/http/parsehttprequest.c \
net/http/parseurl.c \
@ -105,18 +105,20 @@ o/$(MODE)/tool/net/redbean-demo.com: \
test/net/http/parsehttprequest_test.c \
test/net/http/parseurl_test.c
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ zip -qj $@ o/$(MODE)/tool/net/.ape tool/net/.init.lua tool/net/.reload.lua tool/net/redbean.lua tool/net/404.html tool/net/favicon.ico tool/net/redbean.png tool/net/redbean-form.lua tool/net/redbean-xhr.lua
@$(COMPILE) -AZIP -T$@ zip -qj0 $@ tool/net/seekable.txt
@$(COMPILE) -AZIP -T$@ zip -q $@ tool/net tool/net/index.html tool/net/redbean.css tool/net/redbean.c net/http/parsehttprequest.c net/http/parseurl.c net/http/encodeurl.c test/net/http/parsehttprequest_test.c test/net/http/parseurl_test.c
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-demo
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean-demo/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ zip -qj $@ o/$(MODE)/tool/net/.redbean-demo/.ape tool/net/demo/.init.lua tool/net/demo/.reload.lua tool/net/demo/hello.lua tool/net/demo/redbean.lua tool/net/demo/404.html tool/net/favicon.ico tool/net/redbean.png tool/net/demo/redbean-form.lua tool/net/demo/redbean-xhr.lua
@$(COMPILE) -AZIP -T$@ zip -qj0 $@ tool/net/demo/seekable.txt
@$(COMPILE) -AZIP -T$@ zip -q $@ tool/net/ tool/net/demo/ tool/net/demo/index.html tool/net/demo/redbean.css tool/net/redbean.c net/http/parsehttprequest.c net/http/parseurl.c net/http/encodeurl.c test/net/http/parsehttprequest_test.c test/net/http/parseurl_test.c
o/$(MODE)/tool/net/redbean-static.com: \
o/$(MODE)/tool/net/redbean-static.com.dbg \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ zip -qj $@ o/$(MODE)/tool/net/.ape tool/net/favicon.ico tool/net/redbean.png
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-static
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean-static/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ zip -qj $@ o/$(MODE)/tool/net/.redbean-static/.ape tool/net/favicon.ico tool/net/redbean.png
o/$(MODE)/tool/net/redbean-static.com.dbg: \
$(TOOL_NET_DEPS) \

File diff suppressed because it is too large Load diff