runTLS method

Future<int> runTLS({
  1. required String privateKeyFilename,
  2. required String certificateName,
  3. required String certChainFilename,
})

Starts the Web server with HTTPS.

For an insecure HTTP server, use run instead.

This is a secure HTTPS server. First initialize the SecureSocket database and invoke this method with privateKeyFilename, certificateName and certChainFilename.

var certDb = Platform.script.resolve('pkCert').toFilePath();
SecureSocket.initialize(database: certDb, password: "p@ssw0rd");
s.run(privateKeyFilename: "a.pvt",
      certificateName: "myCert",
      certChainFilename: "a.crt");

This method will return a Future whose value is the total number of requests processed by the server. This value is only available if/when the server is cleanly stopped. But normally a server listens for requests "forever" and never stops.

Throws a StateError if the server is already running.

Implementation

Future<int> runTLS(
    {required String privateKeyFilename,
    required String certificateName,
    required String certChainFilename}) async {
  if (_svr != null) {
    throw StateError('server already running');
  }

  // Start the server

  // Secure HTTPS bind
  //
  // Note: this uses the TLS libraries in Dart 1.13 or later.
  // https://dart-lang.github.io/server/tls-ssl.html
  _isSecure = true;

  if (bindPort < _minPort || _maxPort < bindPort) {
    bindPort = 443; // default HTTPS port
  }

  final securityContext = SecurityContext()
    ..useCertificateChain(certChainFilename)
    ..usePrivateKey(privateKeyFilename);

  final httpServer = await HttpServer.bindSecure(
      bindAddress, bindPort, securityContext,
      v6Only: v6Only, backlog: 5);

  return _run(httpServer, isSecure: true);
}