From f18200bf1b9b9d1a3f2ad042672542655477ba98 Mon Sep 17 00:00:00 2001
From: Tom Scavo <trscavo@internet2.edu>
Date: Sat, 17 Jun 2017 16:37:59 -0400
Subject: [PATCH] Add options for HEAD requests and HTTP Compression

---
 bin/cget.sh | 52 +++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 41 insertions(+), 11 deletions(-)

diff --git a/bin/cget.sh b/bin/cget.sh
index a3d2539..d8d6487 100755
--- a/bin/cget.sh
+++ b/bin/cget.sh
@@ -29,7 +29,7 @@ display_help () {
 	server responds with 304 Not Modified, the cached resource 
 	is output instead.
 	
-	Usage: ${0##*/} [-hvq] [-F | -C] URL
+	Usage: ${0##*/} [-hvqFCIx] URL
 	
 	This script takes a single command-line argument. The URL 
 	argument is the absolute URL of an HTTP resource. The script 
@@ -39,24 +39,41 @@ display_help () {
 	Options:
 	   -h      Display this help message
 	   -v      Log verbose messages
-	   -q      Log no messages
-	   -F      Enables "Force Output Mode"
-	   -C      Enables "Cache Only Mode"
+	   -q      Log no messages other than warnings or errors
+	   -F      Enable "Fresh Content Mode"
+	   -C      Enable "Cache Only Mode"
+	   -I      Enable "Header Only Mode"
+	   -x      Enable "Compressed Mode"
 
 	Option -h is mutually exclusive of all other options.
 	
 	The default behavior of the script is modified by using 
-	option -F or -C, which are mutually exclusive. Force Output 
-	Mode (option -F) forces the return of a fresh resource. The 
-	resource is output on stdout if and only if the server 
-	responds with 200. If the response is 304, the script silently
-	fails with exit code 1.
+	option -F, -C, or -I. Options -F and -C are mutually exclusive 
+	of each other. Option -I may be used with option -C (but not 
+	with option -F).
+	
+	Fresh Content Mode (option -F) forces the return of a fresh resource. 
+	The resource is output on stdout if and only if the server responds 
+	with 200. If the response is 304, the script silently fails with 
+	status code 1.
 	 
 	Cache Only Mode (option -C) bypasses the GET request altogether 
 	and goes directly to cache. If the resource resides in cache, 
 	it is output on stdout, otherwise the script silently fails
 	with exit code 1.
 	
+	Header Only Mode (option -I) issues a HEAD request instead of a GET 
+	request, in which case, only the response headers are returned in the 
+	output. Note that nothing is written to cache when option -I is used.
+	
+	Compressed Mode (option -x) enables HTTP Compression by adding an 
+	Accept-Encoding header to the request; that is, if option -x is 
+	enabled, the client merely indicates its support for HTTP Compression 
+	in the request. The server may or may not compress the response.
+	
+	Important! This implementation treats compressed and uncompressed 
+	requests for the same resource as two distinct resources.
+	
 	ENVIRONMENT
 	
 	This script leverages a handful of environment variables:
@@ -82,8 +99,15 @@ display_help () {
 	
 	  \$ url=http://md.incommon.org/InCommon/InCommon-metadata.xml
 	  \$ ${0##*/} \$url      # Retrieve the resource using HTTP conditional GET
-	  \$ ${0##*/} -F \$url   # Enable Force Output Mode
+	  \$ ${0##*/} -F \$url   # Enable Fresh Content Mode
 	  \$ ${0##*/} -C \$url   # Enable Cache Only Mode
+	  \$ ${0##*/} -x \$url   # Enable Compressed Mode
+	  
+	Note that the first and last examples result in distinct cached
+	resources. The content of a compressed resource will be the 
+	same as the content of an uncompressed resource but the headers 
+	will be different. In particular, a compressed header will include
+	a Content-Encoding header.
 HELP_MSG
 }
 
@@ -136,7 +160,7 @@ http_tools.sh"
 #######################################################################
 
 help_mode=false; local_opts=
-while getopts ":hvqFC" opt; do
+while getopts ":hvqFCIx" opt; do
 	case $opt in
 		h)
 			help_mode=true
@@ -154,6 +178,12 @@ while getopts ":hvqFC" opt; do
 		C)
 			local_opts="$local_opts -$opt"
 			;;
+		I)
+			local_opts="$local_opts -$opt"
+			;;
+		x)
+			local_opts="$local_opts -$opt"
+			;;
 		\?)
 			echo "ERROR: $script_name: Unrecognized option: -$OPTARG" >&2
 			exit 2