-
Notifications
You must be signed in to change notification settings - Fork 3.2k
curl/wget (need help) #11
Comments
I got it working. It's a little slow as it requires two Example script: #!/usr/bin/env bash
#
# Download a file in pure bash.
download() {
IFS=/ read -r _ _ host query <<< "$1"
# Send the HTTP request.
exec 3<"/dev/tcp/${host}/80"; {
printf '%s\r\n%s\r\n\r\n' \
"GET /${query} HTTP/1.0" \
"Host: $host"
} >&3
# Strip the HTTP headers.
while IFS= read -r line; do
[[ "$line" == $'\r' ]] && break
done <&3
# Output the file.
nul='\0'
while IFS= read -d '' -r line || { nul=""; [[ -n "$line" ]]; }; do
printf "%s%b" "$line" "$nul"
done <&3
exec 3>&-
}
download "$1" |
The fist loop is reasonably slow as it will just drop a sane amount of headers, i can't understand how the 2nd loop (a simple cat !!) can be so complicated (hence, slow i guess) |
Bash is slow at file IO and it doesn't handle binary data very well. I'm sure it can be optimized but I have some doubts as to whether or not this will ever be faster than |
According to the "bash bible" - yours :p a simple cat alternative might be
Yet i cannot make this work with my design, but i do not understand why |
|
All the other examples here make sense and can often be faster than invoking another program. However, in the case of networking, I think it makes sense to depend on the binaries, both for useability and performance. In the case of wget / curl replacements, all of these only work when you have a HTTP endpoint. This code is not going to work for HTTPS. |
I'm trying to PR a working curl / wget (using /dev/tcp)
Yet 'im stuck on the && cat to handle the file body (binary)
I'm sure i can use a new file descriptor && echo , but my bash skill ends here 😞
I can link & use a pure bash [1], yet i'm sure there is something more elegant to do here.
[1] https://unix.stackexchange.com/questions/83926/how-to-download-a-file-using-just-bash-and-nothing-else-no-curl-wget-perl-et
The text was updated successfully, but these errors were encountered: