4 ms·
darkhttpd is cool, and it's pretty featureful for a small program, but it's also 2500 lines of code. If you liked it, you might also like my server httpdito: h
by kragen 6y ago
darkhttpd is cool, and it's pretty featureful for a small program, but it's also 2500 lines of code.
If you liked it, you might also like my server httpdito: http://canonical.org/~kragen/sw/dev3/server.s http://canonical.org/~kragen/sw/dev3/server.s which has documentation in http://canonical.org/~kragen/sw/dev3/httpdito-readme http://canonical.org/~kragen/sw/dev3/httpdito-readme. It's also an HTTP server in a single source file, but it's i386 assembly, for Linux, that doesn't use libc. It's only about 700 lines of code, and the executable is up to 2060 bytes now that I've added CSS and PDF support to it.
httpdito is, to my surprise, practically useful on occasion as an alternative to things like `python -m SimpleHTTPServer`.
Unlike the server in Koshkin's excellent comment https://news.ycombinator.com/item?id=26672683 https://news.ycombinator.com/item?id=26672683 httpdito does send CRLFs as the HTTP standard demands. Unfortunately it also requires them in the request. Also—and this is a key point on which most software falls down—httpdito's documentation contains a section entitled "Are you insane?", which really is a question every programmer ought to answer in their documentation.
It doesn't have most of the things darkhttpd has, though. It doesn't support IPv6, HEAD, directory listings, byte ranges, conditional requests, keepalive, name-based virtual hosting, sendfile, logging, chroot, or privsep, and doesn't run on Solaris, SPARC, or ARM. But if you're looking for features like "small memory footprint" and "no installation needed" and "no messing around with config files" it may actually be better than darkhttpd. Due in large part to improvements in modern Linux and its small memory footprint (normally 5 pages) it's efficient enough to saturate a gigabit connection.
- mysterydip 6y agoDo you have plans for any of those features, or is it complete for the use cases it is solving?
- kragen 6y agoNo, I didn't write it to solve use cases at all, but rather because I thought it would be awesome to write a web server in assembly. As the readme says, we all have moments where we do things we regret. Youthful† lapses in judgment. If you git clone http://canonical.org/~kragen/sw/dev3/.git http://canonical.org/~kragen/sw/dev3/.git you will see that I wrote most of it one weekend in 02013, removed the dependency on libc and added forking the next weekend, and tweaked it slightly over the next month and a half. Then in 02019 I added CSS and PDF to the mime-types list. So it's not, like, an ongoing project. It's just a program I wrote once, like most things in that directory. I did just fix the documentation, though, because it was still claiming it was under 2000 bytes. ______ † Technically 37 is not actually that youthful.
- mysterydip 6y agoFair enough. Thanks for providing it, I have a few uses in mind.
- kragen 6y agoYou're welcome! I hope you enjoy it!
- jamiek88 6y agoHey why do you add an extra preceding zero on years?
- simlevesque 6y agohttps://en.wikipedia.org/wiki/Long_Now_Foundation https://en.wikipedia.org/wiki/Long_Now_Foundation
- colejohnson66 6y agoFor your website, it seems the files aren’t being encoded right because Firefox on iOS is rendering them as something else. I’m getting mojibake with apostrophes and the like: > ### setup_socket: there’s little interesting to say here that’s not ### in, say, Beej’s Socket Guide.
- kragen 6y agoThanks! Yeah, what happened was that Apache wasn't sending a content-type header until I tweaked the .htaccess file, which was about 14 minutes before you posted your comment. I had to ctrl-shift-r in Firefox to get it to notice the changed content-type.
- IgorPartola 6y agoIt’s funny, this is the exact kind of thing I’d want on a non-x86 system. Raspberry Pico for example would be a great place for a 2KB HTTP server but with not support for HTTPS, IPv6, or for any non-x86 architectures it’s a bit of a non-starter for my use cases. Still, very cool project!
- kragen 6y agoThe CPU architecture is actually the least of your concerns there—I'm pretty sure qemu-user can run httpdito on ARM with less than an order of magnitude performance overhead. There are a lot of embedded systems where an HTTP transaction per second per MHz would be more than sufficient. The bigger problem is that the Raspberry Pico is a dual-core Cortex-M0+, which doesn't have an MMU, so it can't run Linux and especially can't handle fork(). But httpdito is basically scripting the Linux system call interface in assembly language—it needs to run on top of a filesystem, an implementation of multitasking that provides allocation of different memory to different tasks, and a TCP/IP stack. Any one of these is probably a larger amount of complexity than the 296 CPU instructions in httpdito. The smallest TCP/IP stack I know of is Adam Dunkels's uIP. Running `sloccount .` in uip/uip cloned from https://github.com/adamdunkels/uip https://github.com/adamdunkels/uip gives a count of 2796 lines of source code ("generated using David A. Wheeler's 'SLOCCount'."). uIP can run successfully on systems with as little as 2KiB of RAM, as long as you have somewhere else to put the code, but for most uses lwIP is a better choice; it minimally needs 10KiB or so. uIP is part of Dunkels's Contiki, which includes a fairly full-featured web server and a somewhat less-full-featured browser. I think he got both the server and the browser to run in 16KiB of RAM on a Commodore PET, but not at the same time. (twIP http://dunkels.com/adam/twip.html http://dunkels.com/adam/twip.html is only 139 bytes of C source but doesn't support TCP or any physical-layer protocol such as Ethernet, PPP,or SLIP.) However, Adam Dunkels has also written Miniweb http://dunkels.com/adam/miniweb/ http://dunkels.com/adam/miniweb/, which implements HTTP and enough of TCP and IP to support it, in 400 lines of C. It needs at least 30 bytes of RAM. Like twIP, it doesn't provide a physical layer. But that's solvable.
- jimmyed 6y agoNot having MMU means there's no virtual memory and instructions refer to physical memory addresses, cmiiw? You say Linux won't work without MMU, it can't handle physical addresses? Moreover, why won't fork() work without MMU?
- deleted 6y ago[deleted]
- fulafel 6y agoNice. Have you considered publishing it under a FOSS license?
- kragen 6y agoThanks. I thought I'd done so when I wrote it in 02013! I've now corrected the omission by adding a Creative Commons Public Domain dedication. Thank you for pointing that out!