Module httpserver

This module implements a simple HTTP-Server.

Example:

import strutils, sockets, httpserver

var counter = 0
proc handleRequest(client: Socket, path, query: string): bool {.procvar.} =
  inc(counter)
  client.send("Hello for the $#th time." % $counter & wwwNL)
  return false # do not stop processing

run(handleRequest, Port(80))

Types

TServer = object of RootObj
  socket: Socket
  port: Port
  client*: Socket             ## the socket to write the file data to
  reqMethod*: string          ## Request method. GET or POST.
  path*, query*: string       ## path and query the client requested
  headers*: StringTableRef    ## headers with which the client made the request
  body*: string               ## only set with POST requests
  ip*: string                 ## ip address of the requesting client
  
contains the current server state   Source
PAsyncHTTPServer = ref TAsyncHTTPServer
  Source

Consts

wwwNL = "\x0D\x0A"
  Source

Procs

proc serveFile(client: Socket; filename: string) {.
    raises: [ValueError, OSError, Exception], 
    tags: [WriteIOEffect, ReadIOEffect].}
serves a file to the client.   Source
proc open(s: var TServer; port = Port(80); reuseAddr = false) {.
    raises: [OSError], tags: [WriteIOEffect, ReadIOEffect].}
creates a new server at port port. If port == 0 a free port is acquired that can be accessed later by the port proc.   Source
proc port(s: var TServer): Port {.raises: [], tags: [].}
get the port number the server has acquired.   Source
proc next(s: var TServer) {.raises: [OSError, TimeoutError, ValueError, 
                                     OverflowError], 
                            tags: [ReadIOEffect, TimeEffect, WriteIOEffect].}
proceed to the first/next request.   Source
proc close(s: TServer) {.raises: [], tags: [].}
closes the server (and the socket the server uses).   Source
proc run(handleRequest: proc (client: Socket; path, query: string): bool {.
    closure.}; port = Port(80)) {.raises: [OSError, TimeoutError, ValueError, 
    OverflowError], tags: [WriteIOEffect, ReadIOEffect, TimeEffect].}
encapsulates the server object and main loop   Source
proc asyncHTTPServer(handleRequest: proc (server: PAsyncHTTPServer; 
    client: Socket; path, query: string): bool {.closure, gcsafe.}; 
                     port = Port(80); address = ""; reuseAddr = false): PAsyncHTTPServer {.
    raises: [OSError], tags: [WriteIOEffect, ReadIOEffect].}
Creates an Asynchronous HTTP server at port.   Source
proc register(d: Dispatcher; s: PAsyncHTTPServer) {.raises: [], tags: [].}
Registers a PAsyncHTTPServer with a Dispatcher.   Source
proc close(h: PAsyncHTTPServer) {.raises: [], tags: [].}
Closes the PAsyncHTTPServer.   Source