Commit d96c29ab authored by Dave Cramer's avatar Dave Cramer

Applied Anders patch to move the startup code out of Connection into StartupPacket

* Introduces a new class, StartupPacket.
* Moves a lot of constants from Connection to StartupPacket.
* Makes two instance variables in Connection into locals.
parent 8e9b215f
...@@ -11,7 +11,7 @@ import org.postgresql.util.*; ...@@ -11,7 +11,7 @@ import org.postgresql.util.*;
import org.postgresql.core.*; import org.postgresql.core.*;
/* /*
* $Id: Connection.java,v 1.43 2002/03/09 17:08:39 davec Exp $ * $Id: Connection.java,v 1.44 2002/03/21 02:39:06 davec Exp $
* *
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or * This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
* JDBC2 versions of the Connection class. * JDBC2 versions of the Connection class.
...@@ -25,7 +25,6 @@ public abstract class Connection ...@@ -25,7 +25,6 @@ public abstract class Connection
private String PG_HOST; private String PG_HOST;
private int PG_PORT; private int PG_PORT;
private String PG_USER; private String PG_USER;
private String PG_PASSWORD;
private String PG_DATABASE; private String PG_DATABASE;
private boolean PG_STATUS; private boolean PG_STATUS;
private String compatible; private String compatible;
...@@ -52,11 +51,6 @@ public abstract class Connection ...@@ -52,11 +51,6 @@ public abstract class Connection
// src/include/libpq/pqcomm.h // src/include/libpq/pqcomm.h
protected static final int PG_PROTOCOL_LATEST_MAJOR = 2; protected static final int PG_PROTOCOL_LATEST_MAJOR = 2;
protected static final int PG_PROTOCOL_LATEST_MINOR = 0; protected static final int PG_PROTOCOL_LATEST_MINOR = 0;
private static final int SM_DATABASE = 64;
private static final int SM_USER = 32;
private static final int SM_OPTIONS = 64;
private static final int SM_UNUSED = 64;
private static final int SM_TTY = 64;
private static final int AUTH_REQ_OK = 0; private static final int AUTH_REQ_OK = 0;
private static final int AUTH_REQ_KRB4 = 1; private static final int AUTH_REQ_KRB4 = 1;
...@@ -65,8 +59,10 @@ public abstract class Connection ...@@ -65,8 +59,10 @@ public abstract class Connection
private static final int AUTH_REQ_CRYPT = 4; private static final int AUTH_REQ_CRYPT = 4;
private static final int AUTH_REQ_MD5 = 5; private static final int AUTH_REQ_MD5 = 5;
// New for 6.3, salt value for crypt authorisation public final static int PGASYNC_IDLE = 0; /* nothing's happening, dude */
private String salt; public final static int PGASYNC_BUSY = 1; /* query in progress */
public final static int PGASYNC_READY = 2; /* result ready for PQgetResult */
// These are used to cache oids, PGTypes and SQLTypes // These are used to cache oids, PGTypes and SQLTypes
private static Hashtable sqlTypeCache = new Hashtable(); // oid -> SQLType private static Hashtable sqlTypeCache = new Hashtable(); // oid -> SQLType
...@@ -85,6 +81,7 @@ public abstract class Connection ...@@ -85,6 +81,7 @@ public abstract class Connection
public int pid; public int pid;
public int ckey; public int ckey;
public int asyncStatus = PGASYNC_READY;
/* /*
* This is called by Class.forName() from within org.postgresql.Driver * This is called by Class.forName() from within org.postgresql.Driver
*/ */
...@@ -149,12 +146,16 @@ public abstract class Connection ...@@ -149,12 +146,16 @@ public abstract class Connection
this_driver = d; this_driver = d;
this_url = url; this_url = url;
PG_DATABASE = database; PG_DATABASE = database;
PG_USER = info.getProperty("user"); PG_USER = info.getProperty("user");
PG_PASSWORD = info.getProperty("password", "");
String password = info.getProperty("password", "");
PG_PORT = port; PG_PORT = port;
PG_HOST = host; PG_HOST = host;
PG_STATUS = CONNECTION_BAD; PG_STATUS = CONNECTION_BAD;
if (info.getProperty("compatible") == null) if (info.getProperty("compatible") == null)
{ {
compatible = d.getMajorVersion() + "." + d.getMinorVersion(); compatible = d.getMajorVersion() + "." + d.getMinorVersion();
...@@ -184,14 +185,10 @@ public abstract class Connection ...@@ -184,14 +185,10 @@ public abstract class Connection
// Now we need to construct and send a startup packet // Now we need to construct and send a startup packet
try try
{ {
// Ver 6.3 code new StartupPacket(PG_PROTOCOL_LATEST_MAJOR,
pg_stream.SendInteger(4 + 4 + SM_DATABASE + SM_USER + SM_OPTIONS + SM_UNUSED + SM_TTY, 4); PG_PROTOCOL_LATEST_MINOR,
pg_stream.SendInteger(PG_PROTOCOL_LATEST_MAJOR, 2); PG_USER,
pg_stream.SendInteger(PG_PROTOCOL_LATEST_MINOR, 2); database).writeTo(pg_stream);
pg_stream.Send(database.getBytes(), SM_DATABASE);
// This last send includes the unused fields
pg_stream.Send(PG_USER.getBytes(), SM_USER + SM_OPTIONS + SM_UNUSED + SM_TTY);
// now flush the startup packets to the backend // now flush the startup packets to the backend
pg_stream.flush(); pg_stream.flush();
...@@ -202,6 +199,7 @@ public abstract class Connection ...@@ -202,6 +199,7 @@ public abstract class Connection
do do
{ {
int beresp = pg_stream.ReceiveChar(); int beresp = pg_stream.ReceiveChar();
String salt = null;
switch (beresp) switch (beresp)
{ {
case 'E': case 'E':
...@@ -255,15 +253,15 @@ public abstract class Connection ...@@ -255,15 +253,15 @@ public abstract class Connection
case AUTH_REQ_PASSWORD: case AUTH_REQ_PASSWORD:
DriverManager.println("postgresql: PASSWORD"); DriverManager.println("postgresql: PASSWORD");
pg_stream.SendInteger(5 + PG_PASSWORD.length(), 4); pg_stream.SendInteger(5 + password.length(), 4);
pg_stream.Send(PG_PASSWORD.getBytes()); pg_stream.Send(password.getBytes());
pg_stream.SendInteger(0, 1); pg_stream.SendInteger(0, 1);
pg_stream.flush(); pg_stream.flush();
break; break;
case AUTH_REQ_CRYPT: case AUTH_REQ_CRYPT:
DriverManager.println("postgresql: CRYPT"); DriverManager.println("postgresql: CRYPT");
String crypted = UnixCrypt.crypt(salt, PG_PASSWORD); String crypted = UnixCrypt.crypt(salt, password);
pg_stream.SendInteger(5 + crypted.length(), 4); pg_stream.SendInteger(5 + crypted.length(), 4);
pg_stream.Send(crypted.getBytes()); pg_stream.Send(crypted.getBytes());
pg_stream.SendInteger(0, 1); pg_stream.SendInteger(0, 1);
...@@ -272,7 +270,7 @@ public abstract class Connection ...@@ -272,7 +270,7 @@ public abstract class Connection
case AUTH_REQ_MD5: case AUTH_REQ_MD5:
DriverManager.println("postgresql: MD5"); DriverManager.println("postgresql: MD5");
byte[] digest = MD5Digest.encode(PG_USER, PG_PASSWORD, salt); byte[] digest = MD5Digest.encode(PG_USER, password, salt);
pg_stream.SendInteger(5 + digest.length, 4); pg_stream.SendInteger(5 + digest.length, 4);
pg_stream.Send(digest); pg_stream.Send(digest);
pg_stream.SendInteger(0, 1); pg_stream.SendInteger(0, 1);
...@@ -429,7 +427,7 @@ public abstract class Connection ...@@ -429,7 +427,7 @@ public abstract class Connection
*/ */
public java.sql.ResultSet ExecSQL(String sql, java.sql.Statement stat) throws SQLException public java.sql.ResultSet ExecSQL(String sql, java.sql.Statement stat) throws SQLException
{ {
return new QueryExecutor(sql, stat, pg_stream, this).execute(); return new QueryExecutor2(sql, stat, pg_stream, this).execute();
} }
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment