#!/bin/bash

### HTTP Header Display
### by Michael Lee
### v1.1 2007-07-20

###   Introduction
#
#  Using wget, this script queries an HTTP/HTTPS server and displays the
#  resulting headers, following redirections as appropriate.  Wget v1.10.2
#  or higher is recommended, but older versions can be used by disabling
#  (commenting out) unavailable features.
#

###   Installation
#
#  0. Install wget!
#  1. Place this script somewhere in your executable PATH.
#

###   Usage
#
#  This script takes a single argument: the target URL.  The script will display
#  the headers and other information associated with the request, and optionally
#  save the body of the server's reply, depending on the configuration below.
#

###   License
#
#  Copyright (C) 2007  Michael Lee <kirbysdl@hotmail.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#



# ------------------------------------------------------------------------------
#   Configuration
# ------------------------------------------------------------------------------

# Wget's self-reported identity to the server.  By default, MSIE 6 on Windows XP
# is used.  Alternately, (1) specify another browser's UA string, (2) send 
# nothing, or (3) expose wget's identity.
#  default UserAgent="--user-agent=\"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\""
#  (1)     UserAgent="--user-agent=\"Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4\""
#  (2)     UserAgent="--user-agent=\"\""
#  (3)     UserAgent=""
UserAgent="--user-agent=\"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\""

# Certificate storage location for verifying a server's SSL identity.
# Leave blank to have httphead attempt to find a certificate cache, or give a
# wget option with argument, such as:
#  Certs="--ca-directory=/home/myself/ssl/cert"
#  Certs="--ca-certificate=/etc/customssl/cert-bundle.crt"
Certs=""

# Value of referer header in HTTP request. By default, the same as the requested
# URL.  Alternately, (1) a custom URL or (2) blank.
#   default Referer="--referer=$1"
#   (1)     Referer="--referer=http://example.com/somefile.html"
#   (2)     Referer=""
Referer="--referer=$1"

# Determines how output should be stored.  By default, a HEAD request is sent,
# and the body of the reply is not retrieved.  Some servers might send different
# response headers depending on the request method, so a GET request may be
# preferable.  In that case, you can (1) send the body to /dev/null, (2) delete
# the file after downloading, (3) specify a custom filename, or (4) leave this
# blank to save to wget's default location.
#   Default Output="--spider"
#   (1)     Output="-O /dev/null"
#   (2)     Output="--delete-after"
#   (3)     Output="-O /home/myself/filename.html"
#   (4)     Output=""
Output="--spider"



# ------------------------------------------------------------------------------
#   Stuff Happens Here
# ------------------------------------------------------------------------------

if [[ "$#" == "0" ]]; then
  echo `basename $0`: You must specify the URL to retrieve
  exit 1
fi

if [[ "$#" != "1" ]]; then
  echo Usage: `basename $0` URL
  exit 2
fi

# Find certs
if [[ "z$Certs" == "z" ]]; then
  # works in Linux, but may require installation of a ca-certificates package
  # and execution of c_rehash
  if [[ -d "/etc/ssl/certs" ]]; then
    Certs="--ca-directory=/etc/ssl/certs"
  # works in Mac OS X, Cygwin, and Linux when curl is installed
  elif [[ -r "/usr/share/curl/curl-ca-bundle.crt" ]]; then
    Certs="--ca-certificate=/usr/share/curl/curl-ca-bundle.crt"
  # default: skip certificate validity check (required to avoid errors)
  else
    Certs="--no-check-certificate"
  fi
fi

# Display configuration
echo -e "Agent	$UserAgent"
echo -e "Certs	$Certs"
echo -e "Referer	$Referer"
echo -e "Output	$Output"
echo -e "Target	$1"
echo

# Do it!
# We use xargs because otherwise wget balks at the useragent specification
time echo $UserAgent $Certs $Referer $Output -S "$1" | xargs wget
