1 /* This file is part of Pazpar2.
2 Copyright (C) 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
25 #include <sys/types.h>
29 #include <yaz/matchstr.h>
35 #include <sys/types.h>
37 struct database_hosts {
42 // Create a new host structure for hostport
43 static struct host *create_host(const char *proxy,
45 iochan_man_t iochan_man)
49 host = xmalloc(sizeof(struct host));
53 host->proxy = xstrdup(proxy);
57 host->tproxy = xstrdup(tproxy);
59 host->connections = 0;
64 if (host_getaddrinfo(host, iochan_man))
72 pazpar2_mutex_create(&host->mutex, "host");
74 yaz_cond_create(&host->cond_ready);
79 struct host *find_host(database_hosts_t hosts, const char *url,
80 const char *proxy, int port,
81 iochan_man_t iochan_man)
86 if (!proxy || !*proxy)
90 tproxy = xmalloc (strlen(url) + 10); /* so we can add :port */
92 if (!strncmp(tproxy, "http://", 7))
94 else if (!strncmp(tproxy, "https://", 8))
99 if (strchr("/?#~", *cp))
104 if (!strchr(tproxy, ':'))
105 sprintf(cp, ":%d", port); /* no port given, add it */
107 yaz_mutex_enter(hosts->mutex);
108 for (p = hosts->hosts; p; p = p->next)
110 if (!yaz_strcmp_null(p->tproxy, tproxy) &&
111 !yaz_strcmp_null(p->proxy, proxy))
118 p = create_host(proxy, tproxy, iochan_man);
121 p->next = hosts->hosts;
125 if (p && p->error) /* already resolved error */
127 yaz_mutex_leave(hosts->mutex);
132 database_hosts_t database_hosts_create(void)
134 database_hosts_t p = xmalloc(sizeof(*p));
137 pazpar2_mutex_create(&p->mutex, "database");
141 void database_hosts_destroy(database_hosts_t *pp)
145 struct host *p = (*pp)->hosts;
148 struct host *p_next = p->next;
149 yaz_mutex_destroy(&p->mutex);
150 yaz_cond_destroy(&p->cond_ready);
155 yaz_mutex_destroy(&(*pp)->mutex);
163 * c-file-style: "Stroustrup"
164 * indent-tabs-mode: nil
166 * vim: shiftwidth=4 tabstop=8 expandtab