Project

General

Profile

lighttpd-1.4.13-mod_geoip_lowercase.patch

spillgroup, 2008-01-31 10:07

View differences:

lighttpd-1.4.13_mod_geoip/src/mod_geoip.c 2006-10-11 16:57:31.000000000 +0200
64 64
	unsigned short mem_cache;
65 65
	buffer	*db_name;
66 66
	buffer *default_country_code;
67
	unsigned short force_lowercase;
67 68
	GeoIP   *gi;
68 69
} plugin_config;
69 70

  
......
126 127
		{ "geoip.db-filename",	NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },	/* 0 */
127 128
		{ "geoip.memory-cache",	NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },    /* 1 */
128 129
		{ "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 */
129 131
		{ NULL,			NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
130 132
	};
131 133
	
......
142 144
		s->db_name = buffer_init();
143 145
		s->mem_cache = 0; /* default: do not load db to cache */
144 146
		s->default_country_code = buffer_init();
147
		s->force_lowercase = 0;
145 148
		s->gi = NULL;
146 149

  
147 150
		cv[0].destination = s->db_name;
148 151
		cv[1].destination = &(s->mem_cache);
149 152
		cv[2].destination = s->default_country_code;
153
		cv[3].destination = &(s->force_lowercase);
150 154

  
151 155
		p->config_storage[i] = s;
152 156
	
......
191 195
	
192 196
	PATCH(db_name);
193 197
	PATCH(mem_cache);
194
        PATCH(gi);
198
	PATCH(gi);
199
	PATCH(default_country_code);
200
	PATCH(force_lowercase);
195 201

  
196 202
	/* skip the first, the global context */
197 203
	for (i = 1; i < srv->config_context->used; i++) {
......
210 216
			}
211 217

  
212 218
			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
			}
215 229
		}
216 230
	}
217 231
	
......
219 233
}
220 234
#undef PATCH
221 235

  
236
char* 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

  
222 248
URIHANDLER_FUNC(mod_geoip_subrequest) {
223 249
        plugin_data *p = p_d;
224
		plugin_config *s = p->config_storage[0];
225 250

  
226 251
        mod_geoip_patch_connection(srv, con, p);
227 252

  
228 253
        if (!buffer_is_empty(p->conf.db_name)) {
229 254
		const char *remote_ip;
230
		GeoIP *gi;
231 255
		data_string *ds;
232 256
		const char *returnedCountry;
257
		char *tmp;
233 258

  
234 259
		remote_ip = inet_ntop_cache_get_ip(srv, &(con->dst_addr));
235 260

  
......
237 262
			/* get the country code 2 chars */
238 263
			if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE"))) {
239 264
				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
					}
243 274

  
244 275
					buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE");
245
					buffer_copy_string(ds->value, returnedCountry);
276
					buffer_copy_string(ds->value, tmp);
246 277
					array_insert_unique(con->environment, (data_unset *)ds);
278
					
279
					free(tmp);
247 280
				} else {
248
					if(s->default_country_code->used > 0) {
281
					if(p->conf.default_country_code->used > 0) {
249 282
						if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
250 283
							ds = data_string_init();
251 284
						}
252 285
						
253 286
						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);
255 288
						array_insert_unique(con->environment, (data_unset *)ds);	
256 289
					}
257 290
				}
......
364 397
				}
365 398

  
366 399
				char latitude[32];
367
				sprintf(&latitude, "%f", gir->latitude);
400
				sprintf(latitude, "%f", gir->latitude);
368 401
				buffer_copy_string(ds->key, "GEOIP_CITY_LATITUDE");
369 402
				buffer_copy_string(ds->value, latitude);
370 403
				array_insert_unique(con->environment, (data_unset *)ds);
......
377 410
                                }
378 411

  
379 412
                                char long_latitude[32];
380
                                sprintf(&long_latitude, "%f", gir->longitude);
413
                                sprintf(long_latitude, "%f", gir->longitude);
381 414
                                buffer_copy_string(ds->key, "GEOIP_CITY_LONG_LATITUDE");
382 415
                                buffer_copy_string(ds->value, long_latitude);
383 416
                                array_insert_unique(con->environment, (data_unset *)ds);
......
390 423
                                }
391 424

  
392 425
                                char dc[5];
393
                                sprintf(&dc, "%i", gir->dma_code);
426
                                sprintf(dc, "%i", gir->dma_code);
394 427
                                buffer_copy_string(ds->key, "GEOIP_CITY_DMA_CODE");
395 428
                                buffer_copy_string(ds->value, dc);
396 429
                                array_insert_unique(con->environment, (data_unset *)ds);
......
403 436
                                }
404 437

  
405 438
                                char ac[5];
406
                                sprintf(&ac, "%i", gir->area_code);
439
                                sprintf(ac, "%i", gir->area_code);
407 440
                                buffer_copy_string(ds->key, "GEOIP_CITY_AREA_CODE");
408 441
                                buffer_copy_string(ds->value, ac);
409 442
                                array_insert_unique(con->environment, (data_unset *)ds);