177 lines
4.5 KiB
Nim
177 lines
4.5 KiB
Nim
|
|
||
|
import
|
||
|
preserves, std/tables
|
||
|
|
||
|
type
|
||
|
HostPatternKind* {.pure.} = enum
|
||
|
`host`, `any`
|
||
|
HostPatternHost* = string
|
||
|
`HostPattern`* {.preservesOr.} = object
|
||
|
case orKind*: HostPatternKind
|
||
|
of HostPatternKind.`host`:
|
||
|
`host`*: HostPatternHost
|
||
|
|
||
|
of HostPatternKind.`any`:
|
||
|
`any`* {.preservesLiteral: "#f".}: bool
|
||
|
|
||
|
|
||
|
HttpListener* {.preservesRecord: "http-listener".} = object
|
||
|
`port`*: BiggestInt
|
||
|
|
||
|
MethodPatternKind* {.pure.} = enum
|
||
|
`any`, `specific`
|
||
|
MethodPatternSpecific* = Symbol
|
||
|
`MethodPattern`* {.preservesOr.} = object
|
||
|
case orKind*: MethodPatternKind
|
||
|
of MethodPatternKind.`any`:
|
||
|
`any`* {.preservesLiteral: "#f".}: bool
|
||
|
|
||
|
of MethodPatternKind.`specific`:
|
||
|
`specific`*: MethodPatternSpecific
|
||
|
|
||
|
|
||
|
MimeType* = Symbol
|
||
|
QueryValueKind* {.pure.} = enum
|
||
|
`string`, `file`
|
||
|
QueryValueString* = string
|
||
|
QueryValueFile* {.preservesRecord: "file".} = object
|
||
|
`filename`*: string
|
||
|
`headers`*: Headers
|
||
|
`body`*: seq[byte]
|
||
|
|
||
|
`QueryValue`* {.preservesOr.} = object
|
||
|
case orKind*: QueryValueKind
|
||
|
of QueryValueKind.`string`:
|
||
|
`string`*: QueryValueString
|
||
|
|
||
|
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`
|
||
|
RequestBodyPresent* = seq[byte]
|
||
|
`RequestBody`* {.preservesOr.} = object
|
||
|
case orKind*: RequestBodyKind
|
||
|
of RequestBodyKind.`present`:
|
||
|
`present`*: RequestBodyPresent
|
||
|
|
||
|
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
|
||
|
`handler`* {.preservesEmbedded.}: HttpRequest
|
||
|
|
||
|
HttpContext* {.preservesRecord: "request".} = object
|
||
|
`req`*: HttpRequest
|
||
|
`res`* {.preservesEmbedded.}: HttpResponse
|
||
|
|
||
|
PathPatternElementKind* {.pure.} = enum
|
||
|
`label`, `wildcard`, `rest`
|
||
|
PathPatternElementLabel* = string
|
||
|
`PathPatternElement`* {.preservesOr.} = object
|
||
|
case orKind*: PathPatternElementKind
|
||
|
of PathPatternElementKind.`label`:
|
||
|
`label`*: PathPatternElementLabel
|
||
|
|
||
|
of PathPatternElementKind.`wildcard`:
|
||
|
`wildcard`* {.preservesLiteral: "_".}: bool
|
||
|
|
||
|
of PathPatternElementKind.`rest`:
|
||
|
`rest`* {.preservesLiteral: "|...|".}: bool
|
||
|
|
||
|
|
||
|
ChunkKind* {.pure.} = enum
|
||
|
`string`, `bytes`
|
||
|
ChunkString* = string
|
||
|
ChunkBytes* = seq[byte]
|
||
|
`Chunk`* {.preservesOr.} = object
|
||
|
case orKind*: ChunkKind
|
||
|
of ChunkKind.`string`:
|
||
|
`string`*: ChunkString
|
||
|
|
||
|
of ChunkKind.`bytes`:
|
||
|
`bytes`*: ChunkBytes
|
||
|
|
||
|
|
||
|
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))
|