9684047874667451e47229f16b5c9eab60286850
braney
  Sat Apr 11 12:15:39 2015 -0700
change mirror redirect to be opt-in.

diff --git src/hg/lib/web.c src/hg/lib/web.c
index fd5a2fc..9f3bd44 100644
--- src/hg/lib/web.c
+++ src/hg/lib/web.c
@@ -221,62 +221,30 @@
 	{
 	puts("<IMG SRC=\"../images/title.jpg\">");
 	}
     puts("</TH></TR>" "\n"
          "" "\n" );
     }
 
 /* Put up the hot links bar. */
 
 char *menuStr = menuBar(theCart, db);
 if(menuStr)
     {
     puts(menuStr);
     }
 
-if (endsWith(scriptName, "hgGateway") && geoMirrorEnabled())
-    {
-    // Show an opt-out alert if user is on a host to which user has been automatically redirected (just once, right after they have been redirected)
-    char *source = cgiOptionalString("source");
-    char *redirect = cgiOptionalString("redirect");
-    if (source != NULL && redirect != NULL && sameString(redirect, "auto"))
-	{
-	char *domain = cgiServerName();
-	char *port = cgiServerPort();
-        // We don't bother maintaining stuff in request URI, because it may contain items like hgsid and other host specific values
-        int newUriSize = 2048;
-	char *newUri = needMem(newUriSize);
-	safef(newUri, newUriSize, "http%s://%s:%s/cgi-bin/hgGateway?redirect=manual&source=%s", 
-	    cgiServerHttpsIsOn() ? "s" : "", source, port, domain);
-
-	printf("<TR><TD COLSPAN=3 id='redirectTd' onclick=\"javascript:document.getElementById('redirectTd').innerHTML='';\">"
-	    "<div style=\"margin: 10px 25%%; border-style:solid; border-width:thin; border-color:#97D897;\">"
-	    "<h3 style=\"background-color: #97D897; text-align: left; margin-top:0px; margin-bottom:0px;\">"
-	    "&nbsp;You've been redirected to your nearest mirror - %s"
-	    "<idiv style=\"float:right;\">[x]</idiv>"
-	    "</h3> "
-	    "<ul style=\"margin:5px;\">"
-	    "<li>Take me back to <a href=\"%s\">%s</a>"
-	    "<idiv style=\"float:right;\"><a href=\"../goldenPath/help/genomeEuro.html\">What is this?</a></idiv>"
-	    "</li>"
-	    "</ul>"
-	    "</div>"
-	    "</TD></TR>\n"
-	    , domain, newUri, source );
-	}
-    }
-
 if(!skipSectionHeader)
 /* this HTML must be in calling code if skipSectionHeader is TRUE */
     {
     webFirstSection(textOutBuf);
     };
 webPushErrHandlers();
 /* set the flag */
 webHeadAlreadyOutputed = TRUE;
 }	/*	static void webStartWrapperDetailedInternal()	*/
 
 void webStartWrapperDetailedArgs(struct cart *theCart, char *db,
 	char *headerText, char *format, va_list args, boolean withHttpHeader,
 	boolean withLogo, boolean skipSectionHeader, boolean withHtmlHeader)
 /* output a CGI and HTML header with the given title in printf format */
 {
@@ -1439,15 +1407,78 @@
         }
     // Don't overwrite any previously set defaults
     if(!contextSpecificHelpLink && link)
         contextSpecificHelpLink = link;
     if(!contextSpecificHelpLabel && label)
         contextSpecificHelpLabel = label;
     }
 if(contextSpecificHelpLink)
     {
     char buf[1024];
     safef(buf, sizeof(buf), "<li><a href='%s'>%s</a></li>", contextSpecificHelpLink, contextSpecificHelpLabel);
     menuStr = replaceChars(menuStr, "<!-- CONTEXT_SPECIFIC_HELP -->", buf);
     }
 return menuStr;
 }
+
+void checkForGeoMirrorRedirect(struct cart *cart)
+// Implement Geo/IP based redirection.
+{
+char *thisNodeStr = geoMirrorNode();
+if (thisNodeStr)   // if geo-mirroring is enabled
+    {
+    char *redirectCookie = findCookieData("redirect");
+    char *redirect = cgiOptionalString("redirect");
+
+    // if we're not already redirected
+    if (redirect == NULL && redirectCookie == NULL) 
+        {
+        int thisNode = sqlUnsigned(thisNodeStr);
+        struct sqlConnection *centralConn = hConnectCentral();
+        char *ipStr = cgiRemoteAddr();
+        int node = defaultNode(centralConn, ipStr);
+
+        // if our node is not the node that's closest.
+        if (thisNode != node)
+            {
+	    char *geoSuffix = cfgOptionDefault("browser.geoSuffix","");
+            char query[1056];
+            sqlSafef(query, sizeof query, "select domain from gbNode%s where node = %d", geoSuffix, node);
+            char *newDomain = sqlQuickString(centralConn, query);
+            char *oldDomain = cgiServerName();
+            char *port = cgiServerPort();
+            char *uri = cgiRequestUri();
+            char *sep = strchr(uri, '?') ? "&" : "?";
+            int newUriSize = strlen(uri) + 1024;
+            char *newUri = needMem(newUriSize);
+            char *oldUri = needMem(newUriSize);
+            safef(oldUri, newUriSize, "http%s://%s:%s%s%sredirect=manual&source=%s", 
+		cgiServerHttpsIsOn() ? "s" : "", oldDomain, port, uri, sep, oldDomain);
+            safef(newUri, newUriSize, "http%s://%s:%s%s%sredirect=manual&source=%s", 
+		cgiServerHttpsIsOn() ? "s" : "", newDomain, port, uri, sep, oldDomain);
+        struct dyString *dy = dyStringNew(256);
+
+	cartCheckForCustomTracks(cart, dy);
+	printf("<TR><TD COLSPAN=3 id='redirectTd' onclick=\"javascript:document.getElementById('redirectTd').innerHTML='';\">"
+	    "<div style=\"margin: 10px 25%%; border-style:solid; border-width:thin; border-color:#97D897;\">"
+	    "<h3 style=\"background-color: #97D897; text-align: left; margin-top:0px; margin-bottom:0px;\">"
+	    "&nbsp;You might want to navigate to your nearest mirror - %s"
+	    "<idiv style=\"float:right;\">[x]</idiv>"
+	    "</h3> "
+	    "<ul style=\"margin:5px;\">"
+	    "<li>Take me to  <a href=\"%s\">%s</a>"
+	    "<idiv style=\"float:right;\"><a href=\"../goldenPath/help/genomeEuro.html\">What is this?</a></idiv>"
+	    "</li>",
+	    newDomain, newUri, newDomain);
+	    
+	printf("<li>Let me stay here   <a href=\"%s\">%s</a>"
+	    "</ul>"
+	    "</div>"
+	    "</TD></TR>\n",
+	    oldUri, oldDomain );
+	printf("%s\n", dy->string);
+            exit(0);
+            }
+        hDisconnectCentral(&centralConn);
+        }
+    }
+}