cziegeler commented on code in PR #75:
URL:
https://github.com/apache/sling-org-apache-sling-engine/pull/75#discussion_r4611395414
##########
src/main/java/org/apache/sling/engine/impl/parameters/RequestPartsIterator.java:
##########
@@ -20,61 +20,62 @@
import java.io.IOException;
import java.io.InputStream;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
-import java.util.List;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.Part;
-import org.apache.commons.fileupload.FileItemIterator;
-import org.apache.commons.fileupload.FileItemStream;
-import org.apache.commons.fileupload.FileUpload;
-import org.apache.commons.fileupload.FileUploadException;
-import org.apache.commons.fileupload.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * Contains a Lazy iterator of Parts from the request stream loaded as the
request is streamed using the Commons FileUpload API.
+ * Contains a Lazy iterator of Parts from the request stream loaded as the
request is streamed
*/
public class RequestPartsIterator implements Iterator<Part> {
- private static final Logger LOG =
LoggerFactory.getLogger(RequestPartsIterator.class);
+ private final Logger log = LoggerFactory.getLogger(getClass());
- /** The CommonsFile Upload streaming API iterator */
- private final FileItemIterator itemIterator;
+ /** The Parts iterator */
+ private Iterator<Part> itemIterator = null;
+
+ /** supplier to retrieve the parts when needed */
+ private final HttpServletRequest request;
/**
- * Create and initialse the iterator using the request. The request must
be fresh. Headers can have been read but the stream
- * must not have been parsed.
- * @param servletRequest the request
- * @throws IOException when there is a problem reading the request.
- * @throws FileUploadException when there is a problem parsing the request.
+ * Create and initialize the iterator using the request.
+ *
+ * @param request the current request
*/
- public RequestPartsIterator(final RequestContext context) throws
FileUploadException, IOException {
- FileUpload upload = new FileUpload();
- upload.setFileCountMax(50);
- itemIterator = upload.getItemIterator(context);
+ public RequestPartsIterator(final HttpServletRequest request) {
+ this.request = request;
}
@Override
public boolean hasNext() {
- try {
- return itemIterator.hasNext();
- } catch (final FileUploadException | IOException e) {
- LOG.error("hasNext Item failed cause:" + e.getMessage(), e);
+ // Use a lazy data iterator creation the first time it is needed to
ensure
+ // the container doesn't parse the uploaded files until necessary
+ if (itemIterator == null) {
+ Collection<Part> parts;
+ try {
+ parts = request.getParts();
Review Comment:
I checked the Jetty implementation and the streaming concern is valid: the
lazy iterator delays multipart parsing, but does not preserve incremental
consumption of uploaded files.
In Jetty 12, ServletApiRequest.getParts() delegates to
ServletMultiPartFormData.getParts(), which waits on a CompletableFuture via
join(). The multipart parser only constructs its final Parts result in
onComplete():
- [ServletApiRequest
delegation](https://github.com/jetty/jetty.project/blob/a4f8a0f5ab33777f99945b4177a004d1a03ca091/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletApiRequest.java#L631-L642)
- [Waiting for the completed multipart
result](https://github.com/jetty/jetty.project/blob/a4f8a0f5ab33777f99945b4177a004d1a03ca091/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletMultiPartFormData.java#L55-L70)
- [Constructing Parts on
completion](https://github.com/jetty/jetty.project/blob/a4f8a0f5ab33777f99945b4177a004d1a03ca091/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/MultiPartFormData.java#L870-L879)
This does not mean a large upload necessarily occupies its full size in RAM:
Jetty spools larger parts to temporary files according to the configured
threshold. However, the application cannot consume the first file through this
iterator while the remaining upload is still arriving. This adds
temporary-storage requirements, disk I/O, and latency compared with true
streaming. Lowering the memory threshold or parsing asynchronously does not
restore incremental delivery to the application.
This is observable compatibility behavior, not just an implementation
detail: [Sling advertises streaming uploads through Sling-UploadMode:
stream](https://sling.apache.org/news/sling-launchpad-9-released.html#streaming-upload-support),
and the [Commons FileUpload streaming
API](https://commons.apache.org/proper/commons-fileupload/streaming.html)
explicitly avoids requiring complete items to be stored before application
access. The standard Servlet getParts() API offers a collection of parts, not a
portable incremental multipart-processing contract.
Recommendation: preserve a genuinely streaming multipart parser for the
explicit streaming-upload path, while using container getParts() for ordinary
uploads. Removing Commons FileUpload completely would require another streaming
implementation, or an explicitly agreed compatibility change. This concern
should remain open until that decision is addressed.
--
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]