1 /* This file is part of Metaproxy.
2 Copyright (C) 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"
33 namespace mp = metaproxy_1;
34 namespace yf = mp::filter;
36 namespace metaproxy_1 {
46 std::list<CGI::Exec> exec_map;
47 std::map<std::string,std::string> env_map;
48 std::map<pid_t,pid_t> children;
56 yf::CGI::CGI() : m_p(new Rep)
63 std::map<pid_t,pid_t>::const_iterator it;
64 boost::mutex::scoped_lock lock(m_mutex);
66 for (it = children.begin(); it != children.end(); it++)
67 kill(it->second, SIGTERM);
74 void yf::CGI::process(mp::Package &package) const
76 Z_GDU *zgdu_req = package.request().get();
82 if (zgdu_req->which != Z_GDU_HTTP_Request)
87 std::string path_info;
88 std::string query_string;
89 const char *path = zgdu_req->u.HTTP_Request->path;
90 yaz_log(YLOG_LOG, "path=%s", path);
91 const char *p_cp = strchr(path, '?');
94 path_info.assign(path, p_cp - path);
95 query_string.assign(p_cp+1);
98 path_info.assign(path);
100 std::list<CGI::Exec>::const_iterator it;
101 metaproxy_1::odr odr;
102 for (it = m_p->exec_map.begin(); it != m_p->exec_map.end(); it++)
104 if (it->path.compare(0, it->path.length(), path_info) == 0)
110 const char *program_cstr = it->program.c_str();
111 std::map<std::string,std::string>::const_iterator it;
116 zgdu_res = odr.create_HTTP_Response(
117 package.session(), zgdu_req->u.HTTP_Request, 400);
118 package.response() = zgdu_res;
127 setenv("SCRIPT_NAME", path_info.c_str(), 1);
128 setenv("PATH_INFO", "", 1);
129 setenv("QUERY_STRING", query_string.c_str(), 1);
130 for (it = m_p->env_map.begin(); it != m_p->env_map.end(); it++)
131 setenv(it->first.c_str(), it->second.c_str(), 1);
135 char *cp1 = xstrdup(program_cstr);
136 char *cp2 = strrchr(cp1, '/');
145 r = execl(program_cstr, program_cstr, (char *) 0);
153 zgdu_res = odr.create_HTTP_Response(
154 package.session(), zgdu_req->u.HTTP_Request, 400);
155 package.response() = zgdu_res;
157 default: /* parent */
161 boost::mutex::scoped_lock lock(m_p->m_mutex);
162 m_p->children[pid] = pid;
164 WRBUF w = wrbuf_alloc();
165 wrbuf_puts(w, "HTTP/1.1 200 OK\r\n");
169 ssize_t rd = read(fds[0], buf, sizeof buf);
172 wrbuf_write(w, buf, rd);
175 waitpid(pid, &status, 0);
179 boost::mutex::scoped_lock lock(m_p->m_mutex);
180 m_p->children.erase(pid);
182 ODR dec = odr_createmem(ODR_DECODE);
183 odr_setbuf(dec, wrbuf_buf(w), wrbuf_len(w), 0);
184 r = z_GDU(dec, &zgdu_res, 0, 0);
187 package.response() = zgdu_res;
191 zgdu_res = odr.create_HTTP_Response(
192 package.session(), zgdu_req->u.HTTP_Request, 400);
193 Z_HTTP_Response *hres = zgdu_res->u.HTTP_Response;
194 z_HTTP_header_add(odr, &hres->headers,
195 "Content-Type", "text/plain");
197 odr_strdup(odr, "Invalid script from script");
198 hres->content_len = strlen(hres->content_buf);
200 package.response() = zgdu_res;
211 void yf::CGI::configure(const xmlNode *ptr, bool test_only, const char *path)
213 yaz_log(YLOG_LOG, "cgi::configure path=%s", path);
214 for (ptr = ptr->children; ptr; ptr = ptr->next)
216 if (ptr->type != XML_ELEMENT_NODE)
218 if (!strcmp((const char *) ptr->name, "map"))
222 const struct _xmlAttr *attr;
223 for (attr = ptr->properties; attr; attr = attr->next)
225 if (!strcmp((const char *) attr->name, "path"))
226 exec.path = mp::xml::get_text(attr->children);
227 else if (!strcmp((const char *) attr->name, "exec"))
228 exec.program = mp::xml::get_text(attr->children);
230 throw mp::filter::FilterException
232 + std::string((const char *) attr->name)
233 + " in cgi section");
235 m_p->exec_map.push_back(exec);
237 else if (!strcmp((const char *) ptr->name, "env"))
239 std::string name, value;
241 const struct _xmlAttr *attr;
242 for (attr = ptr->properties; attr; attr = attr->next)
244 if (!strcmp((const char *) attr->name, "name"))
245 name = mp::xml::get_text(attr->children);
246 else if (!strcmp((const char *) attr->name, "value"))
247 value = mp::xml::get_text(attr->children);
249 throw mp::filter::FilterException
251 + std::string((const char *) attr->name)
252 + " in cgi section");
254 if (name.length() > 0)
255 m_p->env_map[name] = value;
259 throw mp::filter::FilterException("Bad element "
260 + std::string((const char *)
266 static mp::filter::Base* filter_creator()
268 return new mp::filter::CGI;
272 struct metaproxy_1_filter_struct metaproxy_1_filter_cgi = {
283 * c-file-style: "Stroustrup"
284 * indent-tabs-mode: nil
286 * vim: shiftwidth=4 tabstop=8 expandtab