This module implements a simple HTTP-Server.
Warning: This module will soon be deprecated in favour of the asyncdispatch module, you should use it instead.
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
Server = 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 Edit
PAsyncHTTPServer = ref AsyncHTTPServer
- Source Edit
Procs
proc serveFile(client: Socket; filename: string) {.
raises: [ValueError, OSError, Exception], tags: [WriteIOEffect, ReadIOEffect].}- serves a file to the client. Source Edit
proc open(s: var Server; 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 Edit
proc port(s: var Server): Port {.
raises: [], tags: [].}- get the port number the server has acquired. Source Edit
proc next(s: var Server) {.
raises: [OSError, TimeoutError, ValueError, KeyError, OverflowError], tags: [ReadIOEffect, TimeEffect, WriteIOEffect].}- proceed to the first/next request. Source Edit
proc close(s: Server) {.
raises: [], tags: [].}- closes the server (and the socket the server uses). Source Edit
proc run(handleRequest: proc (client: Socket; path, query: string): bool {.
closure.}; port = Port(80)) {.raises: [OSError, TimeoutError, ValueError, KeyError, OverflowError], tags: [WriteIOEffect, ReadIOEffect, TimeEffect].}- encapsulates the server object and main loop Source Edit
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 Edit
proc register(d: Dispatcher; s: PAsyncHTTPServer) {.
raises: [], tags: [].}- Registers a PAsyncHTTPServer with a Dispatcher. Source Edit
proc close(h: PAsyncHTTPServer) {.
raises: [], tags: [].}- Closes the PAsyncHTTPServer. Source Edit