[
https://issues.apache.org/jira/browse/SCB-992?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16669467#comment-16669467
]
ASF GitHub Bot commented on SCB-992:
------------------------------------
liubao68 closed pull request #973: [SCB-992] Synchronous open source code from
Vert.x 3.5.3 version
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/973
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestBodyHandler.java
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestBodyHandler.java
index a02dffc30..7e8105fad 100644
---
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestBodyHandler.java
+++
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestBodyHandler.java
@@ -24,6 +24,7 @@
import java.io.File;
import java.util.Set;
import java.util.UUID;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import javax.ws.rs.core.Response.Status;
@@ -33,7 +34,6 @@
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import io.netty.handler.codec.http.HttpHeaderValues;
-import
io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.ErrorDataDecoderException;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.file.FileSystem;
@@ -78,6 +78,11 @@ public RestBodyHandler(String uploadDirectory) {
@Override
public void handle(RoutingContext context) {
HttpServerRequest request = context.request();
+ if (request.headers().contains(HttpHeaders.UPGRADE, HttpHeaders.WEBSOCKET,
true)) {
+ context.next();
+ return;
+ }
+
// we need to keep state since we can be called again on reroute
Boolean handled = context.get(BODY_HANDLED);
if (handled == null || !handled) {
@@ -129,6 +134,8 @@ public BodyHandler setDeleteUploadedFilesOnEnd(boolean
deleteUploadedFilesOnEnd)
private AtomicInteger uploadCount = new AtomicInteger();
+ AtomicBoolean cleanup = new AtomicBoolean(false);
+
private boolean ended;
private long uploadSize = 0L;
@@ -159,21 +166,37 @@ public BodyHandler setDeleteUploadedFilesOnEnd(boolean
deleteUploadedFilesOnEnd)
if (uploadsDir == null) {
failed = true;
CommonExceptionData data = new CommonExceptionData("not support
file upload.");
- throw new
ErrorDataDecoderException(ExceptionFactory.createConsumerException(data));
+ context.fail(ExceptionFactory.createProducerException(data));
+ return;
}
// *** cse end ***
-
+ if (bodyLimit != -1 && upload.isSizeAvailable()) {
+ // we can try to abort even before the upload starts
+ long size = uploadSize + upload.size();
+ if (size > bodyLimit) {
+ failed = true;
+ context.fail(new
InvocationException(Status.REQUEST_ENTITY_TOO_LARGE,
+ Status.REQUEST_ENTITY_TOO_LARGE.getReasonPhrase()));
+ return;
+ }
+ }
// we actually upload to a file with a generated filename
uploadCount.incrementAndGet();
String uploadedFileName = new File(uploadsDir,
UUID.randomUUID().toString()).getPath();
upload.streamToFileSystem(uploadedFileName);
FileUploadImpl fileUpload = new FileUploadImpl(uploadedFileName,
upload);
fileUploads.add(fileUpload);
- upload.exceptionHandler(context::fail);
+ upload.exceptionHandler(t -> {
+ deleteFileUploads();
+ context.fail(t);
+ });
upload.endHandler(v -> uploadEnded());
});
}
- context.request().exceptionHandler(context::fail);
+ context.request().exceptionHandler(t -> {
+ deleteFileUploads();
+ context.fail(t);
+ });
}
private void makeUploadDir(FileSystem fileSystem) {
@@ -196,8 +219,10 @@ public void handle(Buffer buff) {
uploadSize += buff.length();
if (bodyLimit != -1 && uploadSize > bodyLimit) {
failed = true;
+ // enqueue a delete for the error uploads
context.fail(new InvocationException(Status.REQUEST_ENTITY_TOO_LARGE,
Status.REQUEST_ENTITY_TOO_LARGE.getReasonPhrase()));
+ context.vertx().runOnContext(v -> deleteFileUploads());
} else {
// multipart requests will not end up in the request body
// url encoded should also not, however jQuery by default
@@ -228,18 +253,15 @@ void end() {
}
void doEnd() {
- if (deleteUploadedFilesOnEnd) {
- if (failed) {
- deleteFileUploads();
- } else {
- context.addBodyEndHandler(x -> deleteFileUploads());
- }
- }
-
if (failed) {
+ deleteFileUploads();
return;
}
+ if (deleteUploadedFilesOnEnd) {
+ context.addBodyEndHandler(x -> deleteFileUploads());
+ }
+
HttpServerRequest req = context.request();
if (mergeFormAttributes && req.isExpectMultipart()) {
req.params().addAll(req.formAttributes());
@@ -249,22 +271,23 @@ void doEnd() {
}
private void deleteFileUploads() {
- for (FileUpload fileUpload : context.fileUploads()) {
- FileSystem fileSystem = context.vertx().fileSystem();
- String uploadedFileName = fileUpload.uploadedFileName();
- fileSystem.exists(uploadedFileName, existResult -> {
- if (existResult.failed()) {
- LOGGER.warn("Could not detect if uploaded file exists, not
deleting: " + uploadedFileName,
- existResult.cause());
- } else if (existResult.result()) {
- fileSystem.delete(uploadedFileName, deleteResult -> {
- if (deleteResult.failed()) {
- LOGGER.warn("Delete of uploaded file failed: " +
uploadedFileName,
- deleteResult.cause());
- }
- });
- }
- });
+ if (cleanup.compareAndSet(false, true)) {
+ for (FileUpload fileUpload : context.fileUploads()) {
+ FileSystem fileSystem = context.vertx().fileSystem();
+ String uploadedFileName = fileUpload.uploadedFileName();
+ fileSystem.exists(uploadedFileName, existResult -> {
+ if (existResult.failed()) {
+ LOGGER.warn("Could not detect if uploaded file exists, not
deleting: " + uploadedFileName,
+ existResult.cause());
+ } else if (existResult.result()) {
+ fileSystem.delete(uploadedFileName, deleteResult -> {
+ if (deleteResult.failed()) {
+ LOGGER.warn("Delete of uploaded file failed: " +
uploadedFileName, deleteResult.cause());
+ }
+ });
+ }
+ });
+ }
}
}
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Synchronous open source code from Vert.x 3.5.3 version
> ------------------------------------------------------
>
> Key: SCB-992
> URL: https://issues.apache.org/jira/browse/SCB-992
> Project: Apache ServiceComb
> Issue Type: Task
> Components: Java-Chassis
> Reporter: jeho0815
> Assignee: jeho0815
> Priority: Major
> Fix For: java-chassis-1.1.0
>
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)