1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2011 Index Data
4 Metaproxy 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 Metaproxy 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
19 #include <metaproxy/xmlutil.hpp>
22 #include <yaz/wrbuf.h>
25 #include <yaz/comstack.h>
29 namespace mp = metaproxy_1;
32 static Z_GDU *get_HTTP_Request_url(ODR odr, const char *url)
34 Z_GDU *p = z_get_HTTP_Request(odr);
35 const char *host = url;
36 const char *cp0 = strstr(host, "://");
43 cp1 = strchr(cp0, '/');
45 cp1 = cp0 + strlen(cp0);
49 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
50 memcpy (h, cp0, cp1 - cp0);
52 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, "Host", h);
54 p->u.HTTP_Request->path = odr_strdup(odr, *cp1 ? cp1 : "/");
58 static WRBUF get_url(const char *uri, WRBUF username, WRBUF password,
61 int number_of_redirects = 0;
63 ODR out = odr_createmem(ODR_ENCODE);
64 ODR in = odr_createmem(ODR_DECODE);
68 Z_HTTP_Response *res = 0;
69 const char *location = 0;
70 Z_GDU *gdu = get_HTTP_Request_url(out, uri);
71 yaz_log(YLOG_LOG, "GET %s", uri);
72 gdu->u.HTTP_Request->method = odr_strdup(out, "GET");
73 if (username && password)
75 z_HTTP_header_add_basic_auth(out, &gdu->u.HTTP_Request->headers,
77 wrbuf_cstr(password));
79 z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers, "Accept",
81 if (!z_GDU(out, &gdu, 0, 0))
83 yaz_log(YLOG_WARN, "Can not encode HTTP request URL:%s", uri);
87 COMSTACK conn = cs_create_host(uri, 1, &add);
89 yaz_log(YLOG_WARN, "Bad address for URL:%s", uri);
90 else if (cs_connect(conn, add) < 0)
91 yaz_log(YLOG_WARN, "Can not connect to URL:%s", uri);
95 char *buf = odr_getbuf(out, &len, 0);
97 if (cs_put(conn, buf, len) < 0)
98 yaz_log(YLOG_WARN, "cs_put failed URL:%s", uri);
103 int cs_res = cs_get(conn, &netbuffer, &netlen);
106 yaz_log(YLOG_WARN, "cs_get failed URL:%s", uri);
111 odr_setbuf(in, netbuffer, cs_res, 0);
112 if (!z_GDU(in, &gdu, 0, 0)
113 || gdu->which != Z_GDU_HTTP_Response)
115 yaz_log(YLOG_WARN, "HTTP decoding failed "
120 res = gdu->u.HTTP_Response;
130 location = z_HTTP_header_lookup(res->headers, "Location");
131 if (++number_of_redirects < 10 &&
132 location && (*code == 301 || *code == 302 || *code == 307))
135 uri = odr_strdup(out, location);
140 result = wrbuf_alloc();
141 wrbuf_write(result, res->content_buf, res->content_len);
162 void mp::Torus::read_searchables(std::string url)
169 if (url.length() == 0)
173 WRBUF w = get_url(url.c_str(), 0, 0, &code);
176 doc = xmlParseMemory(wrbuf_buf(w), wrbuf_len(w));
178 yaz_log(YLOG_LOG, "xmlParseMemory OK");
183 xmlDoc *mp::Torus::get_doc()
191 * c-file-style: "Stroustrup"
192 * indent-tabs-mode: nil
194 * vim: shiftwidth=4 tabstop=8 expandtab