diff options
| author | Jannis R <mail@jannisr.de> | 2020-05-03 16:17:03 +0200 |
|---|---|---|
| committer | Jannis R <mail@jannisr.de> | 2020-05-03 16:17:03 +0200 |
| commit | 0e0eb67c7f43338bb2c28f4a354ef2ab95bae6ff (patch) | |
| tree | 7c85610084df20faeb8cdacc730da73c5c758d69 | |
| parent | ffbab4a5cff16e6e0626b699b185580450b169fc (diff) | |
server: require ALPN, response convenience fns
| -rw-r--r-- | lib/response.js | 25 | ||||
| -rw-r--r-- | server.js | 5 |
2 files changed, 26 insertions, 4 deletions
diff --git a/lib/response.js b/lib/response.js index 442cea0..25187af 100644 --- a/lib/response.js +++ b/lib/response.js @@ -51,14 +51,32 @@ const createResponse = () => { // convenience API res.prompt = (promptMsg) => { - if (typeof promptMsg !== 'string') { - throw new Error('invalid promptMsg') - } + if (typeof promptMsg !== 'string') throw new Error('invalid promptMsg') sendHeader(CODES.INPUT, promptMsg) } + res.redirect = (url, permanent = false) => { + sendHeader(permanent ? CODES.REDIRECT_PERMANENT : CODES.REDIRECT_TEMPORARY, url) + } + res.proxyError = (msg) => { + if (typeof msg !== 'string') throw new Error('invalid msg') + sendHeader(CODES.PROXY_ERROR, msg) + } + res.slowDown = (waitForSeconds) => { + if (!Number.isInteger(waitForSeconds)) { + throw new Error('invalid waitForSeconds') + } + sendHeader(CODES.SLOW_DOWN, waitForSeconds + '') + } + res.notFound = () => { + sendHeader(CODES.NOT_FOUND) + } res.gone = () => { sendHeader(CODES.GONE) } + res.badRequest = (msg) => { + if (typeof msg !== 'string') throw new Error('invalid msg') + sendHeader(CODES.BAD_REQUEST, msg) + } res.requestTransientClientCert = (reason) => { if (typeof reason !== 'string') throw new Error('invalid reason') sendHeader(CODES.TRANSIENT_CERT_REQUESTED, reason) @@ -67,7 +85,6 @@ const createResponse = () => { if (typeof reason !== 'string') throw new Error('invalid reason') sendHeader(CODES.AUTHORISED_CERT_REQUIRED, reason) } - // todo: redirect(), serverUnavailable(), slowDown(), badRequest() return res } @@ -22,6 +22,11 @@ const createGeminiServer = (opt = {}, onRequest) => { } const onConnection = (socket) => { + // todo: clarify if this is desired behavior + if (socket.alpnProtocol !== ALPN_ID) { + socket.destroy() + return; + } if ( socket.authorizationError && // allow self-signed certs |
