SAM: Use SAMHandler.writeString() where possible,

Use DataHelper.getASCII() for byte conversion
This commit is contained in:
zzz
2014-06-27 16:46:57 +00:00
parent 7888705b01
commit 38a4728283
5 changed files with 16 additions and 29 deletions

View File

@ -519,13 +519,8 @@ public class SAMBridge implements Runnable, ClientApp {
} catch (SAMException e) { } catch (SAMException e) {
if (_log.shouldLog(Log.ERROR)) if (_log.shouldLog(Log.ERROR))
_log.error("SAM error: " + e.getMessage(), e); _log.error("SAM error: " + e.getMessage(), e);
try {
String reply = "HELLO REPLY RESULT=I2P_ERROR MESSAGE=\"" + e.getMessage() + "\"\n"; String reply = "HELLO REPLY RESULT=I2P_ERROR MESSAGE=\"" + e.getMessage() + "\"\n";
s.write(ByteBuffer.wrap(reply.getBytes("ISO-8859-1"))); SAMHandler.writeString(reply, s);
} catch (IOException ioe) {
if (_log.shouldLog(Log.ERROR))
_log.error("SAM Error sending error reply", ioe);
}
try { s.close(); } catch (IOException ioe) {} try { s.close(); } catch (IOException ioe) {}
} catch (Exception ee) { } catch (Exception ee) {
try { s.close(); } catch (IOException ioe) {} try { s.close(); } catch (IOException ioe) {}

View File

@ -13,6 +13,7 @@ import java.nio.channels.SocketChannel;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Properties; import java.util.Properties;
import net.i2p.data.DataHelper;
import net.i2p.util.I2PAppThread; import net.i2p.util.I2PAppThread;
import net.i2p.util.Log; import net.i2p.util.Log;
@ -132,10 +133,11 @@ abstract class SAMHandler implements Runnable {
} }
} }
/** @return success */
public static boolean writeString(String str, SocketChannel out) public static boolean writeString(String str, SocketChannel out)
{ {
try { try {
writeBytes(ByteBuffer.wrap(str.getBytes("ISO-8859-1")), out); writeBytes(ByteBuffer.wrap(DataHelper.getASCII(str)), out);
} catch (IOException e) { } catch (IOException e) {
//_log.debug("Caught IOException", e); //_log.debug("Caught IOException", e);
return false; return false;

View File

@ -84,23 +84,13 @@ class SAMHandlerFactory {
String ver = chooseBestVersion(minVer, maxVer); String ver = chooseBestVersion(minVer, maxVer);
try {
if (ver == null) { if (ver == null) {
s.write(ByteBuffer.wrap(("HELLO REPLY RESULT=NOVERSION\n").getBytes("ISO-8859-1"))); SAMHandler.writeString("HELLO REPLY RESULT=NOVERSION\n", s);
return null ; return null;
} }
// Let's answer positively // Let's answer positively
s.write(ByteBuffer.wrap(("HELLO REPLY RESULT=OK VERSION=" if (!SAMHandler.writeString("HELLO REPLY RESULT=OK VERSION=" + ver + "\n", s))
+ ver + "\n").getBytes("ISO-8859-1"))); throw new SAMException("Error writing to socket");
} catch (UnsupportedEncodingException e) {
log.error("Caught UnsupportedEncodingException ("
+ e.getMessage() + ")");
throw new SAMException("Character encoding error: "
+ e.getMessage());
} catch (IOException e) {
throw new SAMException("Error writing to socket: "
+ e.getMessage());
}
// ...and instantiate the right SAM handler // ...and instantiate the right SAM handler
int verMajor = getMajor(ver); int verMajor = getMajor(ver);

View File

@ -782,9 +782,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
ByteArrayOutputStream msg = new ByteArrayOutputStream(); ByteArrayOutputStream msg = new ByteArrayOutputStream();
String msgText = "RAW RECEIVED SIZE=" + data.length + "\n"; String msgText = "RAW RECEIVED SIZE=" + data.length + "\n";
msg.write(msgText.getBytes("ISO-8859-1")); msg.write(DataHelper.getASCII(msgText));
msg.write(data); msg.write(data);
msg.flush();
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("sending to client: " + msgText); _log.debug("sending to client: " + msgText);
@ -820,7 +819,7 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
String msgText = "DATAGRAM RECEIVED DESTINATION=" + sender.toBase64() String msgText = "DATAGRAM RECEIVED DESTINATION=" + sender.toBase64()
+ " SIZE=" + data.length + "\n"; + " SIZE=" + data.length + "\n";
msg.write(msgText.getBytes("ISO-8859-1")); msg.write(DataHelper.getASCII(msgText));
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("sending to client: " + msgText); _log.debug("sending to client: " + msgText);
@ -926,7 +925,7 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("sending to client: " + msgText); _log.debug("sending to client: " + msgText);
ByteBuffer prefix = ByteBuffer.wrap(msgText.getBytes("ISO-8859-1")); ByteBuffer prefix = ByteBuffer.wrap(DataHelper.getASCII(msgText));
Object writeLock = getWriteLock(); Object writeLock = getWriteLock();
synchronized (writeLock) { synchronized (writeLock) {

View File

@ -10,6 +10,7 @@ import java.util.Properties;
import net.i2p.client.I2PSessionException; import net.i2p.client.I2PSessionException;
import net.i2p.data.DataFormatException; import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.data.Destination; import net.i2p.data.Destination;
import net.i2p.util.Log; import net.i2p.util.Log;
@ -76,7 +77,7 @@ class SAMv3DatagramSession extends SAMDatagramSession implements SAMv3Handler.Se
} else { } else {
String msg = sender.toBase64()+"\n"; String msg = sender.toBase64()+"\n";
ByteBuffer msgBuf = ByteBuffer.allocate(msg.length()+data.length); ByteBuffer msgBuf = ByteBuffer.allocate(msg.length()+data.length);
msgBuf.put(msg.getBytes("ISO-8859-1")); msgBuf.put(DataHelper.getASCII(msg));
msgBuf.put(data); msgBuf.put(data);
msgBuf.flip(); msgBuf.flip();
this.server.send(this.clientAddress, msgBuf); this.server.send(this.clientAddress, msgBuf);