mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-01-30 19:00:29 +08:00
libpq: PQhost to return active connected host or hostaddr
Previously, PQhost didn't return the connected host details when the connection type was CHT_HOST_ADDRESS (i.e., via hostaddr). Instead, it returned the complete host connection parameter (which could contain multiple hosts) or the default host details, which was confusing and arguably incorrect. Change this to return the actually connected host or hostaddr irrespective of the connection type. When hostaddr but no host was specified, hostaddr is now returned. Never return the original host connection parameter, and document that PQhost cannot be relied on before the connection is established. PQport is similarly changed to always return the active connection port and never the original connection parameter. Author: Hari Babu <kommi.haribabu@gmail.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> Reviewed-by: David G. Johnston <david.g.johnston@gmail.com>
This commit is contained in:
parent
44bd95846a
commit
1944cdc982
@ -1692,7 +1692,7 @@ char *PQpass(const PGconn *conn);
|
|||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Returns the server host name of the connection.
|
Returns the server host name of the active connection.
|
||||||
This can be a host name, an IP address, or a directory path if the
|
This can be a host name, an IP address, or a directory path if the
|
||||||
connection is via Unix socket. (The path case can be distinguished
|
connection is via Unix socket. (The path case can be distinguished
|
||||||
because it will always be an absolute path, beginning
|
because it will always be an absolute path, beginning
|
||||||
@ -1701,6 +1701,30 @@ char *PQpass(const PGconn *conn);
|
|||||||
char *PQhost(const PGconn *conn);
|
char *PQhost(const PGconn *conn);
|
||||||
</synopsis>
|
</synopsis>
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
If the connection parameters specified both <literal>host</literal> and
|
||||||
|
<literal>hostaddr</literal>, then <function>PQhost</function> will
|
||||||
|
return the <literal>host</literal> information. If only
|
||||||
|
<literal>hostaddr</literal> was specified, then that is returned.
|
||||||
|
If multiple hosts were specified in the connection parameters,
|
||||||
|
<function>PQhost</function> returns the host actually connected to.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
<function>PQhost</function> returns <symbol>NULL</symbol> if the
|
||||||
|
<parameter>conn</parameter> argument is <symbol>NULL</symbol>.
|
||||||
|
Otherwise, if there is an error producing the host information (perhaps
|
||||||
|
if the connection has not been fully established or there was an
|
||||||
|
error), it returns an empty string.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
If multiple hosts were specified in the connection parameters, it is
|
||||||
|
not possible to rely on the result of <function>PQhost</function> until
|
||||||
|
the connection is established. The status of the connection can be
|
||||||
|
checked using the function <function>PQstatus</function>.
|
||||||
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
@ -1714,12 +1738,32 @@ char *PQhost(const PGconn *conn);
|
|||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Returns the port of the connection.
|
Returns the port of the active connection.
|
||||||
|
|
||||||
<synopsis>
|
<synopsis>
|
||||||
char *PQport(const PGconn *conn);
|
char *PQport(const PGconn *conn);
|
||||||
</synopsis>
|
</synopsis>
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
If multiple ports were specified in the connection parameters,
|
||||||
|
<function>PQport</function> returns the port actually connected to.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
<function>PQport</function> returns <symbol>NULL</symbol> if the
|
||||||
|
<parameter>conn</parameter> argument is <symbol>NULL</symbol>.
|
||||||
|
Otherwise, if there is an error producing the port information (perhaps
|
||||||
|
if the connection has not been fully established or there was an
|
||||||
|
error), it returns an empty string.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
If multiple ports were specified in the connection parameters, it is
|
||||||
|
not possible to rely on the result of <function>PQport</function> until
|
||||||
|
the connection is established. The status of the connection can be
|
||||||
|
checked using the function <function>PQstatus</function>.
|
||||||
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
|
@ -6017,19 +6017,18 @@ PQhost(const PGconn *conn)
|
|||||||
{
|
{
|
||||||
if (!conn)
|
if (!conn)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (conn->connhost != NULL &&
|
|
||||||
conn->connhost[conn->whichhost].type != CHT_HOST_ADDRESS)
|
if (conn->connhost != NULL)
|
||||||
return conn->connhost[conn->whichhost].host;
|
|
||||||
else if (conn->pghost != NULL && conn->pghost[0] != '\0')
|
|
||||||
return conn->pghost;
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
#ifdef HAVE_UNIX_SOCKETS
|
if (conn->connhost[conn->whichhost].host != NULL &&
|
||||||
return DEFAULT_PGSOCKET_DIR;
|
conn->connhost[conn->whichhost].host[0] != '\0')
|
||||||
#else
|
return conn->connhost[conn->whichhost].host;
|
||||||
return DefaultHost;
|
else if (conn->connhost[conn->whichhost].hostaddr != NULL &&
|
||||||
#endif
|
conn->connhost[conn->whichhost].hostaddr[0] != '\0')
|
||||||
|
return conn->connhost[conn->whichhost].hostaddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
@ -6037,9 +6036,11 @@ PQport(const PGconn *conn)
|
|||||||
{
|
{
|
||||||
if (!conn)
|
if (!conn)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (conn->connhost != NULL)
|
if (conn->connhost != NULL)
|
||||||
return conn->connhost[conn->whichhost].port;
|
return conn->connhost[conn->whichhost].port;
|
||||||
return conn->pgport;
|
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
|
Loading…
Reference in New Issue
Block a user