Ticket #1546: lighttpd-1.4.13-mod_geoip_lowercase.patch

File lighttpd-1.4.13-mod_geoip_lowercase.patch, 5.9 kB (added by spillgroup, 7 months ago)
  • src/mod_geoip.c

    Path for: http://trac.lighttpd.net/trac/ticket/1546
    
    Adds a 'geoip.force_lowercase' config parameter to force all returned country codes in lowercase. 
    
    
    diff -Bru lighttpd-1.4.13/src/mod_geoip.c lighttpd-1.4.13_mod_geoip/src/mod_geoip.c
    old new  
    6464        unsigned short mem_cache; 
    6565        buffer  *db_name; 
    6666        buffer *default_country_code; 
     67        unsigned short force_lowercase; 
    6768        GeoIP   *gi; 
    6869} plugin_config; 
    6970 
     
    126127                { "geoip.db-filename",  NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },     /* 0 */ 
    127128                { "geoip.memory-cache", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },    /* 1 */ 
    128129                { "geoip.default_country_code", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },    /* 2 */ 
     130                { "geoip.force_lowercase", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 3 */ 
    129131                { NULL,                 NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } 
    130132        }; 
    131133         
     
    142144                s->db_name = buffer_init(); 
    143145                s->mem_cache = 0; /* default: do not load db to cache */ 
    144146                s->default_country_code = buffer_init(); 
     147                s->force_lowercase = 0; 
    145148                s->gi = NULL; 
    146149 
    147150                cv[0].destination = s->db_name; 
    148151                cv[1].destination = &(s->mem_cache); 
    149152                cv[2].destination = s->default_country_code; 
     153                cv[3].destination = &(s->force_lowercase); 
    150154 
    151155                p->config_storage[i] = s; 
    152156         
     
    191195         
    192196        PATCH(db_name); 
    193197        PATCH(mem_cache); 
    194         PATCH(gi); 
     198        PATCH(gi); 
     199        PATCH(default_country_code); 
     200        PATCH(force_lowercase); 
    195201 
    196202        /* skip the first, the global context */ 
    197203        for (i = 1; i < srv->config_context->used; i++) { 
     
    210216                        } 
    211217 
    212218                        if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.memory-cache"))) { 
    213                                 PATCH(mem_cache); 
    214                         } 
     219                                PATCH(mem_cache); 
     220                        } 
     221 
     222                        if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.default_country_code"))) { 
     223                                PATCH(default_country_code); 
     224                        } 
     225                         
     226                        if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.force_lowercase"))) { 
     227                                PATCH(force_lowercase); 
     228                        } 
    215229                } 
    216230        } 
    217231         
     
    219233} 
    220234#undef PATCH 
    221235 
     236char* strtolower(const char *tmp) { 
     237        char *str = strdup(tmp); 
     238        size_t len, i; 
     239         
     240        len = strlen(str); 
     241        for(i=0; i<len; i++) { 
     242                str[i] = tolower(str[i]); 
     243        } 
     244         
     245        return str; 
     246} 
     247 
    222248URIHANDLER_FUNC(mod_geoip_subrequest) { 
    223249        plugin_data *p = p_d; 
    224                 plugin_config *s = p->config_storage[0]; 
    225250 
    226251        mod_geoip_patch_connection(srv, con, p); 
    227252 
    228253        if (!buffer_is_empty(p->conf.db_name)) { 
    229254                const char *remote_ip; 
    230                 GeoIP *gi; 
    231255                data_string *ds; 
    232256                const char *returnedCountry; 
     257                char *tmp; 
    233258 
    234259                remote_ip = inet_ntop_cache_get_ip(srv, &(con->dst_addr)); 
    235260 
     
    237262                        /* get the country code 2 chars */ 
    238263                        if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE"))) { 
    239264                                if (NULL != (returnedCountry = GeoIP_country_code_by_addr(p->conf.gi, remote_ip))) { 
    240                                         if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) { 
    241                                                 ds = data_string_init(); 
    242                                         } 
     265                                        if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) { 
     266                                                        ds = data_string_init(); 
     267                                        } 
     268                                                         
     269                                        if(p->conf.force_lowercase) { 
     270                                                tmp = strtolower(returnedCountry); 
     271                                        } else { 
     272                                                tmp = strdup(returnedCountry); 
     273                                        } 
    243274 
    244275                                        buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE"); 
    245                                         buffer_copy_string(ds->value, returnedCountry); 
     276                                        buffer_copy_string(ds->value, tmp); 
    246277                                        array_insert_unique(con->environment, (data_unset *)ds); 
     278                                         
     279                                        free(tmp); 
    247280                                } else { 
    248                                         if(s->default_country_code->used > 0) { 
     281                                        if(p->conf.default_country_code->used > 0) { 
    249282                                                if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) { 
    250283                                                        ds = data_string_init(); 
    251284                                                } 
    252285                                                 
    253286                                                buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE"); 
    254                                                 buffer_copy_string(ds->value, s->default_country_code->ptr); 
     287                                                buffer_copy_string(ds->value, p->conf.default_country_code->ptr); 
    255288                                                array_insert_unique(con->environment, (data_unset *)ds);         
    256289                                        } 
    257290                                } 
     
    364397                                } 
    365398 
    366399                                char latitude[32]; 
    367                                 sprintf(&latitude, "%f", gir->latitude); 
     400                                sprintf(latitude, "%f", gir->latitude); 
    368401                                buffer_copy_string(ds->key, "GEOIP_CITY_LATITUDE"); 
    369402                                buffer_copy_string(ds->value, latitude); 
    370403                                array_insert_unique(con->environment, (data_unset *)ds); 
     
    377410                                } 
    378411 
    379412                                char long_latitude[32]; 
    380                                 sprintf(&long_latitude, "%f", gir->longitude); 
     413                                sprintf(long_latitude, "%f", gir->longitude); 
    381414                                buffer_copy_string(ds->key, "GEOIP_CITY_LONG_LATITUDE"); 
    382415                                buffer_copy_string(ds->value, long_latitude); 
    383416                                array_insert_unique(con->environment, (data_unset *)ds); 
     
    390423                                } 
    391424 
    392425                                char dc[5]; 
    393                                 sprintf(&dc, "%i", gir->dma_code); 
     426                                sprintf(dc, "%i", gir->dma_code); 
    394427                                buffer_copy_string(ds->key, "GEOIP_CITY_DMA_CODE"); 
    395428                                buffer_copy_string(ds->value, dc); 
    396429                                array_insert_unique(con->environment, (data_unset *)ds); 
     
    403436                                } 
    404437 
    405438                                char ac[5]; 
    406                                 sprintf(&ac, "%i", gir->area_code); 
     439                                sprintf(ac, "%i", gir->area_code); 
    407440                                buffer_copy_string(ds->key, "GEOIP_CITY_AREA_CODE"); 
    408441                                buffer_copy_string(ds->value, ac); 
    409442                                array_insert_unique(con->environment, (data_unset *)ds);