#!/bin/bash # FILE: wgapi:back/lib/http/res # DESCRIPTION: Formats an http response from arguments # USAGE: [printf "message" |] res [200] [text/plain] # Accept stdin as body body="$(cat)" # Parse status status='' case "${1}" in ''|'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';; '409') status='Status: 409 Conflict\n';; '500') status='Status: 500 Internal Server Error\n';; *) exit 3;; esac # Parse content-type 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 # 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"