Skip to content

Instantly share code, notes, and snippets.

@wallentx
Last active February 12, 2024 17:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallentx/6830b633232702ebee7b56b17e21b48e to your computer and use it in GitHub Desktop.
Save wallentx/6830b633232702ebee7b56b17e21b48e to your computer and use it in GitHub Desktop.
If a caveman had to make curl, or wget
#!/usr/bin/env bash
function cavemanget() {
local URL=$1
if [ "$URL" = "" ]; then
echo "A caveman version of wget"
echo "Usage: $0 \"URL\""
return 1
fi
local PROTO="$(echo "$URL" | grep :// | sed -e's,^\(.*://\).*,\1,g')"
local URL_NO_PROTO="$(echo "${URL/$PROTO/}")"
local HOST="$(echo "$URL_NO_PROTO" | cut -d/ -f1)"
local URL_PATH="$(echo "$URL_NO_PROTO" | grep / | cut -d/ -f2-)"
if [[ ! $URL =~ "://" ]]; then
PROTO="http://"
fi
local PORT=80
local OPENSSL=""
if [ "$PROTO" == "https://" ]; then
PORT=443
OPENSSL="1"
fi
if [ "$OPENSSL" = "" ]; then
exec 3<>/dev/tcp/"$HOST/$PORT"
echo -e "GET /${URL_PATH} HTTP/1.1\r\nHost: ${HOST}\r\nConnection: close\r\n\r\n" >&3
else
exec 3< <(echo -e "GET /${URL_PATH} HTTP/1.1\r\nHost: ${HOST}\r\nConnection: close\r\n\r\n" | openssl s_client -quiet -ignore_unexpected_eof -connect "$HOST:$PORT" 2>/dev/null)
fi
local CONTENT_TYPE=""
local FILENAME=""
local DISPOSITION=""
local IS_TEXT=0
local IS_JSON=0
local HTTP_START=0
while read -r line; do
local line_lower=$(echo "$line" | tr '[:upper:]' '[:lower:]')
if [[ $HTTP_START -eq 0 && "$line" =~ ^HTTP/ ]]; then
HTTP_START=1
continue
fi
if [[ $HTTP_START -eq 1 ]]; then
if [[ "$line_lower" =~ ^content-type: ]]; then
CONTENT_TYPE="${line_lower#*: }"
if [[ "$CONTENT_TYPE" =~ text/ ]]; then
IS_TEXT=1
fi
if [[ "$CONTENT_TYPE" =~ application/json ]]; then
IS_TEXT=1
IS_JSON=1
fi
fi
if [[ "$line_lower" =~ ^content-disposition: ]]; then
DISPOSITION="${line#*: }"
if [[ "$DISPOSITION" =~ filename=([^;]+) ]]; then
FILENAME="${BASH_REMATCH[1]}"
fi
fi
if [[ "$line" == $'\r' ]]; then
break
fi
fi
done <&3
if [ "$IS_TEXT" -eq 1 ]; then
if command -v jq >/dev/null && [ "$IS_JSON" -eq 1 ]; then
cat <&3 | jq .
else
cat <&3
fi
else
if [ "$FILENAME" = "" ]; then
FILENAME=$(basename "$URL")
fi
echo "Enter the directory to save the file (or press Enter to use the current directory):"
read -r SAVE_DIR
if [ "$SAVE_DIR" = "" ]; then
SAVE_DIR="."
fi
echo "Enter a filename (or press Enter to use '$FILENAME'):"
read -r USER_FILENAME
if [ "$USER_FILENAME" != "" ]; then
FILENAME="$USER_FILENAME"
fi
local SAVE_PATH="${SAVE_DIR%/}/$FILENAME"
cat <&3 > "$SAVE_PATH"
echo "Saved to $SAVE_PATH"
fi
exec 3>&-
}
cavemanget "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment