Skip to content
This repository has been archived by the owner on Oct 16, 2022. It is now read-only.

Problems caused by closures #60

Open
mengyanshou opened this issue Jun 12, 2021 · 3 comments
Open

Problems caused by closures #60

mengyanshou opened this issue Jun 12, 2021 · 3 comments
Labels

Comments

@mengyanshou
Copy link
Contributor

Handler createFileHandler(String path, {String? url, String? contentType}) {
  final file = File(path);
  if (!file.existsSync()) {
    throw ArgumentError.value(path, 'path', 'does not exist.');
  } else if (url != null && !p.url.isRelative(url)) {
    throw ArgumentError.value(url, 'url', 'must be relative.');
  }

  final mimeType = contentType ?? _defaultMimeTypeResolver.lookup(path);
  url ??= p.toUri(p.basename(path)).toString();

  return (request) {
    if (request.url.path != url) return Response.notFound('Not Found');
    return _handleFile(request, file, () => mimeType);
  };
}

this function reture closure,and i tried call this funtion twice,the second time in this closure,the url still is the first time passed in.

@kevmoo
Copy link
Contributor

kevmoo commented Jun 15, 2021

@Nightmare-MY – would you provide a code sample that demonstrates this behavior?

@mengyanshou
Copy link
Contributor Author

mengyanshou commented Jun 18, 2021

add a print in createFileHandler

Handler createFileHandler(String path, {String? url, String? contentType}) {
  final file = File(path);
  if (!file.existsSync()) {
    throw ArgumentError.value(path, 'path', 'does not exist.');
  } else if (url != null && !p.url.isRelative(url)) {
    throw ArgumentError.value(url, 'url', 'must be relative.');
  }

  final mimeType = contentType ?? _defaultMimeTypeResolver.lookup(path);
  url ??= p.toUri(p.basename(path)).toString();

  return (request) {
    print('url -> $url');
    if (request.url.path != url) return Response.notFound('Not Found');
    return _handleFile(request, file, () => mimeType);
  };
}

and exec this code in example

  final handler1 = createFileHandler('example/files/dart.png');

  final handler2 = createFileHandler('example/files/index.html');
  io.serve(handler1, 'localhost', 8000, shared: true);
  io.serve(handler2, 'localhost', 8000, shared: true);

and i can request http://localhost:8000/dart.png to get a image
but i can't get http://localhost:8000/index.html to get html
when i send get request to http://localhost:8000/index.html
the print is url -> dart.png

@kevmoo
Copy link
Contributor

kevmoo commented Jun 18, 2021

CC @natebosch – that IS weird

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

2 participants