From 7ea819b4f40a771e59fafb57b3a60b14945f7397 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sat, 2 May 2020 03:36:19 +0200 Subject: request parser, response formatter --- lib/parser.js | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) (limited to 'lib/parser.js') diff --git a/lib/parser.js b/lib/parser.js index 956c12e..a66a323 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -3,16 +3,14 @@ const {Transform} = require('stream') const {MESSAGES} = require('./statuses') -// https://gemini.circumlunar.space/docs/spec-spec.txt, 1.3.1 -// > Gemini response headers look like this: -// > -// > is a two-digit numeric status code [...]. -// > is any non-zero number of consecutive spaces or tabs. -// > is a UTF-8 encoded string of maximum length 1024, whose meaning is -// > dependent. +// https://gemini.circumlunar.space/docs/spec-spec.txt, 1.2 +// > Gemini requests are a single CRLF-terminated line with the +// > following structure: +// > is a UTF-8 encoded absolute URL, of maximum length +// > 1024 bytes. [...] const CRLF = '\r\n' -const MAX_HEADER_SIZE = 2048 // cutoff +const MAX_HEADER_SIZE = 1024 + CRLF.length const createParser = () => { let headerParsed = false @@ -35,30 +33,12 @@ const createParser = () => { peek.length < MAX_HEADER_SIZE ) return; // keep peeking - const statusCodeAndSpace = peek.slice(0, 3).toString('utf8') - if (!/\d{2} /.test(statusCodeAndSpace)) return invalid() const iCRLF = peek.indexOf(CRLF) if (iCRLF < 0) return invalid() - let statusCode = parseInt(statusCodeAndSpace) - let statusMsg = MESSAGES[statusCode] - if (!statusMsg) { - statusCode = Math.floor(statusCode / 10) * 10 - statusMsg = MESSAGES[statusCode] - if (!statusMsg) return invalid() - } - - const meta = peek.slice(3, iCRLF).toString('utf8').trim() - + const url = peek.slice(0, iCRLF).toString('utf8') headerParsed = true - out.emit('header', { - type: ( - statusCode >= 10 && statusCode < 20 - ? 'request' : 'response' - ), - statusCode, statusMsg, - meta, - }) + out.emit('header', {url}) const iBody = iCRLF + 2 if (peek.length > (iBody + 1)) { @@ -88,7 +68,7 @@ const createParser = () => { // p.on('header', h => console.log('header', h)) // p.on('data', d => console.log('data', d.toString('utf8'))) // const b = str => Buffer.from(str, 'utf8') -// p.write(b('12 gemini://examp')) +// p.write(b('gemini://examp')) // p.write(b('le.org/foo?bar#baz\r\nhel')) // p.end(b('lo server!')) -- cgit v1.3