Skip to content

Commit

Permalink
Use stock JRE Charset instead of magic string
Browse files Browse the repository at this point in the history
Clean up exceptions on non-public methods
  • Loading branch information
garydgregory committed Jul 16, 2023
1 parent 269aef1 commit 0145d1a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.apache.mina.proxy.handlers.socks;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.apache.mina.core.buffer.IoBuffer;
Expand Down Expand Up @@ -72,8 +73,8 @@ public void doHandshake(final NextFilter nextFilter) {
protected void writeRequest(final NextFilter nextFilter, final SocksProxyRequest request) {
try {
boolean isV4ARequest = Arrays.equals(request.getIpAddress(), SocksProxyConstants.FAKE_IP);
byte[] userID = request.getUserName() != null ? request.getUserName().getBytes("ASCII") : null;
byte[] host = request.getHost() != null ? request.getHost().getBytes("ASCII") : null;
byte[] userID = request.getUserName() != null ? request.getUserName().getBytes(StandardCharsets.US_ASCII) : null;
byte[] host = request.getHost() != null ? request.getHost().getBytes(StandardCharsets.US_ASCII) : null;
int len = 9 + userID.length;

if (isV4ARequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
*/
package org.apache.mina.proxy.handlers.socks;

import java.io.UnsupportedEncodingException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.filterchain.IoFilter.NextFilter;
Expand Down Expand Up @@ -113,10 +113,8 @@ private IoBuffer encodeInitialGreetingPacket(final SocksProxyRequest request) {
*
* @param request the socks proxy request data
* @return the encoded buffer
* @throws UnsupportedEncodingException if request's hostname charset
* can't be converted to ASCII.
*/
private IoBuffer encodeProxyRequestPacket(final SocksProxyRequest request) throws UnsupportedEncodingException {
private IoBuffer encodeProxyRequestPacket(final SocksProxyRequest request) {
int len = 6;
InetSocketAddress adr = request.getEndpointAddress();
byte addressType = 0;
Expand All @@ -131,7 +129,7 @@ private IoBuffer encodeProxyRequestPacket(final SocksProxyRequest request) throw
addressType = SocksProxyConstants.IPV4_ADDRESS_TYPE;
}
} else {
host = request.getHost() != null ? request.getHost().getBytes("ASCII") : null;
host = request.getHost() != null ? request.getHost().getBytes(StandardCharsets.US_ASCII) : null;

if (host != null) {
len += 1 + host.length;
Expand Down Expand Up @@ -167,11 +165,9 @@ private IoBuffer encodeProxyRequestPacket(final SocksProxyRequest request) throw
* @return the encoded buffer, if null then authentication step is over
* and handshake process can jump immediately to the next step without waiting
* for a server reply.
* @throws UnsupportedEncodingException if some string charset convertion fails
* @throws GSSException when something fails while using GSSAPI
*/
private IoBuffer encodeAuthenticationPacket(final SocksProxyRequest request) throws UnsupportedEncodingException,
GSSException {
private IoBuffer encodeAuthenticationPacket(final SocksProxyRequest request) throws GSSException {
byte method = ((Byte) getSession().getAttribute(Socks5LogicHandler.SELECTED_AUTH_METHOD)).byteValue();

switch (method) {
Expand All @@ -186,8 +182,8 @@ private IoBuffer encodeAuthenticationPacket(final SocksProxyRequest request) thr

case SocksProxyConstants.BASIC_AUTH:
// The basic auth scheme packet is sent
byte[] user = request.getUserName().getBytes("ASCII");
byte[] pwd = request.getPassword().getBytes("ASCII");
byte[] user = request.getUserName().getBytes(StandardCharsets.US_ASCII);
byte[] pwd = request.getPassword().getBytes(StandardCharsets.US_ASCII);
IoBuffer buf = IoBuffer.allocate(3 + user.length + pwd.length);

buf.put(SocksProxyConstants.BASIC_AUTH_SUBNEGOTIATION_VERSION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.mina.proxy.utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
* ByteUtilities.java - Byte manipulation functions.
Expand Down Expand Up @@ -192,21 +193,21 @@ public static final void changeByteEndianess(byte[] b, int offset, int length) {
*
* @param s the string to convert
* @return the result byte array
* @throws UnsupportedEncodingException if the string is not an OEM string
* @throws UnsupportedEncodingException Never thrown.
*/
public static final byte[] getOEMStringAsByteArray(String s) throws UnsupportedEncodingException {
return s.getBytes("ASCII");
return s.getBytes(StandardCharsets.US_ASCII);
}

/**
* Converts an UTF-16LE string as defined in NTLM protocol to a byte array.
*
* @param s the string to convert
* @return the result byte array
* @throws UnsupportedEncodingException if the string is not an UTF-16LE string
* @throws UnsupportedEncodingException Never thrown.
*/
public static final byte[] getUTFStringAsByteArray(String s) throws UnsupportedEncodingException {
return s.getBytes("UTF-16LE");
return s.getBytes(StandardCharsets.UTF_16LE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -288,7 +289,7 @@ public static String stringTo8859_1(String str) throws UnsupportedEncodingExcept
return "";
}

return new String(str.getBytes("UTF8"), "8859_1");
return new String(str.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.mina.transport;

import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.future.ConnectFuture;
Expand Down Expand Up @@ -167,7 +168,7 @@ public void testSuspendResumeReadWrite() throws Exception {
}

private void write(IoSession session, String s) throws Exception {
session.write(IoBuffer.wrap(s.getBytes("ASCII")));
session.write(IoBuffer.wrap(s.getBytes(StandardCharsets.US_ASCII)));
}

private int read(IoSession session) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import static org.junit.Assert.assertTrue;

import java.nio.charset.StandardCharsets;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.filterchain.IoFilterChain;
import org.apache.mina.core.filterchain.IoFilter.NextFilter;
Expand Down Expand Up @@ -102,7 +104,7 @@ public void setUp() {
@Test
public void testCompression() throws Exception {
// prepare the input data
IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
IoBuffer buf = IoBuffer.wrap(strCompress.getBytes(StandardCharsets.UTF_8));
IoBuffer actualOutput = actualDeflater.deflate(buf);
buf.flip();
WriteRequest writeRequest = new DefaultWriteRequest(buf);
Expand Down Expand Up @@ -147,7 +149,7 @@ public void testCompression() throws Exception {
@Test
public void testDecompression() throws Exception {
// prepare the input data
IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
IoBuffer buf = IoBuffer.wrap(strCompress.getBytes(StandardCharsets.UTF_8));
IoBuffer byteInput = actualDeflater.deflate(buf);
IoBuffer actualOutput = actualInflater.inflate(byteInput);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.apache.mina.core.buffer.IoBuffer;
import org.junit.Before;
Expand Down Expand Up @@ -52,7 +53,7 @@ public void testCompression() throws Exception {
for (int i = 0; i < 10; i++) {
strInput += "The quick brown fox jumps over the lazy dog. ";
}
IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes(StandardCharsets.UTF_8));

// increase the count to have the compression and decompression
// done using the same instance of Zlib
Expand All @@ -68,21 +69,21 @@ public void testCompression() throws Exception {
@Test
public void testCorruptedData() throws Exception {
String strInput = "Hello World";
IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes(StandardCharsets.UTF_8));

IoBuffer byteCompressed = deflater.deflate(byteInput);
// change the contents to something else. Since this doesn't check
// for integrity, it wont throw an exception
byteCompressed.put(5, (byte) 0xa);
IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
String strOutput = byteUncompressed.getString(StandardCharsets.UTF_8.newDecoder());
assertFalse(strOutput.equals(strInput));
}

@Test
public void testCorruptedHeader() throws Exception {
String strInput = "Hello World";
IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes(StandardCharsets.UTF_8));

IoBuffer byteCompressed = deflater.deflate(byteInput);
// write a bad value into the zlib header. Make sure that
Expand All @@ -103,7 +104,7 @@ public void testFragments() throws Exception {
for (int i = 0; i < 10; i++) {
strInput += "The quick brown fox jumps over the lazy dog. ";
}
IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes(StandardCharsets.UTF_8));
IoBuffer byteCompressed = null;

for (int i = 0; i < 5; i++) {
Expand Down

0 comments on commit 0145d1a

Please sign in to comment.