serveRequest method

Future serveRequest(
  1. HttpRequest request
)

Serve a single HttpRequest, in this VirtualDirectory.

Implementation

Future serveRequest(HttpRequest request) async {
  var iterator = HasCurrentIterator(request.uri.pathSegments.iterator);
  iterator.moveNext();
  for (var segment in _pathPrefixSegments) {
    if (!iterator.hasCurrent || iterator.current != segment) {
      _serveErrorPage(HttpStatus.notFound, request);
      return request.response.done;
    }
    iterator.moveNext();
  }

  var entity = await _locateResource('.', iterator);
  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.
    _unawaited(request.response.redirect(Uri.parse('${request.uri}/'),
        status: HttpStatus.movedPermanently));
  } else {
    assert(entity == null);
    _serveErrorPage(HttpStatus.notFound, request);
  }
  return request.response.done;
}