diff --git a/src/syndicate/drivers/http_driver.nim b/src/syndicate/drivers/http_driver.nim index 04ea1a6..6cece94 100644 --- a/src/syndicate/drivers/http_driver.nim +++ b/src/syndicate/drivers/http_driver.nim @@ -16,8 +16,6 @@ when defined(posix): proc echo(args: varargs[string, `$`]) {.used.} = stderr.writeLine(args) -proc `$`(b: seq[byte]): string = cast[string](b) - proc badRequest(conn: Connection; msg: string) = 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) = ## 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 token: string off: int @@ -91,13 +92,13 @@ proc parseRequest(conn: Connection; text: string): (int, HttpRequest) = for e in vals.mitems: e = e.toLowerAscii if k == Symbol"host": - result[1].host = e + result[1].host = RequestHost(orKind: RequestHostKind.`present`, present: e) if v == "": v = move e else: v.add ", " v.add e if k == Symbol"host": - result[1].host = v + result[1].host = RequestHost(orKind: RequestHostKind.`present`, present: v) result[1].headers[k] = v result[0] = off @@ -140,6 +141,9 @@ proc send(ses: Session; chunk: Chunk) = of ChunkKind.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 = ## Check if `HttpBinding` `b` matches `HttpRequest` `r`. result = @@ -241,9 +245,8 @@ proc service(turn: var Turn; exch: Exchange) = if handler.isNone: stop(turn) else: - let - cap = newCap(turn, exch) - ctx = publish(turn, handler.get, HttpContext( + let cap = newCap(turn, exch) + publish(turn, handler.get, HttpContext( req: exch.req, res: embed cap, )) diff --git a/src/syndicate/protocols/http.nim b/src/syndicate/protocols/http.nim index 747282a..42a5bbc 100644 --- a/src/syndicate/protocols/http.nim +++ b/src/syndicate/protocols/http.nim @@ -47,7 +47,7 @@ type HttpRequest* {.preservesRecord: "http-request".} = object `sequenceNumber`*: BiggestInt - `host`*: string + `host`*: RequestHost `port`*: BiggestInt `method`*: Symbol `path`*: seq[string] @@ -115,6 +115,17 @@ type `req`*: HttpRequest `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 `label`, `wildcard`, `rest` `PathPatternElement`* {.preservesOr.} = object @@ -149,6 +160,7 @@ proc `$`*(x: HostPattern | HttpListener | MethodPattern | MimeType | QueryValue HttpService | HttpBinding | HttpContext | + RequestHost | PathPatternElement | Chunk | PathPattern): string = @@ -163,6 +175,7 @@ proc encode*(x: HostPattern | HttpListener | MethodPattern | MimeType | HttpService | HttpBinding | HttpContext | + RequestHost | PathPatternElement | Chunk | PathPattern): seq[byte] =