gemfedwiki

gemini fedwiki proxy
git clone git://jb55.com/gemfedwiki
Log | Files | Refs | README

commit d6357cd4c1561750d1ba22a2ef55cb01dbce9e0a
parent 8b196be36005d45dd8742cad4081cb3c9df3feae
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 27 Oct 2021 23:19:16 -0700

fix a link parsing bug

Diffstat:
Mindex.js | 72+++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 55 insertions(+), 17 deletions(-)

diff --git a/index.js b/index.js @@ -16,6 +16,20 @@ function consume_until(cursor, c) return false } +function consume_while(cursor, fn) +{ + for (; cursor.pos < cursor.str.length; cursor.pos++) { + if (fn(cursor.str[cursor.pos])) continue + return true + } + return false +} + +function not_space_or_bracket(c) +{ + return c !== " " && c !== "]" +} + function pull_char(cursor) { if (cursor.pos >= cursor.str.length) @@ -49,23 +63,18 @@ function consume_whitespace(cursor) } } -function parse_link(cursor, links, root) +function parse_external_link(cursor, links, start) { - let c, i, url, name, start - - start = cursor.pos - - if (!consume_char(cursor, "[")) - return false - - if ((c = pull_char(cursor)) && c == '[') - return parse_slug_link(cursor, links, root, start) - - cursor.pos-- consume_whitespace(cursor) - i = cursor.pos + let i = cursor.pos + + for (; cursor.pos < cursor.str.length; cursor.pos++) { + if (cursor.str[cursor.pos] === ']') + return false; + if (cursor.str[cursor.pos] === ' ') + break + } - if (!consume_until(cursor, " ")) return false url = cursor.str.slice(i, cursor.pos++) i = cursor.pos @@ -78,6 +87,22 @@ function parse_link(cursor, links, root) return true } +function parse_link(cursor, links, root) +{ + let c, i, url, name + + let start = cursor.pos + + if (!consume_char(cursor, "[")) + return false + + if ((c = pull_char(cursor)) && c == '[') + return parse_slug_link(cursor, links, root, start) + + cursor.pos-- + return parse_external_link(cursor, links, start) +} + function parse_links(text, root) { let cursor = {pos: 0, str: text} @@ -88,7 +113,7 @@ function parse_links(text, root) return links if (!parse_link(cursor, links, root)) - return links + continue } return links @@ -188,7 +213,10 @@ async function handle_request(req, res) res.sendHeader(20, 'text/gemini') return res.end(gmi) } catch (e) { - return res.notFound() + if (e.status === 404) + return res.notFound() + else + return crashed(res, e) } } @@ -204,10 +232,20 @@ async function handle_request(req, res) res.sendHeader(20, 'text/gemini') return res.end(gmi) } catch (e) { - return res.notFound() + if (e.status === 404) + return res.notFound() + else + return crashed(res, e) } } +function crashed(res, err) +{ + console.log(err) + res.sendHeader(40) + return res.end() +} + function serve(opts) { // opts = { cert, key, passphrase }