Newer
Older
invertedlogic / Scripts / wget.sh
#!/bin/bash
#===============================================================================
#
#          FILE:  wget.sh
# 
#         USAGE:  ./wget.sh URL 
# 
#   DESCRIPTION:  Simplified version of wget written as a shell script.
#                 Might be useful for bootstrapping a system or for
#                 debugging a HTTP server or for educational purposes.
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  John Ryland (JR), (jryland@invertedlogic.com)
#       COMPANY:  InvertedLogic
#       VERSION:  1.0
#       CREATED:  10/07/2009
#      REVISION:  ---
#===============================================================================

function show_help
{
  echo
  echo "Usage:  $0 [flags] URL"
  echo "Flags:"
  echo "          -v         verbose"
  echo "          -f         force overwriting of existing files"
  echo "          -o file    specify output filename (default is name on server)"
  echo "          --help     displays this help"
  echo
  exit
}

force=0
verbose=0
output=""

for (( i=0 ; $i < $# ; i++ ))
do
  var="${@:$((i+1)):1}"
  # echo "argument -$i- => -$var-"
  case $var in
    -f) force=1 ;;
    -v) verbose=1 ;;
    -o) shift ; output="${@:$((i+1)):1}" ;;
    --help) show_help ;;
    -?) show_help ;;
    *) url=$var ;;
  esac
done

[ "$url" == "" ] && echo && echo "Error:  No URL specified" && show_help
protocol=`echo $url | cut -d ':' -f 1`
method="GET"
server=`echo $url | cut -d '/' -f 3`
path_offset=`expr ${#protocol} + ${#server} + 4`
path=`echo $url | cut -c $path_offset-4000`
[ "$protocol" == "" ] && echo && echo "Error:  Invalid URL" && show_help
[ "$server" == "" ]   && echo && echo "Error:  Invalid URL" && show_help
[ "$path" == "" ]     && path="/"
[ "$output" == "" ]   && output=`basename $path`
[ "$output" == "/" ]  && output="index.html"

function debug
{
  [ "$verbose" == "1" ] && echo "$@"
}

function filter_header
{
  read status
  read line
  # If there is an error, the status code will not be "OK", turn on debug 
  [ `echo "$status" | grep -c "OK"` != "1" ] && verbose=1
  debug ""
  debug "Protocol: $protocol"
  debug "Method:   $method"
  debug "Server:   $server"
  debug "Path:     $path"
  debug "Output:   $output"
  debug "Status:   $status"
  debug "Header:   $line"
  while read line ; do [ "$line" == "
" ] && break ; debug "          $line" ; done
  while read line ; do echo $line ; done > $output
  debug ""
}

function do_request
{
nc $server 80 << EOF
$method $path HTTP/1.1
Host: $server

EOF
}

if [ "$force" == "0" ]
then
  if [ -f "$output" ]
  then
    echo "Bailing out because output file $output exists."
    echo "Use -f option to force overwriting existing files"
    echo "or -o to specify an alternative output filename."
    exit
  fi
fi

do_request | filter_header