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