RustForge

foundry

Foundry OS: the operating system built on the Rustbelt kernel

foundry / scripts / http-reply.sh
1097 bytes ยท raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/sh
# A one-shot HTTP/1.0 server on stdin/stdout, for QEMU's guestfwd:
#   -netdev user,...,guestfwd=tcp:10.0.2.100:80-cmd:sh scripts/http-reply.sh
# QEMU runs this once per connection the guest makes to 10.0.2.100:80, with
# the connection as stdin/stdout. No host port is opened, so tests can't
# collide with anything else on the machine.
#   GET /     -> a short greeting
#   GET /big  -> `seq 1 20000` (108894 bytes: exercises TCP flow control)
#   other     -> 404
cr=$(printf '\r')
read -r method path version
while IFS= read -r line; do               # skip the request headers
    [ "$line" = "$cr" ] || [ -z "$line" ] && break
done
reply() { # status, body
    printf 'HTTP/1.0 %s\r\nContent-Type: text/plain\r\nContent-Length: %s\r\nServer: rustbelt-test\r\n\r\n%s' \
        "$1" "$(printf '%s' "$2" | wc -c)" "$2"
}
case "$path" in
    /)    reply "200 OK" "Hello from the host, via QEMU guestfwd!
" ;;
    /big) printf 'HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 108894\r\n\r\n'
          seq 1 20000 ;;
    *)    reply "404 Not Found" "not found
" ;;
esac