aboutsummaryrefslogtreecommitdiff
path: root/preprocessor/main.sh
blob: 081e5a16207329261f631ae7f09f00cd8af80526 (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
#!/bin/bash
read -r REQUEST_LINE
export REQUEST_LINE="$REQUEST_LINE"
export REQUEST_METHOD=$(echo "$REQUEST_LINE" | cut -d' ' -f1)
export REQUEST_DOCUMENT=$(echo "$REQUEST_LINE" | cut -d' ' -f2)
export REQUEST_PROTOCOL=$(echo "$REQUEST_LINE" | rev | cut -d' ' -f1 | rev)

export e404=$(cat errors/404.bhtml | envsubst)

echo "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)" >> log
if [ -f "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)" ]; then
    if [[ $(file --mime-type -b "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)") == text/html ]]; then
        export data=$(cat "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)" | envsubst)
    fi

    echo "HTTP/1.1 200 OK"
    echo "Server: Bashweb/"$bashweb_version"; bash/"$(bash --version | head -1 | cut -d' ' -f4)"; "$(uname -s)"/"$(uname -r)
    echo "Connection: close"
    if [[ $(file --mime-type -b "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)") == text/html ]]; then
        echo "Content-Length: "$(echo "$data" | wc -c)
    fi
    echo "Content-Type: "$(file --mime-type -b "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)")
    echo $(file --mime-type -b "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)") >> log
    echo ""
    #echo "$data"
    #echo "garbage"
    if [[ $(file --mime-type -b "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)") == text/html ]]; then
        echo "$data"
    else
        cat "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)"
    fi
else
    echo "HTTP/1.1 404 Not Found"
    echo "Server: Bashweb/"$bashweb_version"; bash/"$(bash --version | head -1 | cut -d' ' -f4)"; "$(uname -s)"/"$(uname -r)
    echo "Connection: close"
    echo "Content-Length: "$(echo "$e404" | wc -c)
    echo "Content-Type: text/html"
    echo ""
    echo "$e404"
fi