ca3bf0fb980985a6f6bd111b45ab1c8a729ffddd
larrym
  Tue Apr 3 13:57:22 2012 -0700
replace use of SUPPORT_EURONODE with geoMirrorEnabled()
diff --git src/hg/hgGateway/hgGateway.c src/hg/hgGateway/hgGateway.c
index 04becc3..9bb7031 100644
--- src/hg/hgGateway/hgGateway.c
+++ src/hg/hgGateway/hgGateway.c
@@ -8,30 +8,31 @@
 #include "web.h"
 #include "cart.h"
 #include "hdb.h"
 #include "dbDb.h"
 #include "hgFind.h"
 #include "hCommon.h"
 #include "hui.h"
 #include "customTrack.h"
 #include "hubConnect.h"
 #include "hgConfig.h"
 #include "jsHelper.h"
 #include "hPrint.h"
 #include "suggest.h"
 #include "search.h"
 #include "internet.h"
+#include "geoMirror.h"
 
 struct cart *cart = NULL;
 struct hash *oldVars = NULL;
 char *clade = NULL;
 char *organism = NULL;
 char *db = NULL;
 
 void hgGateway()
 /* hgGateway - Human Genome Browser Gateway. */
 {
 char *defaultPosition = hDefaultPos(db);
 char *position = cloneString(cartUsualString(cart, "position", defaultPosition));
 boolean gotClade = hGotClade();
 char *survey = cfgOptionEnv("HGDB_SURVEY", "survey");
 char *surveyLabel = cfgOptionEnv("HGDB_SURVEY_LABEL", "surveyLabel");
@@ -292,106 +293,95 @@
     cartWebStart(theCart, db, "%s %s%s Gateway\n", organism, buffer, hBrowserName());
     htmlDoEscape();
     }
 hgGateway();
 cartWebEnd();
 }
 
 char *excludeVars[] = {NULL};
 
 int main(int argc, char *argv[])
 /* Process command line. */
 {
 oldVars = hashNew(10);
 cgiSpoof(&argc, argv);
 
-#ifdef SUPPORT_EURONODE
-boolean onWeb = cgiIsOnWeb();
-if (onWeb)
+if(cgiIsOnWeb())
     {
     // check IP for redirection
-    char *thisNodeStr = cfgOption("browser.node");
+    // NOTE that we want to redirect people as quickly as possible, so for efficiency purposes, this code is designed to be
+    // called BEFORE the cart is loaded (so we only use CGI parameters and/or cookies).
+
+    char *thisNodeStr = geoMirrorNode();
     if (thisNodeStr)
 	{
-	char *redirect = cgiOptionalString("redirect");
-	char *source = cgiOptionalString("source");
-	// check for the cookie called redirect (which suppress redirection)
 	char *redirectCookie = findCookieData("redirect");
+        char *redirect = cgiOptionalString("redirect");
 
-	fprintf(stderr, "GALT redirectCookie=%s redirect=%s source=%s\n", 
-		redirectCookie, redirect, source); fflush(stderr); // DEBUG REMOVE
+	fprintf(stderr, "GALT redirectCookie=%s redirect=%s\n", 
+		redirectCookie, redirect); fflush(stderr); // DEBUG REMOVE
 
-	if (!(
-              (redirect && !source) ||   // we are on main site after user has actively choose to leave mirror and come back to the main site
-              redirectCookie             // main site after above has happened (XXXX I think)
-              ))
+	if (redirect == NULL && redirectCookie == NULL)
 	    {
 	    int thisNode = sqlUnsigned(thisNodeStr);
 	    struct sqlConnection *centralConn = hConnectCentral();
 	    char query[1024];
 	    char *ipStr = cgiRemoteAddr();
 	    bits32 ip = 0;
 	    internetDottedQuadToIp(ipStr, &ip);
+
             // we assume no overlaps in geoIpNode table, so we can use limit 1 to make query very efficient.
 	    safef(query, sizeof query, "select ipStart, ipEnd, node from geoIpNode where %u >= ipStart order by ipStart desc limit 1", ip);
 	    char **row;
 	    struct sqlResult *sr = sqlGetResult(centralConn, query);
 	    int defaultNode = 1;
 	    if ((row = sqlNextRow(sr)) != NULL)
 		{
 		uint ipStart = sqlUnsigned(row[0]);
 		uint ipEnd = sqlUnsigned(row[1]);
 		if (ipStart <= ip && ipEnd >= ip)
 		    {
 		    defaultNode = sqlSigned(row[2]);
 		    }
 		}
 	    sqlFreeResult(&sr);
 
 	    fprintf(stderr, "GALT thisNodeStr=%s thisNode=%d ipStr=%s ip=%u defaultNode (for user) %d\n", 
 		thisNodeStr, thisNode,
 		ipStr, ip, defaultNode); fflush(stderr); // DEBUG REMOVE
 
 	    // get location of redirect node
 	    if (thisNode != defaultNode)
 		{
 		safef(query, sizeof query, "select domain from gbNode where node = %d", defaultNode);
 		char *newDomain = sqlQuickString(centralConn, query);
 		fprintf(stderr, "GALT newDomain=%s\n", newDomain); fflush(stderr); // DEBUG REMOVE
 		char *oldDomain = cgiServerName();
 		char *port = cgiServerPort();
 		char *uri = cgiRequestUri();
-		    //   /cgi-bin/test.cgi?x=15&y=youdog
-		char *sep = "?";
-		if (strchr(uri,'?') != 0)
-		    sep = "&";
-
+		char *sep = strchr(uri, '?') ? "&" : "?";
 		int newUriSize = strlen(uri)+1024;
 		char *newUri = needMem(newUriSize);
 		// TODO what about https?
-		safef(newUri, newUriSize, "http://%s:%s%s%sredirect=mirror&source=%s", newDomain, port, uri, sep, oldDomain);
-
-		 //   "set-cookie: redirect=; path=/; expires=Mon, 18-Apr-2011 21:45:28 GMT"  
-                 //     the time of the set-cookie expire was immediately now()
+		safef(newUri, newUriSize, "http://%s:%s%s%sredirect=auto&source=%s", newDomain, port, uri, sep, oldDomain);
 		struct dyString *dy = dyStringNew(256);		
 		dyStringPrintf(dy,
 		    "HTTP/1.1 302 found: \n"
 		    "Content-Type: text/html; charset=iso-8859-1\n"
 		    "Connection: close\n"
 		    "Location: %s\n"
 		    "\n"
 		    "<html><head><title>Redirecting to closer site</title></head>\n"
 		    "<body><a href=\"%s\">%s</a></body>\n"
 		    , newUri , newUri, newUri);
 		fprintf(stderr, "GALT redirect response:\n%s", dy->string); fflush(stderr); // DEBUG REMOVE
 		puts(dyStringContents(dy));
 		exit(0);
 		}
 	    hDisconnectCentral(&centralConn);
 	    }
 	}
     }
-#endif
 
 cartEmptyShell(doMiddle, hUserCookie(), excludeVars, oldVars);
 return 0;
 }