2023-12-31 17:15:06 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
preserves, std/tables
|
|
|
|
|
|
|
|
type
|
|
|
|
HostPatternKind* {.pure.} = enum
|
|
|
|
`host`, `any`
|
|
|
|
`HostPattern`* {.preservesOr.} = object
|
|
|
|
case orKind*: HostPatternKind
|
|
|
|
of HostPatternKind.`host`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`host`*: string
|
2023-12-31 17:15:06 +00:00
|
|
|
|
|
|
|
of HostPatternKind.`any`:
|
|
|
|
`any`* {.preservesLiteral: "#f".}: bool
|
|
|
|
|
|
|
|
|
|
|
|
HttpListener* {.preservesRecord: "http-listener".} = object
|
|
|
|
`port`*: BiggestInt
|
|
|
|
|
|
|
|
MethodPatternKind* {.pure.} = enum
|
|
|
|
`any`, `specific`
|
|
|
|
`MethodPattern`* {.preservesOr.} = object
|
|
|
|
case orKind*: MethodPatternKind
|
|
|
|
of MethodPatternKind.`any`:
|
|
|
|
`any`* {.preservesLiteral: "#f".}: bool
|
|
|
|
|
|
|
|
of MethodPatternKind.`specific`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`specific`*: Symbol
|
2023-12-31 17:15:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
MimeType* = Symbol
|
|
|
|
QueryValueKind* {.pure.} = enum
|
|
|
|
`string`, `file`
|
|
|
|
QueryValueFile* {.preservesRecord: "file".} = object
|
|
|
|
`filename`*: string
|
|
|
|
`headers`*: Headers
|
|
|
|
`body`*: seq[byte]
|
|
|
|
|
|
|
|
`QueryValue`* {.preservesOr.} = object
|
|
|
|
case orKind*: QueryValueKind
|
|
|
|
of QueryValueKind.`string`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`string`*: string
|
2023-12-31 17:15:06 +00:00
|
|
|
|
|
|
|
of QueryValueKind.`file`:
|
|
|
|
`file`*: QueryValueFile
|
|
|
|
|
|
|
|
|
|
|
|
HttpRequest* {.preservesRecord: "http-request".} = object
|
|
|
|
`sequenceNumber`*: BiggestInt
|
|
|
|
`host`*: string
|
|
|
|
`port`*: BiggestInt
|
|
|
|
`method`*: Symbol
|
|
|
|
`path`*: seq[string]
|
|
|
|
`headers`*: Headers
|
|
|
|
`query`*: Table[Symbol, seq[QueryValue]]
|
|
|
|
`body`*: RequestBody
|
|
|
|
|
|
|
|
RequestBodyKind* {.pure.} = enum
|
|
|
|
`present`, `absent`
|
|
|
|
`RequestBody`* {.preservesOr.} = object
|
|
|
|
case orKind*: RequestBodyKind
|
|
|
|
of RequestBodyKind.`present`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`present`*: seq[byte]
|
2023-12-31 17:15:06 +00:00
|
|
|
|
|
|
|
of RequestBodyKind.`absent`:
|
|
|
|
`absent`* {.preservesLiteral: "#f".}: bool
|
|
|
|
|
|
|
|
|
|
|
|
Headers* = Table[Symbol, string]
|
|
|
|
HttpResponseKind* {.pure.} = enum
|
|
|
|
`status`, `header`, `chunk`, `done`
|
|
|
|
HttpResponseStatus* {.preservesRecord: "status".} = object
|
|
|
|
`code`*: BiggestInt
|
|
|
|
`message`*: string
|
|
|
|
|
|
|
|
HttpResponseHeader* {.preservesRecord: "header".} = object
|
|
|
|
`name`*: Symbol
|
|
|
|
`value`*: string
|
|
|
|
|
|
|
|
HttpResponseChunk* {.preservesRecord: "chunk".} = object
|
|
|
|
`chunk`*: Chunk
|
|
|
|
|
|
|
|
HttpResponseDone* {.preservesRecord: "done".} = object
|
|
|
|
`chunk`*: Chunk
|
|
|
|
|
|
|
|
`HttpResponse`* {.preservesOr.} = object
|
|
|
|
case orKind*: HttpResponseKind
|
|
|
|
of HttpResponseKind.`status`:
|
|
|
|
`status`*: HttpResponseStatus
|
|
|
|
|
|
|
|
of HttpResponseKind.`header`:
|
|
|
|
`header`*: HttpResponseHeader
|
|
|
|
|
|
|
|
of HttpResponseKind.`chunk`:
|
|
|
|
`chunk`*: HttpResponseChunk
|
|
|
|
|
|
|
|
of HttpResponseKind.`done`:
|
|
|
|
`done`*: HttpResponseDone
|
|
|
|
|
|
|
|
|
|
|
|
HttpService* {.preservesRecord: "http-service".} = object
|
|
|
|
`host`*: HostPattern
|
|
|
|
`port`*: BiggestInt
|
|
|
|
`method`*: MethodPattern
|
|
|
|
`path`*: PathPattern
|
|
|
|
|
|
|
|
HttpBinding* {.preservesRecord: "http-bind".} = object
|
|
|
|
`host`*: HostPattern
|
|
|
|
`port`*: BiggestInt
|
|
|
|
`method`*: MethodPattern
|
|
|
|
`path`*: PathPattern
|
2024-01-07 22:11:59 +00:00
|
|
|
`handler`* {.preservesEmbedded.}: Value
|
2023-12-31 17:15:06 +00:00
|
|
|
|
|
|
|
HttpContext* {.preservesRecord: "request".} = object
|
|
|
|
`req`*: HttpRequest
|
2024-01-07 22:11:59 +00:00
|
|
|
`res`* {.preservesEmbedded.}: Value
|
2023-12-31 17:15:06 +00:00
|
|
|
|
|
|
|
PathPatternElementKind* {.pure.} = enum
|
|
|
|
`label`, `wildcard`, `rest`
|
|
|
|
`PathPatternElement`* {.preservesOr.} = object
|
|
|
|
case orKind*: PathPatternElementKind
|
|
|
|
of PathPatternElementKind.`label`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`label`*: string
|
2023-12-31 17:15:06 +00:00
|
|
|
|
|
|
|
of PathPatternElementKind.`wildcard`:
|
|
|
|
`wildcard`* {.preservesLiteral: "_".}: bool
|
|
|
|
|
|
|
|
of PathPatternElementKind.`rest`:
|
|
|
|
`rest`* {.preservesLiteral: "|...|".}: bool
|
|
|
|
|
|
|
|
|
|
|
|
ChunkKind* {.pure.} = enum
|
|
|
|
`string`, `bytes`
|
|
|
|
`Chunk`* {.preservesOr.} = object
|
|
|
|
case orKind*: ChunkKind
|
|
|
|
of ChunkKind.`string`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`string`*: string
|
2023-12-31 17:15:06 +00:00
|
|
|
|
|
|
|
of ChunkKind.`bytes`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`bytes`*: seq[byte]
|
2023-12-31 17:15:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
PathPattern* = seq[PathPatternElement]
|
|
|
|
proc `$`*(x: HostPattern | HttpListener | MethodPattern | MimeType | QueryValue |
|
|
|
|
HttpRequest |
|
|
|
|
RequestBody |
|
|
|
|
Headers |
|
|
|
|
HttpResponse |
|
|
|
|
HttpService |
|
|
|
|
HttpBinding |
|
|
|
|
HttpContext |
|
|
|
|
PathPatternElement |
|
|
|
|
Chunk |
|
|
|
|
PathPattern): string =
|
|
|
|
`$`(toPreserves(x))
|
|
|
|
|
|
|
|
proc encode*(x: HostPattern | HttpListener | MethodPattern | MimeType |
|
|
|
|
QueryValue |
|
|
|
|
HttpRequest |
|
|
|
|
RequestBody |
|
|
|
|
Headers |
|
|
|
|
HttpResponse |
|
|
|
|
HttpService |
|
|
|
|
HttpBinding |
|
|
|
|
HttpContext |
|
|
|
|
PathPatternElement |
|
|
|
|
Chunk |
|
|
|
|
PathPattern): seq[byte] =
|
|
|
|
encode(toPreserves(x))
|