wagon/back/lib/http_res

31 lines
792 B
Plaintext
Raw Normal View History

2022-09-06 20:57:41 -06:00
#!/bin/bash
# FILE: wgapi:back/lib/http/res
# DESCRIPTION: Formats an http response from arguments
# USAGE: [printf "message" |] res [200] [text/plain]
2022-09-10 15:07:13 -06:00
body="$(cat)"
printf "${*}\n${body}" >>"${LOGFILE}"
2022-09-06 20:57:41 -06:00
# Parse status
status=''
case "${1}" in
2022-09-10 14:16:49 -06:00
''|'0'|'200') status='Status: 200 OK\n';;
'201') status='Status: 201 Created\n';;
'202') status='Status: 202 Accepted\n';;
'400') status='Status: 400 Bad Request\n';;
'500') status='Status: 500 Internal Server Error\n';;
2022-09-06 20:57:41 -06:00
*) exit 3;;
esac
# Parse inputs
content_type=''
if [ "${2}" == '' ] && [ "${body}" == '' ]
then content_type=''
elif [ "${2}" == '' ]
then content_type='Content-type: text/plain\n'
else content_type="Content-type: ${2}\n"
fi
# Send response
2022-09-10 15:01:50 -06:00
printf "${status}${content_type}Date: $(date)\n\n${body}\n"