On Wed, 1 May 2024 21:12:05 GMT, robert engels <[email protected]> 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
Maybe it will be a bit easier if we talk about some concrete use cases
## You want to send a response with zero body and you know it up front.
### Without constants
exchange.sendResponseHeaders(200, -1);
### With constants
exchange.sendResponseHeaders(200, HttpExchange.NO_CONTENT);
### With a class to hold the response length
exchange.sendResponseHeaders(200, ResponseLength.known(0));
### With a body abstraction
exchange.sendResponse(200, Body.empty());
## You want to send a response and you can compute the response length, but you
don't know it up front and it could be zero
### Without constants
exchange.sendResponseHeaders(200, bytes.length == 0 ? -1 : bytes.length);
try (var body = exchange.getResponseBody) {
body.write(bytes);
}
### With constants
exchange.sendResponseHeaders(200, bytes.length == 0 ? HttpExchange.NO_CONTENT :
bytes.length);
try (var body = exchange.getResponseBody) {
body.write(bytes);
}
### With a class to hold the response length
exchange.sendResponseHeaders(200, ResponseLength.known(bytes.length));
try (var body = exchange.getResponseBody) {
body.write(bytes);
}
### With a body abstraction
exchange.sendResponse(200, Body.of(bytes));
## You want to send a response and you cannot compute the response length up
front
### Without constants
exchange.sendResponseHeaders(200, 0);
try (var body = exchange.getResponseBody) {
inputStream.transferTo(body);
}
### With constants
exchange.sendResponseHeaders(200, HttpExchange.CHUNKED_CONTENT);
try (var body = exchange.getResponseBody) {
inputStream.transferTo(body);
}
### With a class to hold the response length
exchange.sendResponseHeaders(200, ResponseLength.unknown());
try (var body = exchange.getResponseBody) {
inputStream.transferTo(body);
}
### With a body abstraction
exchange.sendResponse(200, Body.of(inputStream));
-------------
PR Comment: https://git.openjdk.org/jdk/pull/18955#issuecomment-2180978541