papegaaij commented on code in PR #1697:
URL: https://github.com/apache/wicket/pull/1697#discussion_r4425953456


##########
wicket-core/src/main/java/org/apache/wicket/request/resource/AbstractResource.java:
##########
@@ -902,20 +928,27 @@ protected boolean 
setResponseContentRangeHeaderFields(WebResponse webResponse,
 
                        if (startbyte != null && endbyte != null)
                        {
-                               // if end byte hasn't been set
-                               if (endbyte == -1)
+                               long first = firstBytePosition(startbyte, 
contentLength);
+                               if (first >= contentLength)

Review Comment:
   Only the first range is parsed (line 747, WICKET-5995), so the 416 decision 
rests on that range alone. `Range: bytes=100-200,0-5` on a 25-byte resource now 
gets 416, although `0-5` can be served. RFC 9110 ยง15.5.17 only allows 416 when 
none of the requested ranges can be served. Before this change the same request 
got 206. Sending 416 only when the header holds a single range (or falling back 
to 200 with the full content otherwise) would avoid it.



##########
wicket-core/src/main/java/org/apache/wicket/request/resource/AbstractResource.java:
##########
@@ -744,8 +751,25 @@ protected void setRequestRangeMetaData(WebRequest 
webRequest)
                        String startByteString = rangeParts[0];
                        String endByteString = rangeParts[1];
 
-                       long startbyte = !Strings.isEmpty(startByteString) ? 
Long.parseLong(startByteString) : 0;
-                       long endbyte = !Strings.isEmpty(endByteString) ? 
Long.parseLong(endByteString) : -1;
+                       Long startbyte = null;
+                       Long endbyte = null;
+                       if (!Strings.isEmpty(startByteString))
+                       {
+                               long first = Long.parseLong(startByteString);

Review Comment:
   Malformed headers still throw here, so they end up as a 500 rather than 
being ignored with a 200 as the PR description says:
   
   - `bytes=5`: `Strings.split` returns one element, so `rangeParts[1]` throws 
`ArrayIndexOutOfBoundsException`
   - `bytes=`: same, on `rangeParts[0]`
   - `bytes=abc-`, `bytes=-99999999999999999999`: `NumberFormatException` here 
and at line 768
   
   Catching these and leaving the metadata unset would let them fall through to 
a normal 200.



##########
wicket-core/src/main/java/org/apache/wicket/request/resource/AbstractResource.java:
##########
@@ -744,8 +751,25 @@ protected void setRequestRangeMetaData(WebRequest 
webRequest)
                        String startByteString = rangeParts[0];
                        String endByteString = rangeParts[1];
 
-                       long startbyte = !Strings.isEmpty(startByteString) ? 
Long.parseLong(startByteString) : 0;
-                       long endbyte = !Strings.isEmpty(endByteString) ? 
Long.parseLong(endByteString) : -1;
+                       Long startbyte = null;
+                       Long endbyte = null;
+                       if (!Strings.isEmpty(startByteString))
+                       {
+                               long first = Long.parseLong(startByteString);
+                               long last = !Strings.isEmpty(endByteString) ? 
Long.parseLong(endByteString) : -1;
+                               if (last == -1 || last >= first)
+                               {
+                                       startbyte = first;
+                                       endbyte = last;
+                               }
+                       }
+                       else if (!Strings.isEmpty(endByteString))
+                       {
+                               long suffixLength = 
Long.parseLong(endByteString);
+                               // a zero-length suffix selects no bytes, just 
like a range starting past the end
+                               startbyte = suffixLength > 0 ? -suffixLength : 
Long.MAX_VALUE;

Review Comment:
   `CONTENT_RANGE_STARTBYTE` is public, and on 10.x a custom resource may read 
it to slice its stream itself (skip + `BoundedInputStream`, the way 
`PartWriterCallback` did). With this change the value can be negative (suffix 
range) or `Long.MAX_VALUE` (`bytes=-0`), and such a resource then sends a body 
that contradicts the `Content-Range` header `AbstractResource` writes. For 
`bytes=-5` on 25 bytes, `skip(-5)` skips nothing and the bound becomes 25+5+1, 
so all 25 bytes go out under `206 bytes 20-24/25`. japicmp won't catch this 
because the type doesn't change. On a maintenance line I'd keep these keys 
absolute and carry the suffix length separately (e.g. a new metadata key), or 
at the very least call the change out in the release notes.



##########
wicket-core/src/main/java/org/apache/wicket/request/resource/AbstractResource.java:
##########
@@ -902,20 +928,27 @@ protected boolean 
setResponseContentRangeHeaderFields(WebResponse webResponse,
 
                        if (startbyte != null && endbyte != null)
                        {
-                               // if end byte hasn't been set
-                               if (endbyte == -1)
+                               long first = firstBytePosition(startbyte, 
contentLength);
+                               if (first >= contentLength)
                                {
-                                       endbyte = contentLength - 1;
+                                       
webResponse.setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
+                                       webResponse.setContentRange(

Review Comment:
   Minor: by this point the `IStaticCacheableResource` caching headers have 
already been applied (public, long max-age for package resources), so the 416 
goes out with them. A shared cache that honours explicit freshness could store 
`416 bytes */25` for a versioned URL and serve it for that URL afterwards. 
Disabling caching on the 416 path would avoid that.



##########
wicket-core/src/main/java/org/apache/wicket/request/resource/PartWriterCallback.java:
##########
@@ -117,10 +120,12 @@ public void writeData(Attributes attributes) throws 
IOException
 
                        if (startbyte != null || endbyte != null)
                        {
-                               // skipping the first bytes which are
-                               // requested to be skipped by the client
                                if (startbyte != null)
                                {
+                                       startbyte = 
AbstractResource.firstBytePosition(startbyte, contentLength);

Review Comment:
   `AbstractResource` only applies a range when `contentLength > 0` and 
accept-range is `BYTES`, but `PartWriterCallback` slices the body whenever the 
metadata is present. With an unknown length (-1), `bytes=-5` gives 
`firstBytePosition(-5, -1)` = 0, endbyte -1, bound 0: a 200 without 
`Content-Range` and an empty body. Before this PR it sent the first 6 bytes, 
also wrong but not empty. (`bytes=0-` already behaved like this.) Ignoring the 
range here when the length is unknown, matching `AbstractResource`, would fix 
both.



##########
wicket-core/src/main/java/org/apache/wicket/request/resource/PartWriterCallback.java:
##########
@@ -136,13 +141,15 @@ public void writeData(Attributes attributes) throws 
IOException
                                        endbyte = contentLength;
                                }
 
+                               boolean startsPastTheEnd = contentLength >= 0 
&& startbyte >= contentLength;
+
                                BoundedInputStream boundedInputStream = null;
                                try
                                {
                                        // Stream is going to be read from the 
starting point next to the skipped bytes
                                        // till the end byte computed by the 
range between startbyte / endbyte
                                        boundedInputStream = new 
BoundedInputStream(inputStream,
-                                               (endbyte - startbyte) + 1);
+                                               startsPastTheEnd ? 0 : (endbyte 
- startbyte) + 1);

Review Comment:
   Related edge case: with `contentLength` -1 and `bytes=-0`, startbyte is 
`Long.MAX_VALUE` and `startsPastTheEnd` is false because it requires 
`contentLength >= 0`. endbyte becomes -1, so `(-1 - Long.MAX_VALUE) + 1` is 
negative, which commons-io's `BoundedInputStream` treats as unbounded. Whatever 
the skip left behind is then written, for a range that selects no bytes.



##########
wicket-core/src/main/java/org/apache/wicket/request/resource/PartWriterCallback.java:
##########
@@ -75,17 +76,19 @@ public class PartWriterCallback extends WriteCallback
         * Creates a part writer callback.<br>
         * <br>
         * Reads a part of the given input stream. If the startbyte parameter 
is not null the number of
-        * bytes are skipped till the stream is read. If the endbyte is not 
null the stream is read till
-        * endbyte, else to the end of the whole stream. If startbyte and 
endbyte is null the whole
-        * stream is copied.
+        * bytes are skipped till the stream is read; a negative startbyte 
selects the last that many
+        * bytes of the stream instead. If the endbyte is not null the stream 
is read till endbyte, else
+        * to the end of the whole stream. If startbyte and endbyte is null the 
whole stream is copied.
         *
         * @param inputStream
         *            the input stream to read from
         * @param contentLength
-        *            content length of the input stream. Ignored if 
<em>endByte</em> is specified
+        *            content length of the input stream. Ignored if 
<em>endByte</em> is specified and

Review Comment:
   This now says `contentLength` is ignored when `endByte` is given and 
`startbyte` isn't negative, but `writeData` always uses it through 
`startsPastTheEnd`. For example, startbyte=30, endbyte=40, contentLength=10 
writes nothing.



##########
wicket-core/src/main/java/org/apache/wicket/request/resource/PartWriterCallback.java:
##########
@@ -136,13 +141,15 @@ public void writeData(Attributes attributes) throws 
IOException
                                        endbyte = contentLength;

Review Comment:
   Pre-existing, but the new ternary below works around it: for an open-ended 
range endbyte is set to `contentLength` rather than `contentLength - 1`, so the 
bound is one byte too long. It shows when the stream is longer than the 
declared length: `bytes=0-` then sends `contentLength + 1` bytes against 
`Content-Range 0-(len-1)`. Using `contentLength - 1` here and clamping the 
bound at 0 would fix the off-by-one and make `startsPastTheEnd` unnecessary.



##########
wicket-core/src/main/java/org/apache/wicket/request/resource/AbstractResource.java:
##########
@@ -902,20 +928,27 @@ protected boolean 
setResponseContentRangeHeaderFields(WebResponse webResponse,
 
                        if (startbyte != null && endbyte != null)
                        {
-                               // if end byte hasn't been set
-                               if (endbyte == -1)
+                               long first = firstBytePosition(startbyte, 
contentLength);
+                               if (first >= contentLength)
                                {
-                                       endbyte = contentLength - 1;
+                                       
webResponse.setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);

Review Comment:
   The empty body for the 416 is only enforced inside `PartWriterCallback` 
(`startsPastTheEnd`). `respond()` still runs the write callback, because 
`needsBody()` looks at `resourceResponse.getStatusCode()`, not the status just 
set on the `WebResponse`. A resource that accepts `BYTES` but writes its own 
body still sends the full content with the 416. `PackageResource` with 
`readBuffered` also reads the whole resource into memory only to send nothing. 
Skipping the write callback in `AbstractResource` when the range can't be 
served (e.g. have `setResponseContentRangeHeaderFields` report it) would cover 
every resource and make the special case in `PartWriterCallback` unnecessary.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to