1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2010 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 "filter_cgi.hpp"
20 #include <metaproxy/package.hpp>
21 #include <metaproxy/util.hpp>
22 #include "gduutil.hpp"
32 namespace mp = metaproxy_1;
33 namespace yf = mp::filter;
35 namespace metaproxy_1 {
45 std::list<CGI::Exec> exec_map;
50 yf::CGI::CGI() : m_p(new Rep)
56 { // must have a destructor because of boost::scoped_ptr
59 void yf::CGI::process(mp::Package &package) const
61 Z_GDU *zgdu_req = package.request().get();
67 if (zgdu_req->which != Z_GDU_HTTP_Request)
72 std::string path_info;
73 std::string query_string;
74 const char *path = zgdu_req->u.HTTP_Request->path;
75 yaz_log(YLOG_LOG, "path=%s", path);
76 const char *p_cp = strchr(path, '?');
79 path_info.assign(path, p_cp - path);
80 query_string.assign(p_cp+1);
83 path_info.assign(path);
85 std::list<CGI::Exec>::const_iterator it;
87 for (it = m_p->exec_map.begin(); it != m_p->exec_map.end(); it++)
89 if (it->path.compare(path_info) == 0)
99 setenv("PATH_INFO", path_info.c_str(), 1);
100 setenv("QUERY_STRING", query_string.c_str(), 1);
101 r = execl(it->program.c_str(), it->program.c_str(), (char *) 0);
107 zgdu_res = odr.create_HTTP_Response(
108 package.session(), zgdu_req->u.HTTP_Request, 400);
109 package.response() = zgdu_res;
111 default: /* parent */
112 waitpid(pid, &status, 0);
114 zgdu_res = odr.create_HTTP_Response(
115 package.session(), zgdu_req->u.HTTP_Request, 200);
116 package.response() = zgdu_res;
125 void yf::CGI::configure(const xmlNode *ptr, bool test_only)
127 for (ptr = ptr->children; ptr; ptr = ptr->next)
129 if (ptr->type != XML_ELEMENT_NODE)
131 if (!strcmp((const char *) ptr->name, "map"))
135 const struct _xmlAttr *attr;
136 for (attr = ptr->properties; attr; attr = attr->next)
138 if (!strcmp((const char *) attr->name, "path"))
139 exec.path = mp::xml::get_text(attr->children);
140 else if (!strcmp((const char *) attr->name, "exec"))
141 exec.program = mp::xml::get_text(attr->children);
143 throw mp::filter::FilterException
145 + std::string((const char *) attr->name)
146 + " in cgi section");
148 m_p->exec_map.push_back(exec);
152 throw mp::filter::FilterException("Bad element "
153 + std::string((const char *)
159 static mp::filter::Base* filter_creator()
161 return new mp::filter::CGI;
165 struct metaproxy_1_filter_struct metaproxy_1_filter_cgi = {
176 * c-file-style: "Stroustrup"
177 * indent-tabs-mode: nil
179 * vim: shiftwidth=4 tabstop=8 expandtab