Ticket #1546: lighttpd-1.4.11-mod_geoip_default_country_code.patch

File lighttpd-1.4.11-mod_geoip_default_country_code.patch, 2.9 kB (added by spillgroup, 8 months ago)
  • src/mod_geoip.c

    Patch for: http://trac.lighttpd.net/trac/ticket/1546
    
    Adds a 'geoip.default_country_code' config parameter to use when no matching country could be found. 
    
    
    diff -Bru --ignore-all-space lighttpd-1.4.11/src/mod_geoip.c lighttpd-1.4.11-mod_geoip/src/mod_geoip.c
    old new  
    6363typedef struct { 
    6464        unsigned short mem_cache; 
    6565        buffer  *db_name; 
     66        buffer *default_country_code; 
    6667        GeoIP   *gi; 
    6768} plugin_config; 
    6869 
     
    100101                        if (!s) continue; 
    101102                         
    102103                        buffer_free(s->db_name); 
     104                        buffer_free(s->default_country_code); 
    103105                         
    104106                        /* clean up */ 
    105107                        GeoIP_delete(s->gi); 
     
    123125        config_values_t cv[] = {  
    124126                { "geoip.db-filename",  NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },     /* 0 */ 
    125127                { "geoip.memory-cache", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },    /* 1 */ 
     128                { "geoip.default_country_code", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },    /* 2 */ 
    126129                { NULL,                 NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } 
    127130        }; 
    128131         
     
    138141                 
    139142                s->db_name = buffer_init(); 
    140143                s->mem_cache = 0; /* default: do not load db to cache */ 
     144                s->default_country_code = buffer_init(); 
    141145                s->gi = NULL; 
    142146 
    143147                cv[0].destination = s->db_name; 
    144148                cv[1].destination = &(s->mem_cache); 
     149                cv[2].destination = s->default_country_code; 
    145150 
    146151                p->config_storage[i] = s; 
    147152         
     
    159164                                mode = GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE; 
    160165 
    161166                        if (NULL == (s->gi = GeoIP_open(s->db_name->ptr, mode))) { 
    162                                 log_error_write(srv, __FILE__, __LINE__, "s", 
    163                                         "failed to open GeoIP database!!!"); 
     167                                log_error_write(srv, __FILE__, __LINE__, "ss", 
     168                                        "failed to open GeoIP database: ", s->db_name->ptr); 
    164169 
    165170                                return HANDLER_ERROR; 
    166171                        } 
     
    216221 
    217222URIHANDLER_FUNC(mod_geoip_subrequest) { 
    218223        plugin_data *p = p_d; 
     224                plugin_config *s = p->config_storage[0]; 
    219225 
    220226        mod_geoip_patch_connection(srv, con, p); 
    221227 
     
    238245                                        buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE"); 
    239246                                        buffer_copy_string(ds->value, returnedCountry); 
    240247                                        array_insert_unique(con->environment, (data_unset *)ds); 
     248                                } else { 
     249                                        if(s->default_country_code->used > 0) { 
     250                                                if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) { 
     251                                                        ds = data_string_init(); 
     252                                                } 
     253                                                 
     254                                                buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE"); 
     255                                                buffer_copy_string(ds->value, s->default_country_code->ptr); 
     256                                                array_insert_unique(con->environment, (data_unset *)ds);         
     257                                        } 
    241258                                } 
    242259                        } 
    243260 
     
    406423        p->name        = buffer_init_string("geoip"); 
    407424         
    408425        p->init        = mod_geoip_init; 
    409         p->handle_subrequest_start = mod_geoip_subrequest; 
     426//      p->handle_subrequest_start = mod_geoip_subrequest; 
     427        p->handle_uri_clean = mod_geoip_subrequest; 
    410428        p->set_defaults  = mod_geoip_set_defaults; 
    411429        p->cleanup     = mod_geoip_free; 
    412430