Open the server.dart
file and type the following lines:
import 'dart:io'; main() { var pkcertDB = Platform.script.resolve('pkcert').toFilePath(); SecureSocket.initialize(database: pkcertDB, password: 'changeit'), HttpServer .bindSecure(InternetAddress.ANY_IP_V6, 8443, certificateName: 'localhost_cert') .then((server) { server.listen((HttpRequest request) { request.response.write('Hello, world!'), request.response.close(); }); }); }
This is an implementation of the well-known Hello, World!
example. I always keep the password of my certificate in the code only for demonstration purposes. Please keep your password in an external encrypted file. The code of the server is pretty straightforward. One small exception is that it references SecureSocket
instead of the Socket
class. By calling a static initialize
method of this class, we initialize the NSS library. Now, we should organize binding with the static bindSecure
method of HttpServer
to create an HTTPS server. Let's run it and open the following URL in the Dartium web browser:
https://localhost:8443/index.html
All the magic, such as TLS handshaking, keys, and message exchange, happens behind the scenes. As our server's certificate is self-signed, a web browser informs us about that fact, as shown in the following screenshot:
Click on the Certificate information link to see the full certificate information, as shown in the following screenshot:
Now, close the warning message and click on the Proceed anyway button to see the result of the HTTP request:
We were successful in achieving the following goals:
HttpServer
accepts the self-signed certificate and works with it18.188.190.175