1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 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
20 #include <libxml/parser.h>
21 #include <libxml/tree.h>
22 #include <libxslt/xslt.h>
23 #include <libxslt/transform.h>
24 #include <libxslt/xsltutils.h>
26 #include <sys/types.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
38 #include <netinet/in.h>
40 static struct host *hosts = 0; // The hosts we know about
41 static struct database *databases = 0; // The databases we know about
44 static xmlDoc *get_explain_xml(const char *id)
50 if (!config || !config->targetprofiles)
52 yaz_log(YLOG_WARN, "Config must be loaded and specify targetprofiles");
55 if (config->targetprofiles->type != Targetprofiles_local)
57 yaz_log(YLOG_FATAL, "Only supports local type");
60 dir = config->targetprofiles->src;
62 sprintf(path, "%s/%s", dir, ide);
64 return xmlParseFile(path);
69 // Create a new host structure for hostport
70 static struct host *create_host(const char *hostport)
74 host = xmalloc(sizeof(struct host));
75 host->hostport = xstrdup(hostport);
76 host->connections = 0;
79 if (host_getaddrinfo(host))
81 xfree(host->hostport);
90 static struct host *find_host(const char *hostport)
93 for (p = hosts; p; p = p->next)
94 if (!strcmp(p->hostport, hostport))
96 return create_host(hostport);
99 static struct database *load_database(const char *id)
102 struct zr_explain *explain = 0;
107 struct setting *idset;
109 yaz_log(YLOG_LOG, "New database: %s", id);
111 nmem = nmem_create();
113 if (config && config->targetprofiles
114 && (doc = get_explain_xml(id)))
116 explain = zr_read_xml(nmem, xmlDocGetRootElement(doc));
121 if (strlen(id) > 255)
123 strcpy(hostport, id);
124 if ((dbname = strchr(hostport, '/')))
128 if (!(host = find_host(hostport)))
130 db = nmem_malloc(nmem, sizeof(*db));
131 memset(db, 0, sizeof(*db));
133 db->url = nmem_strdup(nmem, id);
134 db->databases = xmalloc(2 * sizeof(char *));
135 db->databases[0] = nmem_strdup(nmem, dbname);
136 db->databases[1] = 0;
138 db->explain = explain;
142 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * settings_num());
143 memset(db->settings, 0, sizeof(struct settings*) * settings_num());
144 idset = nmem_malloc(nmem, sizeof(*idset));
145 idset->precedence = 0;
146 idset->name = "pz:id";
147 idset->target = idset->value = db->url;
149 db->settings[PZ_ID] = idset;
151 db->next = databases;
157 // Return a database structure by ID. Load and add to list if necessary
158 // new==1 just means we know it's not in the list
159 struct database *find_database(const char *id, int new)
164 for (p = databases; p; p = p->next)
165 if (!strcmp(p->url, id))
168 return load_database(id);
171 // This whole session_grep database thing should be moved elsewhere
173 int match_zurl(const char *zurl, const char *pattern)
177 if (!strcmp(pattern, "*"))
179 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
181 char *db = strchr(zurl, '/');
184 if (!strcmp(pattern + 2, db))
189 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
191 if (!strncmp(pattern, zurl, len))
196 else if (!strcmp(pattern, zurl))
202 // This will be generalized at some point
203 static int match_criterion(struct setting **settings, struct database_criterion *c)
205 int offset = settings_offset(c->name);
206 struct database_criterion_value *v;
210 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
213 if (!settings[offset])
215 for (v = c->values; v; v = v->next)
219 if (match_zurl(settings[offset]->value, v->value))
224 if (!strcmp(settings[offset]->value, v->value))
234 int database_match_criteria(struct setting **settings, struct database_criterion *cl)
236 for (; cl; cl = cl->next)
237 if (!match_criterion(settings, cl))
239 if (cl) // one of the criteria failed to match -- skip this db
245 // Cycles through databases, calling a handler function on the ones for
246 // which all criteria matched.
247 int session_grep_databases(struct session *se, struct database_criterion *cl,
248 void (*fun)(void *context, struct session_database *db))
250 struct session_database *p;
253 for (p = se->databases; p; p = p->next)
255 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
257 if (!p->settings[PZ_NAME])
259 if (database_match_criteria(p->settings, cl))
268 int predef_grep_databases(void *context, struct database_criterion *cl,
269 void (*fun)(void *context, struct database *db))
274 for (p = databases; p; p = p->next)
275 if (database_match_criteria(p->settings, cl))
286 * indent-tabs-mode: nil
288 * vim: shiftwidth=4 tabstop=8 expandtab