Skip to content

Instantly share code, notes, and snippets.

@Esonhugh
Forked from myl7/req.sh
Created November 15, 2021 08:22
Show Gist options
  • Save Esonhugh/325067d780710804783f1cffe4a491d1 to your computer and use it in GitHub Desktop.
Save Esonhugh/325067d780710804783f1cffe4a491d1 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
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