Skip to content
Browse files

Works!! Added tests

  • Loading branch information...
1 parent 05a8be4 commit 935ef72455d2db6b5962d548b4b91a6f4a4b259f @cosimo committed Jan 28, 2010
Showing with 49 additions and 122 deletions.
  1. +3 −2 .gitignore
  2. BIN geoip
  3. +26 −20 geoip.c
  4. +0 −90 geoip.vcl
  5. +4 −3 t/basic.t
  6. +16 −7 t/test.pm
View
5 .gitignore
@@ -1,2 +1,3 @@
-accept-language
-accept-language.vcl
+geoip
+geoip.vcl
+.*.sw[op]
View
BIN geoip
Binary file not shown.
View
46 geoip.c
@@ -11,7 +11,7 @@
#include <pthread.h>
#define vcl_string char
-#define JSON_MAXLEN 255
+#define HEADER_MAXLEN 255
pthread_mutex_t geoip_mutex = PTHREAD_MUTEX_INITIALIZER;
GeoIP* gi;
@@ -25,7 +25,7 @@ void geoip_init () {
}
}
-static inline int geoip_lookup(vcl_string *ip, vcl_string *json) {
+static inline int geoip_lookup(vcl_string *ip, vcl_string *resolved) {
int lookup_success = 0;
pthread_mutex_lock(&geoip_mutex);
@@ -39,19 +39,18 @@ static inline int geoip_lookup(vcl_string *ip, vcl_string *json) {
/* Lookup succeeded */
if (record) {
lookup_success = 1;
- snprintf(json, JSON_MAXLEN, "{\"city\":\"%s\",\"country\":\"%s\",\"lat\":\"%f\",\"lon\":\"%f\",\"classC\":\"%s\",\"netmask\":\"%d\"}",
+ snprintf(resolved, HEADER_MAXLEN, "city:%s, country:%s, lat:%f, lon:%f, ip:%s",
record->city,
record->country_code,
record->latitude,
record->longitude,
- ip,
- GeoIP_last_netmask(gi)
+ ip
);
}
/* Failed lookup */
else {
- strncpy(json, "{}", JSON_MAXLEN);
+ strncpy(resolved, "", HEADER_MAXLEN);
}
pthread_mutex_unlock(&geoip_mutex);
@@ -60,30 +59,37 @@ static inline int geoip_lookup(vcl_string *ip, vcl_string *json) {
}
#ifdef __VCL__
-void vcl_geoip_handler(const struct session *sp) {
-
+/* Returns the GeoIP info as synthetic response */
+void vcl_geoip_send_synthetic(const struct session *sp) {
+ vcl_string hval[HEADER_MAXLEN];
vcl_string *ip = VRT_IP_string(sp, VRT_r_client_ip(sp));
- vcl_string json[JSON_MAXLEN];
-
- if (geoip_lookup(ip, json)) {
- /* Return json string as synthetic response,
- here varnishd itself responds to client */
- VRT_synth_page(sp, 0, json, vrt_magic_string_end);
+ if (geoip_lookup(ip, hval)) {
+ VRT_synth_page(sp, 0, hval, vrt_magic_string_end);
}
else {
- /* Send an empty JSON response */
- VRT_synth_page(sp, 0, "Geo = {}", vrt_magic_string_end);
+ VRT_synth_page(sp, 0, "", vrt_magic_string_end);
}
+}
- return;
+/* Sets "X-Geo-IP" header with the geoip resolved information */
+void vcl_geoip_set_header(const struct session *sp) {
+ vcl_string hval[HEADER_MAXLEN];
+ vcl_string *ip = VRT_IP_string(sp, VRT_r_client_ip(sp));
+ if (geoip_lookup(ip, hval)) {
+ VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", hval, vrt_magic_string_end);
+ }
+ else {
+ /* Send an empty header */
+ VRT_SetHdr(sp, HDR_REQ, "\011X-Geo-IP:", "", vrt_magic_string_end);
+ }
}
#else
int main(int argc, char **argv) {
- vcl_string json[JSON_MAXLEN] = "";
+ vcl_string resolved[HEADER_MAXLEN] = "";
if (argc == 2 && argv[1]) {
- geoip_lookup(argv[1], json);
+ geoip_lookup(argv[1], resolved);
}
- printf("%s\n", json);
+ printf("%s\n", resolved);
return 0;
}
#endif /* __VCL__ */
View
90 geoip.vcl
@@ -1,90 +0,0 @@
-C{
-
-/* ------------------------------------------------------------------ */
-/* THIS FILE IS AUTOMATICALLY GENERATED BY ./gen_vcl.pl. DO NOT EDIT. */
-
-/*
- * Varnish-powered Geo IP lookup
- * Cosimo, 28/01/2010
- */
-
-#include <assert.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <GeoIPCity.h>
-#include <pthread.h>
-
-#define vcl_string char
-#define JSON_MAXLEN 255
-
-pthread_mutex_t geoip_mutex = PTHREAD_MUTEX_INITIALIZER;
-GeoIP* gi;
-GeoIPRecord *record;
-
-/* Init GeoIP code */
-void geoip_init () {
- if (!gi) {
- gi = GeoIP_open_type(GEOIP_CITY_EDITION_REV1,GEOIP_MEMORY_CACHE);
- assert(gi);
- }
-}
-
-static inline int geoip_lookup(vcl_string *ip, vcl_string *json) {
- int lookup_success = 0;
-
- pthread_mutex_lock(&geoip_mutex);
-
- if (!gi) {
- geoip_init();
- }
-
- record = GeoIP_record_by_addr(gi, ip);
-
- /* Lookup succeeded */
- if (record) {
- lookup_success = 1;
- snprintf(json, JSON_MAXLEN, "{\"city\":\"%s\",\"country\":\"%s\",\"lat\":\"%f\",\"lon\":\"%f\",\"classC\":\"%s\",\"netmask\":\"%d\"}",
- record->city,
- record->country_code,
- record->latitude,
- record->longitude,
- ip,
- GeoIP_last_netmask(gi)
- );
- }
-
- /* Failed lookup */
- else {
- strncpy(json, "{}", JSON_MAXLEN);
- }
-
- pthread_mutex_unlock(&geoip_mutex);
-
- return lookup_success;
-}
-
-void vcl_geoip_handler(const struct session *sp) {
-
- vcl_string *ip = VRT_IP_string(sp, VRT_r_client_ip(sp));
- vcl_string json[JSON_MAXLEN];
-
- if (geoip_lookup(ip, json)) {
- /* Return json string as synthetic response,
- here varnishd itself responds to client */
- VRT_synth_page(sp, 0, json, vrt_magic_string_end);
- }
- else {
- /* Send an empty JSON response */
- VRT_synth_page(sp, 0, "Geo = {}", vrt_magic_string_end);
- }
-
- return;
-}
-
-/* vim: syn=c ts=4 et sts=4 sw=4 tw=0
-*/
-
-/* THIS FILE IS AUTOMATICALLY GENERATED BY ./gen_vcl.pl. DO NOT EDIT. */
-/* ------------------------------------------------------------------ */
-}C
View
7 t/basic.t
@@ -8,16 +8,17 @@ use strict;
require test;
my @ip = (
- [ '213.236.208.22' => 'NO' ],
+ [ '213.236.208.22' => 'NO' => 'Oslo' ],
);
-Test::More::plan(tests => 1 + @ip);
+Test::More::plan(tests => 1 + (2 * @ip));
test::update_binary();
# Basically test for supported languages
for (@ip) {
- my ($ip, $country) = @{ $_ };
+ my ($ip, $country, $city) = @{ $_ };
test::is_country($ip, $country);
+ test::is_city($ip, $city);
}
View
23 t/test.pm
@@ -26,19 +26,28 @@ sub run_binary {
return $output;
}
+sub get_bit {
+ my ($geoip_header, $bit_type) = @_;
+ my ($result) = ($geoip_header =~ m{$bit_type:([^,]*),});
+ return $result;
+}
+
sub is_country {
my ($ip, $expected_country, $message) = @_;
- my $json_str = run_binary($ip);
- my $json_obj = JSON::XS->new();
- #diag('JSON:'.$json_str);
- my $parsed = $json_obj->decode($json_str);
- my $country = $parsed->{'country'};
-
+ my $geoip_str = run_binary($ip);
+ my $country = get_bit($geoip_str, 'country');
$message ||= qq(Lookup of ip '$ip' should correspond to country '$expected_country');
-
return is($country, $expected_country, $message);
}
+sub is_city {
+ my ($ip, $expected_city, $message) = @_;
+ my $geoip_str = run_binary($ip);
+ my $city = get_bit($geoip_str, 'city');
+ $message ||= qq(Lookup of ip '$ip' should correspond to city '$expected_city');
+ return is($city, $expected_city, $message);
+}
+
1;
=pod

0 comments on commit 935ef72

Please sign in to comment.
Something went wrong with that request. Please try again.