Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bung87 committed May 28, 2023
1 parent 82a678a commit 4465902
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
16 changes: 7 additions & 9 deletions src/scorper/http/streamserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ proc genericHeaders(headers = newHttpHeaders()): lent HttpHeaders {.tags: [TimeE
func getExt*(req: ImpRequest, mime: string): string {.inline.} =
result = req.server.mimeDb.getExt(mime, default = "")

func getMimetype*(req: ImpRequest, ext: string): string{.inline.} =
func getMimetype*(req: ImpRequest, ext: string): string {.inline.} =
result = req.server.mimeDb.getMimetype(ext, default = "")

macro acceptMime*(req: ImpRequest, ext: untyped, headers: HttpHeaders, body: untyped) =
Expand All @@ -143,7 +143,7 @@ macro acceptMime*(req: ImpRequest, ext: untyped, headers: HttpHeaders, body: unt
else:
`body`

func gzip*(req: ImpRequest): bool = GzipEnable and req.headers.hasKey("Accept-Encoding") and
func gzip*(req: ImpRequest): bool {.inline.} = GzipEnable and req.headers.hasKey("Accept-Encoding") and
string(req.headers["Accept-Encoding"]).contains("gzip")

template devLog(req: ImpRequest, content: untyped) =
Expand Down Expand Up @@ -172,9 +172,8 @@ proc resp*(req: ImpRequest, content: sink string,
# If the headers did not contain a Content-Length use our own
if req.responded == true:
return
let gzip = req.gzip()
let originalLen = content.len
let needCompress = gzip and originalLen >= gzipMinLength
let needCompress = req.gzip() and originalLen >= gzipMinLength
var ctn: string
var length: int
handleCompress(needCompress, content, ctn, length)
Expand Down Expand Up @@ -202,9 +201,8 @@ proc respError*(req: ImpRequest, code: HttpCode, content: sink string, headers =
if req.responded == true:
return
var headers = genericHeaders(headers)
let gzip = req.gzip()
let originalLen = content.len
let needCompress = gzip and originalLen >= gzipMinLength
let needCompress = req.gzip() and originalLen >= gzipMinLength
var ctn: string
var length: int
handleCompress(needCompress, content, ctn, length)
Expand All @@ -227,9 +225,8 @@ proc respError*(req: ImpRequest, code: HttpCode, headers = newHttpHeaders()): Fu
return
var headers = genericHeaders(headers)
var content = $code
let gzip = req.gzip()
let originalLen = content.len
let needCompress = gzip and originalLen >= gzipMinLength
let needCompress = req.gzip() and content.len >= gzipMinLength
var ctn: string
var length: int
handleCompress(needCompress, content, ctn, length)
Expand Down Expand Up @@ -716,8 +713,9 @@ proc processRequest(
return true

req.path = scorper.httpParser.getPath()
const prefix = "http://" & (when isSecurity: "s" else: "")
try:
req.url = parseUrl("http://" & (when isSecurity: "s" else: "") & req.hostname & req.path)[]
req.url = parseUrl(prefix & req.hostname & req.path)[]
except ValueError as e:
scorper.logSub.next(e.msg)
asyncSpawn req.respError(Http400)
Expand Down
5 changes: 3 additions & 2 deletions src/scorper/private/pnode_parse.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ proc parsePNodeStr*(str: string): PNode =

pars.lex.errorHandler =
proc(conf: ConfigRef; info: TLineInfo; msg: TMsgKind; arg: string) =
if msg notin {hintLineTooLong}:
raise ParseError(msg: arg)
when declared(hintLineTooLong):
if msg notin {hintLineTooLong}:
raise ParseError(msg: arg)

try:
result = parseAll(pars)
Expand Down

0 comments on commit 4465902

Please sign in to comment.