mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-11-28 06:11:32 +01:00
CLEANUP: da: move global settings out of the global section
We replaced global.deviceatlas with global_deviceatlas since there's no need to store all this into the global section. This removes the last #ifdefs, and now the code is 100% self-contained in da.c. The file da.h was now removed because it was only used to load dac.h, which is more easily loaded directly from da.c. It provides another good example of how to integrate code in the future without touching the core parts.
This commit is contained in:
parent
b7a671477f
commit
bee9dde31f
@ -1,9 +0,0 @@
|
||||
#ifndef _IMPORT_DA_H
|
||||
#define _IMPORT_DA_H
|
||||
#ifdef USE_DEVICEATLAS
|
||||
|
||||
#include <types/global.h>
|
||||
#include <dac.h>
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@ -26,7 +26,6 @@
|
||||
|
||||
#include <common/config.h>
|
||||
#include <common/standard.h>
|
||||
#include <import/da.h>
|
||||
#include <types/freq_ctr.h>
|
||||
#include <types/listener.h>
|
||||
#include <types/proxy.h>
|
||||
@ -177,19 +176,6 @@ struct global {
|
||||
#endif
|
||||
struct proxy *stats_fe; /* the frontend holding the stats settings */
|
||||
struct vars vars; /* list of variables for the process scope. */
|
||||
#ifdef USE_DEVICEATLAS
|
||||
struct {
|
||||
void *atlasimgptr;
|
||||
char *jsonpath;
|
||||
char *cookiename;
|
||||
size_t cookienamelen;
|
||||
da_atlas_t atlas;
|
||||
da_evidence_id_t useragentid;
|
||||
da_severity_t loglevel;
|
||||
char separator;
|
||||
unsigned char daset:1;
|
||||
} deviceatlas;
|
||||
#endif
|
||||
};
|
||||
|
||||
extern struct global global;
|
||||
|
||||
98
src/da.c
98
src/da.c
@ -6,7 +6,27 @@
|
||||
#include <proto/log.h>
|
||||
#include <proto/proto_http.h>
|
||||
#include <proto/sample.h>
|
||||
#include <import/da.h>
|
||||
#include <dac.h>
|
||||
|
||||
static struct {
|
||||
void *atlasimgptr;
|
||||
char *jsonpath;
|
||||
char *cookiename;
|
||||
size_t cookienamelen;
|
||||
da_atlas_t atlas;
|
||||
da_evidence_id_t useragentid;
|
||||
da_severity_t loglevel;
|
||||
char separator;
|
||||
unsigned char daset:1;
|
||||
} global_deviceatlas = {
|
||||
.loglevel = 0,
|
||||
.jsonpath = 0,
|
||||
.cookiename = 0,
|
||||
.cookienamelen = 0,
|
||||
.useragentid = 0,
|
||||
.daset = 0,
|
||||
.separator = '|',
|
||||
};
|
||||
|
||||
static int da_json_file(char **args, int section_type, struct proxy *curpx,
|
||||
struct proxy *defpx, const char *file, int line,
|
||||
@ -16,7 +36,7 @@ static int da_json_file(char **args, int section_type, struct proxy *curpx,
|
||||
memprintf(err, "deviceatlas json file : expects a json path.\n");
|
||||
return -1;
|
||||
}
|
||||
global.deviceatlas.jsonpath = strdup(args[1]);
|
||||
global_deviceatlas.jsonpath = strdup(args[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -34,7 +54,7 @@ static int da_log_level(char **args, int section_type, struct proxy *curpx,
|
||||
if (loglevel < 0 || loglevel > 3) {
|
||||
memprintf(err, "deviceatlas log level : expects a log level between 0 and 3, %s given.\n", args[1]);
|
||||
} else {
|
||||
global.deviceatlas.loglevel = (da_severity_t)loglevel;
|
||||
global_deviceatlas.loglevel = (da_severity_t)loglevel;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -48,7 +68,7 @@ static int da_property_separator(char **args, int section_type, struct proxy *cu
|
||||
memprintf(err, "deviceatlas property separator : expects a character argument.\n");
|
||||
return -1;
|
||||
}
|
||||
global.deviceatlas.separator = *args[1];
|
||||
global_deviceatlas.separator = *args[1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -60,9 +80,9 @@ static int da_properties_cookie(char **args, int section_type, struct proxy *cur
|
||||
memprintf(err, "deviceatlas cookie name : expects a string argument.\n");
|
||||
return -1;
|
||||
} else {
|
||||
global.deviceatlas.cookiename = strdup(args[1]);
|
||||
global_deviceatlas.cookiename = strdup(args[1]);
|
||||
}
|
||||
global.deviceatlas.cookienamelen = strlen(global.deviceatlas.cookiename);
|
||||
global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -79,7 +99,7 @@ static da_status_t da_haproxy_seek(void *ctx, off_t off)
|
||||
static void da_haproxy_log(da_severity_t severity, da_status_t status,
|
||||
const char *fmt, va_list args)
|
||||
{
|
||||
if (global.deviceatlas.loglevel && severity <= global.deviceatlas.loglevel) {
|
||||
if (global_deviceatlas.loglevel && severity <= global_deviceatlas.loglevel) {
|
||||
char logbuf[256];
|
||||
vsnprintf(logbuf, sizeof(logbuf), fmt, args);
|
||||
Warning("deviceatlas : %s.\n", logbuf);
|
||||
@ -95,16 +115,16 @@ static int init_deviceatlas(void)
|
||||
{
|
||||
int err_code = 0;
|
||||
|
||||
if (global.deviceatlas.jsonpath != 0) {
|
||||
if (global_deviceatlas.jsonpath != 0) {
|
||||
FILE *jsonp;
|
||||
da_property_decl_t extraprops[] = {{0, 0}};
|
||||
size_t atlasimglen;
|
||||
da_status_t status;
|
||||
|
||||
jsonp = fopen(global.deviceatlas.jsonpath, "r");
|
||||
jsonp = fopen(global_deviceatlas.jsonpath, "r");
|
||||
if (jsonp == 0) {
|
||||
Alert("deviceatlas : '%s' json file has invalid path or is not readable.\n",
|
||||
global.deviceatlas.jsonpath);
|
||||
global_deviceatlas.jsonpath);
|
||||
err_code |= ERR_ALERT | ERR_FATAL;
|
||||
goto out;
|
||||
}
|
||||
@ -112,17 +132,17 @@ static int init_deviceatlas(void)
|
||||
da_init();
|
||||
da_seterrorfunc(da_haproxy_log);
|
||||
status = da_atlas_compile(jsonp, da_haproxy_read, da_haproxy_seek,
|
||||
&global.deviceatlas.atlasimgptr, &atlasimglen);
|
||||
&global_deviceatlas.atlasimgptr, &atlasimglen);
|
||||
fclose(jsonp);
|
||||
if (status != DA_OK) {
|
||||
Alert("deviceatlas : '%s' json file is invalid.\n",
|
||||
global.deviceatlas.jsonpath);
|
||||
global_deviceatlas.jsonpath);
|
||||
err_code |= ERR_ALERT | ERR_FATAL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
status = da_atlas_open(&global.deviceatlas.atlas, extraprops,
|
||||
global.deviceatlas.atlasimgptr, atlasimglen);
|
||||
status = da_atlas_open(&global_deviceatlas.atlas, extraprops,
|
||||
global_deviceatlas.atlasimgptr, atlasimglen);
|
||||
|
||||
if (status != DA_OK) {
|
||||
Alert("deviceatlas : data could not be compiled.\n");
|
||||
@ -130,14 +150,14 @@ static int init_deviceatlas(void)
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (global.deviceatlas.cookiename == 0) {
|
||||
global.deviceatlas.cookiename = strdup(DA_COOKIENAME_DEFAULT);
|
||||
global.deviceatlas.cookienamelen = strlen(global.deviceatlas.cookiename);
|
||||
if (global_deviceatlas.cookiename == 0) {
|
||||
global_deviceatlas.cookiename = strdup(DA_COOKIENAME_DEFAULT);
|
||||
global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename);
|
||||
}
|
||||
|
||||
global.deviceatlas.useragentid = da_atlas_header_evidence_id(&global.deviceatlas.atlas,
|
||||
global_deviceatlas.useragentid = da_atlas_header_evidence_id(&global_deviceatlas.atlas,
|
||||
"user-agent");
|
||||
global.deviceatlas.daset = 1;
|
||||
global_deviceatlas.daset = 1;
|
||||
|
||||
fprintf(stdout, "Deviceatlas module loaded.\n");
|
||||
}
|
||||
@ -148,14 +168,14 @@ out:
|
||||
|
||||
static void deinit_deviceatlas(void)
|
||||
{
|
||||
if (global.deviceatlas.jsonpath != 0) {
|
||||
free(global.deviceatlas.jsonpath);
|
||||
if (global_deviceatlas.jsonpath != 0) {
|
||||
free(global_deviceatlas.jsonpath);
|
||||
}
|
||||
|
||||
if (global.deviceatlas.daset == 1) {
|
||||
free(global.deviceatlas.cookiename);
|
||||
da_atlas_close(&global.deviceatlas.atlas);
|
||||
free(global.deviceatlas.atlasimgptr);
|
||||
if (global_deviceatlas.daset == 1) {
|
||||
free(global_deviceatlas.cookiename);
|
||||
da_atlas_close(&global_deviceatlas.atlas);
|
||||
free(global_deviceatlas.atlasimgptr);
|
||||
}
|
||||
|
||||
da_fini();
|
||||
@ -177,14 +197,14 @@ static int da_haproxy(const struct arg *args, struct sample *smp, da_deviceinfo_
|
||||
i = 0;
|
||||
|
||||
for (; propname != 0; i ++, propname = (const char *)args[i].data.str.str) {
|
||||
status = da_atlas_getpropid(&global.deviceatlas.atlas,
|
||||
status = da_atlas_getpropid(&global_deviceatlas.atlas,
|
||||
propname, &prop);
|
||||
if (status != DA_OK) {
|
||||
chunk_appendf(tmp, "%c", global.deviceatlas.separator);
|
||||
chunk_appendf(tmp, "%c", global_deviceatlas.separator);
|
||||
continue;
|
||||
}
|
||||
pprop = ∝
|
||||
da_atlas_getproptype(&global.deviceatlas.atlas, *pprop, &proptype);
|
||||
da_atlas_getproptype(&global_deviceatlas.atlas, *pprop, &proptype);
|
||||
|
||||
switch (proptype) {
|
||||
case DA_TYPE_BOOLEAN: {
|
||||
@ -216,7 +236,7 @@ static int da_haproxy(const struct arg *args, struct sample *smp, da_deviceinfo_
|
||||
break;
|
||||
}
|
||||
|
||||
chunk_appendf(tmp, "%c", global.deviceatlas.separator);
|
||||
chunk_appendf(tmp, "%c", global_deviceatlas.separator);
|
||||
}
|
||||
|
||||
da_close(devinfo);
|
||||
@ -240,7 +260,7 @@ static int da_haproxy_conv(const struct arg *args, struct sample *smp, void *pri
|
||||
char useragentbuf[1024] = { 0 };
|
||||
int i;
|
||||
|
||||
if (global.deviceatlas.daset == 0 || smp->data.u.str.len == 0) {
|
||||
if (global_deviceatlas.daset == 0 || smp->data.u.str.len == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -250,8 +270,8 @@ static int da_haproxy_conv(const struct arg *args, struct sample *smp, void *pri
|
||||
|
||||
useragent = (const char *)useragentbuf;
|
||||
|
||||
status = da_search(&global.deviceatlas.atlas, &devinfo,
|
||||
global.deviceatlas.useragentid, useragent, 0);
|
||||
status = da_search(&global_deviceatlas.atlas, &devinfo,
|
||||
global_deviceatlas.useragentid, useragent, 0);
|
||||
|
||||
return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
|
||||
}
|
||||
@ -269,7 +289,7 @@ static int da_haproxy_fetch(const struct arg *args, struct sample *smp, const ch
|
||||
char vbuf[DA_MAX_HEADERS][1024] = {{ 0 }};
|
||||
int i, nbh = 0;
|
||||
|
||||
if (global.deviceatlas.daset == 0) {
|
||||
if (global_deviceatlas.daset == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -302,7 +322,7 @@ static int da_haproxy_fetch(const struct arg *args, struct sample *smp, const ch
|
||||
pval = (hctx.line + hctx.val);
|
||||
|
||||
if (strcmp(hbuf, "Accept-Language") == 0) {
|
||||
evid = da_atlas_accept_language_evidence_id(&global.deviceatlas.
|
||||
evid = da_atlas_accept_language_evidence_id(&global_deviceatlas.
|
||||
atlas);
|
||||
} else if (strcmp(hbuf, "Cookie") == 0) {
|
||||
char *p, *eval;
|
||||
@ -313,16 +333,16 @@ static int da_haproxy_fetch(const struct arg *args, struct sample *smp, const ch
|
||||
* The cookie value, if it exists, is located between the current header's
|
||||
* value position and the next one
|
||||
*/
|
||||
if (extract_cookie_value(pval, eval, global.deviceatlas.cookiename,
|
||||
global.deviceatlas.cookienamelen, 1, &p, &pl) == NULL) {
|
||||
if (extract_cookie_value(pval, eval, global_deviceatlas.cookiename,
|
||||
global_deviceatlas.cookienamelen, 1, &p, &pl) == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
vlen = (size_t)pl;
|
||||
pval = p;
|
||||
evid = da_atlas_clientprop_evidence_id(&global.deviceatlas.atlas);
|
||||
evid = da_atlas_clientprop_evidence_id(&global_deviceatlas.atlas);
|
||||
} else {
|
||||
evid = da_atlas_header_evidence_id(&global.deviceatlas.atlas,
|
||||
evid = da_atlas_header_evidence_id(&global_deviceatlas.atlas,
|
||||
hbuf);
|
||||
}
|
||||
|
||||
@ -339,7 +359,7 @@ static int da_haproxy_fetch(const struct arg *args, struct sample *smp, const ch
|
||||
++ nbh;
|
||||
}
|
||||
|
||||
status = da_searchv(&global.deviceatlas.atlas, &devinfo,
|
||||
status = da_searchv(&global_deviceatlas.atlas, &devinfo,
|
||||
ev, nbh);
|
||||
|
||||
return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
|
||||
|
||||
@ -166,17 +166,6 @@ struct global global = {
|
||||
#ifdef DEFAULT_MAXSSLCONN
|
||||
.maxsslconn = DEFAULT_MAXSSLCONN,
|
||||
#endif
|
||||
#endif
|
||||
#ifdef USE_DEVICEATLAS
|
||||
.deviceatlas = {
|
||||
.loglevel = 0,
|
||||
.jsonpath = 0,
|
||||
.cookiename = 0,
|
||||
.cookienamelen = 0,
|
||||
.useragentid = 0,
|
||||
.daset = 0,
|
||||
.separator = '|',
|
||||
},
|
||||
#endif
|
||||
/* others NULL OK */
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user