Skip to content

Commit

Permalink
Merge pull request #1691 from fl4via/backport-fixes_2.3.x
Browse files Browse the repository at this point in the history
[UNDERTOW-2457] Backport: Share bytes across multiples attempts to parse v1 by …
  • Loading branch information
fl4via authored Oct 21, 2024
2 parents a2f4759 + 8f6f4d2 commit e6eb76a
Showing 1 changed file with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ProxyProtocolReadListener implements ChannelListener<StreamSourceChannel>
private final UndertowXnioSsl ssl;
private final ByteBufferPool bufferPool;
private final OptionMap sslOptionMap;
private final StringBuilder stringBuilder = new StringBuilder();

private int byteCount;
private String protocol;
Expand Down Expand Up @@ -222,7 +223,6 @@ private void parseProxyProtocolV2(PooledByteBuffer buffer, AtomicBoolean freeBuf
}

private void parseProxyProtocolV1(PooledByteBuffer buffer, AtomicBoolean freeBuffer) throws IOException {
final StringBuilder stringBuilder = new StringBuilder();
while (buffer.getBuffer().hasRemaining()) {
char c = (char) buffer.getBuffer().get();
if (byteCount < NAME.length) {
Expand Down Expand Up @@ -281,31 +281,46 @@ private void parseProxyProtocolV1(PooledByteBuffer buffer, AtomicBoolean freeBuf
throw UndertowMessages.MESSAGES.invalidProxyHeader();
}
} else if (sourceAddress == null) {
sourceAddress = parseAddress(stringBuilder.toString(), protocol);
stringBuilder.setLength(0);
try {
sourceAddress = parseAddress(stringBuilder.toString(), protocol);
} finally {
stringBuilder.setLength(0);
}
} else if (destAddress == null) {
destAddress = parseAddress(stringBuilder.toString(), protocol);
stringBuilder.setLength(0);
try {
destAddress = parseAddress(stringBuilder.toString(), protocol);
} finally {
stringBuilder.setLength(0);
}
} else {
sourcePort = Integer.parseInt(stringBuilder.toString());
stringBuilder.setLength(0);
try {
sourcePort = Integer.parseInt(stringBuilder.toString());
} finally {
stringBuilder.setLength(0);
}
}
break;
case '\r':
if (destPort == -1 && sourcePort != -1 && !carriageReturnSeen && stringBuilder.length() > 0) {
destPort = Integer.parseInt(stringBuilder.toString());
stringBuilder.setLength(0);
try {
destPort = Integer.parseInt(stringBuilder.toString());
} finally {
stringBuilder.setLength(0);
}
carriageReturnSeen = true;
} else if (protocol == null) {
if (UNKNOWN.equals(stringBuilder.toString())) {
parsingUnknown = true;
carriageReturnSeen = true;
}
stringBuilder.setLength(0);
} else {
stringBuilder.setLength(0);
throw UndertowMessages.MESSAGES.invalidProxyHeader();
}
break;
case '\n':
stringBuilder.setLength(0);
throw UndertowMessages.MESSAGES.invalidProxyHeader();
default:
stringBuilder.append(c);
Expand Down

0 comments on commit e6eb76a

Please sign in to comment.