Skip to content

Commit

Permalink
Remove -L LOG_FILE option
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Scavo committed May 1, 2017
1 parent 3b8b1da commit d214d76
Showing 1 changed file with 34 additions and 49 deletions.
83 changes: 34 additions & 49 deletions lib/http_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
# -C check the cache only
# -d the cache directory (REQUIRED)
# -T a temporary directory (REQUIRED)
# -L a log file
#
# Use option -F or -C to alter the default behavior of the function.
#
Expand Down Expand Up @@ -120,13 +119,12 @@ conditional_get () {
local cache_only_mode=false
local cache_dir
local tmp_dir
local log_file
local location

local opt
local OPTARG
local OPTIND
while getopts ":vFCd:T:L:" opt; do
while getopts ":vFCd:T:" opt; do
case $opt in
v)
verbose_mode=true
Expand All @@ -145,9 +143,6 @@ conditional_get () {
T)
tmp_dir="$OPTARG"
;;
L)
log_file="$OPTARG"
;;
\?)
echo "ERROR: $FUNCNAME: Unrecognized option: -$OPTARG" >&2
return 2
Expand All @@ -159,16 +154,6 @@ conditional_get () {
esac
done

# a log file is optional
if [ -z "$log_file" ]; then
log_file=/dev/null
else
if [ ! -f "$log_file" ]; then
echo "ERROR: $FUNCNAME: log file does not exist: $log_file" >&2
return 2
fi
fi

# a temporary directory is required
if [ -z "$tmp_dir" ]; then
echo "ERROR: $FUNCNAME: no temporary directory specified" >&2
Expand All @@ -178,7 +163,7 @@ conditional_get () {
echo "ERROR: $FUNCNAME: directory does not exist: $tmp_dir" >&2
return 2
fi
$verbose_mode && print_log_message -D "$FUNCNAME using temporary directory $tmp_dir" "$log_file"
$verbose_mode && print_log_message -D "$FUNCNAME using temporary directory $tmp_dir"

# a cache directory is required
if [ -z "$cache_dir" ]; then
Expand All @@ -189,7 +174,7 @@ conditional_get () {
echo "ERROR: $FUNCNAME: directory does not exist: $cache_dir" >&2
return 2
fi
$verbose_mode && print_log_message -D "$FUNCNAME using cache directory $cache_dir" "$log_file"
$verbose_mode && print_log_message -D "$FUNCNAME using cache directory $cache_dir"

# determine the URL location
shift $(( OPTIND - 1 ))
Expand All @@ -202,7 +187,7 @@ conditional_get () {
echo "ERROR: $FUNCNAME: empty URL argument" >&2
return 2
fi
$verbose_mode && print_log_message -D "$FUNCNAME using location $location" "$log_file"
$verbose_mode && print_log_message -D "$FUNCNAME using location $location"

#######################################################################
#
Expand All @@ -220,26 +205,26 @@ conditional_get () {
)
exit_code=$?
if [ $exit_code -ne 0 ]; then
print_log_message -E "$FUNCNAME failed to hash the location URL (exit code: $exit_code)" "$log_file"
print_log_message -E "$FUNCNAME failed to hash the location URL (exit code: $exit_code)"
return 4
fi

cached_header_file="$cache_dir/${hash}_headers"
cached_content_file="$cache_dir/${hash}_content"

if $verbose_mode; then
print_log_message -D "$FUNCNAME using cached header file: $cached_header_file" "$log_file"
print_log_message -D "$FUNCNAME using cached content file: $cached_content_file" "$log_file"
print_log_message -D "$FUNCNAME using cached header file: $cached_header_file"
print_log_message -D "$FUNCNAME using cached content file: $cached_content_file"
fi

# check if the resource is cached
if [ -f "$cached_header_file" ] && [ -f "$cached_content_file" ]; then
if $cache_only_mode; then
print_log_message "$FUNCNAME reading cached content file: $cached_content_file" "$log_file"
print_log_message "$FUNCNAME reading cached content file: $cached_content_file"
/bin/cat "$cached_content_file"
exit_code=$?
if [ $exit_code -ne 0 ]; then
print_log_message -E "$FUNCNAME unable to cat output ($exit_code)" "$log_file"
print_log_message -E "$FUNCNAME unable to cat output ($exit_code)"
return 3
fi
return 0
Expand All @@ -252,7 +237,7 @@ conditional_get () {

# quiet failure mode
if $cache_only_mode; then
print_log_message -E "$FUNCNAME: resource not cached: $location" "$log_file"
print_log_message -E "$FUNCNAME: resource not cached: $location"
return 1
fi

Expand All @@ -270,9 +255,9 @@ conditional_get () {
tmp_stderr_file="$tmp_dir/${FUNCNAME}_curl_stderr"

if $verbose_mode; then
print_log_message -D "$FUNCNAME using temp header file: ${tmp_header_file}" "$log_file"
print_log_message -D "$FUNCNAME using temp content file: ${tmp_content_file}" "$log_file"
print_log_message -D "$FUNCNAME using temp stderr file: ${tmp_stderr_file}" "$log_file"
print_log_message -D "$FUNCNAME using temp header file: ${tmp_header_file}"
print_log_message -D "$FUNCNAME using temp content file: ${tmp_content_file}"
print_log_message -D "$FUNCNAME using temp stderr file: ${tmp_stderr_file}"
fi

#######################################################################
Expand Down Expand Up @@ -304,7 +289,7 @@ conditional_get () {
header_value=$( get_header_value "$cached_header_file" 'ETag' )
return_code=$?
if [ $return_code -ne 0 ]; then
print_log_message -E "$FUNCNAME: get_header_value (return code: $return_code)" "$log_file"
print_log_message -E "$FUNCNAME: get_header_value (return code: $return_code)"
return 6
fi
if [ -n "$header_value" ]; then
Expand All @@ -314,7 +299,7 @@ conditional_get () {
header_value=$( get_header_value "$cached_header_file" 'Last-Modified' )
return_code=$?
if [ $return_code -ne 0 ]; then
print_log_message -E "$FUNCNAME: get_header_value (return code: $return_code)" "$log_file"
print_log_message -E "$FUNCNAME: get_header_value (return code: $return_code)"
return 6
fi
if [ -n "$header_value" ]; then
Expand All @@ -326,11 +311,11 @@ conditional_get () {

# invoke curl
cmd="/usr/bin/curl $curl_opts $location"
$verbose_mode && print_log_message -D "$FUNCNAME issuing curl command: $cmd" "$log_file"
$verbose_mode && print_log_message -D "$FUNCNAME issuing curl command: $cmd"
eval $cmd
exit_code=$?
if [ $exit_code -ne 0 ]; then
print_log_message -E "$FUNCNAME: curl failed (exit code: $exit_code)" "$log_file"
print_log_message -E "$FUNCNAME: curl failed (exit code: $exit_code)"
return 5
fi

Expand All @@ -345,25 +330,25 @@ conditional_get () {

# sanity check
if [ ! -f "$tmp_header_file" ]; then
print_log_message -E "$FUNCNAME unable to find header file $tmp_header_file" "$log_file"
print_log_message -E "$FUNCNAME unable to find header file $tmp_header_file"
return 3
fi

response_code=$( get_response_code "$tmp_header_file" )
return_code=$?
if [ $return_code -ne 0 ]; then
print_log_message -E "$FUNCNAME: get_response_code failed (return code: $return_code)" "$log_file"
print_log_message -E "$FUNCNAME: get_response_code failed (return code: $return_code)"
return 7
fi
print_log_message "$FUNCNAME received response code: $response_code" "$log_file"
print_log_message "$FUNCNAME received response code: $response_code"

if [ "$response_code" = "200" ]; then

# sanity check
declared_content_length=$( get_header_value "$tmp_header_file" 'Content-Length' )
return_code=$?
if [ $return_code -ne 0 ]; then
print_log_message -E "$FUNCNAME: get_header_value failed (return code: $return_code)" "$log_file"
print_log_message -E "$FUNCNAME: get_header_value failed (return code: $return_code)"
return 6
fi
actual_content_length=$( /bin/cat "$tmp_content_file" \
Expand All @@ -372,19 +357,19 @@ conditional_get () {
)
if [ -n "$declared_content_length" ]; then
if [ "$declared_content_length" != "$actual_content_length" ]; then
print_log_message -E "$FUNCNAME failed content length check" "$log_file"
print_log_message -E "$FUNCNAME failed content length check"
return 3
fi
else
print_log_message -W "$FUNCNAME: Content-Length response header missing" "$log_file"
print_log_message -W "$FUNCNAME: Content-Length response header missing"
fi

if $verbose_mode; then
print_log_message -D "$FUNCNAME downloaded ${actual_content_length} bytes" "$log_file"
print_log_message -D "$FUNCNAME downloaded ${actual_content_length} bytes"
if $do_conditional_get; then
print_log_message -D "$FUNCNAME refreshing cache files" "$log_file"
print_log_message -D "$FUNCNAME refreshing cache files"
else
print_log_message -D "$FUNCNAME initializing cache files" "$log_file"
print_log_message -D "$FUNCNAME initializing cache files"
fi
fi

Expand All @@ -393,28 +378,28 @@ conditional_get () {
exit_code=$?
if [ $exit_code -ne 0 ]; then
/bin/rm -f "$cached_header_file" "$cached_content_file" >&2
print_log_message -E "$FUNCNAME failed copy to file $cached_header_file (exit code: $exit_code)" "$log_file"
print_log_message -E "$FUNCNAME failed copy to file $cached_header_file (exit code: $exit_code)"
return 8
fi
print_log_message "$FUNCNAME writing cached content file: ${cached_content_file}" "$log_file"
print_log_message "$FUNCNAME writing cached content file: ${cached_content_file}"
/bin/cp -f "$tmp_content_file" "$cached_content_file" >&2
exit_code=$?
if [ $exit_code -ne 0 ]; then
/bin/rm -f "$cached_header_file" "$cached_content_file" >&2
print_log_message -E "$FUNCNAME failed copy to file $cached_content_file (exit code: $exit_code)" "$log_file"
print_log_message -E "$FUNCNAME failed copy to file $cached_content_file (exit code: $exit_code)"
return 8
fi

elif [ "$response_code" = "304" ]; then
# quiet failure mode
if $force_output_mode; then
print_log_message -E "$FUNCNAME: resource not modified: $location" "$log_file"
print_log_message -E "$FUNCNAME: resource not modified: $location"
return 1
fi

$verbose_mode && print_log_message -D "$FUNCNAME downloaded 0 bytes (cache is up-to-date)" "$log_file"
$verbose_mode && print_log_message -D "$FUNCNAME downloaded 0 bytes (cache is up-to-date)"
else
print_log_message -E "$FUNCNAME failed with HTTP response code $response_code" "$log_file"
print_log_message -E "$FUNCNAME failed with HTTP response code $response_code"
return 9
fi

Expand All @@ -424,11 +409,11 @@ conditional_get () {
#
#######################################################################

print_log_message "$FUNCNAME reading cached content file: ${cached_content_file}" "$log_file"
print_log_message "$FUNCNAME reading cached content file: ${cached_content_file}"
/bin/cat "$cached_content_file"
exit_code=$?
if [ $exit_code -ne 0 ]; then
print_log_message -E "$FUNCNAME unable to cat output ($exit_code)" "$log_file"
print_log_message -E "$FUNCNAME unable to cat output ($exit_code)"
return 3
fi
return 0
Expand Down

0 comments on commit d214d76

Please sign in to comment.