wagon/back/lib/http_res

28 lines
756 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]
# Parse status
status=''
case "${1}" in
2022-09-10 14:11:42 -06:00
''|'0'|'200') status='HTTP/1.0 200 OK\n';;
'201') status='HTTP/1.0 201 Created\n';;
'202') status='HTTP/1.0 202 Accepted\n';;
'400') status='HTTP/1.0 400 Bad Request\n';;
'500') status='HTTP/1.0 500 Internal Server Error\n';;
2022-09-06 20:57:41 -06:00
*) exit 3;;
esac
# Parse inputs
body="$(cat)"
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 14:11:42 -06:00
printf "${status}${content_type}\nDate:$(date)\n${body}\n"