Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart's http server throws exception

Tags:

http

https

dart

This simple dart program starts and runs for a while, but after some interactions it throws an exception:

import 'dart:io';
import 'package:http_server/http_server.dart';

main() async {


  //files
  var staticFiles = new VirtualDirectory("/path ....");
  staticFiles.allowDirectoryListing = true;
  staticFiles.directoryHandler = (dir, request) {
    var indexUri = new Uri.file(dir.path).resolve('index.html');
    staticFiles.serveFile(new File(indexUri.toFilePath()), request);
  };


  //http
  HttpServer
    .bind(InternetAddress.ANY_IP_V6, 80)
    .then((server) {
      server.forEach(staticFiles.serveRequest);
    }
  );

  //https
  SecurityContext context = new SecurityContext();
  var chain =   Platform.script.resolve('file.pem').toFilePath();
  var key =    Platform.script.resolve('file.pem').toFilePath();
  context.useCertificateChain(chain);
  context.usePrivateKey(key, password: 'pwd');
  HttpServer
    .bindSecure(InternetAddress.ANY_IP_V6, 443, context)
    .then((server) {
      server.forEach(staticFiles.serveRequest);
    }
  );
}

After some time it crashes. The Exception is:

Unhandled exception:
SocketException: OS Error: Broken pipe, errno = 32, address = ::, port = 443
#0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:1108)
#1      _microtaskLoop (dart:async/schedule_microtask.dart:41)
#2      _startMicrotaskLoop (dart:async/schedule_microtask.dart:50)
#3      _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:99)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:152)

Is it a bug?

How can I solve the problem?

And ... a different topic ... why can I start the https server without providing an appropriate password? I expect the server to check if the password is correct?!

I tried to solve it catching exceptions

import 'dart:io';
import 'package:http_server/http_server.dart';

main() async {


  //files
  var staticFiles = new VirtualDirectory("file");
  staticFiles.allowDirectoryListing = true;
  staticFiles.directoryHandler = (dir, request) {
    try{
      var indexUri = new Uri.file(dir.path).resolve('index.html');
      staticFiles.serveFile(new File(indexUri.toFilePath()), request);
    } catch(a,b) {
      print(a);
      print(b);
    }
  };


  //http
  HttpServer
    .bind(InternetAddress.ANY_IP_V6, 80)
    .then((server) {
      try{
        server.forEach(staticFiles.serveRequest);
      } catch(a,b)
      {
        print(a);
        print(b);
      }
    }
  );

  //https
  SecurityContext context = new SecurityContext();
  var chain =   Platform.script.resolve('file.pem').toFilePath();
  var key =    Platform.script.resolve('file.pem').toFilePath();
  context.useCertificateChain(chain);
  context.usePrivateKey(key, password: 'pwd');
  HttpServer
    .bindSecure(InternetAddress.ANY_IP_V6, 443, context)
    .then((server) {
        try {
          server.forEach(staticFiles.serveRequest);
        } catch(a, b){
          print(a);
          print(b);
        }
      }
  );

But it didn't help ...


1 Answers

http_server (and Dart's HTTP support in general) do not support password-based authentication.

You can build such a thing on top of the core support, but that's it.

If you're looking for a static file solution, I'd look at https://pub.dartlang.org/packages/shelf_static

It'll be much easier to get started.

like image 100
Kevin Moore Avatar answered Dec 05 '25 09:12

Kevin Moore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!