Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UNDERTOW-2457] Share bytes across multiples attempts to parse v1 by … #1672

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading