diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/hero.mjs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/hero.mjs b/src/hero.mjs index b47d459..75c9a88 100644 --- a/src/hero.mjs +++ b/src/hero.mjs @@ -380,6 +380,23 @@ export const extractQueryParams = s => { return ret; }; +export const renderStatus = code => + `HTTP/1.1 ${code} ${http.STATUS_CODES[code]}` + +export const renderHeaders = obj => + Object.keys(obj) + .sort(u.strSortFn) + .map(name => `${name}: ${obj[name]}`); + +export const buildHeader = (status, headers) => + [renderStatus(status)] + .concat(renderHeaders(headers)) + .concat(["\r\n"]) + .join("\r\n"); + +export const writeHead = (socket, status, headers) => + socket.write(buildHeader(status, headers)); + export const handle404 = _req => ({ status: 404, body: "Not Found\n", |