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