1 of 32

redbean

Actually Portable Executable Web Server

2 of 32

Liberate Your Web Apps

3 of 32

redbean + cosmopolitan libc + ape

  • It’s a conventional HTTP/1.1 server�
  • Written in conventional style (POSIX ANSI C)�
  • Shipped in a radical new polyglot executable format

4 of 32

redbean + cosmopolitan libc + ape

  • Self-hosted web apps (doesn’t need the cloud)�
  • Multiplatform (uses same binary on all platforms)�
  • Zero dependencies (no interpreter or virtual machine)�
  • No. 3 top “Show HN” thread of all time (Feb 2021)�https://hn.algolia.com/?query=show%20hn

5 of 32

Installation

�$ wget https://justine.lol/redbean/redbean-1.0.com�$ chmod +x redbean-1.0.com

$ ./redbean-1.0.com

I2021-05-21T02:38:29.808287:tool/net/redbean.c:4706:redbean:10388] LISTEN 0.0.0.0:8080 see http://127.0.0.1:8080

​

​

6 of 32

HTTP Server

redbean default listing page

7 of 32

ZIP File System

You open the redbean.com binary in any zip tool and put your assets in it

8 of 32

Application Server

Files with the extension .lua are dynamically served using with a built-in Lua 5.4 interpreter

9 of 32

HTTP Proxy

redbean can be your own private Internet if you configure your browser to use it as an HTTP proxy server

Use wget to mirror websites

Use zip -r redbean.com www.foo.com/ to add mirror directories to executable

10 of 32

Unbloat Your Tech Stack

11 of 32

Footprint

redbean embeds lua5 plus support for six operating systems yet somehow is orders of magnitudes tinier than other servers built for only a single platform and we don’t even apply tools like upx

Electron App (EXE; Windows only) 120,000 kb

​

OpenJDK Shell Script (Linux-only) 100,000 kb

​

.NET distributable (platform-only) 80,000 kb

​

Go (static http; Linux-only) 6,000 kb

​

NGINX (ELF+DSOs; distro-only) 3,000 kb

​

redbean.com (linux+mac+bsd+win) 446 kb

​

redbean.com.gz 256 kb

12 of 32

Memory

redbean has a vertically-integrated tech stack built with a single vision plus a brand new c standard library

Electron 200,000 kb

​

Python HttpServer 12,000 kb

​

V8 7,000 kb

​

NGINX (worker) 580 kb

​

redbean (worker) 200 kb

​

​

​

​

​

​

​

​

Note: Measuring memory use is more art than science

13 of 32

Latency

redbean only needs one system call to serve a static asset (writev) since ZIP central directory is mapped into RAM

redbean uses perfect hash tables for http headers, a hash table for assets, and parsers are all guaranteed linear even when messages are fragmented

Python HttpServer 12,770µs

​

Go 57µs

​

NGINX 26µs

​

redbean 8µs

14 of 32

Throughput

redbean doesn’t need to do anything to serve compressed responses because zip and gzip both use deflate algorithm

benchmarked on intel core i9-9900 using alpine linux default settings

Python HttpServer w/ gzip 100 qps

​

Python ThreadedHttpServer w/ gzip 1,500 qps

​

NGINX w/ gzip 300,000 qps

​

NGINX 400,000 qps

​

Go 400,000 qps

​

redbean w/ unzip+crc 500,000 qps

​

redbean static w/ gzip 1,000,000 qps

​

redbean dynamic lua hello world 1,000,000 qps

​

./redbean-demo.com -s

wrk -H 'Accept-Encoding: gzip' -t 12 -c 120 http://127.0.0.1:8080/tool/net/demo/index.html

15 of 32

What is an APE Binary?�αcτµαlly pδrταblε εxεcµταblε

16 of 32

APE is a Polyglot Executable Format

  • Uses ld script to synthesize PE + SH + ELF + Mach-O + ZIP�
  • Statically-linked using standard x86-pc-linux-gnu toolchain�
  • Runs on Linux, Mac, Windows, FreeBSD, OpenBSD, NetBSD�
  • Boots from BIOS since it embeds its own operating system�
  • No interpreter or virtual machine needed (x86 is byte code)

17 of 32

cat -v redbean.com

Windows MZ Executable

UNIX Sixth Edition Shebang

Mac OS X Mach-O Block Copy

UNIX ELF Header Self-Modifier

18 of 32

Code Morphing Executables

  • redbean.com overwrites its first 64-byte shell script at startup�
  • The executable unmaps itself from memory (avoid ETXTBSY)�
  • Maps itself back into memory using O_RDWR�restores canonical MZqFpD ape file header�
  • StoreAsset() lets Lua use ZIP EXE as document store

19 of 32

REDBEAN.COM

binary header (1)

Windows MZ Executable

BIOS Bootloader

MacOS Support

UNIX Shell Script�Linux+FreeBSD+OpenBSD+NetBSD

20 of 32

REDBEAN.COM

binary header (2)

Linux PT_GNU_STACK

OpenBSD Note

NetBSD Note

Mac OS X Mach-O Structure

21 of 32

REDBEAN.COM

binary zip footer

PKZIP Central Directory

PKZIP End Of Central Directory

22 of 32

Lua Server Pages

23 of 32

Lua Source Code

Any file named foo.lua is served dynamically using a built-in Lua 5.4 interpreter

24 of 32

redbean .lua files

  • Dynamically generated�
  • redbean api�94 functions and 1 module�
  • redbean api is low-level�mostly parsers, escapers, etc.

25 of 32

redbean api example

SetStatus(200)�SetHeader('Content-Type', 'text/html; charset=utf-8')�Write('<!doctype html>\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')

26 of 32

Frameworks

27 of 32

Third Party Web Frameworks

  • Apnan (by James Milne)�https://git.sr.ht/~shakna/anpan

28 of 32

Apnan Framework Example

math.randomseed(os.time())

local anpan = load(LoadAsset("/anpan.lua"))()

anpan.route("/", function(request, response)

local r = anpan.HTMLResponse()

r.body:appendChild(

anpan.HTMLElement("h1")

:addText("Home"))

:appendChild(

anpan.HTMLElement("p")

:addText("Hello, World!"))

...

29 of 32

Legal

30 of 32

Legal

redbean is permissive and doesn’t have any gpl code / dependencies

  • ISC License�
  • Third party: MIT, BSD-2, BSD-3, zlib�
  • Legal notices are embedded inside the redbean.com binary by the GNU linker

31 of 32

redbean is the web server with a heart

32 of 32

Thanks!

Contact Info:

Justine Tunney

Milpitas, California

jtunney@gmail.com

https://justine.lol/redbean/

​