forked from I2P_Developers/i2p.i2p
SAM: Use SAMHandler.writeString() where possible,
Use DataHelper.getASCII() for byte conversion
This commit is contained in:
@ -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";
|
SAMHandler.writeString(reply, s);
|
||||||
s.write(ByteBuffer.wrap(reply.getBytes("ISO-8859-1")));
|
|
||||||
} 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) {}
|
||||||
|
@ -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;
|
||||||
|
@ -84,23 +84,13 @@ class SAMHandlerFactory {
|
|||||||
|
|
||||||
String ver = chooseBestVersion(minVer, maxVer);
|
String ver = chooseBestVersion(minVer, maxVer);
|
||||||
|
|
||||||
try {
|
if (ver == null) {
|
||||||
if (ver == null) {
|
SAMHandler.writeString("HELLO REPLY RESULT=NOVERSION\n", s);
|
||||||
s.write(ByteBuffer.wrap(("HELLO REPLY RESULT=NOVERSION\n").getBytes("ISO-8859-1")));
|
return null;
|
||||||
return null ;
|
|
||||||
}
|
|
||||||
// Let's answer positively
|
|
||||||
s.write(ByteBuffer.wrap(("HELLO REPLY RESULT=OK VERSION="
|
|
||||||
+ ver + "\n").getBytes("ISO-8859-1")));
|
|
||||||
} 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());
|
|
||||||
}
|
}
|
||||||
|
// Let's answer positively
|
||||||
|
if (!SAMHandler.writeString("HELLO REPLY RESULT=OK VERSION=" + ver + "\n", s))
|
||||||
|
throw new SAMException("Error writing to socket");
|
||||||
|
|
||||||
// ...and instantiate the right SAM handler
|
// ...and instantiate the right SAM handler
|
||||||
int verMajor = getMajor(ver);
|
int verMajor = getMajor(ver);
|
||||||
|
@ -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) {
|
||||||
|
@ -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);
|
||||||
|
Reference in New Issue
Block a user