From afc61f07e0cb9b2dea24f7602823657bbb454fe4 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 31 Oct 2021 00:31:45 +0200 Subject: Replaced uuid with the shorter nanoids --- src/short.nim | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'src/short.nim') diff --git a/src/short.nim b/src/short.nim index 1822e20..9ab5f97 100644 --- a/src/short.nim +++ b/src/short.nim @@ -4,10 +4,13 @@ import std/[hashes, re, sequtils, times, uri] import tiny_sqlite import jester import nimja/parser -import uuids +import nanoid import database +const nanoidAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" +const nanoidSize = 10 + const allCss = staticRead("../static/all.css") const cssRoute = "/static/all.css." & $hash(allCss) const favicon = staticRead("../static/favicon.ico") @@ -55,12 +58,13 @@ func renderNoShort(req: ShortUrl): string {.raises: [].} = func renderError(code: int, msg: string): string {.raises: [].} = compileTemplateFile(getScriptDir() / "templates/error.html") -proc handleToken(tokenStr: string): (HttpCode, string) {.raises: [].} = - var token: UUID +proc handleToken(token: string): (HttpCode, string) {.raises: [].} = try: - token = parseUUID(tokenStr) - except ValueError: - return (Http400, renderError(400, "Bad Request")) + let tokenRegexp = re"^[\w]{10}$" + if not match(token, tokenRegexp): + return (Http400, renderError(400, "Bad Request")) + except RegexError: + return (Http500, renderError(500, "RegexError")) db.CleanExpired() try: let req = db.GetUrl(token) @@ -102,11 +106,11 @@ proc handleIndexPost(params: Table[string, string]): (HttpCode, string) {.raises if input.Title == "" or input.Url == "" or exp == 0: return (Http400, renderError(400, "Bad Request")) try: - input.Token = genUUID() + input.Token = generate(alphabet=nanoidAlphabet, size=nanoidSize) except IOError: - return (Http500, renderError(500, "IOError on genUUID")) + return (Http500, renderError(500, "IOError on UUID generation")) except OSError: - return (Http500, renderError(500, "OSError on genUUID")) + return (Http500, renderError(500, "OSError on UUID generation")) input.Created = times.now() input.Expires = input.Created + initDuration(minutes = exp) try: -- cgit v1.2.3