diff --git a/lib/http_tools.sh b/lib/http_tools.sh index e031486..ba073b0 100755 --- a/lib/http_tools.sh +++ b/lib/http_tools.sh @@ -25,7 +25,7 @@ # 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 +# Usage: conditional_get [-v] [-F | -C] -d CACHE_DIR -T TMP_DIR [-L LOG_FILE] HTTP_LOCATION # # This function requires two option arguments (CACHE_DIR and TMP_DIR) # and a command-line argument (HTTP_LOCATION). The rest of the command @@ -37,6 +37,7 @@ # -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. # @@ -83,12 +84,18 @@ conditional_get () { - if [ "$COMMAND_PATHS" != true ]; then - echo "ERROR: $FUNCNAME: global command paths not found" >&2 + if [ "$_COMPATIBILITY_MODE" != true ]; then + echo "ERROR: $FUNCNAME: compatibility mode not enabled" >&2 return 2 fi - local script_version="0.7" + # external dependency + if [ "$(type -t print_log_message)" != function ]; then + echo "ERROR: $FUNCNAME: function print_log_message not found" >&2 + return 2 + fi + + local script_version="0.8" local user_agent_string="HTTP Conditional GET client $script_version" local hash @@ -113,13 +120,13 @@ conditional_get () { local cache_only_mode=false local cache_dir local tmp_dir - local tmp_log_file + local log_file local location local opt local OPTARG local OPTIND - while getopts ":vFCd:T:" opt; do + while getopts ":vFCd:T:L:" opt; do case $opt in v) verbose_mode=true @@ -138,6 +145,9 @@ conditional_get () { T) tmp_dir="$OPTARG" ;; + L) + log_file="$OPTARG" + ;; \?) echo "ERROR: $FUNCNAME: Unrecognized option: -$OPTARG" >&2 return 2 @@ -149,6 +159,16 @@ 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 @@ -158,8 +178,7 @@ conditional_get () { echo "ERROR: $FUNCNAME: directory does not exist: $tmp_dir" >&2 return 2 fi - tmp_log_file="$tmp_dir/${FUNCNAME}_log" - $verbose_mode && echo "$FUNCNAME using temporary directory $tmp_dir" > "$tmp_log_file" + $verbose_mode && print_log_message "$FUNCNAME using temporary directory $tmp_dir" "$log_file" # a cache directory is required if [ -z "$cache_dir" ]; then @@ -170,7 +189,7 @@ conditional_get () { echo "ERROR: $FUNCNAME: directory does not exist: $cache_dir" >&2 return 2 fi - $verbose_mode && echo "$FUNCNAME using cache directory $cache_dir" >> "$tmp_log_file" + $verbose_mode && print_log_message "$FUNCNAME using cache directory $cache_dir" "$log_file" # determine the URL location shift $(( OPTIND - 1 )) @@ -183,7 +202,7 @@ conditional_get () { echo "ERROR: $FUNCNAME: empty URL argument" >&2 return 2 fi - $verbose_mode && echo "$FUNCNAME using location $location" >> "$tmp_log_file" + $verbose_mode && print_log_message "$FUNCNAME using location $location" "$log_file" ####################################################################### # @@ -209,15 +228,16 @@ conditional_get () { cached_content_file="$cache_dir/${hash}_content" if $verbose_mode; then - echo "$FUNCNAME using cached header file: ${cached_header_file}" >> "$tmp_log_file" - echo "$FUNCNAME using cached content file: ${cached_content_file}" >> "$tmp_log_file" + print_log_message "$FUNCNAME using cached header file: ${cached_header_file}" "$log_file" + print_log_message "$FUNCNAME using cached content file: ${cached_content_file}" "$log_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" /bin/cat "$cached_content_file" - return 0 + return fi conditional_get_mode=true @@ -226,7 +246,10 @@ conditional_get () { /bin/rm -f "$cached_header_file" "$cached_content_file" >&2 # quiet failure mode - $cache_only_mode && return 1 + if $cache_only_mode; then + echo "ERROR: $FUNCNAME: resource not cached: $location" >&2 + return 1 + fi conditional_get_mode=false fi @@ -242,9 +265,9 @@ conditional_get () { tmp_stderr_file="$tmp_dir/${FUNCNAME}_curl_stderr" if $verbose_mode; then - echo "$FUNCNAME using temp header file: ${tmp_header_file}" >> "$tmp_log_file" - echo "$FUNCNAME using temp content file: ${tmp_content_file}" >> "$tmp_log_file" - echo "$FUNCNAME using temp stderr file: ${tmp_stderr_file}" >> "$tmp_log_file" + print_log_message "$FUNCNAME using temp header file: ${tmp_header_file}" "$log_file" + print_log_message "$FUNCNAME using temp content file: ${tmp_content_file}" "$log_file" + print_log_message "$FUNCNAME using temp stderr file: ${tmp_stderr_file}" "$log_file" fi ####################################################################### @@ -298,7 +321,7 @@ conditional_get () { # invoke curl cmd="/usr/bin/curl $curl_opts $location" - $verbose_mode && printf "$FUNCNAME issuing curl command: %s\n" "$cmd" >> "$tmp_log_file" + $verbose_mode && print_log_message "$FUNCNAME issuing curl command: $cmd" "$log_file" eval $cmd exit_code=$? if [ $exit_code -ne 0 ]; then @@ -327,7 +350,7 @@ conditional_get () { echo "ERROR: $FUNCNAME: get_response_code failed (return code: $return_code)" >&2 return 7 fi - $verbose_mode && printf "$FUNCNAME received response code: %d\n" "$response_code" >> "$tmp_log_file" + print_log_message "$FUNCNAME received response code: $response_code" "$log_file" if [ "$response_code" = "200" ]; then @@ -352,11 +375,11 @@ conditional_get () { fi if $verbose_mode; then - echo "$FUNCNAME downloaded ${actual_content_length} bytes" >> "$tmp_log_file" + print_log_message "$FUNCNAME downloaded ${actual_content_length} bytes" "$log_file" if $do_conditional_get; then - echo "$FUNCNAME refreshing cache files" >> "$tmp_log_file" + print_log_message "$FUNCNAME refreshing cache files" "$log_file" else - echo "$FUNCNAME initializing cache files" >> "$tmp_log_file" + print_log_message "$FUNCNAME initializing cache files" "$log_file" fi fi @@ -368,6 +391,7 @@ conditional_get () { echo "ERROR: $FUNCNAME failed copy to file $cached_header_file (exit code: $exit_code)" >&2 return 8 fi + print_log_message "$FUNCNAME writing cached content file: ${cached_content_file}" "$log_file" /bin/cp -f "$tmp_content_file" "$cached_content_file" >&2 exit_code=$? if [ $exit_code -ne 0 ]; then @@ -378,9 +402,12 @@ conditional_get () { elif [ "$response_code" = "304" ]; then # quiet failure mode - $force_output_mode && return 1 + if $force_output_mode; then + echo "ERROR: $FUNCNAME: resource not modified: $location" >&2 + return 1 + fi - $verbose_mode && echo "$FUNCNAME downloaded 0 bytes (cache is up-to-date)" >> "$tmp_log_file" + $verbose_mode && print_log_message "$FUNCNAME downloaded 0 bytes (cache is up-to-date)" "$log_file" else echo "ERROR: $FUNCNAME failed with HTTP response code $response_code" >&2 return 9 @@ -392,8 +419,9 @@ conditional_get () { # ####################################################################### + print_log_message "$FUNCNAME reading cached content file: ${cached_content_file}" "$log_file" /bin/cat "$cached_content_file" - return 0 + return } ####################################################################### @@ -413,8 +441,8 @@ conditional_get () { get_response_code () { - if [ "$COMMAND_PATHS" != true ]; then - echo "ERROR: $FUNCNAME: global command paths not found" >&2 + if [ "$_COMPATIBILITY_MODE" != true ]; then + echo "ERROR: $FUNCNAME: compatibility mode not enabled" >&2 return 2 fi @@ -455,8 +483,8 @@ get_response_code () { get_header_value () { - if [ "$COMMAND_PATHS" != true ]; then - echo "ERROR: $FUNCNAME: global command paths not found" >&2 + if [ "$_COMPATIBILITY_MODE" != true ]; then + echo "ERROR: $FUNCNAME: compatibility mode not enabled" >&2 return 2 fi