1 /* $Id: filter_sru_to_z3950.cpp,v 1.3 2006-09-13 21:49:34 marc Exp $
2 Copyright (c) 2005-2006, Index Data.
4 See the LICENSE file for details
11 #include "filter_sru_to_z3950.hpp"
16 #include <boost/thread/mutex.hpp>
23 namespace mp = metaproxy_1;
24 namespace yf = mp::filter;
30 std::string stringify(const T& x)
39 namespace metaproxy_1 {
41 class SRUtoZ3950::Rep {
42 //friend class SRUtoZ3950;
44 void configure(const xmlNode *xmlnode);
45 void process(metaproxy_1::Package &package) const;
47 std::string protocol_type(const Z_HTTP_Request &http_req) const;
48 std::string debug_http(const Z_HTTP_Request &http_req) const;
49 void http_response(metaproxy_1::Package &package,
50 const std::string &content,
51 int http_code = 200) const;
56 yf::SRUtoZ3950::SRUtoZ3950() : m_p(new Rep)
60 yf::SRUtoZ3950::~SRUtoZ3950()
61 { // must have a destructor because of boost::scoped_ptr
64 void yf::SRUtoZ3950::configure(const xmlNode *xmlnode)
66 m_p->configure(xmlnode);
69 void yf::SRUtoZ3950::process(mp::Package &package) const
71 m_p->process(package);
74 void yf::SRUtoZ3950::Rep::configure(const xmlNode *xmlnode)
78 void yf::SRUtoZ3950::Rep::process(mp::Package &package) const
80 Z_GDU *zgdu_req = package.request().get();
82 // ignoring all non HTTP_Request packages
83 if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
91 // only working on HTTP_Request packages now
92 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
94 // TODO: SRU package checking and translation to Z3950 package
96 std::cout << protocol_type(*http_req) << "\n";
103 // int ret_code = 2; /* 2=NOT TAKEN, 1=TAKEN, 0=SOAP TAKEN */
104 // Z_SRW_PDU *sru_res = 0;
105 // Z_SOAP *soap_package = 0;
106 // char *charset = 0;
107 // Z_SRW_diagnostic *diagnostic = 0;
108 // int num_diagnostic = 0;
111 // ret_code = yaz_sru_decode(http_req, &sru_res,
112 // &soap_package, odr, &charset,
113 // &diagnostic, &num_diagnostic)))
115 // ret_code = yaz_srw_decode(http_req, &sru_res,
116 // &soap_package, odr, &charset)))
121 // sending Z3950 package through pipeline
125 // TODO: Z3950 response parsing and translation to SRU package
126 Z_HTTP_Response* http_res = 0;
128 std::string content = debug_http(*http_req);
131 http_response(package, content, http_code);
133 //package.response() = zgdu_res;
137 yf::SRUtoZ3950::Rep::protocol_type(const Z_HTTP_Request &http_req) const
139 std::string protocol("HTTP");
141 const std::string mime_text_xml("text/xml");
142 const std::string mime_soap_xml("application/soap+xml");
143 const std::string mime_urlencoded("application/x-www-form-urlencoded");
145 const std::string http_method(http_req.method);
147 //const std::string http_type(z_HTTP_header_lookup(http_req.headers,
149 // TODO: there is a sude condition in the above, fix it in YAZ
150 const std::string http_type("xyz");
152 if (http_method == "GET")
155 if (http_method == "POST"
156 && http_type == mime_urlencoded)
159 if ( http_method == "POST"
160 && (http_type == mime_text_xml
161 || http_type == mime_soap_xml))
168 yf::SRUtoZ3950::Rep::debug_http(const Z_HTTP_Request &http_req) const
170 std::string message("<html>\n<body>\n<h1>"
171 "Metaproxy SRUtoZ3950 filter"
174 message += "<h3>HTTP Info</h3><br/>\n";
176 message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
177 message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
178 message += "<b>Path: </b> " + std::string(http_req.path) + "<br/>\n";
179 message += "<b>Content-Type:</b>"
180 + std::string(z_HTTP_header_lookup(http_req.headers, "Content-Type"))
182 message += "<b>Content-Length:</b>"
183 + std::string(z_HTTP_header_lookup(http_req.headers,
188 message += "<h3>Headers</h3><br/>\n";
190 Z_HTTP_Header* header = http_req.headers;
192 message += "<b>Header: </b> <i>"
193 + std::string(header->name) + ":</i> "
194 + std::string(header->value) + "<br/>\n";
195 header = header->next;
198 message += "</body>\n</html>\n";
202 void yf::SRUtoZ3950::Rep::http_response(metaproxy_1::Package &package,
203 const std::string &content,
207 Z_GDU *zgdu_req = package.request().get();
211 = odr.create_HTTP_Response(package.session(),
212 zgdu_req->u.HTTP_Request,
215 zgdu_res->u.HTTP_Response->content_len = content.size();
216 zgdu_res->u.HTTP_Response->content_buf
217 = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
219 strncpy(zgdu_res->u.HTTP_Response->content_buf,
220 content.c_str(), zgdu_res->u.HTTP_Response->content_len);
222 //z_HTTP_header_add(odr, &hres->headers,
223 // "Content-Type", content_type.c_str());
224 package.response() = zgdu_res;
230 static mp::filter::Base* filter_creator()
232 return new mp::filter::SRUtoZ3950;
236 struct metaproxy_1_filter_struct metaproxy_1_filter_sru_to_z3950 = {
247 * indent-tabs-mode: nil
248 * c-file-style: "stroustrup"
250 * vim: shiftwidth=4 tabstop=8 expandtab