Hi Guys

I am writing a test that starts a small HttpServer, and I write my HttpHandler like this:

    static class MyHttpHandler implements HttpHandler {
        private final URI root;
        MyHttpHandler(String fileroot) {
            root = new File(fileroot).toURI();
        }
        public void handle(HttpExchange t) throws IOException {
            URI uri = root.resolve(t.getRequestURI());
            ...

Here, root is the root of the webserver, and when a request comes in, I'd like to resolve the request URI to the real URI in a file system.

Unfortunately, t.getRequestURI() already returns an absolute URI (say, "/"), so the result uri is the same of it. We all know the "/" in "GET /" is in fact not an absolute URI.

What is the elegant way to resolve it? I'm now using

  URI uri = root.resolve(t.getRequestURI().toString().substring(1));

but it looks so ugly.

Thanks
Max

Reply via email to