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>
36 #include <sys/types.h>
38 #include <sys/socket.h>
44 #include <netinet/in.h>
47 static struct host *hosts = 0; // The hosts we know about
48 static struct database *databases = 0; // The databases we know about
51 static xmlDoc *get_explain_xml(const char *id)
57 if (!config || !config->targetprofiles)
59 yaz_log(YLOG_WARN, "Config must be loaded and specify targetprofiles");
62 if (config->targetprofiles->type != Targetprofiles_local)
64 yaz_log(YLOG_FATAL, "Only supports local type");
67 dir = config->targetprofiles->src;
69 sprintf(path, "%s/%s", dir, ide);
71 return xmlParseFile(path);
76 // Create a new host structure for hostport
77 static struct host *create_host(const char *hostport)
81 host = xmalloc(sizeof(struct host));
82 host->hostport = xstrdup(hostport);
83 host->connections = 0;
86 if (host_getaddrinfo(host))
88 xfree(host->hostport);
97 static struct host *find_host(const char *hostport)
100 for (p = hosts; p; p = p->next)
101 if (!strcmp(p->hostport, hostport))
103 return create_host(hostport);
106 static struct database *load_database(const char *id)
109 struct zr_explain *explain = 0;
114 struct setting *idset;
116 yaz_log(YLOG_LOG, "New database: %s", id);
118 nmem = nmem_create();
120 if (config && config->targetprofiles
121 && (doc = get_explain_xml(id)))
123 explain = zr_read_xml(nmem, xmlDocGetRootElement(doc));
128 if (strlen(id) > 255)
130 strcpy(hostport, id);
131 if ((dbname = strchr(hostport, '/')))
135 if (!(host = find_host(hostport)))
137 db = nmem_malloc(nmem, sizeof(*db));
138 memset(db, 0, sizeof(*db));
140 db->url = nmem_strdup(nmem, id);
141 db->databases = xmalloc(2 * sizeof(char *));
142 db->databases[0] = nmem_strdup(nmem, dbname);
143 db->databases[1] = 0;
145 db->explain = explain;
149 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * settings_num());
150 memset(db->settings, 0, sizeof(struct settings*) * settings_num());
151 idset = nmem_malloc(nmem, sizeof(*idset));
152 idset->precedence = 0;
153 idset->name = "pz:id";
154 idset->target = idset->value = db->url;
156 db->settings[PZ_ID] = idset;
158 db->next = databases;
164 // Return a database structure by ID. Load and add to list if necessary
165 // new==1 just means we know it's not in the list
166 struct database *find_database(const char *id, int new)
171 for (p = databases; p; p = p->next)
172 if (!strcmp(p->url, id))
175 return load_database(id);
178 // This whole session_grep database thing should be moved elsewhere
180 int match_zurl(const char *zurl, const char *pattern)
184 if (!strcmp(pattern, "*"))
186 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
188 char *db = strchr(zurl, '/');
191 if (!strcmp(pattern + 2, db))
196 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
198 if (!strncmp(pattern, zurl, len))
203 else if (!strcmp(pattern, zurl))
209 // This will be generalized at some point
210 static int match_criterion(struct setting **settings, struct database_criterion *c)
212 int offset = settings_offset(c->name);
213 struct database_criterion_value *v;
217 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
220 if (!settings[offset])
222 for (v = c->values; v; v = v->next)
226 if (match_zurl(settings[offset]->value, v->value))
231 if (!strcmp(settings[offset]->value, v->value))
241 int database_match_criteria(struct setting **settings, struct database_criterion *cl)
243 for (; cl; cl = cl->next)
244 if (!match_criterion(settings, cl))
246 if (cl) // one of the criteria failed to match -- skip this db
252 // Cycles through databases, calling a handler function on the ones for
253 // which all criteria matched.
254 int session_grep_databases(struct session *se, struct database_criterion *cl,
255 void (*fun)(void *context, struct session_database *db))
257 struct session_database *p;
260 for (p = se->databases; p; p = p->next)
262 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
264 if (!p->settings[PZ_NAME])
266 if (database_match_criteria(p->settings, cl))
275 int predef_grep_databases(void *context, struct database_criterion *cl,
276 void (*fun)(void *context, struct database *db))
281 for (p = databases; p; p = p->next)
282 if (database_match_criteria(p->settings, cl))
293 * c-file-style: "Stroustrup"
294 * indent-tabs-mode: nil
296 * vim: shiftwidth=4 tabstop=8 expandtab