1 /* $Id: filter_sru_to_z3950.cpp,v 1.2 2006-09-13 14:56:07 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;
43 void process(metaproxy_1::Package & package) const;
49 yf::SRUtoZ3950::SRUtoZ3950() : m_p(new Rep)
54 yf::SRUtoZ3950::~SRUtoZ3950()
55 { // must have a destructor because of boost::scoped_ptr
58 void yf::SRUtoZ3950::process(mp::Package &package) const
60 //std::cout << m_p->protocol_type(package) << "\n";
61 //m_p->debug_http_headers(package);
62 m_p->process(package);
65 void yf::SRUtoZ3950::Rep::process(mp::Package &package) const
67 Z_GDU *zgdu_req = package.request().get();
69 // ignoring all non HTTP_Request packages
70 if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
75 // only working on HTTP_Request packages now
76 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
78 // TODO: SRU package checking and translation to Z3950 package
82 // int ret_code = 2; /* 2=NOT TAKEN, 1=TAKEN, 0=SOAP TAKEN */
83 // Z_SRW_PDU *sru_res = 0;
84 // Z_SOAP *soap_package = 0;
86 // Z_SRW_diagnostic *diagnostic = 0;
87 // int num_diagnostic = 0;
90 // if (!(ret_code = yaz_sru_decode(http_req, &sru_res,
91 // &soap_package, odr, &charset,
92 // &diagnostic, &num_diagnostic)))
94 // protocol = "SRU GET/POST";
96 // else if (!(ret_code = yaz_srw_decode(http_req, &sru_res,
97 // &soap_package, odr, &charset)))
99 // protocol = "SRU SOAP";
103 // protocol = "HTTP";
106 std::string protocol("HTTP");
108 const std::string mime_text_xml("text/xml");
109 const std::string mime_soap_xml("application/soap+xml");
110 const std::string mime_urlencoded("application/x-www-form-urlencoded");
112 const std::string http_method(http_req->method);
113 const std::string http_type(z_HTTP_header_lookup(http_req->headers,
117 if (http_method == "GET")
118 protocol = "SRU GET";
119 else if ( http_method == "POST"
120 && http_type == mime_urlencoded)
121 protocol = "SRU POST";
122 else if ( http_method == "POST"
123 && (http_type == mime_text_xml
124 || http_type == mime_soap_xml))
125 protocol = "SRU SOAP";
127 std::cout << "SRUtoZ3950 " << protocol << "\n";
134 std::string message("<html>\n<body>\n<h1>"
135 "Metaproxy SRUtoZ3950 filter"
138 message += "<h3>HTTP Info</h3><br/>\n";
140 message += "<b>Method: </b> " + std::string(http_req->method) + "<br/>\n";
141 message += "<b>Version:</b> " + std::string(http_req->version) + "<br/>\n";
142 message += "<b>Path: </b> " + std::string(http_req->path) + "<br/>\n";
143 message += "<b>Content-Type:</b>"
144 + std::string(z_HTTP_header_lookup(http_req->headers, "Content-Type"))
146 message += "<b>Content-Length:</b>"
147 + std::string(z_HTTP_header_lookup(http_req->headers,
152 message += "<h3>Headers</h3><br/>\n";
154 Z_HTTP_Header* header = http_req->headers;
156 message += "<b>Header: </b> <i>"
157 + std::string(header->name) + ":</i> "
158 + std::string(header->value) + "<br/>\n";
159 header = header->next;
162 message += "</body>\n</html>\n";
164 //std::cout << message << "\n";
168 // sending Z3950 package through pipeline
172 // TODO: Z3950 response parsing and translation to SRU package
173 //Z_HTTP_Response* http_res = 0;
179 = odr.create_HTTP_Response(package.session(),
180 zgdu_req->u.HTTP_Request, 200);
183 // zgdu_res->u.HTTP_Response->content_len = message.size();
184 // zgdu_res->u.HTTP_Response->content_buf
185 // = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
187 // strncpy(zgdu_res->u.HTTP_Response->content_buf,
188 // message.c_str(), zgdu_res->u.HTTP_Response->content_len);
190 // z_HTTP_header_add(o, &hres->headers,
191 // "Content-Type", content_type.c_str());
193 package.response() = zgdu_res;
198 static mp::filter::Base* filter_creator()
200 return new mp::filter::SRUtoZ3950;
204 struct metaproxy_1_filter_struct metaproxy_1_filter_sru_to_z3950 = {
215 * indent-tabs-mode: nil
216 * c-file-style: "stroustrup"
218 * vim: shiftwidth=4 tabstop=8 expandtab