import strtabs, sockets, scgi var counter = 0 proc handleRequest(client: TSocket, input: string, headers: PStringTable): bool {.procvar.} = inc(counter) client.writeStatusOkTextContent() client.send("Hello for the $#th time." % $counter & "\c\L") return false # do not stop processing run(handleRequest)
Types
EScgi* = object of EIO
- the exception that is raised, if a SCGI error occurs
TScgiState* = object of TObject server: TSocket bufLen: int client*: TSocket ## the client socket to send data to headers*: PStringTable ## the parsed headers input*: string ## the input buffer
- SCGI state object
TAsyncScgiState* = object of TScgiState handleRequest: proc (server: var TAsyncScgiState; client: TSocket; input: string; headers: PStringTable) {.closure.} asyncServer: PAsyncSocket
PAsyncScgiState* = ref TAsyncScgiState
Procs
proc scgiError*(msg: string) {.noreturn.}
- raises an EScgi exception with message msg.
proc open*(s: var TScgiState; port = TPort(4000); address = "127.0.0.1")
- opens a connection.
proc close*(s: var TScgiState)
- closes the connection.
proc next*(s: var TScgistate; timeout: int = - 1): bool
- proceed to the first/next request. Waits timeout miliseconds for a request, if timeout is -1 then this function will never time out. Returns True if a new request has been processed.
proc writeStatusOkTextContent*(c: TSocket; contentType = "text/html")
-
sends the following string to the socket c:
Status: 200 OK\r\LContent-Type: text/html\r\L\r\L
You should send this before sending your HTML page, for example.
proc run*(handleRequest: proc (client: TSocket; input: string; headers: PStringTable): bool {.nimcall.}; port = TPort(4000))
- encapsulates the SCGI object and main loop.
proc open*(handleRequest: proc (server: var TAsyncScgiState; client: TSocket; input: string; headers: PStringTable) {.closure.}; port = TPort(4000); address = "127.0.0.1"): PAsyncScgiState
- Alternative of open for asyncio compatible SCGI.
proc register*(d: PDispatcher; s: PAsyncScgiState): PDelegate {.discardable.}
- Registers s with dispatcher d.
proc close*(s: PAsyncScgiState)
- Closes the PAsyncScgiState.