On Wed, 1 May 2024 21:12:05 GMT, robert engels <d...@openjdk.org> wrote:
>> improve the HttpExchange api with documented constants and convenience >> methods to avoid common bugs > > robert engels has updated the pull request incrementally with two additional > commits since the last revision: > > - Merge remote-tracking branch 'robaho/HttpExchange-api-change' into > HttpExchange-api-change > > # Conflicts: > # > src/jdk.httpserver/share/classes/com/sun/net/httpserver/HttpExchange.java > - update api changes based on comments I've doodled on this myself a little and one design that I think *might* have value is a `Body` interface. interface Body { OptionalInt contentLength(); // or sizeHint() void writeTo(OutputStream os); } To illustrate why, consider this method public final void sendResponse(int code,byte[] data) throws IOException { sendResponseHeaders(code,data.length); try (var os = getResponseBody()) { os.write(data); } } The first issue is that if `data.length` is 0 that starts a chunked transfer where what they'd really want to do is send the NO_CONTENT value of `-1`. public final void sendResponse(int code,byte[] data) throws IOException { sendResponseHeaders(code,data.length == 0 ? -1 : data.length); try (var os = getResponseBody()) { os.write(data); } } And then there is the matter of the gap between sending headers and writing the body. public final void sendResponse(int code,byte[] data) throws IOException { sendResponseHeaders(code,data.length == 0 ? -1 : data.length); data = new byte[] {}; try (var os = getResponseBody()) { os.write(data); } } Where hypothetically the assertion made when sending the headers can be invalidated by mistake. Asking users to provide a body and boxing up that strangeness in there could be one approach. (Bikeshed on whether OptionalInt is appropriate - `ContentLength.of(123)`, `ContentLength.unknown()`, `ContentLength.of(0)` could be another option) exchange.sendResponse(200, Body.of("Hello, world")); try (var is = getClass().getResourceAsStream()) { exchange.sendResponse(200, Body.of(is)); } exchange.sendResponse(200, Body.empty()); ------------- PR Comment: https://git.openjdk.org/jdk/pull/18955#issuecomment-2089266766