Re-enable threaded host resolving
[pazpar2-moved-to-github.git] / src / database.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2011 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <assert.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <yaz/log.h>
28 #include <yaz/nmem.h>
29
30 #include "ppmutex.h"
31 #include "session.h"
32 #include "host.h"
33 #include "pazpar2_config.h"
34 #include "settings.h"
35 #include "http.h"
36 #include "database.h"
37
38 #include <sys/types.h>
39
40 enum pazpar2_database_criterion_type {
41     PAZPAR2_STRING_MATCH,
42     PAZPAR2_SUBSTRING_MATCH
43 };
44
45 struct database_criterion_value {
46     char *value;
47     struct database_criterion_value *next;
48 };
49
50 struct database_criterion {
51     char *name;
52     enum pazpar2_database_criterion_type type;
53     struct database_criterion_value *values;
54     struct database_criterion *next;
55 };
56
57 struct database *new_database(const char *id, NMEM nmem)
58 {
59     struct database *db;
60     struct setting *idset;
61
62     db = nmem_malloc(nmem, sizeof(*db));
63     db->id = nmem_strdup(nmem, id);
64     db->num_settings = PZ_MAX_EOF;
65     db->settings = nmem_malloc(nmem, sizeof(struct settings*) * 
66                                db->num_settings);
67     db->next = 0;
68     memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
69     idset = nmem_malloc(nmem, sizeof(*idset));
70     idset->precedence = 0;
71     idset->name = "pz:id";
72     idset->target = idset->value = db->id;
73     idset->next = 0;
74     db->settings[PZ_ID] = idset;
75
76     return db;
77 }
78
79 // Return a database structure by ID. Load and add to list if necessary
80 // new==1 just means we know it's not in the list
81 struct database *create_database_for_service(const char *id,
82                                              struct conf_service *service)
83 {
84     struct database *p;
85     for (p = service->databases; p; p = p->next)
86         if (!strcmp(p->id, id))
87             return p;
88     
89     p = new_database(id, service->nmem);
90     
91     p->next = service->databases;
92     service->databases = p;
93
94     return p;
95 }
96
97 // This whole session_grep database thing should be moved elsewhere
98
99 int match_zurl(const char *zurl, const char *pattern)
100 {
101     int len;
102
103     if (!strcmp(pattern, "*"))
104         return 1;
105     else if (!strncmp(pattern, "*/", 2))   // host wildcard.. what the heck is that for?
106     {
107         char *db = strchr(zurl, '/');
108         if (!db)
109             return 0;
110         if (!strcmp(pattern + 2, db))
111             return 1;
112         else
113             return 0;
114     }
115     else if (*(pattern + (len = strlen(pattern) - 1)) == '*')  // db wildcard
116     {
117         if (!strncmp(pattern, zurl, len))
118             return 1;
119         else
120             return 2;
121     }
122     else if (!strcmp(pattern, zurl))
123         return 1;
124     else
125         return 0;
126 }
127
128 // This will be generalized at some point
129 static int match_criterion(struct setting **settings,
130                            struct conf_service *service, 
131                            struct database_criterion *c)
132 {
133     int offset = settings_lookup_offset(service, c->name);
134     struct database_criterion_value *v;
135
136     if (offset < 0)
137     {
138         yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
139         return 0;
140     }
141     if (!settings[offset])
142         return 0;
143     for (v = c->values; v; v = v->next)
144     {
145         if (c->type == PAZPAR2_STRING_MATCH)
146         {
147             if (offset == PZ_ID)
148             {
149                 if (match_zurl(settings[offset]->value, v->value))
150                     break;
151             }
152             else 
153             {
154                 if (!strcmp(settings[offset]->value, v->value))
155                     break;
156             }
157         }            
158         else if (c->type == PAZPAR2_SUBSTRING_MATCH)
159         {
160             if (strstr(settings[offset]->value, v->value))
161                 break;
162         }
163     }
164     if (v)
165         return 1;
166     else
167         return 0;
168 }
169
170 // parses crit1=val1,crit2=val2|val3,...
171 static struct database_criterion *create_database_criterion(NMEM m,
172                                                             const char *buf)
173 {
174     struct database_criterion *res = 0;
175     char **values;
176     int num;
177     int i;
178
179     if (!buf || !*buf)
180         return 0;
181     nmem_strsplit(m, ",", buf,  &values, &num);
182     for (i = 0; i < num; i++)
183     {
184         char **subvalues;
185         int subnum;
186         int subi;
187         struct database_criterion *new = nmem_malloc(m, sizeof(*new));
188         char *eq;
189         if ((eq = strchr(values[i], '=')))
190             new->type = PAZPAR2_STRING_MATCH;
191         else if ((eq = strchr(values[i], '~')))
192             new->type = PAZPAR2_SUBSTRING_MATCH;
193         else
194         {
195             yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
196             return 0;
197         }
198         *(eq++) = '\0';
199         new->name = values[i];
200         nmem_strsplit(m, "|", eq, &subvalues, &subnum);
201         new->values = 0;
202         for (subi = 0; subi < subnum; subi++)
203         {
204             struct database_criterion_value *newv
205                 = nmem_malloc(m, sizeof(*newv));
206             newv->value = subvalues[subi];
207             newv->next = new->values;
208             new->values = newv;
209         }
210         new->next = res;
211         res = new;
212     }
213     return res;
214 }
215
216 static int database_match_criteria(struct setting **settings,
217                                    struct conf_service *service,
218                                    struct database_criterion *cl)
219 {
220     for (; cl; cl = cl->next)
221         if (!match_criterion(settings, service, cl))
222             break;
223     if (cl) // one of the criteria failed to match -- skip this db
224         return 0;
225     else
226         return 1;
227 }
228
229 // Cycles through databases, calling a handler function on the ones for
230 // which all criteria matched.
231 int session_grep_databases(struct session *se, const char *filter,
232                            void (*fun)(struct session *se, struct session_database *db))
233 {
234     struct session_database *p;
235     NMEM nmem = nmem_create();
236     int i = 0;
237     struct database_criterion *cl = create_database_criterion(nmem, filter);
238
239     for (p = se->databases; p; p = p->next)
240     {
241         if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
242             continue;
243         if (!p->settings[PZ_NAME])
244             continue;
245         if (database_match_criteria(p->settings, se->service, cl))
246         {
247             (*fun)(se, p);
248             i++;
249         }
250     }
251     nmem_destroy(nmem);
252     return i;
253 }
254
255 int predef_grep_databases(void *context, struct conf_service *service,
256                           void (*fun)(void *context, struct database *db))
257 {
258     struct database *p;
259     int i = 0;
260
261     for (p = service->databases; p; p = p->next)
262         if (database_match_criteria(p->settings, service, 0))
263         {
264             (*fun)(context, p);
265             i++;
266         }
267     return i;
268 }
269
270 /*
271  * Local variables:
272  * c-basic-offset: 4
273  * c-file-style: "Stroustrup"
274  * indent-tabs-mode: nil
275  * End:
276  * vim: shiftwidth=4 tabstop=8 expandtab
277  */
278