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

Formatting, javadocs, more secure secret

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