avoid dependency with libpcre by using regex module

This commit is contained in:
Ivan Folgueira Bande 2025-01-09 21:08:44 +01:00
parent 516467e4c3
commit 3a21949ece
5 changed files with 28 additions and 23 deletions

View File

@ -1,10 +1,10 @@
import
std/[times, strutils, asyncnet, os, sequtils, sets, strformat],
regex,
results,
chronos,
chronos/threadsync,
metrics,
re,
chronicles
import ./query_metrics
@ -247,8 +247,8 @@ proc dbConnQuery*(
let cleanedQuery = ($query).replace(" ", "").replace("\n", "")
## remove everything between ' or " all possible sequence of numbers. e.g. rm partition partition
var querySummary = cleanedQuery.replace(re"""(['"]).*?\1""", "")
querySummary = querySummary.replace(re"\d+", "")
var querySummary = cleanedQuery.replace(re2("""(['"]).*?\\1"""), "")
querySummary = querySummary.replace(re2"\d+", "")
querySummary = "query_tag_" & querySummary[0 ..< min(querySummary.len, 200)]
var queryStartTime = getTime().toUnixFloat()

View File

@ -3,7 +3,8 @@
{.push raises: [].}
import
std/[sequtils, nre, strformat],
std/[sequtils, strformat],
regex,
results,
chronos,
chronos/threadsync,
@ -23,17 +24,20 @@ proc new*(T: type PgAsyncPool, dbUrl: string, maxConnections: int): DatabaseResu
var connString: string
try:
let regex = re("""^postgres:\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)$""")
let matches = find(dbUrl, regex).get.captures
let user = matches[0]
let password = matches[1]
let host = matches[2]
let port = matches[3]
let dbName = matches[4]
let regex = re2("""^postgres:\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)$""")
var m: RegexMatch2
if dbUrl.match(regex, m) == false:
return err("could not properly parse dbUrl: " & dbUrl)
let user = m.captures[0]
let password = m.captures[1]
let host = m.captures[2]
let port = m.captures[3]
let dbName = m.captures[4]
connString =
fmt"user={user} host={host} port={port} dbname={dbName} password={password}"
except KeyError, InvalidUnicodeError, RegexInternalError, ValueError, StudyError,
SyntaxError:
except KeyError, ValueError:
return err("could not parse postgres string: " & getCurrentExceptionMsg())
let pool = PgAsyncPool(

View File

@ -1,34 +1,35 @@
{.push raises: [].}
import
std/[options, strutils, re, net],
std/[options, strutils, net],
regex,
results,
chronicles,
chronos,
chronos/apps/http/httpserver
type OriginHandlerMiddlewareRef* = ref object of HttpServerMiddlewareRef
allowedOriginMatcher: Option[Regex]
allowedOriginMatcher: Option[Regex2]
everyOriginAllowed: bool
proc isEveryOriginAllowed(maybeAllowedOrigin: Option[string]): bool =
return maybeAllowedOrigin.isSome() and maybeAllowedOrigin.get() == "*"
proc compileOriginMatcher(maybeAllowedOrigin: Option[string]): Option[Regex] =
proc compileOriginMatcher(maybeAllowedOrigin: Option[string]): Option[Regex2] =
if maybeAllowedOrigin.isNone():
return none(Regex)
return none(Regex2)
let allowedOrigin = maybeAllowedOrigin.get()
if (len(allowedOrigin) == 0):
return none(Regex)
return none(Regex2)
try:
var matchOrigin: string
if allowedOrigin == "*":
matchOrigin = r".*"
return some(re(matchOrigin, {reIgnoreCase, reExtended}))
return some(re2(matchOrigin, {regexCaseless, regexExtended}))
let allowedOrigins = allowedOrigin.split(",")
@ -54,11 +55,11 @@ proc compileOriginMatcher(maybeAllowedOrigin: Option[string]): Option[Regex] =
let finalExpression = matchExpressions.join("|")
return some(re(finalExpression, {reIgnoreCase, reExtended}))
return some(re2(finalExpression, {regexCaseless, regexExtended}))
except RegexError:
var msg = getCurrentExceptionMsg()
error "Failed to compile regex", source = allowedOrigin, err = msg
return none(Regex)
return none(Regex2)
proc originsMatch(
originHandler: OriginHandlerMiddlewareRef, requestOrigin: string

View File

@ -1,7 +1,7 @@
{.push raises: [].}
import
std/[nre, options, sequtils, strutils, strformat, times, sugar],
std/[options, sequtils, strutils, strformat, times, sugar],
stew/[byteutils, arrayops],
results,
chronos,

View File

@ -4,7 +4,7 @@ else:
{.push raises: [].}
import
std/[nre, options, sequtils, strutils, strformat, times],
std/[options, sequtils, strutils, strformat, times],
stew/[byteutils, arrayops],
results,
chronos,