Merge pull request #13 from oschwald/all-data-types

Support all MaxMind DB data types
This commit is contained in:
Lee 2014-12-13 22:21:25 +00:00
commit eb8a26df11

View file

@ -50,6 +50,15 @@ static ngx_int_t ngx_http_geoip2_cidr_value(ngx_conf_t *cf, ngx_str_t *net,
static void ngx_http_geoip2_cleanup(void *data); static void ngx_http_geoip2_cleanup(void *data);
#define FORMAT(fmt, ...) \
p = ngx_palloc(r->pool, NGX_OFF_T_LEN); \
if (p == NULL) { \
return NGX_ERROR; \
} \
v->len = ngx_sprintf(p, fmt, __VA_ARGS__) - p; \
v->data = p; \
break
static ngx_command_t ngx_http_geoip2_commands[] = { static ngx_command_t ngx_http_geoip2_commands[] = {
{ ngx_string("geoip2"), { ngx_string("geoip2"),
@ -184,18 +193,42 @@ ngx_http_geoip2_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
} }
switch (entry_data.type) { switch (entry_data.type) {
case MMDB_DATA_TYPE_BOOLEAN:
FORMAT("%d", entry_data.boolean);
case MMDB_DATA_TYPE_UTF8_STRING: case MMDB_DATA_TYPE_UTF8_STRING:
v->data = (u_char *) entry_data.utf8_string; v->data = (u_char *) entry_data.utf8_string;
v->len = entry_data.data_size; v->len = entry_data.data_size;
break; break;
case MMDB_DATA_TYPE_UINT32: case MMDB_DATA_TYPE_BYTES:
p = ngx_palloc(r->pool, NGX_OFF_T_LEN); v->data = (u_char *) entry_data.bytes;
if (p == NULL) { v->len = entry_data.data_size;
return NGX_ERROR;
}
v->len = ngx_sprintf(p, "%O", entry_data.uint32) - p;
v->data = p;
break; break;
case MMDB_DATA_TYPE_FLOAT:
FORMAT("%.5f", entry_data.float_value);
case MMDB_DATA_TYPE_DOUBLE:
FORMAT("%.5f", entry_data.double_value);
case MMDB_DATA_TYPE_UINT16:
FORMAT("%uD", entry_data.uint16);
case MMDB_DATA_TYPE_UINT32:
FORMAT("%uD", entry_data.uint32);
case MMDB_DATA_TYPE_INT32:
FORMAT("%D", entry_data.int32);
case MMDB_DATA_TYPE_UINT64:
FORMAT("%uL", entry_data.uint64);
case MMDB_DATA_TYPE_UINT128: ;
#if MMDB_UINT128_IS_BYTE_ARRAY
uint8_t *val = (uint8_t *)entry_data.uint128;
FORMAT( "0x%02x%02x%02x%02x%02x%02x%02x%02x"
"%02x%02x%02x%02x%02x%02x%02x%02x",
val[0], val[1], val[2], val[3],
val[4], val[5], val[6], val[7],
val[8], val[9], val[10], val[11],
val[12], val[13], val[14], val[15]);
#else
mmdb_uint128_t val = entry_data.uint128;
FORMAT("0x%016uxL%016uxL",
(uint64_t) (val >> 64), (uint64_t) val);
#endif
default: default:
goto not_found; goto not_found;
} }