Update http driver to latest schema

This commit is contained in:
Emery Hemingway 2024-04-19 11:44:35 +02:00
parent 6487ef65d0
commit 13d3995507
2 changed files with 24 additions and 8 deletions

View File

@ -16,8 +16,6 @@ when defined(posix):
proc echo(args: varargs[string, `$`]) {.used.} = proc echo(args: varargs[string, `$`]) {.used.} =
stderr.writeLine(args) stderr.writeLine(args)
proc `$`(b: seq[byte]): string = cast[string](b)
proc badRequest(conn: Connection; msg: string) = proc badRequest(conn: Connection; msg: string) =
conn.send(SupportedVersion & " 400 " & msg, endOfMessage = true) conn.send(SupportedVersion & " 400 " & msg, endOfMessage = true)
@ -33,6 +31,9 @@ proc extractQuery(s: var string): Table[Symbol, seq[QueryValue]] =
proc parseRequest(conn: Connection; text: string): (int, HttpRequest) = proc parseRequest(conn: Connection; text: string): (int, HttpRequest) =
## Parse an `HttpRequest` request out of a `text` from a `Connection`. ## Parse an `HttpRequest` request out of a `text` from a `Connection`.
result[1].host = RequestHost(orKind: RequestHostKind.absent)
result[1].body = RequestBody(orKind: RequestBodyKind.absent)
var var
token: string token: string
off: int off: int
@ -91,13 +92,13 @@ proc parseRequest(conn: Connection; text: string): (int, HttpRequest) =
for e in vals.mitems: for e in vals.mitems:
e = e.toLowerAscii e = e.toLowerAscii
if k == Symbol"host": if k == Symbol"host":
result[1].host = e result[1].host = RequestHost(orKind: RequestHostKind.`present`, present: e)
if v == "": v = move e if v == "": v = move e
else: else:
v.add ", " v.add ", "
v.add e v.add e
if k == Symbol"host": if k == Symbol"host":
result[1].host = v result[1].host = RequestHost(orKind: RequestHostKind.`present`, present: v)
result[1].headers[k] = v result[1].headers[k] = v
result[0] = off result[0] = off
@ -140,6 +141,9 @@ proc send(ses: Session; chunk: Chunk) =
of ChunkKind.bytes: of ChunkKind.bytes:
ses.send(chunk.bytes) ses.send(chunk.bytes)
func `==`(s: string; rh: RequestHost): bool =
rh.orKind == RequestHostKind.present and rh.present == s
proc match(b: HttpBinding, r: HttpRequest): bool = proc match(b: HttpBinding, r: HttpRequest): bool =
## Check if `HttpBinding` `b` matches `HttpRequest` `r`. ## Check if `HttpBinding` `b` matches `HttpRequest` `r`.
result = result =
@ -241,9 +245,8 @@ proc service(turn: var Turn; exch: Exchange) =
if handler.isNone: if handler.isNone:
stop(turn) stop(turn)
else: else:
let let cap = newCap(turn, exch)
cap = newCap(turn, exch) publish(turn, handler.get, HttpContext(
ctx = publish(turn, handler.get, HttpContext(
req: exch.req, req: exch.req,
res: embed cap, res: embed cap,
)) ))

View File

@ -47,7 +47,7 @@ type
HttpRequest* {.preservesRecord: "http-request".} = object HttpRequest* {.preservesRecord: "http-request".} = object
`sequenceNumber`*: BiggestInt `sequenceNumber`*: BiggestInt
`host`*: string `host`*: RequestHost
`port`*: BiggestInt `port`*: BiggestInt
`method`*: Symbol `method`*: Symbol
`path`*: seq[string] `path`*: seq[string]
@ -115,6 +115,17 @@ type
`req`*: HttpRequest `req`*: HttpRequest
`res`* {.preservesEmbedded.}: Value `res`* {.preservesEmbedded.}: Value
RequestHostKind* {.pure.} = enum
`present`, `absent`
`RequestHost`* {.preservesOr.} = object
case orKind*: RequestHostKind
of RequestHostKind.`present`:
`present`*: string
of RequestHostKind.`absent`:
`absent`* {.preservesLiteral: "#f".}: bool
PathPatternElementKind* {.pure.} = enum PathPatternElementKind* {.pure.} = enum
`label`, `wildcard`, `rest` `label`, `wildcard`, `rest`
`PathPatternElement`* {.preservesOr.} = object `PathPatternElement`* {.preservesOr.} = object
@ -149,6 +160,7 @@ proc `$`*(x: HostPattern | HttpListener | MethodPattern | MimeType | QueryValue
HttpService | HttpService |
HttpBinding | HttpBinding |
HttpContext | HttpContext |
RequestHost |
PathPatternElement | PathPatternElement |
Chunk | Chunk |
PathPattern): string = PathPattern): string =
@ -163,6 +175,7 @@ proc encode*(x: HostPattern | HttpListener | MethodPattern | MimeType |
HttpService | HttpService |
HttpBinding | HttpBinding |
HttpContext | HttpContext |
RequestHost |
PathPatternElement | PathPatternElement |
Chunk | Chunk |
PathPattern): seq[byte] = PathPattern): seq[byte] =