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>
37 #include <sys/socket.h>
43 #include <netinet/in.h>
46 static struct host *hosts = 0; // The hosts we know about
47 static struct database *databases = 0; // The databases we know about
50 static xmlDoc *get_explain_xml(const char *id)
56 if (!config || !config->targetprofiles)
58 yaz_log(YLOG_WARN, "Config must be loaded and specify targetprofiles");
61 if (config->targetprofiles->type != Targetprofiles_local)
63 yaz_log(YLOG_FATAL, "Only supports local type");
66 dir = config->targetprofiles->src;
68 sprintf(path, "%s/%s", dir, ide);
70 return xmlParseFile(path);
75 // Create a new host structure for hostport
76 static struct host *create_host(const char *hostport)
80 host = xmalloc(sizeof(struct host));
81 host->hostport = xstrdup(hostport);
82 host->connections = 0;
85 if (host_getaddrinfo(host))
87 xfree(host->hostport);
96 static struct host *find_host(const char *hostport)
99 for (p = hosts; p; p = p->next)
100 if (!strcmp(p->hostport, hostport))
102 return create_host(hostport);
105 static struct database *load_database(const char *id)
108 struct zr_explain *explain = 0;
113 struct setting *idset;
115 yaz_log(YLOG_LOG, "New database: %s", id);
117 nmem = nmem_create();
119 if (config && config->targetprofiles
120 && (doc = get_explain_xml(id)))
122 explain = zr_read_xml(nmem, xmlDocGetRootElement(doc));
127 if (strlen(id) > 255)
129 strcpy(hostport, id);
130 if ((dbname = strchr(hostport, '/')))
134 if (!(host = find_host(hostport)))
136 db = nmem_malloc(nmem, sizeof(*db));
137 memset(db, 0, sizeof(*db));
139 db->url = nmem_strdup(nmem, id);
140 db->databases = xmalloc(2 * sizeof(char *));
141 db->databases[0] = nmem_strdup(nmem, dbname);
142 db->databases[1] = 0;
144 db->explain = explain;
148 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * settings_num());
149 memset(db->settings, 0, sizeof(struct settings*) * settings_num());
150 idset = nmem_malloc(nmem, sizeof(*idset));
151 idset->precedence = 0;
152 idset->name = "pz:id";
153 idset->target = idset->value = db->url;
155 db->settings[PZ_ID] = idset;
157 db->next = databases;
163 // Return a database structure by ID. Load and add to list if necessary
164 // new==1 just means we know it's not in the list
165 struct database *find_database(const char *id, int new)
170 for (p = databases; p; p = p->next)
171 if (!strcmp(p->url, id))
174 return load_database(id);
177 // This whole session_grep database thing should be moved elsewhere
179 int match_zurl(const char *zurl, const char *pattern)
183 if (!strcmp(pattern, "*"))
185 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
187 char *db = strchr(zurl, '/');
190 if (!strcmp(pattern + 2, db))
195 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
197 if (!strncmp(pattern, zurl, len))
202 else if (!strcmp(pattern, zurl))
208 // This will be generalized at some point
209 static int match_criterion(struct setting **settings, struct database_criterion *c)
211 int offset = settings_offset(c->name);
212 struct database_criterion_value *v;
216 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
219 if (!settings[offset])
221 for (v = c->values; v; v = v->next)
225 if (match_zurl(settings[offset]->value, v->value))
230 if (!strcmp(settings[offset]->value, v->value))
240 int database_match_criteria(struct setting **settings, struct database_criterion *cl)
242 for (; cl; cl = cl->next)
243 if (!match_criterion(settings, cl))
245 if (cl) // one of the criteria failed to match -- skip this db
251 // Cycles through databases, calling a handler function on the ones for
252 // which all criteria matched.
253 int session_grep_databases(struct session *se, struct database_criterion *cl,
254 void (*fun)(void *context, struct session_database *db))
256 struct session_database *p;
259 for (p = se->databases; p; p = p->next)
261 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
263 if (!p->settings[PZ_NAME])
265 if (database_match_criteria(p->settings, cl))
274 int predef_grep_databases(void *context, struct database_criterion *cl,
275 void (*fun)(void *context, struct database *db))
280 for (p = databases; p; p = p->next)
281 if (database_match_criteria(p->settings, cl))
292 * indent-tabs-mode: nil
294 * vim: shiftwidth=4 tabstop=8 expandtab