diff options
| author | Jannis R <mail@jannisr.de> | 2020-05-02 03:36:19 +0200 |
|---|---|---|
| committer | Jannis R <mail@jannisr.de> | 2020-05-02 16:35:50 +0200 |
| commit | 7ea819b4f40a771e59fafb57b3a60b14945f7397 (patch) | |
| tree | c953779e635196c85a958b48a3b99a4abcf111dd /lib/parser.js | |
| parent | 40909d0f57ccfb246c4b7d887c4763a70157567e (diff) | |
request parser, response formatter
Diffstat (limited to 'lib/parser.js')
| -rw-r--r-- | lib/parser.js | 38 |
1 files changed, 9 insertions, 29 deletions
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: -// > <STATUS><whitespace><META><CR><LF> -// > <STATUS> is a two-digit numeric status code [...]. -// > <whitespace> is any non-zero number of consecutive spaces or tabs. -// > <META> is a UTF-8 encoded string of maximum length 1024, whose meaning is -// > <STATUS> dependent. +// https://gemini.circumlunar.space/docs/spec-spec.txt, 1.2 +// > Gemini requests are a single CRLF-terminated line with the +// > following structure: <URL><CR><LF> +// > <URL> 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!')) |
