A single-file web server that runs on six operating systems • The Register
A bunch of almost insanely clever technical tricks come together into something practical with redbean 2: a web server plus content in a one file which runs on any x86-64 operating system.
The project is the culmination – so far – of a series of remarkable, programmer-inspired hacks Justine Tunney: αcτµαlly pδrταblε εxεcµταblε, Cosmopolitan libc, and the original redbean. It may take a little while to explain what it does, so bear with us. We promise you will be be impressed.
For starters, redbean uses a remarkable hack called APE, which stands for Truly portable executable – that its author stylizes αcτµαlly pδrταblε εxεcµταblε. (If you know the Greek alphabet, it reads like “actmally pdrtable execmtable”, but hey, that looks cool.)
The gist of the idea of APE is this: whatever programming language you use, if you compile your code to a native binary, it produces x86-64 machine code – and any x86-64 processor executes the same machine code. If you wrote a program that didn’t use any external code, then in principle the only difference between a Windows binary and (say) a Linux binary is the format of the file that contains the binary. So if you could only move thisyour program will be able to run on any operating system.
Linux uses a format called Executable and linkable format (ELF). Previously, proprietary x86 Unixes used Common Object File Format or COFF. That’s why the recent conversion from Lotus 1-2-3 for Unix to Linux used a tool called
coffsyrup to convert COFF to ELF.
The joke in the APE name comes from the fact that Windows NT still uses a format derived from the old COFF standard, called portable executable (PE). The PE format is only “portable” insofar as you are running a Windows
.EXE file under DOS, it will actually run: PE files contain a small DOS stub that displays the message “This program cannot be run in DOS mode.” But “portable executables” aren’t really portable at all – unlike, say, family applicationswhich ran natively under DOS and OS/2.
APE files are, as their name suggests, Really Portable executables. A single APE file runs fine on any x86-64 operating system. Take the file and run it in Windows, then it acts as a Windows executable. To run the same file on macOS, and it’s a macOS executable. Run it on Linux, and it’s a Linux binary… and the same on FreeBSD, and OpenBSD, and NetBSD, even though they all have their own different executable formats.
The catch is that almost all real programs have to call some library, even if only the standard C library… which is where the cosmopolitan libc “Cosmo” is a libc for APE, so C programs can be compiled and run on any operating system supported by APE.
With Cosmo and the APE format, you can write a C program and compile it into a single file that will load and run on six completely different operating systems. Oh, and if that wasn’t enough, the same binary can also be booted directly from the PC’s BIOS. Which means you can run it under Tunney’s Flashing lightsand watch the PC’s registers change live, like an old-fashioned mainframe.
At this point, the minds of many technicians may well be in shock with astonishment. So to give you something nice to look at, there are also demo programs. The demo video on the Blinkenlights page shows printimage.com, which can print JPG files to the text console. If you are still not impressed, printvideo.com same for the video.
the original Red bean made the combination of APE and Cosmo something slightly more useful. After all, as amazing as the combination is, having only one libc is pretty limited in the 21st century. You can’t build just any type of GUI application, eg. But every OS supported by APE can run a web browser, so redbean has also integrated a web server (and SQLite database and Lua scripts) into a single binary. All you have to do is change the file extension to
.ZIPadd your static web content using an archive manager, save it, rename it again and bingo: standalone web application.
Think of it as being a bit like an Electron app, except instead of integrating the entire browser into your innocent little program, you use your existing browser.
Now, red bean 2.o (yes, that means two dots-oh, not two dots-zero) takes the original redbean and adds a REPL – for non-Lispy types, an interactive command line – and a Unix SysV API (yes, even on Windows), plus logging and more.
Speaking of Lisp, another of Tunney’s amazing projects is SectorLispwhich embeds a Lisp interpreter in a PC boot sector. Sector Lisp 2 also adds garbage collection, making it the smallest GC language in the world. Given this interest in programs that can be started directly, without an operating system at all, you might not be surprised that a future goal of redbean is to make it bootable: to integrate a TCP/IP stack and network card drivers, for a standalone tool. ®
If you don’t take our word for it and want to try redbean for yourself, be aware that there are issues on some operating systems. Recent versions of macOS (11 and 12) break some of the tricks that allow APE to work, just like Ubuntu. pirate news discussion about the project and its background contain workarounds.
Comments are closed.