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 db->num_settings = settings_num(service);
168 memset(db->settings, 0, sizeof(struct settings*) * settings_num(service));
169 idset = nmem_malloc(service->nmem, sizeof(*idset));
170 idset->precedence = 0;
171 idset->name = "pz:id";
172 idset->target = idset->value = db->url;
174 db->settings[PZ_ID] = idset;
176 db->next = service->databases;
177 service->databases = db;
182 // Return a database structure by ID. Load and add to list if necessary
183 // new==1 just means we know it's not in the list
184 struct database *find_database(const char *id, int new,
185 struct conf_service *service)
190 for (p = service->databases; p; p = p->next)
191 if (!strcmp(p->url, id))
194 return load_database(id, service);
197 // This whole session_grep database thing should be moved elsewhere
199 int match_zurl(const char *zurl, const char *pattern)
203 if (!strcmp(pattern, "*"))
205 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
207 char *db = strchr(zurl, '/');
210 if (!strcmp(pattern + 2, db))
215 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
217 if (!strncmp(pattern, zurl, len))
222 else if (!strcmp(pattern, zurl))
228 // This will be generalized at some point
229 static int match_criterion(struct setting **settings,
230 struct conf_service *service,
231 struct database_criterion *c)
233 int offset = settings_offset(service, c->name);
234 struct database_criterion_value *v;
238 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
241 if (!settings[offset])
243 for (v = c->values; v; v = v->next)
245 if (c->type == PAZPAR2_STRING_MATCH)
249 if (match_zurl(settings[offset]->value, v->value))
254 if (!strcmp(settings[offset]->value, v->value))
258 else if (c->type == PAZPAR2_SUBSTRING_MATCH)
260 if (strstr(settings[offset]->value, v->value))
270 int database_match_criteria(struct setting **settings,
271 struct conf_service *service,
272 struct database_criterion *cl)
274 for (; cl; cl = cl->next)
275 if (!match_criterion(settings, service, cl))
277 if (cl) // one of the criteria failed to match -- skip this db
283 // Cycles through databases, calling a handler function on the ones for
284 // which all criteria matched.
285 int session_grep_databases(struct session *se, struct database_criterion *cl,
286 void (*fun)(void *context, struct session_database *db))
288 struct session_database *p;
291 for (p = se->databases; p; p = p->next)
293 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
295 if (!p->settings[PZ_NAME])
297 if (database_match_criteria(p->settings, se->service, cl))
306 int predef_grep_databases(void *context, struct conf_service *service,
307 struct database_criterion *cl,
308 void (*fun)(void *context, struct database *db))
313 for (p = service->databases; p; p = p->next)
314 if (database_match_criteria(p->settings, service, cl))
325 * c-file-style: "Stroustrup"
326 * indent-tabs-mode: nil
328 * vim: shiftwidth=4 tabstop=8 expandtab