Skip to content

Commit 8443b16

Browse files
authored
Issue #12775 avoid unnecessary exception in getInputStream (#12789)
* Issue #12775 avoid unnecessary exception in getInputStream
1 parent 4d10c20 commit 8443b16

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -917,24 +917,38 @@ public Content.Chunk read()
917917
try
918918
{
919919
HttpStream stream;
920+
boolean expecting100;
921+
HttpChannelState httpChannel;
920922
try (AutoLock ignored = _lock.lock())
921923
{
922-
HttpChannelState httpChannel = lockedGetHttpChannelState();
923-
924+
httpChannel = lockedGetHttpChannelState();
924925
Content.Chunk error = httpChannel._readFailure;
925926
httpChannel._readFailure = Content.Chunk.next(error);
926927
if (error != null)
927928
return error;
928929

929930
stream = httpChannel._stream;
931+
expecting100 = httpChannel._expects100Continue;
930932
}
931933
Content.Chunk chunk = stream.read();
932934

933935
if (LOG.isDebugEnabled())
934936
LOG.debug("read {}", chunk);
935937

936-
if (chunk != null && chunk.hasRemaining())
937-
_contentBytesRead.add(chunk.getByteBuffer().remaining());
938+
if (chunk == null)
939+
return null;
940+
941+
if (expecting100)
942+
{
943+
// No need to send 100 continues as content has already arrived
944+
try (AutoLock ignored = _lock.lock())
945+
{
946+
httpChannel._expects100Continue = false;
947+
}
948+
}
949+
950+
if (chunk.hasRemaining())
951+
_contentBytesRead.add(chunk.remaining());
938952

939953
if (chunk instanceof Trailers trailers)
940954
_trailers = trailers.getTrailers();

jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletApiRequest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.eclipse.jetty.http.HttpField;
6464
import org.eclipse.jetty.http.HttpFields;
6565
import org.eclipse.jetty.http.HttpHeader;
66+
import org.eclipse.jetty.http.HttpHeaderValue;
6667
import org.eclipse.jetty.http.HttpStatus;
6768
import org.eclipse.jetty.http.HttpURI;
6869
import org.eclipse.jetty.http.HttpVersion;
@@ -916,9 +917,12 @@ public ServletInputStream getInputStream() throws IOException
916917
{
917918
if (_inputState != ServletContextRequest.INPUT_NONE && _inputState != ServletContextRequest.INPUT_STREAM)
918919
throw new IllegalStateException("READER");
920+
921+
// Try to write a 100 continue if it is necessary
922+
if (_inputState == ServletContextRequest.INPUT_NONE && _servletContextRequest.getHeaders().contains(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()))
923+
_servletChannel.getResponse().writeInterim(HttpStatus.CONTINUE_100, HttpFields.EMPTY);
924+
919925
_inputState = ServletContextRequest.INPUT_STREAM;
920-
// Try to write a 100 continue, ignoring failure result if it was not necessary.
921-
_servletChannel.getResponse().writeInterim(HttpStatus.CONTINUE_100, HttpFields.EMPTY);
922926
return getServletRequestInfo().getHttpInput();
923927
}
924928

jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/Request.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,12 @@ public ServletInputStream getInputStream() throws IOException
914914
{
915915
if (_inputState != INPUT_NONE && _inputState != INPUT_STREAM)
916916
throw new IllegalStateException("READER");
917+
918+
// Try to write a 100 continue if it is necessary.
919+
if (_inputState == INPUT_NONE && _coreRequest.getHeaders().contains(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()))
920+
_channel.getCoreResponse().writeInterim(HttpStatus.CONTINUE_100, HttpFields.EMPTY);
921+
917922
_inputState = INPUT_STREAM;
918-
// Try to write a 100 continue, ignoring failure result if it was not necessary.
919-
_channel.getCoreResponse().writeInterim(HttpStatus.CONTINUE_100, HttpFields.EMPTY);
920923
return _input;
921924
}
922925

0 commit comments

Comments
 (0)