servePath method

Future<bool> servePath(
  1. String path,
  2. RequestContext req,
  3. ResponseContext res
)

Writes the file at the given virtual path to a response.

Implementation

Future<bool> servePath(
    String path, RequestContext req, ResponseContext res) async {
  if (_prefix.isNotEmpty) {
    // Only replace the *first* incidence
    // Resolve: https://github.com/angel-dart/angel/issues/41
    path = path.replaceFirst(RegExp('^${_pathify(_prefix)}'), '');
  }

  if (path.isEmpty) path = '.';
  path = path.replaceAll(_straySlashes, '');

  var absolute = source.absolute.uri.resolve(path).toFilePath();
  var parent = source.absolute.uri.toFilePath();

  if (!p.isWithin(parent, absolute) && !p.equals(parent, absolute)) {
    return true;
  }

  // Update to the correct file separator based on file system
  if (absolute.contains('\\') && fileSystem.path.separator == '/') {
    absolute = absolute.replaceAll('\\', '/');
    _log.warning(
        'Incompatible file system type is used. Changed file separator from "\\" to "/".');
  } else if (absolute.contains('/') && fileSystem.path.separator == '\\') {
    absolute = absolute.replaceAll('/', '\\');
    _log.warning(
        'Incompatible file system type. Changed file separator from "/" to "\\".');
  }

  var stat = await fileSystem.stat(absolute);
  return await serveStat(absolute, path, stat, req, res);
}