For Torus record fetch, proxy may be given
[metaproxy-moved-to-github.git] / src / torus.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 Index Data
3
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
7 version.
8
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
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 #include <metaproxy/xmlutil.hpp>
20
21 #include <string.h>
22 #include <yaz/wrbuf.h>
23 #include <yaz/url.h>
24 #include <metaproxy/util.hpp>
25 #include "torus.hpp"
26
27 namespace mp = metaproxy_1;
28
29 xmlDoc *mp::get_searchable(std::string url_template, const std::string &db,
30                            const std::string &proxy)
31 {
32     // http://newmk2.indexdata.com/torus2/searchable.ebsco/records/?query=udb=aberdeenUni
33     xmlDoc *doc = 0;
34     size_t found;
35
36     found = url_template.find("%db");
37     if (found != std::string::npos)
38         url_template.replace(found, found+3, mp::util::uri_encode(db));
39
40     Z_HTTP_Header *http_headers = 0;
41     mp::odr odr;
42     
43     z_HTTP_header_add(odr, &http_headers, "Accept","application/xml");
44
45     yaz_url_t url_p = yaz_url_create();
46     if (proxy.length())
47         yaz_url_set_proxy(url_p, proxy.c_str());
48
49     Z_HTTP_Response *http_response = yaz_url_exec(url_p,
50                                                   url_template.c_str(),
51                                                   "GET",
52                                                   http_headers,
53                                                   0, /* content buf */
54                                                   0  /* content_len */
55         );
56     if (http_response->code == 200 && http_response->content_buf)
57         doc = xmlParseMemory(http_response->content_buf,
58                              http_response->content_len);
59     yaz_url_destroy(url_p);
60     return doc;
61 }
62
63 /*
64  * Local variables:
65  * c-basic-offset: 4
66  * c-file-style: "Stroustrup"
67  * indent-tabs-mode: nil
68  * End:
69  * vim: shiftwidth=4 tabstop=8 expandtab
70  */
71