wagon/back/lib/http_res

34 lines
938 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:09:39 -06:00
# Accept stdin as body
2022-09-10 15:07:13 -06:00
body="$(cat)"
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';;
2022-09-10 15:09:39 -06:00
'409') status='Status: 409 Conflict\n';;
2022-09-10 14:16:49 -06:00
'500') status='Status: 500 Internal Server Error\n';;
2022-09-06 20:57:41 -06:00
*) exit 3;;
esac
2022-09-10 15:09:39 -06:00
# Parse content-type
2022-09-06 20:57:41 -06:00
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-11-07 17:28:29 -07:00
# Ignore shellcheck trying to get you to put this in multiple args
# It'll return literal \n instead of newlines
printf "${status}${content_type}Date: $(date)\n\n${body}\n"