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
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
26 #include <libxslt/xslt.h>
27 #include <libxslt/transform.h>
28 #include <libxslt/xsltutils.h>
30 #include <sys/types.h>
39 #include <sys/types.h>
41 #include <sys/socket.h>
47 #include <netinet/in.h>
50 static struct host *hosts = 0; // The hosts we know about
51 static struct database *databases = 0; // The databases we know about
54 static xmlDoc *get_explain_xml(const char *id)
60 if (!config || !config->targetprofiles)
62 yaz_log(YLOG_WARN, "Config must be loaded and specify targetprofiles");
65 if (config->targetprofiles->type != Targetprofiles_local)
67 yaz_log(YLOG_FATAL, "Only supports local type");
70 dir = config->targetprofiles->src;
72 sprintf(path, "%s/%s", dir, ide);
74 return xmlParseFile(path);
79 // Create a new host structure for hostport
80 static struct host *create_host(const char *hostport)
84 host = xmalloc(sizeof(struct host));
85 host->hostport = xstrdup(hostport);
86 host->connections = 0;
89 if (host_getaddrinfo(host))
91 xfree(host->hostport);
100 static struct host *find_host(const char *hostport)
103 for (p = hosts; p; p = p->next)
104 if (!strcmp(p->hostport, hostport))
106 return create_host(hostport);
109 static struct database *load_database(const char *id)
112 struct zr_explain *explain = 0;
117 struct setting *idset;
119 yaz_log(YLOG_LOG, "New database: %s", id);
121 nmem = nmem_create();
123 if (config && config->targetprofiles
124 && (doc = get_explain_xml(id)))
126 explain = zr_read_xml(nmem, xmlDocGetRootElement(doc));
131 if (strlen(id) > 255)
133 strcpy(hostport, id);
134 if ((dbname = strchr(hostport, '/')))
138 if (!(host = find_host(hostport)))
140 db = nmem_malloc(nmem, sizeof(*db));
141 memset(db, 0, sizeof(*db));
143 db->url = nmem_strdup(nmem, id);
144 db->databases = xmalloc(2 * sizeof(char *));
145 db->databases[0] = nmem_strdup(nmem, dbname);
146 db->databases[1] = 0;
148 db->explain = explain;
152 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * settings_num());
153 memset(db->settings, 0, sizeof(struct settings*) * settings_num());
154 idset = nmem_malloc(nmem, sizeof(*idset));
155 idset->precedence = 0;
156 idset->name = "pz:id";
157 idset->target = idset->value = db->url;
159 db->settings[PZ_ID] = idset;
161 db->next = databases;
167 // Return a database structure by ID. Load and add to list if necessary
168 // new==1 just means we know it's not in the list
169 struct database *find_database(const char *id, int new)
174 for (p = databases; p; p = p->next)
175 if (!strcmp(p->url, id))
178 return load_database(id);
181 // This whole session_grep database thing should be moved elsewhere
183 int match_zurl(const char *zurl, const char *pattern)
187 if (!strcmp(pattern, "*"))
189 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
191 char *db = strchr(zurl, '/');
194 if (!strcmp(pattern + 2, db))
199 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
201 if (!strncmp(pattern, zurl, len))
206 else if (!strcmp(pattern, zurl))
212 // This will be generalized at some point
213 static int match_criterion(struct setting **settings, struct database_criterion *c)
215 int offset = settings_offset(c->name);
216 struct database_criterion_value *v;
220 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
223 if (!settings[offset])
225 for (v = c->values; v; v = v->next)
229 if (match_zurl(settings[offset]->value, v->value))
234 if (!strcmp(settings[offset]->value, v->value))
244 int database_match_criteria(struct setting **settings, struct database_criterion *cl)
246 for (; cl; cl = cl->next)
247 if (!match_criterion(settings, cl))
249 if (cl) // one of the criteria failed to match -- skip this db
255 // Cycles through databases, calling a handler function on the ones for
256 // which all criteria matched.
257 int session_grep_databases(struct session *se, struct database_criterion *cl,
258 void (*fun)(void *context, struct session_database *db))
260 struct session_database *p;
263 for (p = se->databases; p; p = p->next)
265 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
267 if (!p->settings[PZ_NAME])
269 if (database_match_criteria(p->settings, cl))
278 int predef_grep_databases(void *context, struct database_criterion *cl,
279 void (*fun)(void *context, struct database *db))
284 for (p = databases; p; p = p->next)
285 if (database_match_criteria(p->settings, cl))
296 * indent-tabs-mode: nil
298 * vim: shiftwidth=4 tabstop=8 expandtab