提交 0458f3d4 authored 作者: Thomas Mueller's avatar Thomas Mueller

Formatting, javadocs, more secure secret

上级 06b743c0
......@@ -79,8 +79,9 @@ public class PgServer implements Service {
private boolean stop;
private boolean trace;
private ServerSocket serverSocket;
private final Set<PgServerThread> running = Collections.synchronizedSet(new HashSet<PgServerThread>());
private final AtomicInteger pid= new AtomicInteger(0);
private final Set<PgServerThread> running = Collections.
synchronizedSet(new HashSet<PgServerThread>());
private final AtomicInteger pid = new AtomicInteger();
private String baseDir;
private boolean allowOthers;
private boolean isDaemon;
......@@ -260,11 +261,13 @@ public class PgServer implements Service {
}
/**
* @return the thread with the given process-id
* Get the thread with the given process id.
*
* @return the thread
*/
PgServerThread getThread(int processId) {
for (PgServerThread c : New.arrayList(running)) {
if (c.getProcessId()==processId) {
if (c.getProcessId() == processId) {
return c;
}
}
......@@ -457,8 +460,12 @@ public class PgServer implements Service {
}
/**
* A fake wrapper around pg_get_expr(expr_text, relation_oid), in PG it decompiles the "internal form of an
* expression, assuming that any Vars in it refer to the relation indicated by the second parameter".
* A fake wrapper around pg_get_expr(expr_text, relation_oid), in PostgreSQL
* it "decompiles the internal form of an expression, assuming that any vars
* in it refer to the relation indicated by the second parameter".
*
* @param exprText the expression text
* @param relationOid the relation object id
*/
public static String getPgExpr(String exprText, int relationOid) {
return null;
......
......@@ -29,7 +29,7 @@ import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Properties;
import java.util.Random;
import org.h2.command.CommandInterface;
import org.h2.constant.SysProperties;
import org.h2.engine.ConnectionInfo;
......@@ -40,6 +40,7 @@ import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
import org.h2.util.ScriptReader;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
......@@ -74,7 +75,7 @@ public class PgServerThread implements Runnable {
PgServerThread(Socket socket, PgServer server) {
this.server = server;
this.socket = socket;
this.secret = new Random().nextInt();
this.secret = (int) MathUtils.secureRandomLong();
}
@Override
......@@ -151,10 +152,10 @@ public class PgServerThread implements Runnable {
int pid = readInt();
int key = readInt();
PgServerThread c = server.getThread(pid);
if (c!=null) {
if (c != null) {
c.cancelRequest(key);
} else {
sendErrorResponse("unknown process: "+pid);
sendErrorResponse("unknown process: " + pid);
}
close();
} else if (version == 80877103) {
......@@ -354,7 +355,7 @@ public class PgServerThread implements Runnable {
sendCommandComplete(prep, prep.getUpdateCount());
}
} catch (Exception e) {
if (prep.isCancelled()) {
if (prep.wasCancelled()) {
sendCancelQueryResponse();
} else {
sendErrorResponse(e);
......@@ -401,7 +402,7 @@ public class PgServerThread implements Runnable {
sendCommandComplete(stat, stat.getUpdateCount());
}
} catch (SQLException e) {
if (stat!=null && stat.isCancelled()) {
if (stat != null && stat.wasCancelled()) {
sendCancelQueryResponse();
} else {
sendErrorResponse(e);
......@@ -491,7 +492,7 @@ public class PgServerThread implements Runnable {
break;
default:
String s = rs.getString(column);
if (s==null) {
if (s == null) {
writeInt(-1);
} else {
byte[] data = s.getBytes(getEncoding());
......@@ -524,7 +525,7 @@ public class PgServerThread implements Runnable {
break;
case PgServer.PG_TYPE_BYTEA:
byte[] data = rs.getBytes(column);
if (data==null) {
if (data == null) {
writeInt(-1);
} else {
writeInt(data.length);
......@@ -548,7 +549,7 @@ public class PgServerThread implements Runnable {
boolean text = (i >= formatCodes.length) || (formatCodes[i] == 0);
int col = i + 1;
int paramLen = readInt();
if (paramLen==-1) {
if (paramLen == -1) {
prep.setNull(i, Types.NULL);
} else if (text) {
// plain text
......@@ -679,7 +680,7 @@ public class PgServerThread implements Runnable {
// type = PgServer.PG_TYPE_INT2VECTOR;
// }
precision[i] = meta.getColumnDisplaySize(i + 1);
if (type!=Types.NULL) {
if (type != Types.NULL) {
server.checkType(pgType);
}
types[i] = pgType;
......@@ -705,8 +706,12 @@ public class PgServerThread implements Runnable {
}
}
/** @return should the given type be formatted as binary or plain text? */
private boolean formatAsText(int pgType) {
/**
* Check whether the given type should be formatted as text.
*
* @return true for binary
*/
private static boolean formatAsText(int pgType) {
switch (pgType) {
// TODO: add more types to send as binary once compatibility is confirmed
case PgServer.PG_TYPE_BYTEA:
......@@ -956,8 +961,7 @@ public class PgServerThread implements Runnable {
* @param secret the private key of the command
*/
private synchronized void cancelRequest(int secret) throws IOException {
if (activeRequest != null)
{
if (activeRequest != null) {
if (secret != this.secret) {
sendErrorResponse("invalid cancel secret");
return;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论