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;
50 std::string documentroot;
51 void child(Z_HTTP_Request *, const CGI::Exec *);
58 yf::CGI::CGI() : m_p(new Rep)
65 std::map<pid_t,pid_t>::const_iterator it;
66 boost::mutex::scoped_lock lock(m_mutex);
68 for (it = children.begin(); it != children.end(); it++)
69 kill(it->second, SIGTERM);
76 void yf::CGI::Rep::child(Z_HTTP_Request *hreq, const CGI::Exec *it)
78 const char *path_cstr = hreq->path;
79 std::string path(path_cstr);
80 const char *program_cstr = it->program.c_str();
81 std::string script_name(path, 0, it->path.length());
82 std::string rest(path, it->path.length());
83 std::string query_string;
84 std::string path_info;
85 size_t qpos = rest.find('?');
86 if (qpos == std::string::npos)
90 query_string.assign(rest, qpos + 1, std::string::npos);
91 path_info.assign(rest, 0, qpos);
93 setenv("REQUEST_METHOD", hreq->method, 1);
94 setenv("REQUEST_URI", path_cstr, 1);
95 setenv("SCRIPT_NAME", script_name.c_str(), 1);
96 setenv("PATH_INFO", path_info.c_str(), 1);
97 setenv("QUERY_STRING", query_string.c_str(), 1);
99 v = z_HTTP_header_lookup(hreq->headers, "Cookie");
101 setenv("HTTP_COOKIE", v, 1);
102 v = z_HTTP_header_lookup(hreq->headers, "User-Agent");
104 setenv("HTTP_USER_AGENT", v, 1);
105 v = z_HTTP_header_lookup(hreq->headers, "Accept");
107 setenv("HTTP_ACCEPT", v, 1);
108 v = z_HTTP_header_lookup(hreq->headers, "Accept-Encoding");
110 setenv("HTTP_ACCEPT_ENCODING", v, 1);
111 setenv("DOCUMENT_ROOT", documentroot.c_str(), 1);
112 setenv("GATEWAY_INTERFACE", "CGI/1.1", 1);
113 // apply user-defined environment
114 std::map<std::string,std::string>::const_iterator it_e;
115 for (it_e = env_map.begin();
116 it_e != env_map.end(); it_e++)
117 setenv(it_e->first.c_str(), it_e->second.c_str(), 1);
118 // change directory to configuration root
119 // then to CGI program directory (could be relative)
120 chdir(documentroot.c_str());
121 char *program = xstrdup(program_cstr);
122 char *cp = strrchr(program, '/');
130 int r = execl(cp, cp, (char *) 0);
136 void yf::CGI::process(mp::Package &package) const
138 Z_GDU *zgdu_req = package.request().get();
144 if (zgdu_req->which != Z_GDU_HTTP_Request)
151 std::list<CGI::Exec>::const_iterator it;
152 metaproxy_1::odr odr;
153 Z_HTTP_Request *hreq = zgdu_req->u.HTTP_Request;
154 const char *path_cstr = hreq->path;
155 for (it = m_p->exec_map.begin(); it != m_p->exec_map.end(); it++)
157 if (strncmp(it->path.c_str(), path_cstr, it->path.length()) == 0)
163 zgdu_res = odr.create_HTTP_Response(
164 package.session(), hreq, 400);
165 package.response() = zgdu_res;
169 pid_t pid = ::fork();
175 m_p->child(hreq, &(*it));
180 zgdu_res = odr.create_HTTP_Response(
181 package.session(), hreq, 400);
182 package.response() = zgdu_res;
184 default: /* parent */
188 boost::mutex::scoped_lock lock(m_p->m_mutex);
189 m_p->children[pid] = pid;
191 WRBUF w = wrbuf_alloc();
192 wrbuf_puts(w, "HTTP/1.1 200 OK\r\n");
196 ssize_t rd = read(fds[0], buf, sizeof buf);
199 wrbuf_write(w, buf, rd);
202 waitpid(pid, &status, 0);
206 boost::mutex::scoped_lock lock(m_p->m_mutex);
207 m_p->children.erase(pid);
209 ODR dec = odr_createmem(ODR_DECODE);
210 odr_setbuf(dec, wrbuf_buf(w), wrbuf_len(w), 0);
211 r = z_GDU(dec, &zgdu_res, 0, 0);
214 package.response() = zgdu_res;
218 zgdu_res = odr.create_HTTP_Response(
219 package.session(), zgdu_req->u.HTTP_Request, 400);
220 Z_HTTP_Response *hres = zgdu_res->u.HTTP_Response;
221 z_HTTP_header_add(odr, &hres->headers,
222 "Content-Type", "text/plain");
224 odr_strdup(odr, "Invalid script from script");
225 hres->content_len = strlen(hres->content_buf);
227 package.response() = zgdu_res;
238 void yf::CGI::configure(const xmlNode *ptr, bool test_only, const char *path)
240 yaz_log(YLOG_LOG, "cgi::configure path=%s", path);
241 for (ptr = ptr->children; ptr; ptr = ptr->next)
243 if (ptr->type != XML_ELEMENT_NODE)
245 if (!strcmp((const char *) ptr->name, "map"))
249 const struct _xmlAttr *attr;
250 for (attr = ptr->properties; attr; attr = attr->next)
252 if (!strcmp((const char *) attr->name, "path"))
253 exec.path = mp::xml::get_text(attr->children);
254 else if (!strcmp((const char *) attr->name, "exec"))
255 exec.program = mp::xml::get_text(attr->children);
257 throw mp::filter::FilterException
259 + std::string((const char *) attr->name)
260 + " in cgi section");
262 m_p->exec_map.push_back(exec);
264 else if (!strcmp((const char *) ptr->name, "env"))
266 std::string name, value;
268 const struct _xmlAttr *attr;
269 for (attr = ptr->properties; attr; attr = attr->next)
271 if (!strcmp((const char *) attr->name, "name"))
272 name = mp::xml::get_text(attr->children);
273 else if (!strcmp((const char *) attr->name, "value"))
274 value = mp::xml::get_text(attr->children);
276 throw mp::filter::FilterException
278 + std::string((const char *) attr->name)
279 + " in cgi section");
281 if (name.length() > 0)
282 m_p->env_map[name] = value;
284 else if (!strcmp((const char *) ptr->name, "documentroot"))
286 m_p->documentroot = path;
290 throw mp::filter::FilterException("Bad element "
291 + std::string((const char *)
295 if (m_p->documentroot.length() == 0)
296 m_p->documentroot = ".";
299 static mp::filter::Base* filter_creator()
301 return new mp::filter::CGI;
305 struct metaproxy_1_filter_struct metaproxy_1_filter_cgi = {
316 * c-file-style: "Stroustrup"
317 * indent-tabs-mode: nil
319 * vim: shiftwidth=4 tabstop=8 expandtab