1

In node.js one can just adjust the url of a request by doing something like this:

app.use(function(req, res, next) {
  if (req.url.slice(-1) === '/') {
    req.url = req.url.slice(0, -1);
  }
  next();
});

The following fails in dart, as all the request properties, the Uri and its path only have getters. Any suggestions how this can be achieved? Maybe by creating a new HttpRequest and piping its response to the original request?

void main() {
  var virDir = new VirtualDirectory("../somewhere_else");

  HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 8080).then((server) {
    print("Serving at ${server.address}:${server.port}");
    server.listen((HttpRequest request) {
      request.uri.path = "/newPath";
      virDir.serverRequest(request);
    });
  });
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Chenqua
  • 33
  • 2
  • Why do you need to change the path / redirect the request? – Robert Jul 03 '14 at 20:35
  • @Robert In our project setup we have a dart server and a dart client. Both are application packages and follow the pub layout conventions. Server dart file in server/bin and the client stuff in client/web. The server can serve the client via VirtualDirectory pointing to the client/web/build directory (or redirects to pub serve in dev environment). To distinguish from other requests to the server, the URL has to start with /client/. But this also means, that everything has to be in client/web/client/*, unless the /client/ part can be removed from the original request uri path. – Chenqua Jul 03 '14 at 21:17
  • Can you extend from HttpRequest and overwrite the getters you need? – Robert Jul 04 '14 at 07:24
  • Does this help? http://stackoverflow.com/questions/20295603 – Günter Zöchbauer Jul 04 '14 at 11:47
  • @Robert No, I don't think so, cause the HttpRequest has already been created by the server. – Chenqua Jul 07 '14 at 10:56
  • @GünterZöchbauer The linked example differs, because there, the VirtualDirectory handles all requests, that where not matched before. In our scenario, we want to handle all requests, that start with /client/*, but remove the /client/ before the request is handled by the VirtualDirectory. – Chenqua Jul 07 '14 at 11:00
  • I think you should make a feature request then at dartbug.com/new – Günter Zöchbauer Jul 07 '14 at 11:01
  • There is an issue for this: https://code.google.com/p/dart/issues/detail?id=17353 It already has a proposal that enables VirtualDirectory to take a pathPrefix that will be trimmed from the request. – Chenqua Jul 29 '14 at 11:59

2 Answers2

2

Have you tried using serveFile instead of serveRequest? This could be a temporary workaround till the behavior you want is implemented, or addressed by the Dart team devs.

I haven't tested this code, but it should theoretically work:

void main() {
  var virDir = new VirtualDirectory("../somewhere_else");

  HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 8080).then((server) {
    print("Serving at ${server.address}:${server.port}");
    server.listen((HttpRequest request) {
      var newUriPath = "../somwhere_else/${request.uri.path.replace("/client/","")}";
      virDir.serveFile(new File(newUriPath), request);
    });
  });
}
Daegalus
  • 1,420
  • 1
  • 10
  • 8
2

As of version 0.9.3 of http_server (VirtualDirectory), I've added an optional pathPrefix named argument to the constructor.

See http://www.dartdocs.org/documentation/http_server/0.9.3/index.html#http_server/http_server.VirtualDirectory#id_VirtualDirectory-

That should make it possible to do exactly what you are trying to do, without rewriting the Uri of the request.

Anders Johnsen
  • 616
  • 3
  • 7