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(struct conf_targetprofiles *targetprofiles,
57 if (targetprofiles->type != Targetprofiles_local)
59 yaz_log(YLOG_FATAL, "Only supports local type");
62 dir = targetprofiles->src;
64 sprintf(path, "%s/%s", dir, ide);
66 return xmlParseFile(path);
71 // Create a new host structure for hostport
72 static struct host *create_host(const char *hostport)
76 host = xmalloc(sizeof(struct host));
77 host->hostport = xstrdup(hostport);
78 host->connections = 0;
81 if (host_getaddrinfo(host))
83 xfree(host->hostport);
92 static struct host *find_host(const char *hostport)
95 for (p = hosts; p; p = p->next)
96 if (!strcmp(p->hostport, hostport))
98 return create_host(hostport);
101 static struct database *load_database(const char *id,
102 struct conf_service *service)
105 struct zr_explain *explain = 0;
110 struct setting *idset;
112 yaz_log(YLOG_LOG, "New database: %s", id);
114 if (service->targetprofiles
115 && (doc = get_explain_xml(service->targetprofiles, id)))
117 explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
122 if (strlen(id) > 255)
124 strcpy(hostport, id);
125 if ((dbname = strchr(hostport, '/')))
129 if (!(host = find_host(hostport)))
131 db = nmem_malloc(service->nmem, sizeof(*db));
132 memset(db, 0, sizeof(*db));
134 db->url = nmem_strdup(service->nmem, id);
135 db->databases = xmalloc(2 * sizeof(char *));
136 db->databases[0] = nmem_strdup(service->nmem, dbname);
137 db->databases[1] = 0;
139 db->explain = explain;
143 db->settings = nmem_malloc(service->nmem, sizeof(struct settings*) *
144 settings_num(service));
145 memset(db->settings, 0, sizeof(struct settings*) * settings_num(service));
146 idset = nmem_malloc(service->nmem, sizeof(*idset));
147 idset->precedence = 0;
148 idset->name = "pz:id";
149 idset->target = idset->value = db->url;
151 db->settings[PZ_ID] = idset;
153 db->next = service->databases;
154 service->databases = db;
159 // Return a database structure by ID. Load and add to list if necessary
160 // new==1 just means we know it's not in the list
161 struct database *find_database(const char *id, int new,
162 struct conf_service *service)
167 for (p = service->databases; p; p = p->next)
168 if (!strcmp(p->url, id))
171 return load_database(id, service);
174 // This whole session_grep database thing should be moved elsewhere
176 int match_zurl(const char *zurl, const char *pattern)
180 if (!strcmp(pattern, "*"))
182 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
184 char *db = strchr(zurl, '/');
187 if (!strcmp(pattern + 2, db))
192 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
194 if (!strncmp(pattern, zurl, len))
199 else if (!strcmp(pattern, zurl))
205 // This will be generalized at some point
206 static int match_criterion(struct setting **settings,
207 struct conf_service *service,
208 struct database_criterion *c)
210 int offset = settings_offset(service, c->name);
211 struct database_criterion_value *v;
215 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
218 if (!settings[offset])
220 for (v = c->values; v; v = v->next)
224 if (match_zurl(settings[offset]->value, v->value))
229 if (!strcmp(settings[offset]->value, v->value))
239 int database_match_criteria(struct setting **settings,
240 struct conf_service *service,
241 struct database_criterion *cl)
243 for (; cl; cl = cl->next)
244 if (!match_criterion(settings, service, 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, se->service, cl))
275 int predef_grep_databases(void *context, struct conf_service *service,
276 struct database_criterion *cl,
277 void (*fun)(void *context, struct database *db))
282 for (p = service->databases; p; p = p->next)
283 if (database_match_criteria(p->settings, service, cl))
294 * c-file-style: "Stroustrup"
295 * indent-tabs-mode: nil
297 * vim: shiftwidth=4 tabstop=8 expandtab