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
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,
106 struct conf_service *service)
109 struct zr_explain *explain = 0;
114 struct setting *idset;
116 yaz_log(YLOG_LOG, "New database: %s", id);
118 if (config && config->targetprofiles
119 && (doc = get_explain_xml(id)))
121 explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
126 if (strlen(id) > 255)
128 strcpy(hostport, id);
129 if ((dbname = strchr(hostport, '/')))
133 if (!(host = find_host(hostport)))
135 db = nmem_malloc(service->nmem, sizeof(*db));
136 memset(db, 0, sizeof(*db));
138 db->url = nmem_strdup(service->nmem, id);
139 db->databases = xmalloc(2 * sizeof(char *));
140 db->databases[0] = nmem_strdup(service->nmem, dbname);
141 db->databases[1] = 0;
143 db->explain = explain;
147 db->settings = nmem_malloc(service->nmem, sizeof(struct settings*) *
148 settings_num(service));
149 memset(db->settings, 0, sizeof(struct settings*) * settings_num(service));
150 idset = nmem_malloc(service->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 = service->databases;
158 service->databases = db;
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,
166 struct conf_service *service)
171 for (p = service->databases; p; p = p->next)
172 if (!strcmp(p->url, id))
175 return load_database(id, service);
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,
211 struct conf_service *service,
212 struct database_criterion *c)
214 int offset = settings_offset(service, c->name);
215 struct database_criterion_value *v;
219 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
222 if (!settings[offset])
224 for (v = c->values; v; v = v->next)
228 if (match_zurl(settings[offset]->value, v->value))
233 if (!strcmp(settings[offset]->value, v->value))
243 int database_match_criteria(struct setting **settings,
244 struct conf_service *service,
245 struct database_criterion *cl)
247 for (; cl; cl = cl->next)
248 if (!match_criterion(settings, service, cl))
250 if (cl) // one of the criteria failed to match -- skip this db
256 // Cycles through databases, calling a handler function on the ones for
257 // which all criteria matched.
258 int session_grep_databases(struct session *se, struct database_criterion *cl,
259 void (*fun)(void *context, struct session_database *db))
261 struct session_database *p;
264 for (p = se->databases; p; p = p->next)
266 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
268 if (!p->settings[PZ_NAME])
270 if (database_match_criteria(p->settings, se->service, cl))
279 int predef_grep_databases(void *context, struct conf_service *service,
280 struct database_criterion *cl,
281 void (*fun)(void *context, struct database *db))
286 for (p = service->databases; p; p = p->next)
287 if (database_match_criteria(p->settings, service, cl))
298 * c-file-style: "Stroustrup"
299 * indent-tabs-mode: nil
301 * vim: shiftwidth=4 tabstop=8 expandtab