All Classes Namespaces Functions Variables Groups Pages
HttpServer Module

The HttpServer module provides a multi-threaded HTTP server to Qore programs.

The HttpServer implemented here was designed mostly for serving RPC-style requests, however it can also be used to serve files with an appropriate handler.

It has not been audited for security.

To use this module, use `"%requires HttpServer"` in your code. See examples/httpserver.q for an example program using this module

All the public symbols in the module are defined in the HttpServer namespace

The main classes are:

- AbstractAuthenticator: can be implemented to provide authentication and authorization info to the server
- AbstractHttpRequestHandler: this class must be subclassed to provide the real work of the HttpServer handling connections
- HttpServer: this class implements the main interface to the HTTP server provided by this module

Example:
  #!/usr/bin/env qore
  %requires HttpServer
  %requires Mime
  class MyHandler inherits AbstractHttpRequestHandler {
  hash handleRequest(hash $cx, hash $hdr, *data $body) {
  # NOTE: change "%y" to "%N" to get a more readable multi-line output format for container values
  log("request received on %s from %s: context: %y hdr: %y body size: %d byte%s", $cx."peer-info".desc, $cx."socket-info".desc, $cx, $hdr, $body.size(), $body.size() == 1 ? "" : "s");
  return (
  "code": 200,
  "body": sprintf("<title>Test Page</title><body><h1>Qore HTTP server v%s</h1><p>%y on %s PID %s TID %d connection %d</p><p>Qore %s</p></body>", HttpServer::Version, now_us(), gethostname(), getpid(), gettid(), $cx.id, Qore::VersionString),
  "close": True,
  );
  }
  }
  sub log(string $str) {
  printf("%y: %s\n", now_us(), vsprintf($str, $argv));
  }
  const MyHttpPort = 19001;
  # create our handler object
  my MyHandler $myHandler();
  # create the http server object
  my HttpServer $hs(\log(), \log());
  # add our handler to the server
  $hs.setHandler("my-handler", "/", MimeTypeHtml, $myHandler);
  # set our handler as the default handler
  $hs.setDefaultHandler("my-handler", $myHandler);
  # start a listener
  my hash $lh = $hs.addListener(MyHttpPort);
  # output a log message
  log("started listener on %s", $lh.desc);