Skip to content

Commit

Permalink
clean request
Browse files Browse the repository at this point in the history
  • Loading branch information
bung87 committed Jun 12, 2023
1 parent bfe1f40 commit 4b082d5
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 18 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,10 @@ type
headers*: HttpHeaders
protocol*: tuple[major, minor: int]
url*: Url
path*: string # http req path
hostname*: string
ip*: string
params*: Table[string, string]
query*: seq[(string, string)]
ScorperCallback* = proc (req: Request): Future[void] {.closure, gcsafe.}
Scorper* = ref object of StreamServer
# inherited (partial)
Expand Down
2 changes: 1 addition & 1 deletion src/scorper/http/exts/resumable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ proc isComplete*(resumable: Resumable): bool =

proc handleResumableUpload*(req: Request; resumableKeys = newResumableKeys()): Future[ResumableResult]{.async.} =
template resumableParam(key: untyped): untyped =
req.query[resumableKeys.`key`]
req.url.query[resumableKeys.`key`]

var resumable: Resumable
discard parseBiggestUInt(resumableParam(totalChunks), resumable.totalChunks)
Expand Down
4 changes: 1 addition & 3 deletions src/scorper/http/httprequest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ type
headers*: HttpHeaders
protocol*: tuple[major, minor: int]
url*: typeof(Url()[])
path*: string # http req path
hostname*: string
ip*: string
params*: Table[string, string]
query*: seq[(string, string)]


16 changes: 4 additions & 12 deletions src/scorper/http/streamserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ proc formatCommon*(r: ImpRequest, status: HttpCode, size: int): string =
# LogFormat "%h %l %u %t \"%r\" %>s %b" common
# LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
let remoteUser = os.getEnv("REMOTE_USER", "-")
result = fmt"""{r.hostname} - {remoteUser} {$now()} "{r.meth} {r.path} HTTP/{r.protocol.major}.{r.protocol.minor}" {status} {size}"""
result = fmt"""{r.hostname} - {remoteUser} {$now()} "{r.meth} {r.url.path} HTTP/{r.protocol.major}.{r.protocol.minor}" {status} {size}"""

proc genericHeaders(headers = newHttpHeaders()): lent HttpHeaders {.tags: [TimeEffect].} =
## genericHeaders contains Date,X-Frame-Options
Expand Down Expand Up @@ -640,7 +640,7 @@ proc defaultErrorHandle(req: ImpRequest, err: ref Exception | HttpError; headers
toUgly(s, %* {"error": err.msg})
await req.respError(code, s, headers)
of "js":
let cbName: string = req.query["callback"]
let cbName: string = req.url.query["callback"]
var s: string
toUgly(s, %* {"error": err.msg})
await req.respError(code, fmt"""{cbName}({s});""", headers)
Expand Down Expand Up @@ -712,10 +712,10 @@ proc processRequest(
await req.respError(Http501)
return true

req.path = scorper.httpParser.getPath()
let path = scorper.httpParser.getPath()
const prefix = "http" & (when isSecurity: "s" else: "") & "://"
try:
req.url = parseUrl(prefix & req.hostname & req.path)[]
req.url = parseUrl(prefix & req.hostname & path)[]
except ValueError as e:
scorper.logSub.next(e.msg)
asyncSpawn req.respError(Http400)
Expand Down Expand Up @@ -767,10 +767,6 @@ proc processRequest(
var keep = true
case scorper.kind
of CbKind.cb:
when defined(gcArc) or defined(gcOrc):
req.query = move(req.url.query)
else:
shallowCopy(req.query, req.url.query)
handlePreProcessMiddlewares(req)
tryHandle(req, scorper.callback(req), keep)
if not keep:
Expand All @@ -780,10 +776,6 @@ proc processRequest(
let matched = scorper.router.match($req.meth, req.url.path)
if matched.success:
req.params = matched.route.params[]
when defined(gcArc) or defined(gcOrc):
req.query = move(req.url.query)
else:
shallowCopy(req.query, req.url.query)
req.prefix = matched.route.prefix
handlePreProcessMiddlewares(req)
tryHandle(req, matched.handler(req), keep)
Expand Down

0 comments on commit 4b082d5

Please sign in to comment.