.bashrc.d | Concept Code: Downloading files with Bash only

A Core of my .bashrc.d Concept are one the one hand the repositories where you can download other users scripts and extend your Bash in this way and on the other hand that it everything should be a bash-only. To use a repository the script requires to access the files in it. Downloading via HTTP work bash-only, but HTTPS sadly not, currently it forces me to use mktemp, openssl, cat and rm, maybe I have to drop HTTPS, but I don't like to do so... will make up my mind about it.

The HTTP stuff I'm doing via the /dev/tcp-Fake-Dev that is available in Bash, doing request by an simple echo redirected to the fake dev and reading the response with cat. Currently I'm searching for something Bash-only to replace cat. For HTTPS I used the s_client command from openssl, but there the redirections does not work correctly, which requires me to use the commands from above paragraph. This not from my own thoughts, I've read up about it.

And now: Ducks: The code.


# Copyleft 2015 Christoph "criztovyl" Schulz
# GPL v3 and later
#!/usr/bin/env bash
fetch(){ 
    # Args: url
    local url=$1
    IFS=":" read -a parts<<<"$url"
    local proto=${parts[0]}
    type fetch_$proto &>/dev/null || echo "Protocol \"$proto\" not implemented." && fetch_$proto $url
}

http_https_host(){
    # Returns the host part of an url
    # Args: urlResource resultVar
    #  resultVar defaults to "host"
    local urlResource=$1
    local resultVar=$2
    [ -z "$resultVar" ] && resultVar="host"    

    IFS="/" read -a resource <<<"$urlResource"
    host="${resource[2]}"

    eval "$resultVar=$host"

}
fetch_http(){
    # Args: url urlParts
    local url=$1

    http_https_host $url
    echo "host $host"

    exec 3<> /dev/tcp/$host/80
    echo -e "GET $url HTTP/1.1\nHost: $host\nConnection: close\n\n" 1>&3
    cat 0<&3
}
fetch_https(){
    # Args: url urlParts
    local url=$1
    http_https_host $url
    echo "host $host"
    tmp=`mktemp`
    echo -e "GET $url\nHost: $host\nConnection: close\n\n" | openssl s_client -quiet -connect $host:443 2>/dev/null > $tmp
    cat $tmp
    rm -f $tmp
}
#fetch_ftp(){}
#fetch_from_dir(){}

Christoph Schulz

Christoph Schulz
I am Dev. Maybe.

Podman Minikube on Debian 11

Where I can I try to replace docker with podman, rootless podman to be specific.Recently I have been playing around with minikube for kap...… Continue reading

Packaging Fractal as DEB 1/?

Published on January 16, 2020