BBlack has submitted this change and it was merged.

Change subject: Send GeoIP lookup result as 'GeoIP' cookie
......................................................................


Send GeoIP lookup result as 'GeoIP' cookie

If the incoming request did not include a cookie header with a GeoIP cookie,
perform a GeoIP look-up and send it via Set-Cookie header. The format of the
cookie is:

  Set-Cookie: GeoIP=CountryCode:City:Latitude:Longitude:AddressFamily; path=/

For example:

  Set-Cookie: GeoIP=US:San_Francisco:37.7749:-122.4194:v6; path=/

When GeoIP lookup fails, all fields are empty except the last (address family).
For example:

  Set-Cookie: GeoIP=::::v4; path=/

Setting the cookie even on lookup failures is useful for two reasons:

* It ensures the IP is not looked up again for the remainder of the current
  browser session.
* The address family can be used by JavaScript code to determine whether the
  user is possibly dual-stack, in which case a separate request can be made to
  geoip.wikimedia.org (which is IPv4-only, and thus forces dual-stack users to
  connect via IPv4).

If the response already contains a Set-Cookie header, the GeoIP cookie is added
to it.

Change-Id: Ic4d2fccbc1b5f674997e2aee6929d2f935c5a870
---
M templates/varnish/geoip.inc.vcl.erb
M templates/varnish/text-frontend.inc.vcl.erb
2 files changed, 59 insertions(+), 0 deletions(-)

Approvals:
  BBlack: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/templates/varnish/geoip.inc.vcl.erb 
b/templates/varnish/geoip.inc.vcl.erb
index 8284007..4ee2007 100644
--- a/templates/varnish/geoip.inc.vcl.erb
+++ b/templates/varnish/geoip.inc.vcl.erb
@@ -18,6 +18,7 @@
        void geo_init();
        int geo_get_addr_family (const char *addr);
        char * geo_get_xff_ip (const struct sess *sp);
+       char * geo_sanitize_for_cookie (char *string);
        void geo_set_cache_control (const struct sess *sp);
 
        inline void geo_init () {
@@ -56,6 +57,16 @@
                        }
                }
                return xff;
+       }
+
+       char * geo_sanitize_for_cookie (char *string) {
+           char *ptr;
+           for (ptr = string; *ptr; ptr++) {
+               if (strchr(";, ", *ptr)) {
+                   *ptr = '_';
+               }
+           }
+           return string;
        }
 
        void geo_set_cache_control (const struct sess *sp) {
@@ -102,3 +113,47 @@
                geo_set_cache_control(sp);
        }C
 }
+
+
+sub geoip_cookie {
+       C{
+               GeoIPRecord *record;
+               char *set_cookie_header, *cookie;
+               char latlon[20];
+
+               char *ip = geo_get_xff_ip(sp);
+               int af = geo_get_addr_family(ip);
+               if (af == -1) {
+                       ip = VRT_IP_string(sp, VRT_r_client_ip(sp));
+                       af = geo_get_addr_family(ip);
+               }
+
+               geo_init();
+               record = GeoIP_record_by_addr(gi, ip);
+
+               if (record) {
+                       /* Set-Cookie: 
GeoIP=US:San_Francisco:37.7749:-122.4194:v6; path=/ */
+                       snprintf(latlon, sizeof(latlon), "%.4f:%.4f", 
record->latitude, record->longitude);
+                       cookie = VRT_WrkString(sp, "GeoIP=",
+                               record->country_code,                   ":",
+                               geo_sanitize_for_cookie(record->city),  ":",
+                               latlon,                                 ":",
+                               AF_INET6 ? "v6" : "v4",                 "; 
path=/",
+                               vrt_magic_string_end);
+                       GeoIPRecord_delete(record);
+               } else {
+                       /* Set-Cookie: GeoIP=::::v6; path=/ */
+                       cookie = VRT_WrkString(sp, "GeoIP=::::", AF_INET6 ? 
"v6" : "v4",
+                               "; path=/", vrt_magic_string_end);
+               }
+
+               set_cookie_header = VRT_GetHdr(sp, HDR_RESP, "\013set-cookie:");
+               if (set_cookie_header == NULL) {
+                       // New header
+                       VRT_SetHdr(sp, HDR_RESP, "\013Set-Cookie:", cookie, 
vrt_magic_string_end);
+               } else {
+                       // Append to existing header
+                       VRT_SetHdr(sp, HDR_RESP, "\013Set-Cookie:", 
set_cookie_header, "; ", cookie, vrt_magic_string_end);
+               }
+       }C
+}
diff --git a/templates/varnish/text-frontend.inc.vcl.erb 
b/templates/varnish/text-frontend.inc.vcl.erb
index 602e53f..1c2d37a 100644
--- a/templates/varnish/text-frontend.inc.vcl.erb
+++ b/templates/varnish/text-frontend.inc.vcl.erb
@@ -129,4 +129,8 @@
                && req.url !~ "(?i)bcache=1") {
                set resp.http.Cache-Control = "private, s-maxage=0, max-age=0, 
must-revalidate";
        }
+       /* Perform GeoIP look-up and send the result as a session cookie */
+       if (req.http.Cookie !~ "(^|;\s*)GeoIP=[^;]") {
+               call geoip_cookie;
+       }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/113935
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic4d2fccbc1b5f674997e2aee6929d2f935c5a870
Gerrit-PatchSet: 6
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Ori.livneh <o...@wikimedia.org>
Gerrit-Reviewer: BBlack <bbl...@wikimedia.org>
Gerrit-Reviewer: Dr0ptp4kt <ab...@wikimedia.org>
Gerrit-Reviewer: Faidon Liambotis <fai...@wikimedia.org>
Gerrit-Reviewer: GWicke <gwi...@wikimedia.org>
Gerrit-Reviewer: Mark Bergsma <m...@wikimedia.org>
Gerrit-Reviewer: Nemo bis <federicol...@tiscali.it>
Gerrit-Reviewer: Ori.livneh <o...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to