Skip to content

Instantly share code, notes, and snippets.

@myl7
Last active February 14, 2022 06:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save myl7/f74a1fb95d49954092e6822a22d469c2 to your computer and use it in GitHub Desktop.
Save myl7/f74a1fb95d49954092e6822a22d469c2 to your computer and use it in GitHub Desktop.
Zero-dependency HTTP request script from https://www.v2ex.com/t/811424
#!/bin/bash
set -euo pipefail
# From https://www.v2ex.com/t/811424
function __curl() {
read proto server path <<<$(echo ${1//// })
DOC=/${path// //}
HOST=${server//:*}
PORT=${server//*:}
[[ x"${HOST}" == x"${PORT}" ]] && PORT=80
exec 3<>/dev/tcp/${HOST}/$PORT
echo -en "GET ${DOC} HTTP/1.0\r\nHost: ${HOST}\r\n\r\n" >&3
(while read line; do
[[ "$line" == $'\r' ]] && break
done && cat) <&3
exec 3>&-
}
if [ -x "$(command -v curl)" ]; then
curl -o /dev/null $1
elif [ -x "$(command -v wget)" ]; then
wget -q -O- $1
else
__curl $1 >/dev/null
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment