Browse Source

Call onAllDataRead when read() returns -1

When read returns -1, we know we've reached the end of input. Instead
of waiting for the onAllDataRead container callback, this commit
proactively calls onAllDataRead.

Issue: SPR-16521
pull/1697/merge
Rossen Stoyanchev 8 years ago
parent
commit
a6d527e57a
  1. 19
      spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java
  2. 5
      spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHttpHandlerAdapter.java

19
spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -38,6 +38,7 @@ import reactor.core.publisher.Flux; @@ -38,6 +38,7 @@ import reactor.core.publisher.Flux;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@ -60,6 +61,8 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest { @@ -60,6 +61,8 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest {
private static final String SSL_SESSION_ID_ATTRIBUTE = "javax.servlet.request.ssl_session_id";
static final DataBuffer EOF_BUFFER = new DefaultDataBufferFactory().allocateBuffer(0);
protected final Log logger = LogFactory.getLog(getClass());
@ -197,7 +200,7 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest { @@ -197,7 +200,7 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest {
* Invoked only when {@link ServletInputStream#isReady()} returns "true".
*/
@Nullable
protected DataBuffer readFromInputStream() throws IOException {
DataBuffer readFromInputStream() throws IOException {
int read = this.request.getInputStream().read(this.buffer);
if (logger.isTraceEnabled()) {
logger.trace("InputStream read returned " + read + (read != -1 ? " bytes" : ""));
@ -208,6 +211,9 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest { @@ -208,6 +211,9 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest {
dataBuffer.write(this.buffer, 0, read);
return dataBuffer;
}
else if (read == -1) {
return EOF_BUFFER;
}
return null;
}
@ -266,7 +272,14 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest { @@ -266,7 +272,14 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest {
@Nullable
protected DataBuffer read() throws IOException {
if (this.inputStream.isReady()) {
return readFromInputStream();
DataBuffer dataBuffer = readFromInputStream();
if (dataBuffer != EOF_BUFFER) {
return dataBuffer;
}
else {
// No need to wait for container callback...
onAllDataRead();
}
}
return null;
}

5
spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHttpHandlerAdapter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -93,6 +93,9 @@ public class TomcatHttpHandlerAdapter extends ServletHttpHandlerAdapter { @@ -93,6 +93,9 @@ public class TomcatHttpHandlerAdapter extends ServletHttpHandlerAdapter {
release = false;
return dataBuffer;
}
else if (read == -1) {
return EOF_BUFFER;
}
else {
return null;
}

Loading…
Cancel
Save