1

I have a java application that is running with the following java version

$ java -version
java version "11.0.3" 2019-04-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.3+12-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.3+12-LTS, mixed mode)

I was looking into using the built in HttpServer that is in java, and noticed that a HttpContext with path=/foo would accept requests to uri's beginning with said path, for example /foo123 /foobar /fooxxx.

public class IsThisABug {

    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
        server.createContext("/context",IsThisABug::handleRequest);
        server.start();
        System.out.println("Server listening on " + server.getAddress());
    }


    private static void handleRequest(HttpExchange exchange) throws IOException {
        URI requestURI = exchange.getRequestURI();
        String response = "Hello From handleRequest: " + requestURI;
        System.out.println(response);
        exchange.sendResponseHeaders(200, response.getBytes().length);
        OutputStream os = exchange.getResponseBody();
        os.write(response.getBytes());
        os.close();
    }
}

If I go into postman and send a GET request to localhost:8080/context the following output is observed in the terminal window:

Hello From handleRequest: /context

And if I send a request to /contextBar the following output is seen

Hello From handleRequest: /contextBar

I have briefly looked into using import com.sun.net.httpserver.Filter as a means of rejecting any stray requests coming into my /context endpoint but I don't see why this should be necessary.

Does anyone know why this is happening?

tgabb
  • 349
  • 3
  • 12
  • 1
    Regarding `createContext` -> *The path specifies the root URI path for this context.* To understand the expectation better, by *as a means of rejecting any stray requests coming into my `/context`..* did you mean to say `/contextBar` is a stray request? – Naman Jun 07 '19 at 05:11
  • yes, it is my understanding of RESTful convention that a request to that URI should be responded to with a 404 not found, since there is no handler for it, by it I mean `contextBar` – tgabb Jun 07 '19 at 05:13
  • 1
    Well as the documentation reads the path specified while creating context is a root URI, which means paths starting with it would all be handled using the `hadleRequest`, you've specified. – Naman Jun 07 '19 at 05:16
  • 3
    So, that handler is used for all URI's like `context*`?, but my understanding of the root URI would be that `context/someOtherPath` and `/context/foobarBaz` both have the same root uri of `/context`. In this case wouldn't `context/` and `/contextBar/` be considered two different root URI? Given the observed behavior, this would treat `/context/Foo` `/contextFoo/Bar` as having the same root URI? – tgabb Jun 07 '19 at 05:22
  • 1
    I would suggest to register your context with "/context/" – daniel Jun 07 '19 at 12:18
  • 3
    Strictly speaking, this is not a bug. It is behaving as specified. As the poster above suggests, you just need to ensure your contexts end with a '/' character. Probably, the Javadoc should warn about this oddity though. – louisburgh Jun 07 '19 at 13:36

0 Answers0