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

Daniel Fuchs dfuchs at openjdk.org
Fri Oct 31 11:58:12 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 dfuchs (Reviewer).

src/jdk.httpserver/share/classes/com/sun/net/httpserver/SimpleFileServer.java line 95:

> 93:  * <h3>Range requests</h3>
> 94:  *
> 95:  * <p>The file server also supports HTTP <i>range requests</i>, allowing clients to

To make it clear I think we should also mention the file handler:

Suggestion:

 * <p>The file server and {@linkplain #createFileHandler(Path) file handler} also support HTTP 
 * <i>range requests</i>, allowing clients to

src/jdk.httpserver/share/classes/com/sun/net/httpserver/SimpleFileServer.java line 97:

> 95:  * <p>The file server also supports HTTP <i>range requests</i>, allowing clients to
> 96:  * request partial file content using the {@code Range} request header, as specified in
> 97:  * <a href="https://datatracker.ietf.org/doc/html/rfc9110#name-range-requests">RFC 9110</a>.</p>

Suggestion:

 * <a href="https://www.rfc-editor.org/rfc/rfc9110.html#name-range-requests">RFC 9110</a>.</p>

src/jdk.httpserver/share/classes/com/sun/net/httpserver/SimpleFileServer.java line 100:

> 98:  *
> 99:  * <p>The server also supports conditional range requests with the {@code If-Range}
> 100:  * request header using a date.

Suggestion:

 * <p>Conditional range requests with the {@code If-Range}
 * request header using a date are also supported.

src/jdk.httpserver/share/classes/com/sun/net/httpserver/SimpleFileServer.java line 127:

> 125:  * @toolGuide jwebserver
> 126:  *
> 127:  * @since 18

Suggestion:

 * @spec https://www.rfc-editor.org/info/rfc9110
 *       RFC 9110: HTTP Semantics
 *
 * @since 18

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

> 345:                     end = fileSize - 1;
> 346:                 } else {  // "<start>-<end>" or "<start>-"
> 347:                     start = Long.parseLong(startStr);

We only want to accept base 10 numbers, don't we? 

Suggestion:

                    start = Long.parseLong(startStr, 10);

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

> 346:                 } else {  // "<start>-<end>" or "<start>-"
> 347:                     start = Long.parseLong(startStr);
> 348:                     end = endStr.isEmpty() ? fileSize - 1 : Long.parseLong(endStr);

Suggestion:

                    end = endStr.isEmpty() ? fileSize - 1 : Long.parseLong(endStr, 10);

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

> 360:             ranges.add(new RangeEntry(start, end));
> 361:         }
> 362:         return ranges;

I believe we should also return null if we haven't reached the end of the string. Can that happen? Maybe not - I'd expect the unit test to verify that.

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

PR Review: https://git.openjdk.org/jdk/pull/28021#pullrequestreview-3403833732
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481113846
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481127260
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481130641
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481137191
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481153043
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481158906
PR Review Comment: https://git.openjdk.org/jdk/pull/28021#discussion_r2481163698


More information about the net-dev mailing list