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)
89 std::list<CGI::Exec>::const_iterator it;
91 Z_HTTP_Request *hreq = zgdu_req->u.HTTP_Request;
92 const char *path_cstr = hreq->path;
93 for (it = m_p->exec_map.begin(); it != m_p->exec_map.end(); it++)
95 if (strncmp(it->path.c_str(), path_cstr, it->path.length()) == 0)
97 std::string path(path_cstr);
98 const char *program_cstr = it->program.c_str();
99 std::string script_name(path, 0, it->path.length());
100 std::string rest(path, it->path.length());
101 std::string query_string;
102 std::string path_info;
103 size_t qpos = rest.find('?');
104 if (qpos == std::string::npos)
108 query_string.assign(rest, qpos + 1, std::string::npos);
109 path_info.assign(rest, 0, qpos);
115 zgdu_res = odr.create_HTTP_Response(
116 package.session(), hreq, 400);
117 package.response() = zgdu_res;
121 pid_t pid = ::fork();
127 setenv("REQUEST_METHOD", hreq->method, 1);
128 setenv("REQUEST_URI", path_cstr, 1);
129 setenv("SCRIPT_NAME", script_name.c_str(), 1);
130 setenv("PATH_INFO", path_info.c_str(), 1);
131 setenv("QUERY_STRING", query_string.c_str(), 1);
134 v = z_HTTP_header_lookup(hreq->headers, "Cookie");
136 setenv("HTTP_COOKIE", v, 1);
137 v = z_HTTP_header_lookup(hreq->headers, "User-Agent");
139 setenv("HTTP_USER_AGENT", v, 1);
140 v = z_HTTP_header_lookup(hreq->headers, "Accept");
142 setenv("HTTP_ACCEPT", v, 1);
143 v = z_HTTP_header_lookup(hreq->headers, "Accept-Encoding");
145 setenv("HTTP_ACCEPT_ENCODING", v, 1);
146 std::map<std::string,std::string>::const_iterator it;
147 for (it = m_p->env_map.begin();
148 it != m_p->env_map.end(); it++)
149 setenv(it->first.c_str(), it->second.c_str(), 1);
150 char *program = xstrdup(program_cstr);
151 char *cp = strrchr(program, '/');
159 r = execl(cp, cp, (char *) 0);
168 zgdu_res = odr.create_HTTP_Response(
169 package.session(), hreq, 400);
170 package.response() = zgdu_res;
172 default: /* parent */
176 boost::mutex::scoped_lock lock(m_p->m_mutex);
177 m_p->children[pid] = pid;
179 WRBUF w = wrbuf_alloc();
180 wrbuf_puts(w, "HTTP/1.1 200 OK\r\n");
184 ssize_t rd = read(fds[0], buf, sizeof buf);
187 wrbuf_write(w, buf, rd);
190 waitpid(pid, &status, 0);
194 boost::mutex::scoped_lock lock(m_p->m_mutex);
195 m_p->children.erase(pid);
197 ODR dec = odr_createmem(ODR_DECODE);
198 odr_setbuf(dec, wrbuf_buf(w), wrbuf_len(w), 0);
199 r = z_GDU(dec, &zgdu_res, 0, 0);
202 package.response() = zgdu_res;
206 zgdu_res = odr.create_HTTP_Response(
207 package.session(), zgdu_req->u.HTTP_Request, 400);
208 Z_HTTP_Response *hres = zgdu_res->u.HTTP_Response;
209 z_HTTP_header_add(odr, &hres->headers,
210 "Content-Type", "text/plain");
212 odr_strdup(odr, "Invalid script from script");
213 hres->content_len = strlen(hres->content_buf);
215 package.response() = zgdu_res;
226 void yf::CGI::configure(const xmlNode *ptr, bool test_only, const char *path)
228 yaz_log(YLOG_LOG, "cgi::configure path=%s", path);
229 for (ptr = ptr->children; ptr; ptr = ptr->next)
231 if (ptr->type != XML_ELEMENT_NODE)
233 if (!strcmp((const char *) ptr->name, "map"))
237 const struct _xmlAttr *attr;
238 for (attr = ptr->properties; attr; attr = attr->next)
240 if (!strcmp((const char *) attr->name, "path"))
241 exec.path = mp::xml::get_text(attr->children);
242 else if (!strcmp((const char *) attr->name, "exec"))
243 exec.program = mp::xml::get_text(attr->children);
245 throw mp::filter::FilterException
247 + std::string((const char *) attr->name)
248 + " in cgi section");
250 m_p->exec_map.push_back(exec);
252 else if (!strcmp((const char *) ptr->name, "env"))
254 std::string name, value;
256 const struct _xmlAttr *attr;
257 for (attr = ptr->properties; attr; attr = attr->next)
259 if (!strcmp((const char *) attr->name, "name"))
260 name = mp::xml::get_text(attr->children);
261 else if (!strcmp((const char *) attr->name, "value"))
262 value = mp::xml::get_text(attr->children);
264 throw mp::filter::FilterException
266 + std::string((const char *) attr->name)
267 + " in cgi section");
269 if (name.length() > 0)
270 m_p->env_map[name] = value;
274 throw mp::filter::FilterException("Bad element "
275 + std::string((const char *)
281 static mp::filter::Base* filter_creator()
283 return new mp::filter::CGI;
287 struct metaproxy_1_filter_struct metaproxy_1_filter_cgi = {
298 * c-file-style: "Stroustrup"
299 * indent-tabs-mode: nil
301 * vim: shiftwidth=4 tabstop=8 expandtab