1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 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>
37 #include <sys/types.h>
39 #include <sys/socket.h>
45 #include <netinet/in.h>
48 static struct host *hosts = 0; /* thread pr */
50 static xmlDoc *get_explain_xml(struct conf_targetprofiles *targetprofiles,
57 if (targetprofiles->type != Targetprofiles_local)
59 yaz_log(YLOG_FATAL, "Only supports local type");
62 dir = targetprofiles->src;
64 sprintf(path, "%s/%s", dir, ide);
66 return xmlParseFile(path);
71 // Create a new host structure for hostport
72 static struct host *create_host(const char *hostport)
76 host = xmalloc(sizeof(struct host));
77 host->hostport = xstrdup(hostport);
78 host->connections = 0;
81 if (host_getaddrinfo(host))
83 xfree(host->hostport);
92 static struct host *find_host(const char *hostport)
95 for (p = hosts; p; p = p->next)
96 if (!strcmp(p->hostport, hostport))
98 return create_host(hostport);
101 int resolve_database(struct database *db)
108 strcpy(hostport, db->url);
109 if ((p = strchr(hostport, '/')))
111 if (!(host = find_host(hostport)))
118 void resolve_databases(struct conf_service *service)
120 struct database *db = service->databases;
121 for (; db; db = db->next)
122 resolve_database(db);
125 static struct database *load_database(const char *id,
126 struct conf_service *service)
129 struct zr_explain *explain = 0;
133 struct setting *idset;
135 yaz_log(YLOG_LOG, "New database: %s", id);
137 if (service->targetprofiles
138 && (doc = get_explain_xml(service->targetprofiles, id)))
140 explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
145 if (strlen(id) > 255)
147 strcpy(hostport, id);
148 if ((dbname = strchr(hostport, '/')))
152 db = nmem_malloc(service->nmem, sizeof(*db));
153 memset(db, 0, sizeof(*db));
155 db->url = nmem_strdup(service->nmem, id);
156 db->databases = nmem_malloc(service->nmem, 2 * sizeof(char *));
157 db->databases[0] = nmem_strdup(service->nmem, dbname);
158 db->databases[1] = 0;
160 db->explain = explain;
164 db->settings = nmem_malloc(service->nmem, sizeof(struct settings*) *
165 settings_num(service));
166 memset(db->settings, 0, sizeof(struct settings*) * settings_num(service));
167 idset = nmem_malloc(service->nmem, sizeof(*idset));
168 idset->precedence = 0;
169 idset->name = "pz:id";
170 idset->target = idset->value = db->url;
172 db->settings[PZ_ID] = idset;
174 db->next = service->databases;
175 service->databases = db;
180 // Return a database structure by ID. Load and add to list if necessary
181 // new==1 just means we know it's not in the list
182 struct database *find_database(const char *id, int new,
183 struct conf_service *service)
188 for (p = service->databases; p; p = p->next)
189 if (!strcmp(p->url, id))
192 return load_database(id, service);
195 // This whole session_grep database thing should be moved elsewhere
197 int match_zurl(const char *zurl, const char *pattern)
201 if (!strcmp(pattern, "*"))
203 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
205 char *db = strchr(zurl, '/');
208 if (!strcmp(pattern + 2, db))
213 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
215 if (!strncmp(pattern, zurl, len))
220 else if (!strcmp(pattern, zurl))
226 // This will be generalized at some point
227 static int match_criterion(struct setting **settings,
228 struct conf_service *service,
229 struct database_criterion *c)
231 int offset = settings_offset(service, c->name);
232 struct database_criterion_value *v;
236 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
239 if (!settings[offset])
241 for (v = c->values; v; v = v->next)
245 if (match_zurl(settings[offset]->value, v->value))
250 if (!strcmp(settings[offset]->value, v->value))
260 int database_match_criteria(struct setting **settings,
261 struct conf_service *service,
262 struct database_criterion *cl)
264 for (; cl; cl = cl->next)
265 if (!match_criterion(settings, service, cl))
267 if (cl) // one of the criteria failed to match -- skip this db
273 // Cycles through databases, calling a handler function on the ones for
274 // which all criteria matched.
275 int session_grep_databases(struct session *se, struct database_criterion *cl,
276 void (*fun)(void *context, struct session_database *db))
278 struct session_database *p;
281 for (p = se->databases; p; p = p->next)
283 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
285 if (!p->settings[PZ_NAME])
287 if (database_match_criteria(p->settings, se->service, cl))
296 int predef_grep_databases(void *context, struct conf_service *service,
297 struct database_criterion *cl,
298 void (*fun)(void *context, struct database *db))
303 for (p = service->databases; p; p = p->next)
304 if (database_match_criteria(p->settings, service, cl))
315 * c-file-style: "Stroustrup"
316 * indent-tabs-mode: nil
318 * vim: shiftwidth=4 tabstop=8 expandtab