Skip to content

Commit

Permalink
Refactor conditional_get.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Scavo committed Oct 18, 2016
1 parent fd2b232 commit 41a6e2e
Show file tree
Hide file tree
Showing 4 changed files with 437 additions and 48 deletions.
87 changes: 51 additions & 36 deletions bin/cget.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,39 @@ display_help () {
This script retrieves and caches HTTP resources on disk.
A previously cached resource is retrieved via HTTP Conditional
GET [RFC 7232]. If the web server responds with HTTP 200 OK,
the resource is cached and written to stdout. OTOH, if the web
server responds with 304 Not Modified, the cache is refreshed
but no output.
the resource is cached and written to stdout. If the web
server responds with 304 Not Modified, the cached resource
is output instead.
Usage: ${0##*/} [-hv] [-F | -C] URL
This script takes a single command-line argument. The URL
argument is the absolute URL of an HTTP resource. By default,
the script requests the resource at the given URL using the
curl command-line tool.
argument is the absolute URL of an HTTP resource. The script
requests the resource at the given URL using the curl
command-line tool.
This script requires two environment variables. CACHE_DIR is
the absolute path to the cache directory (which may or may not
exist) whereas LIB_DIR specifies a directory containing various
helper scripts.
Options:
-h Display this message
-h Display this help message
-v Write verbose messages to stdout
-F Enables "Force Output Mode"
-C Enables "Cache Only Mode"
Option -h is mutually exclusive of all other options.
The default behavior of the script may be modified by using
option -F or -C, which are mutually exclusive. Force Output Mode
outputs the response body even if the server response is 304.
Cache Only Mode bypasses the GET request altogether and goes
directly to cache. If the resource resides in cache, it is
output on stdout, otherwise an error is thrown.
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, an error is thrown.
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 an error is thrown.
LIBRARY
Expand All @@ -66,9 +69,10 @@ display_help () {
EXAMPLES
${0##*/} URL # Retrieve the resource using HTTP conditional GET
${0##*/} -F URL # Enable Force Output Mode
${0##*/} -C URL # Enable Cache Only Mode
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##*/} -C \$url # Enable Cache Only Mode
HELP_MSG
}

Expand All @@ -81,8 +85,7 @@ script_name=${0##*/} # equivalent to basename $0
# library filenames (always list command_paths first)
LIB_FILENAMES="command_paths.sh
compatible_mktemp.sh
http_tools.sh
conditional_get.sh"
http_tools.sh"

#######################################################################
# Process command-line options and arguments
Expand Down Expand Up @@ -182,34 +185,46 @@ for lib_filename in $LIB_FILENAMES; do
fi
done

# create a temporary directory
tmp_dir=$( make_temp_file -d )
if [ ! -d "$tmp_dir" ] ; then
printf "ERROR: $script_name unable to create temporary dir\n" >&2
exit 2
# determine temporary directory
if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ]; then
# use system temporary directory (remove trailing slash)
TMP_DIR="${TMPDIR%%/}/${script_name%%.*}"
$verbose_mode && printf "$script_name using temp dir: %s\n" "$TMP_DIR"
else
# create temporary directory
tmp_dir="$( make_temp_file -d )"
if [ ! -d "$tmp_dir" ] ; then
printf "ERROR: $script_name unable to create temporary dir\n" >&2
exit 2
fi
# use temporary directory (remove trailing slash)
TMP_DIR="${tmp_dir%%/}/${script_name%%.*}"
$verbose_mode && printf "$script_name creating temp dir: %s\n" "$TMP_DIR"
fi
if [ ! -d "$TMP_DIR" ]; then
/bin/mkdir "$TMP_DIR"
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "ERROR: $script_name failed to create dir: $TMP_DIR" >&2
exit $exit_status
fi
fi
$verbose_mode && printf "$script_name creating temp dir: %s\n" "$tmp_dir"

# temporary file
tmp_file="${TMP_DIR}/http_resource_$$.xml"

#######################################################################
# Main processing
#######################################################################

# invoke the function
response_body=$(
conditional_get $local_opts -d "$CACHE_DIR" -t "$tmp_dir" "$location"
)
# get the resource
conditional_get $local_opts -d "$CACHE_DIR" -t "$TMP_DIR" "$location" > "$tmp_file"
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: ${script_name} failed to get resource: $location" >&2
echo "ERROR: $script_name failed to get resource: $location" >&2
printf "See output log: %s\n" "$TMP_DIR/$conditional_get_log" >&2
exit $exit_code
fi

if $verbose_mode; then
/bin/cat "$tmp_dir/conditional_get_log"
else
if [ -n "$response_body" ]; then
echo "$response_body"
fi
fi

/bin/cat "$tmp_file"
exit 0
1 change: 0 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ done <<SOURCES
$script_bin/lib/command_paths.sh
$script_bin/lib/compatible_date.sh
$script_bin/lib/compatible_mktemp.sh
$script_bin/lib/conditional_get.sh
$script_bin/lib/http_tools.sh
$script_bin/lib/md_tools.sh
$script_bin/lib/extract_entity.xsl
Expand Down
32 changes: 22 additions & 10 deletions lib/conditional_get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ fi

#######################################################################
#
# Request a resource using HTTP Conditional GET [RFC 7232] if possible.
# If the server responds with 200, cache the resource and return the
# response body. If the server responds with 304, cache the resource
# but return nothing.
# Given a web resource and a cache, if the resource is cached, request
# the resource using HTTP Conditional GET [RFC 7232], otherwise issue
# an ordinary GET request. In either case, if the server responds with
# 200, cache the resource and return the response body. If the server
# responds with 304, return the cached resource instead.
#
# Usage: conditional_get [-v] [-F | -C] -d CACHE_DIR -t TMP_DIR HTTP_LOCATION
#
Expand All @@ -36,11 +37,20 @@ fi
#
# Options:
# -v verbose mode
# -F force output the HTTP response body even if 304 response
# -C check the cache only (do not request the resource)
# -F force the return of a fresh resource
# -C check the cache only
# -d the cache directory (REQUIRED)
# -t a temporary directory (REQUIRED)
#
# Use option -F or -C to alter the default behavior of the function.
#
# Option -F forces the return of a fresh resource, that is, if the
# server responds with 304, an error occurs and the function returns
# with a nonzero return code.
#
# Option -C causes the function to go directly to cache. No GET request
# is issued. This option is useful in offline mode.
#
# The output of the curl command-line tool is stored in the following
# temporary files:
#
Expand All @@ -58,7 +68,7 @@ fi
# TODO:
# - follow redirects?
#
# This script is compatible with Mac OS and GNU/Linux.
# This script is compatible with both Mac OS and GNU/Linux.
#######################################################################

conditional_get () {
Expand All @@ -68,7 +78,7 @@ conditional_get () {
return 2
fi

local script_version="0.5"
local script_version="0.6"
local user_agent_string="HTTP Conditional GET client $script_version"

local hash
Expand Down Expand Up @@ -367,9 +377,11 @@ conditional_get () {
#
#######################################################################

if [ "$response_code" = "200" ] || $force_output_mode; then
/bin/cat "${cached_content_file}"
if $force_output_mode && [ "$response_code" = "304" ]; then
echo "ERROR: $FUNCNAME failed to get fresh resource: $location" >&2
return 1
fi

/bin/cat "${cached_content_file}"
return 0
}
Loading

0 comments on commit 41a6e2e

Please sign in to comment.