Created
June 15, 2021 17:09
-
-
Save rojenzaman/95b3c2af2f40885cfe2855a2742e8a25 to your computer and use it in GitHub Desktop.
HTTP Cat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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