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>
38 #include <sys/types.h>
40 #include <sys/socket.h>
46 #include <netinet/in.h>
49 enum pazpar2_database_criterion_type {
51 PAZPAR2_SUBSTRING_MATCH
54 struct database_criterion_value {
56 struct database_criterion_value *next;
59 struct database_criterion {
61 enum pazpar2_database_criterion_type type;
62 struct database_criterion_value *values;
63 struct database_criterion *next;
66 static struct host *hosts = 0; /* thread pr */
68 static xmlDoc *get_explain_xml(struct conf_targetprofiles *targetprofiles,
75 if (targetprofiles->type != Targetprofiles_local)
77 yaz_log(YLOG_FATAL, "Only supports local type");
80 dir = targetprofiles->src;
82 sprintf(path, "%s/%s", dir, ide);
84 return xmlParseFile(path);
89 // Create a new host structure for hostport
90 static struct host *create_host(const char *hostport, iochan_man_t iochan_man)
94 host = xmalloc(sizeof(struct host));
95 host->hostport = xstrdup(hostport);
96 host->connections = 0;
100 if (host_getaddrinfo(host, iochan_man))
102 xfree(host->hostport);
106 yaz_mutex_create(&host->mutex);
113 static struct host *find_host(const char *hostport, iochan_man_t iochan_man)
116 for (p = hosts; p; p = p->next)
117 if (!strcmp(p->hostport, hostport))
119 return create_host(hostport, iochan_man);
122 int resolve_database(struct conf_service *service, struct database *db)
129 strcpy(hostport, db->url);
130 if ((p = strchr(hostport, '/')))
132 if (!(host = find_host(hostport, service->server->iochan_man)))
139 void resolve_databases(struct conf_service *service)
141 struct database *db = service->databases;
142 for (; db; db = db->next)
143 resolve_database(service, db);
146 struct database *new_database(const char *id, NMEM nmem)
151 struct setting *idset;
153 if (strlen(id) > 255)
155 strcpy(hostport, id);
156 if ((dbname = strchr(hostport, '/')))
160 db = nmem_malloc(nmem, sizeof(*db));
161 memset(db, 0, sizeof(*db));
163 db->url = nmem_strdup(nmem, id);
164 db->databases = nmem_malloc(nmem, 2 * sizeof(char *));
165 db->databases[0] = nmem_strdup(nmem, dbname);
166 db->databases[1] = 0;
170 db->num_settings = PZ_NEGOTIATION_CHARSET+1;
171 db->settings = nmem_malloc(nmem, sizeof(struct settings*) *
173 memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
174 idset = nmem_malloc(nmem, sizeof(*idset));
175 idset->precedence = 0;
176 idset->name = "pz:id";
177 idset->target = idset->value = db->url;
179 db->settings[PZ_ID] = idset;
185 static struct database *load_database(const char *id,
186 struct conf_service *service)
189 struct zr_explain *explain = 0;
192 if (service->targetprofiles
193 && (doc = get_explain_xml(service->targetprofiles, id)))
195 explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
199 db = new_database(id, service->nmem);
200 db->explain = explain;
201 db->next = service->databases;
202 service->databases = db;
207 // Return a database structure by ID. Load and add to list if necessary
208 // new==1 just means we know it's not in the list
209 struct database *find_database(const char *id, struct conf_service *service)
212 for (p = service->databases; p; p = p->next)
213 if (!strcmp(p->url, id))
215 return load_database(id, service);
218 // This whole session_grep database thing should be moved elsewhere
220 int match_zurl(const char *zurl, const char *pattern)
224 if (!strcmp(pattern, "*"))
226 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
228 char *db = strchr(zurl, '/');
231 if (!strcmp(pattern + 2, db))
236 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
238 if (!strncmp(pattern, zurl, len))
243 else if (!strcmp(pattern, zurl))
249 // This will be generalized at some point
250 static int match_criterion(struct setting **settings,
251 struct conf_service *service,
252 struct database_criterion *c)
254 int offset = settings_lookup_offset(service, c->name);
255 struct database_criterion_value *v;
259 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
262 if (!settings[offset])
264 for (v = c->values; v; v = v->next)
266 if (c->type == PAZPAR2_STRING_MATCH)
270 if (match_zurl(settings[offset]->value, v->value))
275 if (!strcmp(settings[offset]->value, v->value))
279 else if (c->type == PAZPAR2_SUBSTRING_MATCH)
281 if (strstr(settings[offset]->value, v->value))
291 // parses crit1=val1,crit2=val2|val3,...
292 static struct database_criterion *create_database_criterion(NMEM m,
295 struct database_criterion *res = 0;
302 nmem_strsplit(m, ",", buf, &values, &num);
303 for (i = 0; i < num; i++)
308 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
310 if ((eq = strchr(values[i], '=')))
311 new->type = PAZPAR2_STRING_MATCH;
312 else if ((eq = strchr(values[i], '~')))
313 new->type = PAZPAR2_SUBSTRING_MATCH;
316 yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
320 new->name = values[i];
321 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
323 for (subi = 0; subi < subnum; subi++)
325 struct database_criterion_value *newv
326 = nmem_malloc(m, sizeof(*newv));
327 newv->value = subvalues[subi];
328 newv->next = new->values;
337 static int database_match_criteria(struct setting **settings,
338 struct conf_service *service,
339 struct database_criterion *cl)
341 for (; cl; cl = cl->next)
342 if (!match_criterion(settings, service, cl))
344 if (cl) // one of the criteria failed to match -- skip this db
350 // Cycles through databases, calling a handler function on the ones for
351 // which all criteria matched.
352 int session_grep_databases(struct session *se, const char *filter,
353 void (*fun)(void *context, struct session_database *db))
355 struct session_database *p;
356 NMEM nmem = nmem_create();
358 struct database_criterion *cl = create_database_criterion(nmem, filter);
360 for (p = se->databases; p; p = p->next)
362 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
364 if (!p->settings[PZ_NAME])
366 if (database_match_criteria(p->settings, se->service, cl))
376 int predef_grep_databases(void *context, struct conf_service *service,
377 void (*fun)(void *context, struct database *db))
382 for (p = service->databases; p; p = p->next)
383 if (database_match_criteria(p->settings, service, 0))
394 * c-file-style: "Stroustrup"
395 * indent-tabs-mode: nil
397 * vim: shiftwidth=4 tabstop=8 expandtab