Added test for multi header, HttpObjectDecoder performance improvement for multi header, removed empty else block.
Motivation: For multi-line headers HttpObjectDecoder uses StringBuilder.append(a).append(b) pattern that could be easily replaced with regular a + b. Also oparations with a and b moved out from concat operation to make it friendly for StringOptimizeConcat optimization and thus - faster. Modification: StringBuilder.append(a).append(b) reaplced with a + b. Operations with a and b moved out from concat oparation. Result: Code simpler to read and faster.
This commit is contained in:
parent
82a43727c3
commit
81f9434ec7
@ -574,12 +574,11 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
|
|||||||
do {
|
do {
|
||||||
char firstChar = line.charAt(0);
|
char firstChar = line.charAt(0);
|
||||||
if (name != null && (firstChar == ' ' || firstChar == '\t')) {
|
if (name != null && (firstChar == ' ' || firstChar == '\t')) {
|
||||||
|
//please do not make one line from below code
|
||||||
|
//as it breaks +XX:OptimizeStringConcat optimization
|
||||||
String trimmedLine = line.toString().trim();
|
String trimmedLine = line.toString().trim();
|
||||||
StringBuilder buf = new StringBuilder(value.length() + trimmedLine.length() + 1);
|
String valueStr = String.valueOf(value);
|
||||||
buf.append(value)
|
value = valueStr + ' ' + trimmedLine;
|
||||||
.append(' ')
|
|
||||||
.append(trimmedLine);
|
|
||||||
value = buf.toString();
|
|
||||||
} else {
|
} else {
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
headers.add(name, value);
|
headers.add(name, value);
|
||||||
@ -641,14 +640,11 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
|
|||||||
List<String> current = trailer.trailingHeaders().getAll(lastHeader);
|
List<String> current = trailer.trailingHeaders().getAll(lastHeader);
|
||||||
if (!current.isEmpty()) {
|
if (!current.isEmpty()) {
|
||||||
int lastPos = current.size() - 1;
|
int lastPos = current.size() - 1;
|
||||||
|
//please do not make one line from below code
|
||||||
|
//as it breaks +XX:OptimizeStringConcat optimization
|
||||||
String lineTrimmed = line.toString().trim();
|
String lineTrimmed = line.toString().trim();
|
||||||
CharSequence currentLastPos = current.get(lastPos);
|
String currentLastPos = current.get(lastPos);
|
||||||
StringBuilder b = new StringBuilder(currentLastPos.length() + lineTrimmed.length());
|
current.set(lastPos, currentLastPos + lineTrimmed);
|
||||||
b.append(currentLastPos)
|
|
||||||
.append(lineTrimmed);
|
|
||||||
current.set(lastPos, b.toString());
|
|
||||||
} else {
|
|
||||||
// Content-Length, Transfer-Encoding, or Trailer
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
splitHeader(line);
|
splitHeader(line);
|
||||||
|
@ -174,6 +174,29 @@ public class HttpRequestDecoderTest {
|
|||||||
assertNull(channel.readInbound());
|
assertNull(channel.readInbound());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMultiLineHeader() {
|
||||||
|
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
|
||||||
|
String crlf = "\r\n";
|
||||||
|
String request = "GET /some/path HTTP/1.1" + crlf +
|
||||||
|
"Host: localhost" + crlf +
|
||||||
|
"MyTestHeader: part1" + crlf +
|
||||||
|
" newLinePart2" + crlf +
|
||||||
|
"MyTestHeader2: part21" + crlf +
|
||||||
|
"\t newLinePart22"
|
||||||
|
+ crlf + crlf;
|
||||||
|
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(request, CharsetUtil.US_ASCII)));
|
||||||
|
HttpRequest req = channel.readInbound();
|
||||||
|
assertEquals("part1 newLinePart2", req.headers().get(of("MyTestHeader")));
|
||||||
|
assertEquals("part21 newLinePart22", req.headers().get(of("MyTestHeader2")));
|
||||||
|
|
||||||
|
LastHttpContent c = channel.readInbound();
|
||||||
|
c.release();
|
||||||
|
|
||||||
|
assertFalse(channel.finish());
|
||||||
|
assertNull(channel.readInbound());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyHeaderValue() {
|
public void testEmptyHeaderValue() {
|
||||||
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
|
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
|
||||||
@ -181,7 +204,7 @@ public class HttpRequestDecoderTest {
|
|||||||
String request = "GET /some/path HTTP/1.1" + crlf +
|
String request = "GET /some/path HTTP/1.1" + crlf +
|
||||||
"Host: localhost" + crlf +
|
"Host: localhost" + crlf +
|
||||||
"EmptyHeader:" + crlf + crlf;
|
"EmptyHeader:" + crlf + crlf;
|
||||||
channel.writeInbound(Unpooled.wrappedBuffer(request.getBytes(CharsetUtil.US_ASCII)));
|
channel.writeInbound(Unpooled.copiedBuffer(request, CharsetUtil.US_ASCII));
|
||||||
HttpRequest req = channel.readInbound();
|
HttpRequest req = channel.readInbound();
|
||||||
assertEquals("", req.headers().get(of("EmptyHeader")));
|
assertEquals("", req.headers().get(of("EmptyHeader")));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user