1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
27 #include <sys/types.h>
34 #include "pazpar2_config.h"
40 #include <sys/types.h>
42 #include <sys/socket.h>
48 #include <netinet/in.h>
51 enum pazpar2_database_criterion_type {
53 PAZPAR2_SUBSTRING_MATCH
56 struct database_criterion_value {
58 struct database_criterion_value *next;
61 struct database_criterion {
63 enum pazpar2_database_criterion_type type;
64 struct database_criterion_value *values;
65 struct database_criterion *next;
69 struct database_hosts {
74 static xmlDoc *get_explain_xml(struct conf_targetprofiles *targetprofiles,
81 if (targetprofiles->type != Targetprofiles_local)
83 yaz_log(YLOG_FATAL, "Only supports local type");
86 dir = targetprofiles->src;
88 sprintf(path, "%s/%s", dir, ide);
90 return xmlParseFile(path);
95 // Create a new host structure for hostport
96 static struct host *create_host(const char *hostport, iochan_man_t iochan_man)
100 host = xmalloc(sizeof(struct host));
101 host->hostport = xstrdup(hostport);
102 host->connections = 0;
106 if (host_getaddrinfo(host, iochan_man))
108 xfree(host->hostport);
112 yaz_mutex_create(&host->mutex);
117 static struct host *find_host(database_hosts_t hosts,
118 const char *hostport, iochan_man_t iochan_man)
121 yaz_mutex_enter(hosts->mutex);
122 for (p = hosts->hosts; p; p = p->next)
123 if (!strcmp(p->hostport, hostport))
127 p = create_host(hostport, iochan_man);
130 p->next = hosts->hosts;
134 yaz_mutex_leave(hosts->mutex);
138 int resolve_database(struct conf_service *service, struct database *db)
145 strcpy(hostport, db->url);
146 if ((p = strchr(hostport, '/')))
148 if (!(host = find_host(service->server->database_hosts,
149 hostport, service->server->iochan_man)))
156 void resolve_databases(struct conf_service *service)
158 struct database *db = service->databases;
159 for (; db; db = db->next)
160 resolve_database(service, db);
163 struct database *new_database(const char *id, NMEM nmem)
168 struct setting *idset;
170 if (strlen(id) > 255)
172 strcpy(hostport, id);
173 if ((dbname = strchr(hostport, '/')))
177 db = nmem_malloc(nmem, sizeof(*db));
178 memset(db, 0, sizeof(*db));
180 db->url = nmem_strdup(nmem, id);
181 db->databases = nmem_malloc(nmem, 2 * sizeof(char *));
182 db->databases[0] = nmem_strdup(nmem, dbname);
183 db->databases[1] = 0;
187 db->num_settings = PZ_NEGOTIATION_CHARSET+1;
188 db->settings = nmem_malloc(nmem, sizeof(struct settings*) *
190 memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
191 idset = nmem_malloc(nmem, sizeof(*idset));
192 idset->precedence = 0;
193 idset->name = "pz:id";
194 idset->target = idset->value = db->url;
196 db->settings[PZ_ID] = idset;
202 static struct database *load_database(const char *id,
203 struct conf_service *service)
206 struct zr_explain *explain = 0;
209 if (service->targetprofiles
210 && (doc = get_explain_xml(service->targetprofiles, id)))
212 explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
216 db = new_database(id, service->nmem);
217 db->explain = explain;
218 db->next = service->databases;
219 service->databases = db;
224 // Return a database structure by ID. Load and add to list if necessary
225 // new==1 just means we know it's not in the list
226 struct database *find_database(const char *id, struct conf_service *service)
229 for (p = service->databases; p; p = p->next)
230 if (!strcmp(p->url, id))
232 return load_database(id, service);
235 // This whole session_grep database thing should be moved elsewhere
237 int match_zurl(const char *zurl, const char *pattern)
241 if (!strcmp(pattern, "*"))
243 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
245 char *db = strchr(zurl, '/');
248 if (!strcmp(pattern + 2, db))
253 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
255 if (!strncmp(pattern, zurl, len))
260 else if (!strcmp(pattern, zurl))
266 // This will be generalized at some point
267 static int match_criterion(struct setting **settings,
268 struct conf_service *service,
269 struct database_criterion *c)
271 int offset = settings_lookup_offset(service, c->name);
272 struct database_criterion_value *v;
276 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
279 if (!settings[offset])
281 for (v = c->values; v; v = v->next)
283 if (c->type == PAZPAR2_STRING_MATCH)
287 if (match_zurl(settings[offset]->value, v->value))
292 if (!strcmp(settings[offset]->value, v->value))
296 else if (c->type == PAZPAR2_SUBSTRING_MATCH)
298 if (strstr(settings[offset]->value, v->value))
308 // parses crit1=val1,crit2=val2|val3,...
309 static struct database_criterion *create_database_criterion(NMEM m,
312 struct database_criterion *res = 0;
319 nmem_strsplit(m, ",", buf, &values, &num);
320 for (i = 0; i < num; i++)
325 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
327 if ((eq = strchr(values[i], '=')))
328 new->type = PAZPAR2_STRING_MATCH;
329 else if ((eq = strchr(values[i], '~')))
330 new->type = PAZPAR2_SUBSTRING_MATCH;
333 yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
337 new->name = values[i];
338 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
340 for (subi = 0; subi < subnum; subi++)
342 struct database_criterion_value *newv
343 = nmem_malloc(m, sizeof(*newv));
344 newv->value = subvalues[subi];
345 newv->next = new->values;
354 static int database_match_criteria(struct setting **settings,
355 struct conf_service *service,
356 struct database_criterion *cl)
358 for (; cl; cl = cl->next)
359 if (!match_criterion(settings, service, cl))
361 if (cl) // one of the criteria failed to match -- skip this db
367 // Cycles through databases, calling a handler function on the ones for
368 // which all criteria matched.
369 int session_grep_databases(struct session *se, const char *filter,
370 void (*fun)(void *context, struct session_database *db))
372 struct session_database *p;
373 NMEM nmem = nmem_create();
375 struct database_criterion *cl = create_database_criterion(nmem, filter);
377 for (p = se->databases; p; p = p->next)
379 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
381 if (!p->settings[PZ_NAME])
383 if (database_match_criteria(p->settings, se->service, cl))
393 int predef_grep_databases(void *context, struct conf_service *service,
394 void (*fun)(void *context, struct database *db))
399 for (p = service->databases; p; p = p->next)
400 if (database_match_criteria(p->settings, service, 0))
408 database_hosts_t database_hosts_create(void)
410 database_hosts_t p = xmalloc(sizeof(*p));
413 yaz_mutex_create(&p->mutex);
420 * c-file-style: "Stroustrup"
421 * indent-tabs-mode: nil
423 * vim: shiftwidth=4 tabstop=8 expandtab