asmhttpd is a small concurrent HTTP/1.0 server written in x86-64 Linux assembly (GNU as,
Intel syntax). No libc and no dependencies: it talks to the kernel only through
raw syscalls, and links into a static binary of a few kilobytes.
Inspired by the web-server exercises in pwn.college's Computing 101 course.
- GET: serves regular files of any size, streamed in 4 KiB chunks, with a
correct
Content-Length./maps toindex.html. Query strings are ignored. - POST: writes exactly
Content-Lengthbytes of the request body into the target file (created or truncated, mode0644), even when the body arrives over several packets. - Concurrency: one process per connection (
fork).SIGCHLDis ignored, so the kernel reaps children and no zombies remain. - Errors:
400 Bad Request,403 Forbidden,404 Not Found,405 Method Not Allowed(with anAllowheader),500 Internal Server Error. - Safety checks: any path containing
..is rejected with 403; directories and other non-regular files are rejected with 403. - Robustness:
SIGPIPEis ignored, so a client that disconnects early can't kill the server.SO_REUSEADDRlets you restart it on the same port at once. Partial writes are retried until every byte is sent.
Requires an x86-64 Linux machine (or WSL) with GNU binutils (as, ld) and
make.
make # assembles every src/*.s into build/ and links ./asmhttpd
make test # runs the end-to-end tests (needs bash, curl, nc, cmp)
make clean./asmhttpd [port] [root_dir]| Argument | Default | Meaning |
|---|---|---|
port |
8080 |
TCP port, bound on 0.0.0.0 |
root_dir |
. |
directory to serve; the server chdirs into it |
./asmhttpd 8080 ./www &
curl http://127.0.0.1:8080/ # www/index.html
curl --data 'hello' http://127.0.0.1:8080/note.txt # writes www/note.txt
curl http://127.0.0.1:8080/note.txt # helloinclude/defs.inc syscall numbers, socket/file/signal constants, buffer sizes
src/main.s _start, argument parsing, signal setup, accept loop, fork
src/net.s setup_listener: socket, SO_REUSEADDR, bind, listen
src/http.s handle_client, parse_path, find_header_end, parse_content_length
src/handlers.s handle_get, handle_post
src/util.s write_all, atoi, itoa, print_listening
src/data.s response strings (.rodata), sockaddr/sigaction (.data), buffers (.bss)
Every module includes defs.inc. Symbols used across files are declared
.global; internal functions follow the System V AMD64 calling convention.
┌────────────────────────────────┐
│ parent: _start │
│ chdir(root_dir) │
│ ignore SIGCHLD, SIGPIPE │
│ socket / bind / listen │
└───────────────┬────────────────┘
│
┌────────────────────►▼
│ accept(listen_fd)
│ │ client_fd
│ ▼
│ fork()
│ ┌──────────┴───────────┐
│ pid > 0 pid == 0
│ │ │
│ ▼ ▼
│ close(client_fd) ┌───────────────────────┐
└──────────┘ │ child │
│ close(listen_fd) │
│ handle_client │
│ ├─ read headers │
│ ├─ GET → handle_get│
│ └─ POST → handle_post
│ close(client_fd) │
│ exit(0) │
└───────────────────────┘
children reaped by the kernel (SIGCHLD = SIG_IGN)
The parent only accepts and forks; each child owns exactly one connection and exits when it is done, so a slow or failing client never blocks the others.
- HTTP/1.0 only: one request per connection, always
Connection: close; no keep-alive, no chunked transfer encoding. - Only
GETandPOST. NoHEAD,PUT,DELETE, ranges or conditional requests. - Request line and headers must fit in 4 KiB, or the server answers 400.
- No
Content-Typeheader is sent; clients have to guess the media type. - No URL decoding:
%20and similar are taken literally. - POST overwrites any writable file under the root, with no authentication. Don't expose it to an untrusted network.
- The
..check is a plain substring test, so a name likea..bis also refused. Symlinks inside the root are followed. - No timeouts and no limit on the number of children: a client that never sends its headers keeps a process alive, and a flood of connections can exhaust the process table.
- Linux x86-64 only (raw syscall numbers and
struct statoffsets).