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>
38 #include <sys/types.h>
40 #include <sys/socket.h>
46 #include <netinet/in.h>
49 static struct host *hosts = 0; /* thread pr */
51 static xmlDoc *get_explain_xml(struct conf_targetprofiles *targetprofiles,
58 if (targetprofiles->type != Targetprofiles_local)
60 yaz_log(YLOG_FATAL, "Only supports local type");
63 dir = targetprofiles->src;
65 sprintf(path, "%s/%s", dir, ide);
67 return xmlParseFile(path);
72 // Create a new host structure for hostport
73 static struct host *create_host(const char *hostport)
77 host = xmalloc(sizeof(struct host));
78 host->hostport = xstrdup(hostport);
79 host->connections = 0;
82 if (host_getaddrinfo(host))
84 xfree(host->hostport);
93 static struct host *find_host(const char *hostport)
96 for (p = hosts; p; p = p->next)
97 if (!strcmp(p->hostport, hostport))
99 return create_host(hostport);
102 int resolve_database(struct database *db)
109 strcpy(hostport, db->url);
110 if ((p = strchr(hostport, '/')))
112 if (!(host = find_host(hostport)))
119 void resolve_databases(struct conf_service *service)
121 struct database *db = service->databases;
122 for (; db; db = db->next)
123 resolve_database(db);
126 static struct database *load_database(const char *id,
127 struct conf_service *service)
130 struct zr_explain *explain = 0;
134 struct setting *idset;
136 yaz_log(YLOG_LOG, "New database: %s", id);
138 if (service->targetprofiles
139 && (doc = get_explain_xml(service->targetprofiles, id)))
141 explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
146 if (strlen(id) > 255)
148 strcpy(hostport, id);
149 if ((dbname = strchr(hostport, '/')))
153 db = nmem_malloc(service->nmem, sizeof(*db));
154 memset(db, 0, sizeof(*db));
156 db->url = nmem_strdup(service->nmem, id);
157 db->databases = nmem_malloc(service->nmem, 2 * sizeof(char *));
158 db->databases[0] = nmem_strdup(service->nmem, dbname);
159 db->databases[1] = 0;
161 db->explain = explain;
165 db->settings = nmem_malloc(service->nmem, sizeof(struct settings*) *
166 settings_num(service));
167 memset(db->settings, 0, sizeof(struct settings*) * settings_num(service));
168 idset = nmem_malloc(service->nmem, sizeof(*idset));
169 idset->precedence = 0;
170 idset->name = "pz:id";
171 idset->target = idset->value = db->url;
173 db->settings[PZ_ID] = idset;
175 db->next = service->databases;
176 service->databases = db;
181 // Return a database structure by ID. Load and add to list if necessary
182 // new==1 just means we know it's not in the list
183 struct database *find_database(const char *id, int new,
184 struct conf_service *service)
189 for (p = service->databases; p; p = p->next)
190 if (!strcmp(p->url, id))
193 return load_database(id, service);
196 // This whole session_grep database thing should be moved elsewhere
198 int match_zurl(const char *zurl, const char *pattern)
202 if (!strcmp(pattern, "*"))
204 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
206 char *db = strchr(zurl, '/');
209 if (!strcmp(pattern + 2, db))
214 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
216 if (!strncmp(pattern, zurl, len))
221 else if (!strcmp(pattern, zurl))
227 // This will be generalized at some point
228 static int match_criterion(struct setting **settings,
229 struct conf_service *service,
230 struct database_criterion *c)
232 int offset = settings_offset(service, c->name);
233 struct database_criterion_value *v;
237 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
240 if (!settings[offset])
242 for (v = c->values; v; v = v->next)
246 if (match_zurl(settings[offset]->value, v->value))
251 if (!strcmp(settings[offset]->value, v->value))
261 int database_match_criteria(struct setting **settings,
262 struct conf_service *service,
263 struct database_criterion *cl)
265 for (; cl; cl = cl->next)
266 if (!match_criterion(settings, service, cl))
268 if (cl) // one of the criteria failed to match -- skip this db
274 // Cycles through databases, calling a handler function on the ones for
275 // which all criteria matched.
276 int session_grep_databases(struct session *se, struct database_criterion *cl,
277 void (*fun)(void *context, struct session_database *db))
279 struct session_database *p;
282 for (p = se->databases; p; p = p->next)
284 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
286 if (!p->settings[PZ_NAME])
288 if (database_match_criteria(p->settings, se->service, cl))
297 int predef_grep_databases(void *context, struct conf_service *service,
298 struct database_criterion *cl,
299 void (*fun)(void *context, struct database *db))
304 for (p = service->databases; p; p = p->next)
305 if (database_match_criteria(p->settings, service, cl))
316 * c-file-style: "Stroustrup"
317 * indent-tabs-mode: nil
319 * vim: shiftwidth=4 tabstop=8 expandtab