This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch 2.2.X
in repository https://gitbox.apache.org/repos/asf/mina.git
The following commit(s) were added to refs/heads/2.2.X by this push:
new 46adecbd5 Fixed some wrongly handled \r\n when split in two lines
during a decoding.
46adecbd5 is described below
commit 46adecbd5bfff9b48218270bc4077891f4781cd4
Author: emmanuel lecharny <[email protected]>
AuthorDate: Sun Nov 15 09:21:13 2026 +0100
Fixed some wrongly handled \r\n when split in two lines during a
decoding.
---
.../statemachine/ConsumeToCrLfDecodingState.java | 21 +++-
.../ConsumeToCrLfDecodingStateTest.java | 125 +++++++++++++++++++++
2 files changed, 140 insertions(+), 6 deletions(-)
diff --git
a/mina-core/src/main/java/org/apache/mina/filter/codec/statemachine/ConsumeToCrLfDecodingState.java
b/mina-core/src/main/java/org/apache/mina/filter/codec/statemachine/ConsumeToCrLfDecodingState.java
index 49b9294ae..b87376702 100644
---
a/mina-core/src/main/java/org/apache/mina/filter/codec/statemachine/ConsumeToCrLfDecodingState.java
+++
b/mina-core/src/main/java/org/apache/mina/filter/codec/statemachine/ConsumeToCrLfDecodingState.java
@@ -33,12 +33,12 @@ public abstract class ConsumeToCrLfDecodingState implements
DecodingState {
/**
* Carriage return character
*/
- private static final byte CR = 13;
+ private static final byte CR = '\r';
/**
* Line feed character
*/
- private static final byte LF = 10;
+ private static final byte LF = '\n';
private boolean lastIsCR;
@@ -95,6 +95,14 @@ public abstract class ConsumeToCrLfDecodingState implements
DecodingState {
if (buffer == null) {
product = IoBuffer.allocate(0);
} else {
+ int bufferPos = buffer.position();
+
+ if ((terminatorPos == beginPos) && (bufferPos > 0) &&
(buffer.get(bufferPos - 1) == CR)) {
+ // The terminating CR arrived in a previous chunk and
was accumulated; exclude it from
+ // the product now that it has been paired with an LF.
+ buffer.position(bufferPos - 1);
+ }
+
product = buffer.flip();
buffer = null;
}
@@ -110,12 +118,13 @@ public abstract class ConsumeToCrLfDecodingState
implements DecodingState {
buffer.setAutoExpand(true);
}
+ // Keep any trailing CR in the accumulation buffer: it is only part of
+ // the terminator if the *next* chunk starts with an LF, which is
+ // handled above when the terminator is found. Rewinding over it here
+ // used to drop bare CRs at chunk boundaries, making the decoded
+ // product depend on how the input was fragmented.
buffer.put(in);
- if (lastIsCR) {
- buffer.position(buffer.position() - 1);
- }
-
return this;
}
diff --git
a/mina-core/src/test/java/org/apache/mina/filter/codec/statemachine/ConsumeToCrLfDecodingStateTest.java
b/mina-core/src/test/java/org/apache/mina/filter/codec/statemachine/ConsumeToCrLfDecodingStateTest.java
new file mode 100644
index 000000000..209f105e3
--- /dev/null
+++
b/mina-core/src/test/java/org/apache/mina/filter/codec/statemachine/ConsumeToCrLfDecodingStateTest.java
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.filter.codec.statemachine;
+
+import static org.junit.Assert.assertEquals;
+
+import java.nio.charset.StandardCharsets;
+
+import org.apache.mina.core.buffer.IoBuffer;
+import org.apache.mina.core.filterchain.IoFilter.NextFilter;
+import org.apache.mina.core.session.IoSession;
+import org.apache.mina.filter.codec.ProtocolDecoderOutput;
+import org.junit.Test;
+
+/**
+ * Tests {@link ConsumeToCrLfDecodingState}, in particular that the decoded
+ * product does not depend on how the input was fragmented.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class ConsumeToCrLfDecodingStateTest {
+
+ private static final ProtocolDecoderOutput NULL_OUT = new
ProtocolDecoderOutput() {
+ @Override
+ public void write(Object message) {
+ // Do nothing
+ }
+
+ @Override
+ public void flush(NextFilter nextFilter, IoSession session) {
+ // Do nothing
+ }
+ };
+
+ /**
+ * Feeds the given chunks into a fresh decoding state and returns the
+ * product produced when the CRLF terminator is reached.
+ */
+ private static String decode(String... chunks) throws Exception {
+ final StringBuilder result = new StringBuilder();
+
+ ConsumeToCrLfDecodingState state = new ConsumeToCrLfDecodingState() {
+ @Override
+ protected DecodingState finishDecode(IoBuffer product,
ProtocolDecoderOutput out) {
+ byte[] bytes = new byte[product.remaining()];
+ product.get(bytes);
+ result.append(new String(bytes, StandardCharsets.US_ASCII));
+ return this;
+ }
+ };
+
+ for (String chunk : chunks) {
+
state.decode(IoBuffer.wrap(chunk.getBytes(StandardCharsets.US_ASCII)),
NULL_OUT);
+ }
+
+ return result.toString();
+ }
+
+ @Test
+ public void testUnfragmented() throws Exception {
+ assertEquals("abc", decode("abc\r\n"));
+ assertEquals("abc\rdef", decode("abc\rdef\r\n"));
+ assertEquals("abc\r", decode("abc\r\r\n"));
+ assertEquals("", decode("\r\n"));
+ }
+
+ @Test
+ public void testTerminatorSplitAcrossChunks() throws Exception {
+ assertEquals("abc", decode("abc\r", "\n"));
+ assertEquals("abc", decode("abc", "\r\n"));
+ assertEquals("abc", decode("abc", "\r", "\n"));
+ assertEquals("", decode("\r", "\n"));
+ }
+
+ /**
+ * A bare CR followed by a non-LF byte must produce the same product
+ * whether or not a chunk boundary falls right after the CR.
+ */
+ @Test
+ public void testBareCrAtChunkBoundaryIsPreserved() throws Exception {
+ assertEquals("abc\rdef", decode("abc\r", "def\r\n"));
+ assertEquals("abc\r", decode("abc\r", "\r\n"));
+ assertEquals("abc\rdef", decode("ab", "c\r", "de", "f\r\n"));
+ }
+
+ /**
+ * An empty chunk arriving after a chunk ending in CR must not eat any
+ * accumulated data.
+ */
+ @Test
+ public void testEmptyChunkAfterCrDoesNotDropData() throws Exception {
+ assertEquals("abc\rdef", decode("abc\r", "", "def\r\n"));
+ assertEquals("abc", decode("abc\r", "", "\n"));
+ }
+
+ /**
+ * Every possible split of the same input must yield the same product.
+ */
+ @Test
+ public void testFragmentationInvariance() throws Exception {
+ String input = "abc\rdef\r\n";
+
+ for (int split = 1; split < input.length(); split++) {
+ assertEquals("split at " + split, "abc\rdef",
+ decode(input.substring(0, split), input.substring(split)));
+ }
+ }
+}