diff --git a/bin/request_http_resource.sh b/bin/request_http_resource.sh index 161eeda..0f7c52c 100755 --- a/bin/request_http_resource.sh +++ b/bin/request_http_resource.sh @@ -22,21 +22,15 @@ display_help () { /bin/cat <<- HELP_MSG - This script parses one or more SAML metadata aggregates and - produces a JSON file of vital statistics (such as validUntil, - creationInstant, etc.) + This script requests one or more HTTP resources just-in-time. - The script depends on cached metadata. It will not fetch - a metadata file from the server. + Usage: ${0##*/} [-hv] -d OUT_DIR LOCATION ... - Usage: ${0##*/} [-hv] -d OUT_DIR MD_LOCATION ... - - The script takes one or more metadata locations on the - command line. For each location, the corresponding metadata - is read from cache and parsed. The script produces a JSON - array, with one array element for each metadata location. - If successful, the resulting JSON file is finally moved - to the output directory specified on the command line. + The script takes one or more HTTP locations on the command line. + A HEAD request is issued for each location. The script produces + a JSON array, with one array element for each location. The + resulting JSON file is moved to the output directory specified + on the command line. Options: -h Display this help message @@ -79,34 +73,37 @@ display_help () { $( printf " %s\n" ${lib_filenames[*]} ) - Also, the following XSLT file MUST be installed in LIB_DIR: - - $xsl_filename - OUTPUT - The script outputs a JSON file containing a single array. - Each array element is a JavaScript object with the following - fields: + The script outputs a JSON file to OUT_DIR: + + $out_filename + + The JSON file contains a single array. Each array element is + a JavaScript object with the following fields: - location: location - currentTime: dateTime - validUntil: dateTime - creationInstant: dateTime - validityInterval: duration - untilInvalid: duration - sinceCreation: duration + successFlag boolean success or failure? + message string message string + location string HTTP location + ResponseCode string HTTP response code + Date string HTTP response header + LastModified string HTTP response header + ETag string HTTP response header + ContentLength string HTTP response header + ContentType string HTTP response header For example: { + "successFlag": true, + "message": "Integrity of compressed metadata confirmed", "location": "http://md.incommon.org/InCommon/InCommon-metadata.xml", - "currentTime": "2017-06-06T23:57:54Z", - "validUntil": "2017-06-16T18:41:12Z", - "creationInstant": "2017-06-02T18:41:12Z", - "validityInterval": "P14DT0H0M0S", - "untilInvalid": "P9DT18H43M18S", - "sinceCreation": "P4DT5H16M42S" + "ResponseCode": "200", + "Date": "Fri, 09 Jun 2017 20:04:12 GMT", + "LastModified": "Fri, 09 Jun 2017 19:05:16 GMT", + "ETag": "\"80bbff-5518ba6585320\"", + "ContentLength": "8436735", + "ContentType": "application/samlmetadata+xml" } EXAMPLES @@ -177,11 +174,15 @@ for lib_filename in ${lib_filenames[*]}; do fi done +# output filename +out_filename=http_response_headers.json + ####################################################################### # Process command-line options and arguments ####################################################################### -help_mode=false; local_opts= +help_mode=false +local_opts=; curl_opts="--silent" while getopts ":hvd:" opt; do case $opt in h) @@ -190,6 +191,7 @@ while getopts ":hvd:" opt; do v) LOG_LEVEL=4 local_opts="$local_opts -$opt" + curl_opts="--verbose --progress-bar" ;; d) out_dir="$OPTARG" @@ -252,7 +254,6 @@ if [ $status_code -ne 0 ]; then fi # specify temporary files -out_filename=http_response_headers.json out_file="${tmp_dir}/$out_filename" header_file="${tmp_dir}/resource-header.txt" @@ -268,19 +269,25 @@ escape_special_json_chars () { } append_json_object () { + local message=$( escape_special_json_chars "$message" ) local location=$( escape_special_json_chars "$location" ) local response_code=$( escape_special_json_chars "$response_code" ) - local date=$( escape_special_json_chars "$date" ) + local response_date=$( escape_special_json_chars "$response_date" ) local last_modified=$( escape_special_json_chars "$last_modified" ) local e_tag=$( escape_special_json_chars "$e_tag" ) local content_length=$( escape_special_json_chars "$content_length" ) local content_type=$( escape_special_json_chars "$content_type" ) + local boolean_value="true" + ! $success && boolean_value="false" + /bin/cat <<- JSON_OBJECT { + "successFlag": $boolean_value, + "message": "$message", "location": "$location", "ResponseCode": "$response_code", - "Date": "$date", + "Date": "$response_date", "LastModified": "$last_modified", "ETag": "$e_tag", "ContentLength": "$content_length", @@ -289,34 +296,46 @@ append_json_object () { JSON_OBJECT } -parse_metadata () { +get_response () { + + local status_code location="$1" # get resource headers print_log_message -I "$script_name requesting resource: $location" - /usr/bin/curl $curl_opts --silent --head $location > $header_file + /usr/bin/curl $curl_opts --head $location > $header_file status_code=$? if [ $status_code -ne 0 ]; then + success=false + message="Resource request failed" print_log_message -E "$script_name: curl failed ($status_code) on resource: $location" - clean_up_and_exit -d "$tmp_dir" $status_code + return 3 fi + + # success + message="Resource request successful" + return 0 +} + +parse_response () { + + local header_name + local status_code # get the HTTP response code response_code=$( get_response_code $header_file ) status_code=$? if [ $status_code -ne 0 ]; then - print_log_message -E "$script_name: get_response_code failed ($status_code) to parse response header: $header_file" - clean_up_and_exit -d "$tmp_dir" $status_code + print_log_message -E "$script_name: get_response_code failed ($status_code) to parse response code" fi # get the Date response header header_name=Date - date=$( get_header_value $header_file $header_name ) + response_date=$( get_header_value $header_file $header_name ) status_code=$? if [ $status_code -ne 0 ]; then - print_log_message -E "$script_name: get_header_value failed ($status_code) to parse response header: $header_file" - clean_up_and_exit -d "$tmp_dir" $status_code + print_log_message -E "$script_name: get_header_value failed ($status_code) to parse response header: $header_name" fi # get the Last-Modified response header @@ -324,8 +343,7 @@ parse_metadata () { last_modified=$( get_header_value $header_file $header_name ) status_code=$? if [ $status_code -ne 0 ]; then - print_log_message -E "$script_name: get_header_value failed ($status_code) to parse response header: $header_file" - clean_up_and_exit -d "$tmp_dir" $status_code + print_log_message -E "$script_name: get_header_value failed ($status_code) to parse response header: $header_name" fi # get the ETag response header @@ -333,8 +351,7 @@ parse_metadata () { e_tag=$( get_header_value $header_file $header_name ) status_code=$? if [ $status_code -ne 0 ]; then - print_log_message -E "$script_name: get_header_value failed ($status_code) to parse response header: $header_file" - clean_up_and_exit -d "$tmp_dir" $status_code + print_log_message -E "$script_name: get_header_value failed ($status_code) to parse response header: $header_name" fi # get the Content-Length response header @@ -342,8 +359,7 @@ parse_metadata () { content_length=$( get_header_value $header_file $header_name ) status_code=$? if [ $status_code -ne 0 ]; then - print_log_message -E "$script_name: get_header_value failed ($status_code) to parse response header: $header_file" - clean_up_and_exit -d "$tmp_dir" $status_code + print_log_message -E "$script_name: get_header_value failed ($status_code) to parse response header: $header_name" fi # get the Content-Type response header @@ -351,19 +367,26 @@ parse_metadata () { content_type=$( get_header_value $header_file $header_name ) status_code=$? if [ $status_code -ne 0 ]; then - print_log_message -E "$script_name: get_header_value failed ($status_code) to parse response header: $header_file" - clean_up_and_exit -d "$tmp_dir" $status_code + print_log_message -E "$script_name: get_header_value failed ($status_code) to parse response header: $header_name" fi + + return 0 } -print_response_headers () { +print_output_file () { # begin output list printf "[\n" while true; do - parse_metadata "$1" + success=true + + get_response "$1" + status_code=$? + if [ $status_code -eq 0 ]; then + parse_response + fi append_json_object shift; (( "$#" )) || break @@ -383,7 +406,7 @@ print_response_headers () { print_log_message -I "$script_name BEGIN" # create the JSON output -print_response_headers "$@" > "$out_file" +print_output_file "$@" > "$out_file" print_log_message -I "$script_name writing output file: $out_filename" # move the output file to the web directory