Module asynchttpserver

This module implements a high performance asynchronous HTTP server.

Examples

This example will create an HTTP server on port 8080. The server will respond to all requests with a 200 OK response code and "Hello World" as the response body.

import asynchttpserver, asyncdispatch

var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
  await req.respond(Http200, "Hello World")

waitFor server.serve(Port(8080), cb)

Types

Request = object
  client*: AsyncSocket
  reqMethod*: string
  headers*: StringTableRef
  protocol*: tuple[orig: string, major, minor: int]
  url*: Uri
  hostname*: string
  body*: string
The hostname of the client that made the request.   Source
AsyncHttpServer = ref object
  socket: AsyncSocket
  reuseAddr: bool
  Source
HttpCode = enum
  Http100 = "100 Continue", Http101 = "101 Switching Protocols", Http200 = "200 OK",
  Http201 = "201 Created", Http202 = "202 Accepted", Http204 = "204 No Content",
  Http205 = "205 Reset Content", Http206 = "206 Partial Content",
  Http300 = "300 Multiple Choices", Http301 = "301 Moved Permanently",
  Http302 = "302 Found", Http303 = "303 See Other", Http304 = "304 Not Modified",
  Http305 = "305 Use Proxy", Http307 = "307 Temporary Redirect",
  Http400 = "400 Bad Request", Http401 = "401 Unauthorized",
  Http403 = "403 Forbidden", Http404 = "404 Not Found",
  Http405 = "405 Method Not Allowed", Http406 = "406 Not Acceptable",
  Http407 = "407 Proxy Authentication Required", Http408 = "408 Request Timeout",
  Http409 = "409 Conflict", Http410 = "410 Gone", Http411 = "411 Length Required",
  Http412 = "412 Precondition Failed", Http413 = "413 Request Entity Too Large",
  Http414 = "414 Request-URI Too Long", Http415 = "415 Unsupported Media Type",
  Http416 = "416 Requested Range Not Satisfiable",
  Http417 = "417 Expectation Failed", Http418 = "418 I\'m a teapot",
  Http500 = "500 Internal Server Error", Http501 = "501 Not Implemented",
  Http502 = "502 Bad Gateway", Http503 = "503 Service Unavailable",
  Http504 = "504 Gateway Timeout", Http505 = "505 HTTP Version Not Supported"
  Source
HttpVersion = enum
  HttpVer11, HttpVer10
  Source

Procs

proc `==`(protocol: tuple[orig: string, major, minor: int]; ver: HttpVersion): bool {.
    raises: [], tags: [].}
  Source
proc newAsyncHttpServer(reuseAddr = true): AsyncHttpServer {.raises: [], tags: [].}
Creates a new AsyncHttpServer instance.   Source
proc sendHeaders(req: Request; headers: StringTableRef): Future[void] {.
    raises: [FutureError], tags: [RootEffect].}
Sends the specified headers to the requesting client.   Source
proc respond(req: Request; code: HttpCode; content: string;
            headers: StringTableRef = nil): Future[void] {.raises: [FutureError],
    tags: [RootEffect].}

Responds to the request with the specified HttpCode, headers and content.

This procedure will not close the client socket.

  Source
proc serve(server: AsyncHttpServer; port: Port;
          callback: proc (request: Request): Future[void] {.closure, gcsafe.};
          address = ""): Future[void] {.raises: [FutureError], tags: [WriteIOEffect,
    ReadIOEffect, RootEffect].}

Starts the process of listening for incoming HTTP connections on the specified address and port.

When a request is made by a client the specified callback will be called.

  Source
proc close(server: AsyncHttpServer) {.raises: [], tags: [].}
Terminates the async http server instance.   Source