#!/bin/bash
# ================================================================================
#
# CHECK HTTP
#
#
# -------------------------------------------------------------------------------
# 2023-09-05  v0.1  <axel.hahn@unibe.ch>
# ================================================================================

. $( dirname $0 )/inc_pluginfunctions

export self_APPVERSION=1.0

# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------

# show help text
function showHelp(){
    local _self; _self=$(basename $0)
cat <<EOF
$( ph.showImlHelpHeader )

Makes an http request to chewck the response by
- http status code
- content in http response header
- content in http response body

SYNTAX:
  $_self [-h] ...

OPTIONS:

  -h               this help

PARAMETERS:

  Define request:
  -u URL           Set url to fetch
  -m METHOD        Set a method, eg. HEAD; default: GET

  What to check:
  -s STATUSCODE    Statuscode to ceck
  -r REGEX         String to check in http response header
  -j JQ-FILTER     for JSON Response: filter data by a jq
  -b REGEX         String to check in response body 

  Output:
  -l LABEL         set a custom label; default: METHOD + URL eg.
                   "GET https://example.com/status"

EXAMPLES:

  $_self ...

EOF
}

# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

ph.hasParamoption "h" "$@"; bOptHelp=$?

if [ $bOptHelp -eq 0 -o $# -eq 0 ]; then
    showHelp
    exit 0
fi

ph.require "curl"

sUrl=$(     ph.getValueWithParam ''    u "$@")
sMethod=$(  ph.getValueWithParam 'GET' m "$@" | tr [:lower:] [:upper:])

iStatus=$(  ph.getValueWithParam '200' s "$@")
sHeader=$(  ph.getValueWithParam ''    r "$@")
sBody=$(    ph.getValueWithParam ''    b "$@")
sJq=$(      ph.getValueWithParam ''    j "$@")
sLabel=$(   ph.getValueWithParam "$sMethod $sUrl"    l "$@")


curlParams="-si"
sProblems=
sOK=

if [ -z "$sUrl" ]; then
    ph.setStatus unknown
    ph.status "Wrong parameters - no url was given."
    ph.exit
fi
 
# test -z "$sBody"  && curlParams+=" -I"


out=$( curl $curlParams "$sUrl" )


iHeaderEnd=$( echo "$out" | grep -n ^$'\r' | cut -f 1 -d ':')

_header=$(echo "$out" | sed -n "1,${iHeaderEnd}p")
_body=$(  echo "$out" | sed -n "${iHeaderEnd},\$p")

if [ -n "$sJq" ]; then
    _body=$( jq "$sJq" <<< "$_body" 2>/dev/null )
fi

# echo "HEADER"; echo "$_header"
# echo "BODY"; echo "$_body"


# --- test status
if [ -n "$iStatus" ]; then
    if ! grep -i "^HTTP/[0-9\.]* ${iStatus}" <<< "${_header}" >/dev/null; then
        ph.setStatus critical
        sProblems+="- Http status is not [${iStatus}];\n"
    else
        sOK+="- Http status is [${iStatus}];\n"
    fi
fi

# --- search in http response header
if [ -n "$sHeader" ]; then
    if ! grep -iE "$sHeader" <<< "${_header}" >/dev/null; then
        ph.setStatus critical
        sProblems+="- Header does not contain [${sHeader}];\n"
    else
        sOK+="- [${sHeader}] was found in header;\n"
    fi
    
fi

# --- search in http response body
if [ -n "$sBody" ]; then
    if ! grep -iE "$sBody" <<< "${_body}" >/dev/null; then
        ph.setStatus critical
        sProblems+="- Body does not contain [${sBody}];\n"
    else
        sOK+="- [${sBody}] was found in body;\n"
    fi
    
fi

# --- output
test -n "$sProblems" && sProblems="Problems:\n$sProblems\n"
test -n "$sOK"       && sOK="Found:\n$sOK"

ph.status "$sLabel"

if [ "$sLabel" != "$sMethod $sUrl" ]; then
    echo "$sMethod $sUrl"
fi
echo
echo -e "${sProblems}${sOK}"

echo "HEADER:"; echo; echo "$_header"
echo -n "Eapsed time: "; ph.showtimer

ph.exit

# ----------------------------------------------------------------------