diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java index 726bbc33e68..269b0403cb1 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java @@ -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; 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 { 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 { * 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 { 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 { @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; } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHttpHandlerAdapter.java b/spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHttpHandlerAdapter.java index e9dbb9642b5..741bb8d8e8f 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHttpHandlerAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHttpHandlerAdapter.java @@ -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 { release = false; return dataBuffer; } + else if (read == -1) { + return EOF_BUFFER; + } else { return null; }