~mikini/hometools

ref: 637040b13e70a39c89ba1e76df7dfeed6a0fe916 hometools/http_length -rwxr-xr-x 2.0 KiB
637040b1 — Mikkel Kirkgaard Nielsen Add COPYING and README.md 6 months ago
                                                                                
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /bin/bash

case "$2" in
    --debug) DEBUG=1 ;;
    --dump)  DUMP_RESP=1 ;;
    default) ;;
esac

#[[ $DEBUG ]] && DOC=/x86/volumio/2.411/volumio-2.411-2018-06-15-x86.img.zip
#[[ $DEBUG ]] && HOST=updates.volumio.org

function gethead()
{
    SCHEME=$(echo $1|cut -d: -f1)
    HOST=$(echo $1|cut -d: -f2)
    HOST=$(echo $1|cut -d/ -f3)
    DOC=/$(echo $1|cut -d/ -f4-)

    case $SCHEME in
        http) PORT=80 ;;
        https) PORT=443 ;;
    esac
#    [[ $DEBUG ]] && echo SCHEME=$SCHEME
#    [[ $DEBUG ]] && echo HOST=$HOST
#    [[ $DEBUG ]] && echo PORT=$PORT
#    [[ $DEBUG ]] && echo DOC=$DOC

    case $PORT in
        80)
            echo -ne "HEAD $DOC HTTP/1.1\r\nHost:$HOST\r\n\r\n" | nc $HOST $PORT
            ;;
        443)
            echo -ne "HEAD $DOC HTTP/1.1\r\nHost:$HOST\r\n\r\n" | openssl s_client -quiet -connect $HOST:$PORT 2>/dev/null #${DEBUG:+-debug} # debug will get intermingled with stdout, fix with pipe, fd or something
            ;;
    esac
}

RESP=$(eval gethead $1)
[[ $DEBUG || $DUMP_RESP ]] && echo -e HTTP response dump: \\n"$RESP"
HTTP_SCODE=$(echo "$RESP"|head -1|cut -d" " -f2)
[[ $DEBUG || $DUMP_RESP ]] && echo HTTP status code: $HTTP_SCODE

CL_BYTES=$(echo "$RESP"|grep "Content-Length:" |grep -o "[[:digit:]]*" )

case $HTTP_SCODE in
    300|301|302)
        echo Redirect to: $(echo "$RESP"|grep "Location:"|cut -d: -f2-)
        ;;
    200)
        if [ ! $? -eq 0 ]; then
            echo No Content-Length header found in response!
        else
            echo -n "Document size: $CL_BYTES bytes"
            if [ $CL_BYTES -lt 1024 ]; then
                echo .
            elif [ $CL_BYTES -lt $((1024**2)) ]; then
                echo " ($(($CL_BYTES/1024**1)) KiB)."
            elif [ $CL_BYTES -lt $((1024**3)) ]; then
            echo " ($(($CL_BYTES/(1024**2))) MiB)."
            elif [ $CL_BYTES -lt $((1024**4)) ]; then
              echo " ($(($CL_BYTES/(1024**3))) GiB)."
          else
            echo .
          fi
        fi
        ;;
esac