Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement region lookup #1

Merged
merged 1 commit into from
Nov 28, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/tests/test01.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ varnish v1 -vcl+backend {

sub vcl_recv {
set req.http.X-GeoIP = geoip.country("127.0.0.1");
set req.http.X-Location = geoip.region("127.0.0.1");
}
} -start

client c1 {
txreq -url "/"
rxresp
expect req.http.X-GeoIP == "Unknown"
expect req.http.X-Location == "Unknown"
}

client c1 -run
27 changes: 27 additions & 0 deletions src/vmod_geoip.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// `----

static const char *unknownCountry= "AA";
static const char *unknownRegion= "Unknown";

int
init_function(struct vmod_priv *priv, const struct VCL_conf *conf)
Expand Down Expand Up @@ -53,3 +54,29 @@ vmod_country(struct sess *sp, const char *ip)
}
return(cp);
}

const char *
vmod_region(struct sess *sp, const char *ip)
{
GeoIPRegion *gir;
const char *region;
GeoIP *gi = NULL;

gi = GeoIP_new(GEOIP_STANDARD);
if (gi) {
gir = GeoIP_region_by_addr(gi, ip);
}
if (!gir) {
region = unknownRegion;
}
region = WS_Dup(sp->wrk->ws, gir->region);

if (gi) {
GeoIP_delete(gi);
}
if ( gir) {
GeoIPRegion_delete(gir);
}
return(region);

}
1 change: 1 addition & 0 deletions src/vmod_geoip.vcc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Module geoip
Init init_function
Function STRING country(STRING)
Function STRING region(STRING)