Also use java.util.Base64 in handler-proxy module (#8850)

Motivation

In netty 5 we can use java.util.Base64 which is simpler/faster for some
purposes than netty's own ByteBuf-based Base64 utilities. This covers a
couple of additional places to the one changed in #8837

Modifications

Modify HttpProxyHandler and HttpProxyServer classes to use
java.util.Base64 instead of
io.netty.handler.codec.base64.Base64 (latter is a test
one).

Result

More efficient base64 processing in proxy impl.
This commit is contained in:
Nick Hill 2019-02-12 08:04:09 -08:00 committed by Norman Maurer
parent b9d277dbcb
commit d539864f83
2 changed files with 15 additions and 19 deletions

View File

@ -18,11 +18,9 @@ package io.netty.handler.proxy;
import static java.util.Objects.requireNonNull;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.base64.Base64;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpClientCodec;
@ -35,16 +33,20 @@ import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.AsciiString;
import io.netty.util.CharsetUtil;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
public final class HttpProxyHandler extends ProxyHandler {
private static final String PROTOCOL = "http";
private static final String AUTH_BASIC = "basic";
private static final byte[] BASIC_BYTES = "Basic ".getBytes(StandardCharsets.UTF_8);
private final HttpClientCodec codec = new HttpClientCodec();
private final String username;
private final String password;
@ -93,13 +95,12 @@ public final class HttpProxyHandler extends ProxyHandler {
this.username = username;
this.password = password;
ByteBuf authz = Unpooled.copiedBuffer(username + ':' + password, CharsetUtil.UTF_8);
ByteBuf authzBase64 = Base64.encode(authz, false);
byte[] authzBase64 = Base64.getEncoder().encode(
(username + ':' + password).getBytes(StandardCharsets.UTF_8));
byte[] authzHeader = Arrays.copyOf(BASIC_BYTES, 6 + authzBase64.length);
System.arraycopy(authzBase64, 0, authzHeader, 6, authzBase64.length);
authorization = new AsciiString("Basic " + authzBase64.toString(CharsetUtil.US_ASCII));
authz.release();
authzBase64.release();
authorization = new AsciiString(authzHeader, /*copy=*/ false);
this.outboundHeaders = headers;
this.ignoreDefaultPortsInConnectHostHeader = ignoreDefaultPortsInConnectHostHeader;

View File

@ -16,13 +16,11 @@
package io.netty.handler.proxy;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.base64.Base64;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
@ -32,11 +30,12 @@ import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.SocketUtils;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.*;
@ -88,15 +87,11 @@ final class HttpProxyServer extends ProxyServer {
CharSequence authz = req.headers().get(HttpHeaderNames.PROXY_AUTHORIZATION);
if (authz != null) {
String[] authzParts = authz.toString().split(" ", 2);
ByteBuf authzBuf64 = Unpooled.copiedBuffer(authzParts[1], CharsetUtil.US_ASCII);
ByteBuf authzBuf = Base64.decode(authzBuf64);
byte[] authzCreds = Base64.getDecoder().decode(authzParts[1]);
String expectedAuthz = username + ':' + password;
authzSuccess = "Basic".equals(authzParts[0]) &&
expectedAuthz.equals(authzBuf.toString(CharsetUtil.US_ASCII));
authzBuf64.release();
authzBuf.release();
expectedAuthz.equals(new String(authzCreds, StandardCharsets.UTF_8));
}
} else {
authzSuccess = true;
@ -159,7 +154,7 @@ final class HttpProxyServer extends ProxyServer {
ctx.pipeline().get(HttpServerCodec.class).removeOutboundHandler();
if (sendGreeting) {
ctx.write(Unpooled.copiedBuffer("0\n", CharsetUtil.US_ASCII));
ctx.write(Unpooled.copiedBuffer("0\n", StandardCharsets.US_ASCII));
}
return true;