Fix URL with leading http:// not working PAZ-933
[pazpar2-moved-to-github.git] / src / host.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 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 #include <yaz/matchstr.h>
30
31 #include "ppmutex.h"
32 #include "session.h"
33 #include "host.h"
34
35 #include <sys/types.h>
36
37 struct database_hosts {
38     struct host *hosts;
39     YAZ_MUTEX mutex;
40 };
41
42 // Create a new host structure for hostport
43 static struct host *create_host(const char *proxy,
44                                 const char *tproxy,
45                                 iochan_man_t iochan_man)
46 {
47     struct host *host;
48
49     host = xmalloc(sizeof(struct host));
50     host->proxy = 0;
51     host->tproxy = 0;
52     if (proxy && *proxy)
53         host->proxy = xstrdup(proxy);
54     else
55     {
56         assert(tproxy);
57         host->tproxy = xstrdup(tproxy);
58     }
59     host->connections = 0;
60     host->ipport = 0;
61     host->mutex = 0;
62
63     if (host_getaddrinfo(host, iochan_man))
64     {
65         xfree(host->ipport);
66         xfree(host->tproxy);
67         xfree(host->proxy);
68         xfree(host);
69         return 0;
70     }
71     pazpar2_mutex_create(&host->mutex, "host");
72
73     yaz_cond_create(&host->cond_ready);
74
75     return host;
76 }
77
78 struct host *find_host(database_hosts_t hosts, const char *url,
79                        const char *proxy, int port,
80                        iochan_man_t iochan_man)
81 {
82     struct host *p;
83     char *tproxy = 0;
84
85     if (!proxy || !*proxy)
86     {
87         char *cp;
88
89         tproxy = xmalloc (strlen(url) + 10); /* so we can add :port */
90         strcpy(tproxy, url);
91         if (!strncmp(tproxy, "http://", 7))
92             cp = tproxy + 7;
93         else if (!strncmp(tproxy, "https://", 8))
94             cp = tproxy + 8;
95         else
96             cp = tproxy;
97         for (; *cp; cp++)
98             if (strchr("/?#~", *cp))
99             {
100                 *cp = '\0';
101                 break;
102             }
103         if (!strchr(tproxy, ':'))
104             sprintf(cp, ":%d", port); /* no port given, add it */
105     }
106     yaz_mutex_enter(hosts->mutex);
107     for (p = hosts->hosts; p; p = p->next)
108     {
109         if (!yaz_strcmp_null(p->tproxy, tproxy) &&
110             !yaz_strcmp_null(p->proxy, proxy))
111         {
112             break;
113         }
114     }
115     if (!p)
116     {
117         p = create_host(proxy, tproxy, iochan_man);
118         if (p)
119         {
120             p->next = hosts->hosts;
121             hosts->hosts = p;
122         }
123     }
124     yaz_mutex_leave(hosts->mutex);
125     xfree(tproxy);
126     return p;
127 }
128
129 database_hosts_t database_hosts_create(void)
130 {
131     database_hosts_t p = xmalloc(sizeof(*p));
132     p->hosts = 0;
133     p->mutex = 0;
134     pazpar2_mutex_create(&p->mutex, "database");
135     return p;
136 }
137
138 void database_hosts_destroy(database_hosts_t *pp)
139 {
140     if (*pp)
141     {
142         struct host *p = (*pp)->hosts;
143         while (p)
144         {
145             struct host *p_next = p->next;
146             yaz_mutex_destroy(&p->mutex);
147             yaz_cond_destroy(&p->cond_ready);
148             xfree(p->ipport);
149             xfree(p);
150             p = p_next;
151         }
152         yaz_mutex_destroy(&(*pp)->mutex);
153         xfree(*pp);
154     }
155 }
156
157 /*
158  * Local variables:
159  * c-basic-offset: 4
160  * c-file-style: "Stroustrup"
161  * indent-tabs-mode: nil
162  * End:
163  * vim: shiftwidth=4 tabstop=8 expandtab
164  */
165