Skip to content

Instantly share code, notes, and snippets.

@rojenzaman
Created Jun 15, 2021
Embed
What would you like to do?
HTTP Cat
#!/bin/bash
#
# Usage:
# httpcat URL
if [ "$#" -lt 1 ]; then
echo "Usage: `basename $0` URL"
exit 1
fi
read proto server path <<< "${1//"/"/ }"
DOC=/${path// //}
HOST=${server//:*}
PORT=${server//*:}
[[ x"${HOST}" == x"${PORT}" ]] && PORT=80
exec 3<>/dev/tcp/${HOST}/$PORT
# send request
echo -en "GET ${DOC} HTTP/1.0\r\nHost: ${HOST}\r\n\r\n" >&3
# read the header, it ends in a empty line (just CRLF)
while IFS= read -r line ; do
[[ "$line" == $'\r' ]] && break
done <&3
# read the data
nul='\0'
while IFS= read -d '' -r x || { nul=""; [ -n "$x" ]; }; do
printf "%s$nul" "$x"
done <&3
exec 3>&-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment