Dart的http服务器抛出异常

时间:2017-11-06 13:05:24

标签: http https dart

这个简单的飞镖程序启动并运行一段时间,但在一些交互之后会引发异常:

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);
    }
  );
}

一段时间后它崩溃了。例外是:

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)

这是一个错误吗?

我该如何解决这个问题?

并且...一个不同的主题......为什么我可以在没有提供适当密码的情况下启动https服务器?我希望服务器检查密码是否正确?!

我试图解决它捕捉异常

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);
        }
      }
  );

但它没有帮助......

1 个答案:

答案 0 :(得分:3)

http_server(以及Dart的HTTP支持)不支持基于密码的身份验证。

你可以在核心支持之上构建这样的东西,但那就是它。

如果您正在寻找静态文件解决方案,请查看https://pub.dartlang.org/packages/shelf_static

它会更容易上手。