090d4afcaf0482dfa3f5eaec25d915f1d4a9fa9e
max
  Thu Jun 15 13:34:32 2017 -0700
Cleanup of hgSession to make the distinction between the login server and local
server clearer in the code. Renaming and moving two functions from wikiLink to
hdb, as at least one is not necessarily wikiLink-related and it's good to keep
them together. Also adding a warning to hgSession.c (probably
useless). refs #19632 and also see related tickets there.

diff --git src/hg/lib/hdb.c src/hg/lib/hdb.c
index 5d068e2..39332b3 100644
--- src/hg/lib/hdb.c
+++ src/hg/lib/hdb.c
@@ -33,30 +33,32 @@
 #include "genbank.h"
 #include "chromInfo.h"
 #ifndef GBROWSE
 #include "axtInfo.h"
 #include "ctgPos.h"
 #include "hubConnect.h"
 #include "customTrack.h"
 #include "hgFind.h"
 #endif /* GBROWSE */
 #include "hui.h"
 #include "trackHub.h"
 #include "net.h"
 #include "udc.h"
 #include "paraFetch.h"
 #include "filePath.h"
+#include "wikiLink.h"
+#include "cheapcgi.h"
 
 
 #ifdef LOWELAB
 #define DEFAULT_PROTEINS "proteins060115"
 #define DEFAULT_GENOME "Pyrobaculum aerophilum"
 #else
 #define DEFAULT_PROTEINS "proteins"
 #define DEFAULT_GENOME "Human"
 #endif
 
 
 static struct sqlConnCache *hdbCc = NULL;  /* cache for primary database connection */
 static struct sqlConnCache *centralCc = NULL;
 static char *centralDb = NULL;
 static struct sqlConnCache *cartCc = NULL;  /* cache for cart; normally same as centralCc */
@@ -3294,30 +3296,75 @@
 char* hHttpHost()
 /* return http host from apache or hostname if run from command line  */
 {
 char host[256];
 
 char *httpHost = getenv("HTTP_HOST");
 
 if (httpHost == NULL && !gethostname(host, sizeof(host)))
     // make sure this works when CGIs are run from the command line.
     // small mem leak is acceptable when run from command line
     httpHost = cloneString(host);
 
 return httpHost;
 }
 
+char *hLocalHostCgiBinUrl() 
+/* Return the current full absolute URL of the cgi-bin directory of the local
+ * server in the format http(s)://<host>/<cgiBinDir>, e.g.
+ * https://genome.ucsc.edu/cgi-bin/.
+ * The <host> is coming from the SERVER_NAME variable, which is
+ * the ServerName setting in the Apache config file.
+ * The cgi-bin directory is coming from the SCRIPT_NAME variable, the relative
+ * location of the cgi program relative to Apache's DOCUMENT_ROOT.
+ * Https is used if the variable HTTPS is set by Apache.
+ *
+ * If login.relativeLink=on is set, return only the empty string. 
+ * (This is used on the CIRM server, as it has no way of knowing what its
+ * actual server name or protocol is, it is behind a reverse proxy)
+ * Result has to be free'd. */
+{
+boolean relativeLink = cfgOptionBooleanDefault(CFG_LOGIN_RELATIVE, FALSE);
+if (relativeLink)
+    return cloneString("");
+
+char *cgiDir = cgiScriptDirUrl();
+char buf[2048];
+char *hgLoginHost = cgiServerNamePort();
+safef(buf, sizeof(buf), "http%s://%s%s", cgiAppendSForHttps(), hgLoginHost, cgiDir);
+
+return cloneString(buf);
+}
+
+char *hLoginHostCgiBinUrl() 
+/* Return the current full absolute URL of the cgi-bin directory of the host
+ * used for logins. Genome-euro/genome-asia use genome.ucsc.edu for the login,
+ * as we have only one single server for user accounts.
+ * Returns a string in the format
+ * http(s)://<host>/cgi-bin/ e.g. http://genome.ucsc.edu/cgi-bin/ 
+ * - the <host> is coming from the wiki.host variable in hg.conf.
+ * - https is used unless login.useHttps=off in hg.conf
+ *
+ * If login.relativeLink=on is set, return only the empty string. 
+ * (see hLocalCgiBinUrl)
+ * Result has to be free'd. */
+{
+char buf[2048];
+safef(buf, sizeof(buf), "http%s://%s/cgi-bin/hgLogin",
+      loginUseHttps() ? "s" : "", wikiLinkHost());
+return cloneString(buf);
+}
 
 boolean hHostHasPrefix(char *prefix)
 /* Return TRUE if this is running on web-server with host name prefix */
 {
 if (prefix == NULL)
     return FALSE;
 
 char *httpHost = hHttpHost();
 if (httpHost == NULL)
     return FALSE;
 
 return startsWith(prefix, httpHost);
 }
 
 boolean hIsPrivateHost()