+ # Store hex string from stdin
+ tmphex="$(cat)"
+
+ # Remove spaces
+ hex=''
+ for ((i=0; i<${#tmphex}; i+=1)); do
+ test "${tmphex:$i:1}" == " " || hex="${hex}${tmphex:$i:1}"
+ done
+
+ # Add leading zero
+ test $((${#hex} & 1)) == 0 || hex="0${hex}"
+
+ # Convert to escaped string
+ escapedhex=''
+ for ((i=0; i<${#hex}; i+=2)); do
+ escapedhex=$escapedhex\\x${hex:$i:2}
+ done
+
+ # Convert to binary data
+ printf "${escapedhex}"
+}
+
+_request() {
+ tempcont="$(mktemp)"
+
+ if [[ "${1}" = "head" ]]; then
+ statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}" -I)"
+ elif [[ "${1}" = "get" ]]; then
+ statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}")"
+ elif [[ "${1}" = "post" ]]; then
+ statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}" -d "${3}")"
+ fi
+
+ if [[ ! "${statuscode:0:1}" = "2" ]]; then
+ echo " + ERROR: An error occured while sending ${1}-request to ${2} (Status ${statuscode})" >&2
+ echo >&2
+ echo "Details:" >&2
+ echo "$(<"${tempcont}"))" >&2
+ rm -f "${tempcont}"
+ exit 1
+ fi
+
+ cat "${tempcont}"
+ rm -f "${tempcont}"