RFR: 8355572: Support HTTP Range requests in Simple Web Server [v6]

Michael McMahon michaelm at openjdk.org
Fri Oct 31 12:46:16 UTC 2025


On Fri, 31 Oct 2025 10:12:27 GMT, Peyang <duke at openjdk.org> wrote:

>> Hi all,
>> 
>> [JEP 408](https://openjdk.org/jeps/408) introduced the Simple Web Server in Java 18, providing a minimal webserver for serving static files over HTTP.
>> 
>> [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html#name-range-requests) defines "Range Requests" as an optional feature that allows clients to request a subset of a resource's content. Supporting Range requests in the context of JDK's Simple Web Server means enabling the server to serve only the requested portion of a static file.
>> 
>> This change contains:
>> 
>> 1. Enhances `sun.net.httpserver.simpleserver.FileServerHandler` in the `jdk.httpserver` module to support `Range` and `If-Range` headers.
>> 2. Calculates an `ETag` for each resource based on its last-modified date and file size and sends it to the client on demand for use with the `If-Range` header.
>> 3. Returns the `Accept-Ranges` header for all file retrievals, and `Content-Range` when a client requests a specific range.
>> 4. Adds a new constant `HTTP_RANGE_NOT_SATISFIABLE` to the `Codes` class to indicate invalid ranges.
>> 5. Returns `206 Partial Content` for valid ranges and `416 Range Not Satisfiable` for invalid ranges.
>> 6. Includes corresponding tests to verify correct behavior.
>> 
>> This enhancement was motivated by recent discussions on the net-dev mailing list, which requested support for Range requests along with example use cases: https://mail.openjdk.org/pipermail/net-dev/2025-April/026364.html
>> It was also discussed briefly on the net-dev mailing list: https://mail.openjdk.org/pipermail/net-dev/2025-October/028586.html
>
> Peyang has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Add documentation for HTTP range requests support in SimpleFileServer

Changes requested by michaelm (Reviewer).

A couple more instances

src/jdk.httpserver/share/classes/sun/net/httpserver/simpleserver/FileServerHandler.java line 388:

> 386:             for (RangeEntry range : ranges) {
> 387:                 if (!isSingleRange) {
> 388:                     os.write(("--" + boundary + "\r\n").getBytes(UTF_8));

Suggestion:

                    os.write(("--" + boundary + "\r\n").getBytes(US_ASCII));

Need to import the constant also.

src/jdk.httpserver/share/classes/sun/net/httpserver/simpleserver/FileServerHandler.java line 391:

> 389:                     os.write(("Content-Type: " + fileContentType + "\r\n").getBytes(UTF_8));
> 390:                     os.write("Content-Range: bytes %s-%s/%s\r\n\r\n"
> 391:                             .formatted(range.start, range.end, fileSize).getBytes(UTF_8));

Suggestion:

                            .formatted(range.start, range.end, fileSize).getBytes(US_ASCII));

src/jdk.httpserver/share/classes/sun/net/httpserver/simpleserver/FileServerHandler.java line 391:

> 389:                     os.write(("Content-Type: " + fileContentType + "\r\n").getBytes(UTF_8));
> 390:                     os.write("Content-Range: bytes %s-%s/%s\r\n\r\n"
> 391:                             .formatted(range.start, range.end, fileSize).getBytes(UTF_8));

Suggestion:

                    os.write(("Content-Type: " + fileContentType + "\r\n").getBytes(US_ASCII));
                    os.write("Content-Range: bytes %s-%s/%s\r\n\r\n"
                            .formatted(range.start, range.end, fileSize).getBytes(US_ASCII));

src/jdk.httpserver/share/classes/sun/net/httpserver/simpleserver/FileServerHandler.java line 405:

> 403:                 }
> 404:                 if (!isSingleRange) {
> 405:                     os.write("\r\n".getBytes(UTF_8));

Suggestion:

                    os.write("\r\n".getBytes(US_ASCII));

src/jdk.httpserver/share/classes/sun/net/httpserver/simpleserver/FileServerHandler.java line 410:

> 408:             if (!isSingleRange) {
> 409:                 String closingBoundary = "--" + boundary + "--\r\n";
> 410:                 os.write(closingBoundary.getBytes(UTF_8));

Suggestion:

                os.write(closingBoundary.getBytes(US_ASCII));

-------------

PR Review: https://git.openjdk.org/jdk/pull/28021#pullrequestreview-3404070866
Changes requested by michaelm (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/28021#pullrequestreview-3404106366
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481272262
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481273046
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481288301
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481273795
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481274233


More information about the net-dev mailing list