未捕获错误:错误状态:已发送标头

时间:2015-05-04 18:09:15

标签: http dart

我找不到任何与此错误和飞镖有关的内容,因此问题。

在简单的服务器应用程序中可能导致此错误的原因。

import 'package:auctionprojectServer/auctionprojectServer.dart' as auctionprojectServer;
import 'dart:io';
import 'package:http_server/http_server.dart' show VirtualDirectory;

VirtualDirectory virDir;

main() {
    initVirDir();
    runServer();
}


/**
 *  Code for virtual directory
 */
initVirDir() {
  virDir = new VirtualDirectory(r"..\auctionProject\web")   // change to build\web after 2js
    ..allowDirectoryListing = true
    ..directoryHandler = directoryHandler;
}

directoryHandler(dir, request) {
  var indexUri = new Uri.file(dir.path).resolve('auctionproject.html');
  virDir.serveFile(new File(indexUri.toFilePath()), request);  
}

/* end of virtual directory related code */


/**
 *  Code to handle Http Requests
 */
runServer() async {
  var server = await HttpServer.bind('127.0.0.1', 8080).catchError((e) => print(e));
  await for (HttpRequest request in server) {
    handleAnyRequest(request);
    request.response.close();
  }
}

handleAnyRequest(HttpRequest request) {
  switch (request.method) {
    case 'GET':
      handleGetRequest(request);
      break;
    case 'POST':
      handlePostRequest(request);
      break;
    default: defaultRequestHandler(request);
  }
}

handleGetRequest(HttpRequest req) {
  print('got GET request');
  virDir.serveRequest(req).catchError((e)=> print('annoying error'));  // <<<
}

handlePostRequest(HttpRequest req) {
  print('got POST request');
}

// place for other request types

defaultRequestHandler(HttpRequest req){
  print('some unhandled request!!!\n');
}

/* end of code handling Http Requests */

在注释掉标有// <<<的代码后,错误消失。另一方面annoying error从未打印过,所以我不确定问题的来源。

控制台输出如下所示。

got GET request
got GET request
Unhandled exception:
Uncaught Error: Bad state: Header already sent
Stack Trace:
#0      _HttpResponse.statusCode= (dart:io/http_impl.dart:523)
#1      VirtualDirectory.serveFile.<anonymous closure> (package:http_server/src/virtual_directory.dart:287:16)
#2      _RootZone.runUnary (dart:async/zone.dart:1155)
#3      _Future._propagateToListeners.handleError (dart:async/future_impl.dart:515)
#4      _Future._propagateToListeners (dart:async/future_impl.dart:570)
#5      _Future._completeWithValue (dart:async/future_impl.dart:358)
#6      _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:412)
#7      _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#8      _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#9      _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:96)
#10     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:143)

#0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:886)
#1      _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#2      _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#3      _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:96)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:143)

1 个答案:

答案 0 :(得分:2)

我没有检查,但我认为vdir.serveRequest()是异步的。

您需要等待此次电话

await handleAnyRequest(request);

并确保返回vdir.serveRequest返回的未来,以便等待它。 否则在request.response.close();完成之前调用vdir.serveRequest(),因此出错。