Skip to content

Instantly share code, notes, and snippets.

@rojenzaman
Created June 15, 2021 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rojenzaman/95b3c2af2f40885cfe2855a2742e8a25 to your computer and use it in GitHub Desktop.
Save rojenzaman/95b3c2af2f40885cfe2855a2742e8a25 to your computer and use it in GitHub Desktop.
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