serveRequest method

Future serveRequest (HttpRequest request)

Serve a single HttpRequest, in this VirtualDirectory.

Implementation

Future serveRequest(HttpRequest request) {
  var iterator = request.uri.pathSegments.iterator;
  for (var segment in _pathPrefixSegments) {
    if (!iterator.moveNext() || iterator.current != segment) {
      _serveErrorPage(HttpStatus.notFound, request);
      return request.response.done;
    }
  }
  return _locateResource('.', iterator..moveNext()).then((entity) {
    if (entity is File) {
      serveFile(entity, request);
    } else if (entity is Directory) {
      if (allowDirectoryListing) {
        _serveDirectory(entity, request);
      } else {
        _serveErrorPage(HttpStatus.notFound, request);
      }
    } else if (entity is _DirectoryRedirect) {
      // TODO(ajohnsen): Use HttpRequest.requestedUri once 1.2 is out.
      request.response.redirect(Uri.parse('${request.uri}/'),
          status: HttpStatus.movedPermanently);
    } else {
      assert(entity == null);
      _serveErrorPage(HttpStatus.notFound, request);
    }
    return request.response.done;
  });
}