blob: 5bd9a0c5a098d3bb1a754dd7121b26ebe58136a6 [file] [log] [blame]
scientiamobiled0027ed2016-11-04 10:55:08 +01001#include <stdio.h>
2#include <stdarg.h>
3
4#include <common/cfgparse.h>
5#include <common/chunk.h>
6#include <common/buffer.h>
Willy Tarreaudc2ed472016-12-21 20:20:17 +01007#include <common/errors.h>
scientiamobiled0027ed2016-11-04 10:55:08 +01008#include <proto/arg.h>
9#include <proto/log.h>
10#include <proto/proto_http.h>
11#include <proto/sample.h>
12#include <ebsttree.h>
13#include <ebmbtree.h>
scientiamobiled0027ed2016-11-04 10:55:08 +010014
Willy Tarreaue5d31692016-11-08 18:47:25 +010015#include <wurfl/wurfl.h>
16
Willy Tarreau350c1c62016-12-21 14:57:34 +010017static struct {
18 char *data_file; /* the WURFL data file */
19 char *cache_size; /* the WURFL cache parameters */
20 int engine_mode; /* the WURFL engine mode */
21 int useragent_priority; /* the WURFL ua priority */
22 struct list patch_file_list; /* the list of WURFL patch file to use */
23 char information_list_separator; /* the separator used in request to separate values */
24 struct list information_list; /* the list of WURFL data to return into request */
25 void *handle; /* the handle to WURFL engine */
26 struct eb_root btree; /* btree containing info (name/type) on WURFL data to return */
27} global_wurfl = {
28 .data_file = NULL,
29 .cache_size = NULL,
30 .engine_mode = -1,
31 .useragent_priority = -1,
32 .information_list_separator = ',',
33 .information_list = LIST_HEAD_INIT(global_wurfl.information_list),
34 .patch_file_list = LIST_HEAD_INIT(global_wurfl.patch_file_list),
35 .handle = NULL,
36};
scientiamobiled0027ed2016-11-04 10:55:08 +010037
38#ifdef WURFL_DEBUG
39inline static void ha_wurfl_log(char * message, ...)
40{
41 char logbuf[256];
42 va_list argp;
43
44 va_start(argp, message);
45 vsnprintf(logbuf, sizeof(logbuf), message, argp);
46 va_end(argp);
47 send_log(NULL, LOG_NOTICE, logbuf, NULL);
48}
49#else
50inline static void ha_wurfl_log(char * message, ...)
51{
52}
53#endif
54
55#define HA_WURFL_MAX_HEADER_LENGTH 1024
56
Willy Tarreaue5d31692016-11-08 18:47:25 +010057typedef char *(*PROP_CALLBACK_FUNC)(wurfl_handle wHandle, wurfl_device_handle dHandle);
58
59enum wurfl_data_type {
60 HA_WURFL_DATA_TYPE_UNKNOWN = 0,
61 HA_WURFL_DATA_TYPE_CAP = 100,
62 HA_WURFL_DATA_TYPE_VCAP = 200,
63 HA_WURFL_DATA_TYPE_PROPERTY = 300
64};
65
66typedef struct {
67 char *name;
68 enum wurfl_data_type type;
69 PROP_CALLBACK_FUNC func_callback;
70 struct ebmb_node nd;
71} wurfl_data_t;
72
scientiamobiled0027ed2016-11-04 10:55:08 +010073static const char HA_WURFL_MODULE_VERSION[] = "1.0";
74static const char HA_WURFL_ISDEVROOT_FALSE[] = "FALSE";
75static const char HA_WURFL_ISDEVROOT_TRUE[] = "TRUE";
76static const char HA_WURFL_TARGET_ACCURACY[] = "accuracy";
77static const char HA_WURFL_TARGET_PERFORMANCE[] = "performance";
78static const char HA_WURFL_PRIORITY_PLAIN[] = "plain";
79static const char HA_WURFL_PRIORITY_SIDELOADED_BROWSER[] = "sideloaded_browser";
80static const char HA_WURFL_MIN_ENGINE_VERSION_MANDATORY[] = "1.8.0.0";
81
82static const char HA_WURFL_DATA_TYPE_UNKNOWN_STRING[] = "unknown";
83static const char HA_WURFL_DATA_TYPE_CAP_STRING[] = "capability";
84static const char HA_WURFL_DATA_TYPE_VCAP_STRING[] = "virtual_capability";
85static const char HA_WURFL_DATA_TYPE_PROPERTY_STRING[] = "property";
86
87static const char *ha_wurfl_retrieve_header(const char *header_name, const void *wh);
88static const char *ha_wurfl_get_wurfl_root_id (wurfl_handle wHandle, wurfl_device_handle dHandle);
89static const char *ha_wurfl_get_wurfl_id (wurfl_handle wHandle, wurfl_device_handle dHandle);
90static const char *ha_wurfl_get_wurfl_isdevroot (wurfl_handle wHandle, wurfl_device_handle dHandle);
91static const char *ha_wurfl_get_wurfl_useragent (wurfl_handle wHandle, wurfl_device_handle dHandle);
92static const char *ha_wurfl_get_wurfl_api_version (wurfl_handle wHandle, wurfl_device_handle dHandle);
93static const char *ha_wurfl_get_wurfl_engine_target (wurfl_handle wHandle, wurfl_device_handle dHandle);
94static const char *ha_wurfl_get_wurfl_info (wurfl_handle wHandle, wurfl_device_handle dHandle);
95static const char *ha_wurfl_get_wurfl_last_load_time (wurfl_handle wHandle, wurfl_device_handle dHandle);
96static const char *ha_wurfl_get_wurfl_normalized_useragent (wurfl_handle wHandle, wurfl_device_handle dHandle);
97static const char *ha_wurfl_get_wurfl_useragent_priority (wurfl_handle wHandle, wurfl_device_handle dHandle);
98static const char *(*ha_wurfl_get_property_callback(char *name)) (wurfl_handle wHandle, wurfl_device_handle dHandle);
99
100// ordered property=>function map, suitable for binary search
101static const struct {
102 const char *name;
103 const char *(*func)(wurfl_handle wHandle, wurfl_device_handle dHandle);
104} wurfl_properties_function_map [] = {
105 {"wurfl_api_version", ha_wurfl_get_wurfl_api_version},
106 {"wurfl_engine_target", ha_wurfl_get_wurfl_engine_target},
107 {"wurfl_id", ha_wurfl_get_wurfl_id },
108 {"wurfl_info", ha_wurfl_get_wurfl_info },
109 {"wurfl_isdevroot", ha_wurfl_get_wurfl_isdevroot},
110 {"wurfl_last_load_time", ha_wurfl_get_wurfl_last_load_time},
111 {"wurfl_normalized_useragent", ha_wurfl_get_wurfl_normalized_useragent},
112 {"wurfl_useragent", ha_wurfl_get_wurfl_useragent},
113 {"wurfl_useragent_priority", ha_wurfl_get_wurfl_useragent_priority },
114 {"wurfl_root_id", ha_wurfl_get_wurfl_root_id},
115};
116static const int HA_WURFL_PROPERTIES_NBR = 10;
117
118typedef struct {
119 struct list list;
120 wurfl_data_t data;
121} wurfl_information_t;
122
123typedef struct {
124 struct list list;
125 char *patch_file_path;
126} wurfl_patches_t;
127
128typedef struct {
129 struct sample *wsmp;
130 char header_value[HA_WURFL_MAX_HEADER_LENGTH + 1];
131} ha_wurfl_header_t;
132
133/*
134 * configuration parameters parsing functions
135 */
136static int ha_wurfl_cfg_data_file(char **args, int section_type, struct proxy *curpx,
137 struct proxy *defpx, const char *file, int line,
138 char **err)
139{
140
141 if (*(args[1]) == 0) {
142 memprintf(err, "WURFL: %s expects a value.\n", args[0]);
143 return -1;
144 }
145
Willy Tarreau350c1c62016-12-21 14:57:34 +0100146 global_wurfl.data_file = strdup(args[1]);
scientiamobiled0027ed2016-11-04 10:55:08 +0100147 return 0;
148}
149
150static int ha_wurfl_cfg_cache(char **args, int section_type, struct proxy *curpx,
151 struct proxy *defpx, const char *file, int line,
152 char **err)
153{
154 if (*(args[1]) == 0) {
155 memprintf(err, "WURFL: %s expects a value.\n", args[0]);
156 return -1;
157 }
158
Willy Tarreau350c1c62016-12-21 14:57:34 +0100159 global_wurfl.cache_size = strdup(args[1]);
scientiamobiled0027ed2016-11-04 10:55:08 +0100160 return 0;
161}
162
163static int ha_wurfl_cfg_engine_mode(char **args, int section_type, struct proxy *curpx,
164 struct proxy *defpx, const char *file, int line,
165 char **err)
166{
167 if (*(args[1]) == 0) {
168 memprintf(err, "WURFL: %s expects a value.\n", args[0]);
169 return -1;
170 }
171
172 if (!strcmp(args[1],HA_WURFL_TARGET_ACCURACY)) {
Willy Tarreau350c1c62016-12-21 14:57:34 +0100173 global_wurfl.engine_mode = WURFL_ENGINE_TARGET_HIGH_ACCURACY;
scientiamobiled0027ed2016-11-04 10:55:08 +0100174 return 0;
175 }
176
177 if (!strcmp(args[1],HA_WURFL_TARGET_PERFORMANCE)) {
Willy Tarreau350c1c62016-12-21 14:57:34 +0100178 global_wurfl.engine_mode = WURFL_ENGINE_TARGET_HIGH_PERFORMANCE;
scientiamobiled0027ed2016-11-04 10:55:08 +0100179 return 0;
180 }
181
182 memprintf(err, "WURFL: %s valid values are %s or %s.\n", args[0], HA_WURFL_TARGET_PERFORMANCE, HA_WURFL_TARGET_ACCURACY);
183 return -1;
184}
185
186static int ha_wurfl_cfg_information_list_separator(char **args, int section_type, struct proxy *curpx,
187 struct proxy *defpx, const char *file, int line,
188 char **err)
189{
190 if (*(args[1]) == 0) {
191 memprintf(err, "WURFL: %s expects a single character.\n", args[0]);
192 return -1;
193 }
194
195 if (strlen(args[1]) > 1) {
196 memprintf(err, "WURFL: %s expects a single character, got %s.\n", args[0], args[1]);
197 return -1;
198 }
199
Willy Tarreau350c1c62016-12-21 14:57:34 +0100200 global_wurfl.information_list_separator = *args[1];
scientiamobiled0027ed2016-11-04 10:55:08 +0100201 return 0;
202}
203
204static int ha_wurfl_cfg_information_list(char **args, int section_type, struct proxy *curpx,
205 struct proxy *defpx, const char *file, int line,
206 char **err)
207{
208 int argIdx = 1;
209 wurfl_information_t *wi;
210
211 if (*(args[argIdx]) == 0) {
212 memprintf(err, "WURFL: %s expects a value.\n", args[0]);
213 return -1;
214 }
215
216 while (*(args[argIdx])) {
217 wi = calloc(1, sizeof(*wi));
218
219 if (wi == NULL) {
220 memprintf(err, "WURFL: Error allocating memory for %s element.\n", args[0]);
221 return -1;
222 }
223
224 wi->data.name = strdup(args[argIdx]);
225 wi->data.type = HA_WURFL_DATA_TYPE_UNKNOWN;
226 wi->data.func_callback = NULL;
Willy Tarreau350c1c62016-12-21 14:57:34 +0100227 LIST_ADDQ(&global_wurfl.information_list, &wi->list);
scientiamobiled0027ed2016-11-04 10:55:08 +0100228 ++argIdx;
229 }
230
231 return 0;
232}
233
234static int ha_wurfl_cfg_patch_file_list(char **args, int section_type, struct proxy *curpx,
235 struct proxy *defpx, const char *file, int line,
236 char **err)
237{
238 int argIdx = 1;
239 wurfl_patches_t *wp;
240
241 if (*(args[argIdx]) == 0) {
242 memprintf(err, "WURFL: %s expects a value.\n", args[0]);
243 return -1;
244 }
245
246 while (*(args[argIdx])) {
247 wp = calloc(1, sizeof(*wp));
248
249 if (wp == NULL) {
250 memprintf(err, "WURFL: Error allocating memory for %s element.\n", args[0]);
251 return -1;
252 }
253
254 wp->patch_file_path = strdup(args[argIdx]);
Willy Tarreau350c1c62016-12-21 14:57:34 +0100255 LIST_ADDQ(&global_wurfl.patch_file_list, &wp->list);
scientiamobiled0027ed2016-11-04 10:55:08 +0100256 ++argIdx;
257 }
258
259 return 0;
260}
261
262static int ha_wurfl_cfg_useragent_priority(char **args, int section_type, struct proxy *curpx,
263 struct proxy *defpx, const char *file, int line,
264 char **err)
265{
266 if (*(args[1]) == 0) {
267 memprintf(err, "WURFL: %s expects a value.\n", args[0]);
268 return -1;
269 }
270
271 if (!strcmp(args[1],HA_WURFL_PRIORITY_PLAIN)) {
Willy Tarreau350c1c62016-12-21 14:57:34 +0100272 global_wurfl.useragent_priority = WURFL_USERAGENT_PRIORITY_USE_PLAIN_USERAGENT;
scientiamobiled0027ed2016-11-04 10:55:08 +0100273 return 0;
274 }
275
276 if (!strcmp(args[1],HA_WURFL_PRIORITY_SIDELOADED_BROWSER)) {
Willy Tarreau350c1c62016-12-21 14:57:34 +0100277 global_wurfl.useragent_priority = WURFL_USERAGENT_PRIORITY_OVERRIDE_SIDELOADED_BROWSER_USERAGENT;
scientiamobiled0027ed2016-11-04 10:55:08 +0100278 return 0;
279 }
280
281 memprintf(err, "WURFL: %s valid values are %s or %s.\n", args[0], HA_WURFL_PRIORITY_PLAIN, HA_WURFL_PRIORITY_SIDELOADED_BROWSER);
282 return -1;
283}
284
285/*
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100286 * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*.
scientiamobiled0027ed2016-11-04 10:55:08 +0100287 */
288
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100289static int ha_wurfl_init(void)
scientiamobiled0027ed2016-11-04 10:55:08 +0100290{
291 wurfl_information_t *wi;
292 wurfl_patches_t *wp;
293 wurfl_data_t * wn;
294 int wurfl_result_code = WURFL_OK;
295 int len;
296
297 send_log(NULL, LOG_NOTICE, "WURFL: Loading module v.%s\n", HA_WURFL_MODULE_VERSION);
298 // creating WURFL handler
Willy Tarreau350c1c62016-12-21 14:57:34 +0100299 global_wurfl.handle = wurfl_create();
scientiamobiled0027ed2016-11-04 10:55:08 +0100300
Willy Tarreau350c1c62016-12-21 14:57:34 +0100301 if (global_wurfl.handle == NULL) {
scientiamobiled0027ed2016-11-04 10:55:08 +0100302 Warning("WURFL: Engine handler creation failed");
303 send_log(NULL, LOG_WARNING, "WURFL: Engine handler creation failed\n");
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100304 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100305 }
306
307 send_log(NULL, LOG_NOTICE, "WURFL: Engine handler created - API version %s\n", wurfl_get_api_version() );
308
309 // set wurfl data file
Willy Tarreau350c1c62016-12-21 14:57:34 +0100310 if (global_wurfl.data_file == NULL) {
scientiamobiled0027ed2016-11-04 10:55:08 +0100311 Warning("WURFL: missing wurfl-data-file parameter in global configuration\n");
312 send_log(NULL, LOG_WARNING, "WURFL: missing wurfl-data-file parameter in global configuration\n");
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100313 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100314 }
315
Willy Tarreau350c1c62016-12-21 14:57:34 +0100316 if (wurfl_set_root(global_wurfl.handle, global_wurfl.data_file) != WURFL_OK) {
317 Warning("WURFL: Engine setting root file failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
318 send_log(NULL, LOG_WARNING, "WURFL: Engine setting root file failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100319 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100320 }
321
Willy Tarreau350c1c62016-12-21 14:57:34 +0100322 send_log(NULL, LOG_NOTICE, "WURFL: Engine root file set to %s\n", global_wurfl.data_file);
scientiamobiled0027ed2016-11-04 10:55:08 +0100323 // just a log to inform which separator char has to be used
Willy Tarreau350c1c62016-12-21 14:57:34 +0100324 send_log(NULL, LOG_NOTICE, "WURFL: Information list separator set to '%c'\n", global_wurfl.information_list_separator);
scientiamobiled0027ed2016-11-04 10:55:08 +0100325
326 // load wurfl data needed ( and filter whose are supposed to be capabilities )
Willy Tarreau350c1c62016-12-21 14:57:34 +0100327 if (LIST_ISEMPTY(&global_wurfl.information_list)) {
scientiamobiled0027ed2016-11-04 10:55:08 +0100328 Warning("WURFL: missing wurfl-information-list parameter in global configuration\n");
329 send_log(NULL, LOG_WARNING, "WURFL: missing wurfl-information-list parameter in global configuration\n");
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100330 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100331 } else {
332 // ebtree initialization
Willy Tarreau350c1c62016-12-21 14:57:34 +0100333 global_wurfl.btree = EB_ROOT;
scientiamobiled0027ed2016-11-04 10:55:08 +0100334
335 // checking if informations are valid WURFL data ( cap, vcaps, properties )
Willy Tarreau350c1c62016-12-21 14:57:34 +0100336 list_for_each_entry(wi, &global_wurfl.information_list, list) {
scientiamobiled0027ed2016-11-04 10:55:08 +0100337 // check if information is already loaded looking into btree
Willy Tarreau350c1c62016-12-21 14:57:34 +0100338 if (ebst_lookup(&global_wurfl.btree, wi->data.name) == NULL) {
scientiamobiled0027ed2016-11-04 10:55:08 +0100339 if ((wi->data.func_callback = (PROP_CALLBACK_FUNC) ha_wurfl_get_property_callback(wi->data.name)) != NULL) {
340 wi->data.type = HA_WURFL_DATA_TYPE_PROPERTY;
341 ha_wurfl_log("WURFL: [%s] is a valid wurfl data [property]\n",wi->data.name);
Willy Tarreau350c1c62016-12-21 14:57:34 +0100342 } else if (wurfl_has_virtual_capability(global_wurfl.handle, wi->data.name)) {
scientiamobiled0027ed2016-11-04 10:55:08 +0100343 wi->data.type = HA_WURFL_DATA_TYPE_VCAP;
344 ha_wurfl_log("WURFL: [%s] is a valid wurfl data [virtual capability]\n",wi->data.name);
345 } else {
346 // by default a cap type is assumed to be and we control it on engine load
347 wi->data.type = HA_WURFL_DATA_TYPE_CAP;
348
Willy Tarreau350c1c62016-12-21 14:57:34 +0100349 if (wurfl_add_requested_capability(global_wurfl.handle, wi->data.name) != WURFL_OK) {
350 Warning("WURFL: capability filtering failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
351 send_log(NULL, LOG_WARNING, "WURFL: capability filtering failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100352 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100353 }
354
355 ha_wurfl_log("WURFL: [%s] treated as wurfl capability. Will check its validity later, on engine load\n",wi->data.name);
356 }
357
358 // ebtree insert here
359 len = strlen(wi->data.name);
360
361 wn = malloc(sizeof(wurfl_data_t) + len + 1);
362
363 if (wn == NULL) {
364 Warning("WURFL: Error allocating memory for information tree element.\n");
365 send_log(NULL, LOG_WARNING, "WURFL: Error allocating memory for information tree element.\n");
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100366 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100367 }
368
369 wn->name = wi->data.name;
370 wn->type = wi->data.type;
371 wn->func_callback = wi->data.func_callback;
372 memcpy(wn->nd.key, wi->data.name, len);
373 wn->nd.key[len] = 0;
374
Willy Tarreau350c1c62016-12-21 14:57:34 +0100375 if (!ebst_insert(&global_wurfl.btree, &wn->nd)) {
scientiamobiled0027ed2016-11-04 10:55:08 +0100376 Warning("WURFL: [%s] not inserted in btree\n",wn->name);
377 send_log(NULL, LOG_WARNING, "WURFL: [%s] not inserted in btree\n",wn->name);
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100378 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100379 }
380
381 } else {
382 ha_wurfl_log("WURFL: [%s] already loaded\n",wi->data.name);
383 }
384
385 }
386
387 }
388
389 // filtering mandatory capabilities if engine version < 1.8.0.0
390 if (strcmp(wurfl_get_api_version(), HA_WURFL_MIN_ENGINE_VERSION_MANDATORY) < 0) {
391 wurfl_capability_enumerator_handle hmandatorycapabilityenumerator;
392 ha_wurfl_log("WURFL: Engine version %s < %s - Filtering mandatory capabilities\n", wurfl_get_api_version(), HA_WURFL_MIN_ENGINE_VERSION_MANDATORY);
Willy Tarreau350c1c62016-12-21 14:57:34 +0100393 hmandatorycapabilityenumerator = wurfl_get_mandatory_capability_enumerator(global_wurfl.handle);
scientiamobiled0027ed2016-11-04 10:55:08 +0100394
395 while (wurfl_capability_enumerator_is_valid(hmandatorycapabilityenumerator)) {
396 char *name = (char *)wurfl_capability_enumerator_get_name(hmandatorycapabilityenumerator);
397
Willy Tarreau350c1c62016-12-21 14:57:34 +0100398 if (ebst_lookup(&global_wurfl.btree, name) == NULL) {
399 if (wurfl_add_requested_capability(global_wurfl.handle, name) != WURFL_OK) {
400 Warning("WURFL: Engine adding mandatory capability [%s] failed - %s\n", name, wurfl_get_error_message(global_wurfl.handle));
401 send_log(NULL, LOG_WARNING, "WURFL: Adding mandatory capability [%s] failed - %s\n", name, wurfl_get_error_message(global_wurfl.handle));
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100402 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100403 }
404
405 ha_wurfl_log("WURFL: Mandatory capability [%s] added\n", name);
406 } else {
407 ha_wurfl_log("WURFL: Mandatory capability [%s] already filtered\n", name);
408 }
409
410 wurfl_capability_enumerator_move_next(hmandatorycapabilityenumerator);
411 }
412
413 wurfl_capability_enumerator_destroy(hmandatorycapabilityenumerator);
414 }
415
416 // adding WURFL patches if needed
Willy Tarreau350c1c62016-12-21 14:57:34 +0100417 if (!LIST_ISEMPTY(&global_wurfl.patch_file_list)) {
scientiamobiled0027ed2016-11-04 10:55:08 +0100418
Willy Tarreau350c1c62016-12-21 14:57:34 +0100419 list_for_each_entry(wp, &global_wurfl.patch_file_list, list) {
420 if (wurfl_add_patch(global_wurfl.handle, wp->patch_file_path) != WURFL_OK) {
421 Warning("WURFL: Engine adding patch file failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
422 send_log(NULL, LOG_WARNING, "WURFL: Adding engine patch file failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100423 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100424 }
425 send_log(NULL, LOG_NOTICE, "WURFL: Engine patch file added %s\n", wp->patch_file_path);
426
427 }
428
429 }
430
431 // setting cache provider if specified in cfg, otherwise let engine choose
Willy Tarreau350c1c62016-12-21 14:57:34 +0100432 if (global_wurfl.cache_size != NULL) {
433 if (strpbrk(global_wurfl.cache_size, ",") != NULL) {
434 wurfl_result_code = wurfl_set_cache_provider(global_wurfl.handle, WURFL_CACHE_PROVIDER_DOUBLE_LRU, global_wurfl.cache_size) ;
scientiamobiled0027ed2016-11-04 10:55:08 +0100435 } else {
Willy Tarreau350c1c62016-12-21 14:57:34 +0100436 if (strcmp(global_wurfl.cache_size, "0")) {
437 wurfl_result_code = wurfl_set_cache_provider(global_wurfl.handle, WURFL_CACHE_PROVIDER_LRU, global_wurfl.cache_size) ;
scientiamobiled0027ed2016-11-04 10:55:08 +0100438 } else {
Willy Tarreau350c1c62016-12-21 14:57:34 +0100439 wurfl_result_code = wurfl_set_cache_provider(global_wurfl.handle, WURFL_CACHE_PROVIDER_NONE, 0);
scientiamobiled0027ed2016-11-04 10:55:08 +0100440 }
441
442 }
443
444 if (wurfl_result_code != WURFL_OK) {
Willy Tarreau350c1c62016-12-21 14:57:34 +0100445 Warning("WURFL: Setting cache to [%s] failed - %s\n", global_wurfl.cache_size, wurfl_get_error_message(global_wurfl.handle));
446 send_log(NULL, LOG_WARNING, "WURFL: Setting cache to [%s] failed - %s\n", global_wurfl.cache_size, wurfl_get_error_message(global_wurfl.handle));
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100447 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100448 }
449
Willy Tarreau350c1c62016-12-21 14:57:34 +0100450 send_log(NULL, LOG_NOTICE, "WURFL: Cache set to [%s]\n", global_wurfl.cache_size);
scientiamobiled0027ed2016-11-04 10:55:08 +0100451 }
452
453 // setting engine mode if specified in cfg, otherwise let engine choose
Willy Tarreau350c1c62016-12-21 14:57:34 +0100454 if (global_wurfl.engine_mode != -1) {
455 if (wurfl_set_engine_target(global_wurfl.handle, global_wurfl.engine_mode) != WURFL_OK ) {
456 Warning("WURFL: Setting engine target failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
457 send_log(NULL, LOG_WARNING, "WURFL: Setting engine target failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100458 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100459 }
460
461 }
462
Willy Tarreau350c1c62016-12-21 14:57:34 +0100463 send_log(NULL, LOG_NOTICE, "WURFL: Engine target set to [%s]\n", (global_wurfl.engine_mode == WURFL_ENGINE_TARGET_HIGH_PERFORMANCE) ? (HA_WURFL_TARGET_PERFORMANCE) : (HA_WURFL_TARGET_ACCURACY) );
scientiamobiled0027ed2016-11-04 10:55:08 +0100464
465 // setting ua priority if specified in cfg, otherwise let engine choose
Willy Tarreau350c1c62016-12-21 14:57:34 +0100466 if (global_wurfl.useragent_priority != -1) {
467 if (wurfl_set_useragent_priority(global_wurfl.handle, global_wurfl.useragent_priority) != WURFL_OK ) {
468 Warning("WURFL: Setting engine useragent priority failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
469 send_log(NULL, LOG_WARNING, "WURFL: Setting engine useragent priority failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100470 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100471 }
472
473 }
474
Willy Tarreau350c1c62016-12-21 14:57:34 +0100475 send_log(NULL, LOG_NOTICE, "WURFL: Engine useragent priority set to [%s]\n", (global_wurfl.useragent_priority == WURFL_USERAGENT_PRIORITY_USE_PLAIN_USERAGENT) ? (HA_WURFL_PRIORITY_PLAIN) : (HA_WURFL_PRIORITY_SIDELOADED_BROWSER) );
scientiamobiled0027ed2016-11-04 10:55:08 +0100476
477 // loading WURFL engine
Willy Tarreau350c1c62016-12-21 14:57:34 +0100478 if (wurfl_load(global_wurfl.handle) != WURFL_OK) {
479 Warning("WURFL: Engine load failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
480 send_log(NULL, LOG_WARNING, "WURFL: Engine load failed - %s\n", wurfl_get_error_message(global_wurfl.handle));
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100481 return ERR_WARN;
scientiamobiled0027ed2016-11-04 10:55:08 +0100482 }
483
484 send_log(NULL, LOG_NOTICE, "WURFL: Engine loaded\n");
485 send_log(NULL, LOG_NOTICE, "WURFL: Module load completed\n");
486 return 0;
487}
488
Willy Tarreau800f93f2016-12-21 20:52:38 +0100489static void ha_wurfl_deinit(void)
scientiamobiled0027ed2016-11-04 10:55:08 +0100490{
491 wurfl_information_t *wi, *wi2;
492 wurfl_patches_t *wp, *wp2;
493
494 send_log(NULL, LOG_NOTICE, "WURFL: Unloading module v.%s\n", HA_WURFL_MODULE_VERSION);
Willy Tarreau350c1c62016-12-21 14:57:34 +0100495 wurfl_destroy(global_wurfl.handle);
496 global_wurfl.handle = NULL;
497 free(global_wurfl.data_file);
498 global_wurfl.data_file = NULL;
499 free(global_wurfl.cache_size);
500 global_wurfl.cache_size = NULL;
scientiamobiled0027ed2016-11-04 10:55:08 +0100501
Willy Tarreau350c1c62016-12-21 14:57:34 +0100502 list_for_each_entry_safe(wi, wi2, &global_wurfl.information_list, list) {
scientiamobiled0027ed2016-11-04 10:55:08 +0100503 LIST_DEL(&wi->list);
504 free(wi);
505 }
506
Willy Tarreau350c1c62016-12-21 14:57:34 +0100507 list_for_each_entry_safe(wp, wp2, &global_wurfl.patch_file_list, list) {
scientiamobiled0027ed2016-11-04 10:55:08 +0100508 LIST_DEL(&wp->list);
509 free(wp);
510 }
511
512 send_log(NULL, LOG_NOTICE, "WURFL: Module unloaded\n");
513}
514
515static int ha_wurfl_get_all(const struct arg *args, struct sample *smp, const char *kw, void *private)
516{
517 wurfl_device_handle dHandle;
518 struct chunk *temp;
519 wurfl_information_t *wi;
520 ha_wurfl_header_t wh;
521
522 ha_wurfl_log("WURFL: starting ha_wurfl_get_all\n");
523 wh.wsmp = smp;
Willy Tarreau350c1c62016-12-21 14:57:34 +0100524 dHandle = wurfl_lookup(global_wurfl.handle, &ha_wurfl_retrieve_header, &wh);
scientiamobiled0027ed2016-11-04 10:55:08 +0100525
526 if (!dHandle) {
Willy Tarreau350c1c62016-12-21 14:57:34 +0100527 ha_wurfl_log("WURFL: unable to retrieve device from request %s\n", wurfl_get_error_message(global_wurfl.handle));
scientiamobiled0027ed2016-11-04 10:55:08 +0100528 return 1;
529 }
530
531 temp = get_trash_chunk();
532 chunk_reset(temp);
533
Willy Tarreau350c1c62016-12-21 14:57:34 +0100534 list_for_each_entry(wi, &global_wurfl.information_list, list) {
535 chunk_appendf(temp, "%c", global_wurfl.information_list_separator);
scientiamobiled0027ed2016-11-04 10:55:08 +0100536
537 switch(wi->data.type) {
538 case HA_WURFL_DATA_TYPE_UNKNOWN :
539 ha_wurfl_log("WURFL: %s is of an %s type\n", wi->data.name, HA_WURFL_DATA_TYPE_UNKNOWN_STRING);
540#ifdef WURFL_HEADER_WITH_DETAILS
541 // write WURFL property type and name before its value...
542 chunk_appendf(temp, "%s=%s", HA_WURFL_DATA_TYPE_UNKNOWN_STRING, wi->data.name);
543#endif
544 break;
545 case HA_WURFL_DATA_TYPE_CAP :
546 ha_wurfl_log("WURFL: %s is a %s\n", wi->data.name, HA_WURFL_DATA_TYPE_CAP_STRING);
547#ifdef WURFL_HEADER_WITH_DETAILS
548 // write WURFL property type and name before its value...
549 chunk_appendf(temp, "%s=%s=", HA_WURFL_DATA_TYPE_CAP_STRING, wi->data.name);
550#endif
551 chunk_appendf(temp, "%s", wurfl_device_get_capability(dHandle, wi->data.name));
552 break;
553 case HA_WURFL_DATA_TYPE_VCAP :
554 ha_wurfl_log("WURFL: %s is a %s\n", wi->data.name, HA_WURFL_DATA_TYPE_VCAP_STRING);
555#ifdef WURFL_HEADER_WITH_DETAILS
556 // write WURFL property type and name before its value...
557 chunk_appendf(temp, "%s=%s=", HA_WURFL_DATA_TYPE_VCAP_STRING, wi->data.name);
558#endif
559 chunk_appendf(temp, "%s", wurfl_device_get_virtual_capability(dHandle, wi->data.name));
560 break;
561 case HA_WURFL_DATA_TYPE_PROPERTY :
562 ha_wurfl_log("WURFL: %s is a %s\n", wi->data.name, HA_WURFL_DATA_TYPE_PROPERTY_STRING);
563#ifdef WURFL_HEADER_WITH_DETAILS
564 // write WURFL property type and name before its value...
565 chunk_appendf(temp, "%s=%s=", HA_WURFL_DATA_TYPE_PROPERTY_STRING, wi->data.name);
566#endif
Willy Tarreau350c1c62016-12-21 14:57:34 +0100567 chunk_appendf(temp, "%s", wi->data.func_callback(global_wurfl.handle, dHandle));
scientiamobiled0027ed2016-11-04 10:55:08 +0100568 break;
569 }
570
571 }
572
573 wurfl_device_destroy(dHandle);
574 smp->data.u.str.str = temp->str;
575 smp->data.u.str.len = temp->len;
576 return 1;
577}
578
579static int ha_wurfl_get(const struct arg *args, struct sample *smp, const char *kw, void *private)
580{
581 wurfl_device_handle dHandle;
582 struct chunk *temp;
583 wurfl_data_t *wn = NULL;
584 struct ebmb_node *node;
585 ha_wurfl_header_t wh;
586 int i = 0;
587
588 ha_wurfl_log("WURFL: starting ha_wurfl_get\n");
589 wh.wsmp = smp;
Willy Tarreau350c1c62016-12-21 14:57:34 +0100590 dHandle = wurfl_lookup(global_wurfl.handle, &ha_wurfl_retrieve_header, &wh);
scientiamobiled0027ed2016-11-04 10:55:08 +0100591
592 if (!dHandle) {
Willy Tarreau350c1c62016-12-21 14:57:34 +0100593 ha_wurfl_log("WURFL: unable to retrieve device from request %s\n", wurfl_get_error_message(global_wurfl.handle));
scientiamobiled0027ed2016-11-04 10:55:08 +0100594 return 1;
595 }
596
597 temp = get_trash_chunk();
598 chunk_reset(temp);
599
600 while (args[i].data.str.str) {
Willy Tarreau350c1c62016-12-21 14:57:34 +0100601 chunk_appendf(temp, "%c", global_wurfl.information_list_separator);
602 node = ebst_lookup(&global_wurfl.btree, args[i].data.str.str);
scientiamobiled0027ed2016-11-04 10:55:08 +0100603 wn = container_of(node, wurfl_data_t, nd);
604
605 if (wn) {
606
607 switch(wn->type) {
608 case HA_WURFL_DATA_TYPE_UNKNOWN :
609 ha_wurfl_log("WURFL: %s is of an %s type\n", wn->name, HA_WURFL_DATA_TYPE_UNKNOWN_STRING);
610#ifdef WURFL_HEADER_WITH_DETAILS
611 // write WURFL property type and name before its value...
612 chunk_appendf(temp, "%s=%s", HA_WURFL_DATA_TYPE_UNKNOWN_STRING, wn->name);
613#endif
614 break;
615 case HA_WURFL_DATA_TYPE_CAP :
616 ha_wurfl_log("WURFL: %s is a %s\n", wn->name, HA_WURFL_DATA_TYPE_CAP_STRING);
617#ifdef WURFL_HEADER_WITH_DETAILS
618 // write WURFL property type and name before its value...
619 chunk_appendf(temp, "%s=%s=", HA_WURFL_DATA_TYPE_CAP_STRING, wn->name);
620#endif
621 chunk_appendf(temp, "%s", wurfl_device_get_capability(dHandle, wn->name));
622 break;
623 case HA_WURFL_DATA_TYPE_VCAP :
624 ha_wurfl_log("WURFL: %s is a %s\n", wn->name, HA_WURFL_DATA_TYPE_VCAP_STRING);
625#ifdef WURFL_HEADER_WITH_DETAILS
626 // write WURFL property type and name before its value...
627 chunk_appendf(temp, "%s=%s=", HA_WURFL_DATA_TYPE_VCAP_STRING, wn->name);
628#endif
629 chunk_appendf(temp, "%s", wurfl_device_get_virtual_capability(dHandle, wn->name));
630 break;
631 case HA_WURFL_DATA_TYPE_PROPERTY :
632 ha_wurfl_log("WURFL: %s is a %s\n", wn->name, HA_WURFL_DATA_TYPE_PROPERTY_STRING);
633#ifdef WURFL_HEADER_WITH_DETAILS
634 // write WURFL property type and name before its value...
635 chunk_appendf(temp, "%s=%s=", HA_WURFL_DATA_TYPE_PROPERTY_STRING, wn->name);
636#endif
Willy Tarreau350c1c62016-12-21 14:57:34 +0100637 chunk_appendf(temp, "%s", wn->func_callback(global_wurfl.handle, dHandle));
scientiamobiled0027ed2016-11-04 10:55:08 +0100638 break;
639 }
640
641 } else {
642 ha_wurfl_log("WURFL: %s not in wurfl-information-list \n", args[i].data.str.str);
643 }
644
645 i++;
646 }
647
648 wurfl_device_destroy(dHandle);
649 smp->data.u.str.str = temp->str;
650 smp->data.u.str.len = temp->len;
651 return 1;
652}
653
654static struct cfg_kw_list wurflcfg_kws = {{ }, {
655 { CFG_GLOBAL, "wurfl-data-file", ha_wurfl_cfg_data_file },
656 { CFG_GLOBAL, "wurfl-information-list-separator", ha_wurfl_cfg_information_list_separator },
657 { CFG_GLOBAL, "wurfl-information-list", ha_wurfl_cfg_information_list },
658 { CFG_GLOBAL, "wurfl-patch-file", ha_wurfl_cfg_patch_file_list },
659 { CFG_GLOBAL, "wurfl-cache-size", ha_wurfl_cfg_cache },
660 { CFG_GLOBAL, "wurfl-engine-mode", ha_wurfl_cfg_engine_mode },
661 { CFG_GLOBAL, "wurfl-useragent-priority", ha_wurfl_cfg_useragent_priority },
662 { 0, NULL, NULL },
663 }
664};
665
666/* Note: must not be declared <const> as its list will be overwritten */
667static struct sample_fetch_kw_list fetch_kws = {ILH, {
668 { "wurfl-get-all", ha_wurfl_get_all, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
669 { "wurfl-get", ha_wurfl_get, ARG12(1,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
670 { NULL, NULL, 0, 0, 0 },
671 }
672};
673
674/* Note: must not be declared <const> as its list will be overwritten */
675static struct sample_conv_kw_list conv_kws = {ILH, {
676 { NULL, NULL, 0, 0, 0 },
677 }
678};
679
680__attribute__((constructor))
681static void __wurfl_init(void)
682{
683 /* register sample fetch and format conversion keywords */
684 sample_register_fetches(&fetch_kws);
685 sample_register_convs(&conv_kws);
686 cfg_register_keywords(&wurflcfg_kws);
Willy Tarreau770042d2016-12-21 18:47:13 +0100687 hap_register_build_opts("Built with WURFL support.", 0);
Willy Tarreaudc2ed472016-12-21 20:20:17 +0100688 hap_register_post_check(ha_wurfl_init);
Willy Tarreau800f93f2016-12-21 20:52:38 +0100689 hap_register_post_deinit(ha_wurfl_deinit);
scientiamobiled0027ed2016-11-04 10:55:08 +0100690}
691
692// WURFL properties wrapper functions
693static const char *ha_wurfl_get_wurfl_root_id (wurfl_handle wHandle, wurfl_device_handle dHandle)
694{
695 return wurfl_device_get_root_id(dHandle);
696}
697
698static const char *ha_wurfl_get_wurfl_id (wurfl_handle wHandle, wurfl_device_handle dHandle)
699{
700 return wurfl_device_get_id(dHandle);
701}
702
703static const char *ha_wurfl_get_wurfl_isdevroot (wurfl_handle wHandle, wurfl_device_handle dHandle)
704{
705 if (wurfl_device_is_actual_device_root(dHandle))
706 return HA_WURFL_ISDEVROOT_TRUE;
707 else
708 return HA_WURFL_ISDEVROOT_FALSE;
709}
710
711static const char *ha_wurfl_get_wurfl_useragent (wurfl_handle wHandle, wurfl_device_handle dHandle)
712{
713 return wurfl_device_get_original_useragent(dHandle);
714}
715
716static const char *ha_wurfl_get_wurfl_api_version (wurfl_handle wHandle, wurfl_device_handle dHandle)
717{
718 return wurfl_get_api_version();
719}
720
721static const char *ha_wurfl_get_wurfl_engine_target (wurfl_handle wHandle, wurfl_device_handle dHandle)
722{
723 return wurfl_get_engine_target_as_string(wHandle);
724}
725
726static const char *ha_wurfl_get_wurfl_info (wurfl_handle wHandle, wurfl_device_handle dHandle)
727{
728 return wurfl_get_wurfl_info(wHandle);
729}
730
731static const char *ha_wurfl_get_wurfl_last_load_time (wurfl_handle wHandle, wurfl_device_handle dHandle)
732{
733 return wurfl_get_last_load_time_as_string(wHandle);
734}
735
736static const char *ha_wurfl_get_wurfl_normalized_useragent (wurfl_handle wHandle, wurfl_device_handle dHandle)
737{
738 return wurfl_device_get_normalized_useragent(dHandle);
739}
740
741static const char *ha_wurfl_get_wurfl_useragent_priority (wurfl_handle wHandle, wurfl_device_handle dHandle)
742{
743 return wurfl_get_useragent_priority_as_string(wHandle);
744}
745
746// call function for WURFL properties
747static const char *(*ha_wurfl_get_property_callback(char *name)) (wurfl_handle wHandle, wurfl_device_handle dHandle)
748{
749 int position;
750 int begin = 0;
751 int end = HA_WURFL_PROPERTIES_NBR - 1;
752 int cond = 0;
753
754 while(begin <= end) {
755 position = (begin + end) / 2;
756
757 if((cond = strcmp(wurfl_properties_function_map[position].name, name)) == 0) {
758 ha_wurfl_log("WURFL: ha_wurfl_get_property_callback match %s\n", wurfl_properties_function_map[position].name );
759 return wurfl_properties_function_map[position].func;
760 } else if(cond < 0)
761 begin = position + 1;
762 else
763 end = position - 1;
764
765 }
766
767 return NULL;
768}
769
770static const char *ha_wurfl_retrieve_header(const char *header_name, const void *wh)
771{
772 struct sample *smp;
773 struct hdr_idx *idx;
774 struct hdr_ctx ctx;
775 const struct http_msg *msg;
776 int header_len = HA_WURFL_MAX_HEADER_LENGTH;
777
778 ha_wurfl_log("WURFL: retrieve header request [%s]\n", header_name);
779 smp = ((ha_wurfl_header_t *)wh)->wsmp;
780 idx = &smp->strm->txn->hdr_idx;
781 msg = &smp->strm->txn->req;
782 ctx.idx = 0;
783
784 if (http_find_full_header2(header_name, strlen(header_name), msg->chn->buf->p, idx, &ctx) == 0)
785 return 0;
786
787 if (header_len > ctx.vlen)
788 header_len = ctx.vlen;
789
790 strncpy(((ha_wurfl_header_t *)wh)->header_value, ctx.line + ctx.val, header_len);
791 ((ha_wurfl_header_t *)wh)->header_value[header_len] = '\0';
792 ha_wurfl_log("WURFL: retrieve header request returns [%s]\n", ((ha_wurfl_header_t *)wh)->header_value);
793 return ((ha_wurfl_header_t *)wh)->header_value;
794}