summaryrefslogtreecommitdiff
path: root/index.sh
blob: 3133a2a4677d46422055fc27fe82b5329a91bcae (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/zsh

SCRIPT_FILE="$0"
SCRIPT_CONTENT=$(<"$SCRIPT_FILE")

http_response() {
    local content_length=$(wc -c < "$SCRIPT_FILE")
    printf "HTTP/1.1 200 OK\r\n"
    printf "Content-Type: text/plain\r\n"
    printf "Content-Length: %d\r\n" "$content_length"
    printf "Connection: close\r\n"
    printf "\r\n"
    printf "%s" "$SCRIPT_CONTENT"
}

handle_client() {
    while tcp_read; do
        line=${TCP_LINE%$'\r'}
        [[ -z "$line" ]] && break
    done
    
    tcp_send "$(http_response)"
    
    tcp_close
}

# Load zsh's TCP module and functions
zmodload zsh/net/tcp
autoload -U tcp_open tcp_read tcp_send tcp_close

# Clean up background processes on exit
cleanup() {
    jobs -p | xargs -r kill
    tcp_close -a
    exit 0
}
trap cleanup INT TERM

# Create listening socket once
ztcp -l 8082
server_fd=$REPLY

while true; do
    if tcp_open -a $server_fd; then
        handle_client
    fi
done