14e910a72946536ed61b5c3917930a581f18f16b
larrym
  Wed Jun 20 08:46:22 2012 -0700
refactor code into defaultNode function
diff --git src/hg/hgGateway/hgGateway.c src/hg/hgGateway/hgGateway.c
index d2441c5..1b07885 100644
--- src/hg/hgGateway/hgGateway.c
+++ src/hg/hgGateway/hgGateway.c
@@ -7,31 +7,30 @@
 #include "obscure.h"
 #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");
@@ -305,61 +304,38 @@
 // called from main BEFORE the cart is loaded (so we only use CGI parameters and/or cookies).
 
 char *thisNodeStr = geoMirrorNode();
 if (thisNodeStr)
     {
     char *redirectCookie = findCookieData("redirect");
     char *redirect = cgiOptionalString("redirect");
 
     fprintf(stderr, "GALT redirectCookie=%s redirect=%s\n", 
             redirectCookie, redirect); fflush(stderr); // DEBUG REMOVE
 
     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 (sort-of) assume no overlaps in geoIpNode table, so we can use limit 1 to make query very efficient;
-        // we do accomodate a range that is completely contained in another (to accomodate the hgroaming entry for testing);
-        // this is accomplished by "<= ipEnd" in the sql query.
-        safef(query, sizeof query, "select ipStart, ipEnd, node from geoIpNode where %u >= ipStart and %u <= ipEnd order by ipStart desc limit 1", ip, 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
+        int node = defaultNode(centralConn, ipStr);
 
         // get location of redirect node
-        if (thisNode != defaultNode)
+        if (thisNode != node)
             {
-            safef(query, sizeof query, "select domain from gbNode where node = %d", defaultNode);
+            char query[1056];
+            safef(query, sizeof query, "select domain from gbNode where node = %d", node);
             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();
             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=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"