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; // The hosts 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);
77 // Create a new host structure for hostport
78 static struct host *create_host(const char *hostport)
82 host = xmalloc(sizeof(struct host));
83 host->hostport = xstrdup(hostport);
84 host->connections = 0;
87 if (host_getaddrinfo(host))
89 xfree(host->hostport);
98 static struct host *find_host(const char *hostport)
101 for (p = hosts; p; p = p->next)
102 if (!strcmp(p->hostport, hostport))
104 return create_host(hostport);
107 static struct database *load_database(const char *id,
108 struct conf_service *service)
111 struct zr_explain *explain = 0;
116 struct setting *idset;
118 yaz_log(YLOG_LOG, "New database: %s", id);
121 if (config && config->targetprofiles
122 && (doc = get_explain_xml(id)))
124 explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
130 if (strlen(id) > 255)
132 strcpy(hostport, id);
133 if ((dbname = strchr(hostport, '/')))
137 if (!(host = find_host(hostport)))
139 db = nmem_malloc(service->nmem, sizeof(*db));
140 memset(db, 0, sizeof(*db));
142 db->url = nmem_strdup(service->nmem, id);
143 db->databases = xmalloc(2 * sizeof(char *));
144 db->databases[0] = nmem_strdup(service->nmem, dbname);
145 db->databases[1] = 0;
147 db->explain = explain;
151 db->settings = nmem_malloc(service->nmem, sizeof(struct settings*) *
152 settings_num(service));
153 memset(db->settings, 0, sizeof(struct settings*) * settings_num(service));
154 idset = nmem_malloc(service->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 = service->databases;
162 service->databases = db;
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,
170 struct conf_service *service)
175 for (p = service->databases; p; p = p->next)
176 if (!strcmp(p->url, id))
179 return load_database(id, service);
182 // This whole session_grep database thing should be moved elsewhere
184 int match_zurl(const char *zurl, const char *pattern)
188 if (!strcmp(pattern, "*"))
190 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
192 char *db = strchr(zurl, '/');
195 if (!strcmp(pattern + 2, db))
200 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
202 if (!strncmp(pattern, zurl, len))
207 else if (!strcmp(pattern, zurl))
213 // This will be generalized at some point
214 static int match_criterion(struct setting **settings,
215 struct conf_service *service,
216 struct database_criterion *c)
218 int offset = settings_offset(service, c->name);
219 struct database_criterion_value *v;
223 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
226 if (!settings[offset])
228 for (v = c->values; v; v = v->next)
232 if (match_zurl(settings[offset]->value, v->value))
237 if (!strcmp(settings[offset]->value, v->value))
247 int database_match_criteria(struct setting **settings,
248 struct conf_service *service,
249 struct database_criterion *cl)
251 for (; cl; cl = cl->next)
252 if (!match_criterion(settings, service, cl))
254 if (cl) // one of the criteria failed to match -- skip this db
260 // Cycles through databases, calling a handler function on the ones for
261 // which all criteria matched.
262 int session_grep_databases(struct session *se, struct database_criterion *cl,
263 void (*fun)(void *context, struct session_database *db))
265 struct session_database *p;
268 for (p = se->databases; p; p = p->next)
270 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
272 if (!p->settings[PZ_NAME])
274 if (database_match_criteria(p->settings, se->service, cl))
283 int predef_grep_databases(void *context, struct conf_service *service,
284 struct database_criterion *cl,
285 void (*fun)(void *context, struct database *db))
290 for (p = service->databases; p; p = p->next)
291 if (database_match_criteria(p->settings, service, cl))
302 * c-file-style: "Stroustrup"
303 * indent-tabs-mode: nil
305 * vim: shiftwidth=4 tabstop=8 expandtab