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;
46 std::map<pid_t,pid_t> children;
54 yf::CGI::CGI() : m_p(new Rep)
61 std::map<pid_t,pid_t>::const_iterator it;
62 boost::mutex::scoped_lock lock(m_mutex);
64 for (it = children.begin(); it != children.end(); it++)
65 kill(it->second, SIGTERM);
72 void yf::CGI::process(mp::Package &package) const
74 Z_GDU *zgdu_req = package.request().get();
80 if (zgdu_req->which != Z_GDU_HTTP_Request)
85 std::string path_info;
86 std::string query_string;
87 const char *path = zgdu_req->u.HTTP_Request->path;
88 yaz_log(YLOG_LOG, "path=%s", path);
89 const char *p_cp = strchr(path, '?');
92 path_info.assign(path, p_cp - path);
93 query_string.assign(p_cp+1);
96 path_info.assign(path);
98 std::list<CGI::Exec>::const_iterator it;
100 for (it = m_p->exec_map.begin(); it != m_p->exec_map.end(); it++)
102 if (it->path.compare(path_info) == 0)
112 setenv("PATH_INFO", path_info.c_str(), 1);
113 setenv("QUERY_STRING", query_string.c_str(), 1);
114 r = execl(it->program.c_str(), it->program.c_str(), (char *) 0);
120 zgdu_res = odr.create_HTTP_Response(
121 package.session(), zgdu_req->u.HTTP_Request, 400);
122 package.response() = zgdu_res;
124 default: /* parent */
127 boost::mutex::scoped_lock lock(m_p->m_mutex);
128 m_p->children[pid] = pid;
130 waitpid(pid, &status, 0);
134 boost::mutex::scoped_lock lock(m_p->m_mutex);
135 m_p->children.erase(pid);
137 zgdu_res = odr.create_HTTP_Response(
138 package.session(), zgdu_req->u.HTTP_Request, 200);
139 package.response() = zgdu_res;
148 void yf::CGI::configure(const xmlNode *ptr, bool test_only)
150 for (ptr = ptr->children; ptr; ptr = ptr->next)
152 if (ptr->type != XML_ELEMENT_NODE)
154 if (!strcmp((const char *) ptr->name, "map"))
158 const struct _xmlAttr *attr;
159 for (attr = ptr->properties; attr; attr = attr->next)
161 if (!strcmp((const char *) attr->name, "path"))
162 exec.path = mp::xml::get_text(attr->children);
163 else if (!strcmp((const char *) attr->name, "exec"))
164 exec.program = mp::xml::get_text(attr->children);
166 throw mp::filter::FilterException
168 + std::string((const char *) attr->name)
169 + " in cgi section");
171 m_p->exec_map.push_back(exec);
175 throw mp::filter::FilterException("Bad element "
176 + std::string((const char *)
182 static mp::filter::Base* filter_creator()
184 return new mp::filter::CGI;
188 struct metaproxy_1_filter_struct metaproxy_1_filter_cgi = {
199 * c-file-style: "Stroustrup"
200 * indent-tabs-mode: nil
202 * vim: shiftwidth=4 tabstop=8 expandtab