David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | |
| 3 | #include <common/cfgparse.h> |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 4 | #include <common/errors.h> |
Willy Tarreau | f63386a | 2015-06-01 15:39:50 +0200 | [diff] [blame] | 5 | #include <proto/arg.h> |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 6 | #include <proto/log.h> |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 7 | #include <proto/proto_http.h> |
Willy Tarreau | f63386a | 2015-06-01 15:39:50 +0200 | [diff] [blame] | 8 | #include <proto/sample.h> |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 9 | #include <dac.h> |
| 10 | |
| 11 | static struct { |
| 12 | void *atlasimgptr; |
| 13 | char *jsonpath; |
| 14 | char *cookiename; |
| 15 | size_t cookienamelen; |
| 16 | da_atlas_t atlas; |
| 17 | da_evidence_id_t useragentid; |
| 18 | da_severity_t loglevel; |
| 19 | char separator; |
| 20 | unsigned char daset:1; |
| 21 | } global_deviceatlas = { |
| 22 | .loglevel = 0, |
| 23 | .jsonpath = 0, |
| 24 | .cookiename = 0, |
| 25 | .cookienamelen = 0, |
| 26 | .useragentid = 0, |
| 27 | .daset = 0, |
| 28 | .separator = '|', |
| 29 | }; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 30 | |
| 31 | static int da_json_file(char **args, int section_type, struct proxy *curpx, |
| 32 | struct proxy *defpx, const char *file, int line, |
| 33 | char **err) |
| 34 | { |
| 35 | if (*(args[1]) == 0) { |
| 36 | memprintf(err, "deviceatlas json file : expects a json path.\n"); |
| 37 | return -1; |
| 38 | } |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 39 | global_deviceatlas.jsonpath = strdup(args[1]); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int da_log_level(char **args, int section_type, struct proxy *curpx, |
| 44 | struct proxy *defpx, const char *file, int line, |
| 45 | char **err) |
| 46 | { |
| 47 | int loglevel; |
| 48 | if (*(args[1]) == 0) { |
| 49 | memprintf(err, "deviceatlas log level : expects an integer argument.\n"); |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | loglevel = atol(args[1]); |
| 54 | if (loglevel < 0 || loglevel > 3) { |
| 55 | memprintf(err, "deviceatlas log level : expects a log level between 0 and 3, %s given.\n", args[1]); |
| 56 | } else { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 57 | global_deviceatlas.loglevel = (da_severity_t)loglevel; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int da_property_separator(char **args, int section_type, struct proxy *curpx, |
| 64 | struct proxy *defpx, const char *file, int line, |
| 65 | char **err) |
| 66 | { |
| 67 | if (*(args[1]) == 0) { |
| 68 | memprintf(err, "deviceatlas property separator : expects a character argument.\n"); |
| 69 | return -1; |
| 70 | } |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 71 | global_deviceatlas.separator = *args[1]; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 75 | static int da_properties_cookie(char **args, int section_type, struct proxy *curpx, |
| 76 | struct proxy *defpx, const char *file, int line, |
| 77 | char **err) |
| 78 | { |
| 79 | if (*(args[1]) == 0) { |
| 80 | memprintf(err, "deviceatlas cookie name : expects a string argument.\n"); |
| 81 | return -1; |
| 82 | } else { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 83 | global_deviceatlas.cookiename = strdup(args[1]); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 84 | } |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 85 | global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 89 | static size_t da_haproxy_read(void *ctx, size_t len, char *buf) |
| 90 | { |
| 91 | return fread(buf, 1, len, ctx); |
| 92 | } |
| 93 | |
| 94 | static da_status_t da_haproxy_seek(void *ctx, off_t off) |
| 95 | { |
| 96 | return fseek(ctx, off, SEEK_SET) != -1 ? DA_OK : DA_SYS; |
| 97 | } |
| 98 | |
| 99 | static void da_haproxy_log(da_severity_t severity, da_status_t status, |
| 100 | const char *fmt, va_list args) |
| 101 | { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 102 | if (global_deviceatlas.loglevel && severity <= global_deviceatlas.loglevel) { |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 103 | char logbuf[256]; |
| 104 | vsnprintf(logbuf, sizeof(logbuf), fmt, args); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 105 | ha_warning("deviceatlas : %s.\n", logbuf); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 109 | #define DA_COOKIENAME_DEFAULT "DAPROPS" |
| 110 | |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 111 | /* |
| 112 | * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*. |
| 113 | */ |
| 114 | static int init_deviceatlas(void) |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 115 | { |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 116 | int err_code = 0; |
| 117 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 118 | if (global_deviceatlas.jsonpath != 0) { |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 119 | FILE *jsonp; |
| 120 | da_property_decl_t extraprops[] = {{0, 0}}; |
| 121 | size_t atlasimglen; |
| 122 | da_status_t status; |
| 123 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 124 | jsonp = fopen(global_deviceatlas.jsonpath, "r"); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 125 | if (jsonp == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 126 | ha_alert("deviceatlas : '%s' json file has invalid path or is not readable.\n", |
| 127 | global_deviceatlas.jsonpath); |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 128 | err_code |= ERR_ALERT | ERR_FATAL; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 129 | goto out; |
| 130 | } |
| 131 | |
| 132 | da_init(); |
| 133 | da_seterrorfunc(da_haproxy_log); |
| 134 | status = da_atlas_compile(jsonp, da_haproxy_read, da_haproxy_seek, |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 135 | &global_deviceatlas.atlasimgptr, &atlasimglen); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 136 | fclose(jsonp); |
| 137 | if (status != DA_OK) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 138 | ha_alert("deviceatlas : '%s' json file is invalid.\n", |
| 139 | global_deviceatlas.jsonpath); |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 140 | err_code |= ERR_ALERT | ERR_FATAL; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 141 | goto out; |
| 142 | } |
| 143 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 144 | status = da_atlas_open(&global_deviceatlas.atlas, extraprops, |
| 145 | global_deviceatlas.atlasimgptr, atlasimglen); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 146 | |
| 147 | if (status != DA_OK) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 148 | ha_alert("deviceatlas : data could not be compiled.\n"); |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 149 | err_code |= ERR_ALERT | ERR_FATAL; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 150 | goto out; |
| 151 | } |
| 152 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 153 | if (global_deviceatlas.cookiename == 0) { |
| 154 | global_deviceatlas.cookiename = strdup(DA_COOKIENAME_DEFAULT); |
| 155 | global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 158 | global_deviceatlas.useragentid = da_atlas_header_evidence_id(&global_deviceatlas.atlas, |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 159 | "user-agent"); |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 160 | global_deviceatlas.daset = 1; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 161 | |
| 162 | fprintf(stdout, "Deviceatlas module loaded.\n"); |
| 163 | } |
| 164 | |
| 165 | out: |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 166 | return err_code; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Willy Tarreau | b149eed | 2016-12-21 21:03:49 +0100 | [diff] [blame] | 169 | static void deinit_deviceatlas(void) |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 170 | { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 171 | if (global_deviceatlas.jsonpath != 0) { |
| 172 | free(global_deviceatlas.jsonpath); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 175 | if (global_deviceatlas.daset == 1) { |
| 176 | free(global_deviceatlas.cookiename); |
| 177 | da_atlas_close(&global_deviceatlas.atlas); |
| 178 | free(global_deviceatlas.atlasimgptr); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | da_fini(); |
| 182 | } |
| 183 | |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 184 | static int da_haproxy(const struct arg *args, struct sample *smp, da_deviceinfo_t *devinfo) |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 185 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 186 | struct buffer *tmp; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 187 | da_propid_t prop, *pprop; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 188 | da_status_t status; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 189 | da_type_t proptype; |
| 190 | const char *propname; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 191 | int i; |
| 192 | |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 193 | tmp = get_trash_chunk(); |
| 194 | chunk_reset(tmp); |
| 195 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 196 | propname = (const char *) args[0].data.str.area; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 197 | i = 0; |
| 198 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 199 | for (; propname != 0; i ++, |
| 200 | propname = (const char *) args[i].data.str.area) { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 201 | status = da_atlas_getpropid(&global_deviceatlas.atlas, |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 202 | propname, &prop); |
| 203 | if (status != DA_OK) { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 204 | chunk_appendf(tmp, "%c", global_deviceatlas.separator); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 205 | continue; |
| 206 | } |
| 207 | pprop = ∝ |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 208 | da_atlas_getproptype(&global_deviceatlas.atlas, *pprop, &proptype); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 209 | |
| 210 | switch (proptype) { |
| 211 | case DA_TYPE_BOOLEAN: { |
| 212 | bool val; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 213 | status = da_getpropboolean(devinfo, *pprop, &val); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 214 | if (status == DA_OK) { |
| 215 | chunk_appendf(tmp, "%d", val); |
| 216 | } |
| 217 | break; |
| 218 | } |
| 219 | case DA_TYPE_INTEGER: |
| 220 | case DA_TYPE_NUMBER: { |
| 221 | long val; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 222 | status = da_getpropinteger(devinfo, *pprop, &val); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 223 | if (status == DA_OK) { |
| 224 | chunk_appendf(tmp, "%ld", val); |
| 225 | } |
| 226 | break; |
| 227 | } |
| 228 | case DA_TYPE_STRING: { |
| 229 | const char *val; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 230 | status = da_getpropstring(devinfo, *pprop, &val); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 231 | if (status == DA_OK) { |
| 232 | chunk_appendf(tmp, "%s", val); |
| 233 | } |
| 234 | break; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 235 | } |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 236 | default: |
| 237 | break; |
| 238 | } |
| 239 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 240 | chunk_appendf(tmp, "%c", global_deviceatlas.separator); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 241 | } |
| 242 | |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 243 | da_close(devinfo); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 244 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 245 | if (tmp->data) { |
| 246 | --tmp->data; |
| 247 | tmp->area[tmp->data] = 0; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 250 | smp->data.u.str.area = tmp->area; |
| 251 | smp->data.u.str.data = tmp->data; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 252 | |
| 253 | return 1; |
| 254 | } |
| 255 | |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 256 | static int da_haproxy_conv(const struct arg *args, struct sample *smp, void *private) |
| 257 | { |
| 258 | da_deviceinfo_t devinfo; |
| 259 | da_status_t status; |
| 260 | const char *useragent; |
| 261 | char useragentbuf[1024] = { 0 }; |
| 262 | int i; |
| 263 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 264 | if (global_deviceatlas.daset == 0 || smp->data.u.str.data == 0) { |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 265 | return 1; |
| 266 | } |
| 267 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 268 | i = smp->data.u.str.data > sizeof(useragentbuf) ? sizeof(useragentbuf) : smp->data.u.str.data; |
| 269 | memcpy(useragentbuf, smp->data.u.str.area, i - 1); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 270 | useragentbuf[i - 1] = 0; |
| 271 | |
| 272 | useragent = (const char *)useragentbuf; |
| 273 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 274 | status = da_search(&global_deviceatlas.atlas, &devinfo, |
| 275 | global_deviceatlas.useragentid, useragent, 0); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 276 | |
| 277 | return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo); |
| 278 | } |
| 279 | |
| 280 | #define DA_MAX_HEADERS 24 |
| 281 | |
| 282 | static int da_haproxy_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 283 | { |
| 284 | struct hdr_idx *hidx; |
| 285 | struct hdr_ctx hctx; |
| 286 | const struct http_msg *hmsg; |
| 287 | da_evidence_t ev[DA_MAX_HEADERS]; |
| 288 | da_deviceinfo_t devinfo; |
| 289 | da_status_t status; |
| 290 | char vbuf[DA_MAX_HEADERS][1024] = {{ 0 }}; |
| 291 | int i, nbh = 0; |
| 292 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 293 | if (global_deviceatlas.daset == 0) { |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 294 | return 1; |
| 295 | } |
| 296 | |
| 297 | CHECK_HTTP_MESSAGE_FIRST(); |
| 298 | smp->data.type = SMP_T_STR; |
| 299 | |
| 300 | /** |
| 301 | * Here we go through the whole list of headers from start |
| 302 | * they will be filtered via the DeviceAtlas API itself |
| 303 | */ |
| 304 | hctx.idx = 0; |
| 305 | hidx = &smp->strm->txn->hdr_idx; |
| 306 | hmsg = &smp->strm->txn->req; |
| 307 | |
| 308 | while (http_find_next_header(hmsg->chn->buf->p, hidx, &hctx) == 1 && |
| 309 | nbh < DA_MAX_HEADERS) { |
| 310 | char *pval; |
| 311 | size_t vlen; |
| 312 | da_evidence_id_t evid = -1; |
| 313 | char hbuf[24] = { 0 }; |
| 314 | |
| 315 | /* The HTTP headers used by the DeviceAtlas API are not longer */ |
David Carlier | 91a88b0 | 2017-11-17 08:47:25 +0000 | [diff] [blame] | 316 | if (hctx.del >= sizeof(hbuf) || hctx.del <= 0 || hctx.vlen <= 0) { |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 317 | continue; |
| 318 | } |
| 319 | |
| 320 | vlen = hctx.vlen; |
| 321 | memcpy(hbuf, hctx.line, hctx.del); |
| 322 | hbuf[hctx.del] = 0; |
| 323 | pval = (hctx.line + hctx.val); |
| 324 | |
| 325 | if (strcmp(hbuf, "Accept-Language") == 0) { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 326 | evid = da_atlas_accept_language_evidence_id(&global_deviceatlas. |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 327 | atlas); |
| 328 | } else if (strcmp(hbuf, "Cookie") == 0) { |
| 329 | char *p, *eval; |
| 330 | int pl; |
| 331 | |
| 332 | eval = pval + hctx.vlen; |
| 333 | /** |
| 334 | * The cookie value, if it exists, is located between the current header's |
| 335 | * value position and the next one |
| 336 | */ |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 337 | if (extract_cookie_value(pval, eval, global_deviceatlas.cookiename, |
| 338 | global_deviceatlas.cookienamelen, 1, &p, &pl) == NULL) { |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 339 | continue; |
| 340 | } |
| 341 | |
| 342 | vlen = (size_t)pl; |
| 343 | pval = p; |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 344 | evid = da_atlas_clientprop_evidence_id(&global_deviceatlas.atlas); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 345 | } else { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 346 | evid = da_atlas_header_evidence_id(&global_deviceatlas.atlas, |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 347 | hbuf); |
| 348 | } |
| 349 | |
| 350 | if (evid == -1) { |
| 351 | continue; |
| 352 | } |
| 353 | |
| 354 | i = vlen > sizeof(vbuf[nbh]) ? sizeof(vbuf[nbh]) : vlen; |
| 355 | memcpy(vbuf[nbh], pval, i - 1); |
| 356 | vbuf[nbh][i - 1] = 0; |
| 357 | ev[nbh].key = evid; |
| 358 | ev[nbh].value = vbuf[nbh]; |
| 359 | ev[nbh].value[vlen] = 0; |
| 360 | ++ nbh; |
| 361 | } |
| 362 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 363 | status = da_searchv(&global_deviceatlas.atlas, &devinfo, |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 364 | ev, nbh); |
| 365 | |
| 366 | return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo); |
| 367 | } |
| 368 | |
Willy Tarreau | 0d74f77 | 2015-06-01 15:42:29 +0200 | [diff] [blame] | 369 | static struct cfg_kw_list dacfg_kws = {{ }, { |
| 370 | { CFG_GLOBAL, "deviceatlas-json-file", da_json_file }, |
| 371 | { CFG_GLOBAL, "deviceatlas-log-level", da_log_level }, |
| 372 | { CFG_GLOBAL, "deviceatlas-property-separator", da_property_separator }, |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 373 | { CFG_GLOBAL, "deviceatlas-properties-cookie", da_properties_cookie }, |
Willy Tarreau | 0d74f77 | 2015-06-01 15:42:29 +0200 | [diff] [blame] | 374 | { 0, NULL, NULL }, |
| 375 | }}; |
| 376 | |
Willy Tarreau | f63386a | 2015-06-01 15:39:50 +0200 | [diff] [blame] | 377 | /* Note: must not be declared <const> as its list will be overwritten */ |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 378 | static struct sample_fetch_kw_list fetch_kws = {ILH, { |
David Carlier | 840b024 | 2016-03-16 10:09:55 +0000 | [diff] [blame] | 379 | { "da-csv-fetch", da_haproxy_fetch, ARG12(1,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 380 | { NULL, NULL, 0, 0, 0 }, |
| 381 | }}; |
| 382 | |
| 383 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | f63386a | 2015-06-01 15:39:50 +0200 | [diff] [blame] | 384 | static struct sample_conv_kw_list conv_kws = {ILH, { |
David Carlier | 840b024 | 2016-03-16 10:09:55 +0000 | [diff] [blame] | 385 | { "da-csv-conv", da_haproxy_conv, ARG12(1,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR), NULL, SMP_T_STR, SMP_T_STR }, |
Willy Tarreau | f63386a | 2015-06-01 15:39:50 +0200 | [diff] [blame] | 386 | { NULL, NULL, 0, 0, 0 }, |
| 387 | }}; |
| 388 | |
| 389 | __attribute__((constructor)) |
| 390 | static void __da_init(void) |
| 391 | { |
| 392 | /* register sample fetch and format conversion keywords */ |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 393 | sample_register_fetches(&fetch_kws); |
Willy Tarreau | f63386a | 2015-06-01 15:39:50 +0200 | [diff] [blame] | 394 | sample_register_convs(&conv_kws); |
Willy Tarreau | 0d74f77 | 2015-06-01 15:42:29 +0200 | [diff] [blame] | 395 | cfg_register_keywords(&dacfg_kws); |
Willy Tarreau | 3dd483e | 2016-12-21 18:50:22 +0100 | [diff] [blame] | 396 | hap_register_build_opts("Built with DeviceAtlas support.", 0); |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 397 | hap_register_post_check(init_deviceatlas); |
Willy Tarreau | b149eed | 2016-12-21 21:03:49 +0100 | [diff] [blame] | 398 | hap_register_post_deinit(deinit_deviceatlas); |
Willy Tarreau | f63386a | 2015-06-01 15:39:50 +0200 | [diff] [blame] | 399 | } |