X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ffilter_cgi.cpp;h=9baa17f1e9c07c8bf4a9b5a1402aa422139f6dd7;hb=39fe3f90ff2cf99ec39053df3c0835ab87b0deba;hp=3412ca853cffeb9e66735ab2875a54a792b55a5d;hpb=324f6dcf90e165c4f8e8b8bfda9ed12b7c5d5582;p=metaproxy-moved-to-github.git diff --git a/src/filter_cgi.cpp b/src/filter_cgi.cpp index 3412ca8..9baa17f 100644 --- a/src/filter_cgi.cpp +++ b/src/filter_cgi.cpp @@ -1,5 +1,5 @@ /* This file is part of Metaproxy. - Copyright (C) 2005-2010 Index Data + Copyright (C) Index Data Metaproxy is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -24,6 +24,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include +#include #include #include @@ -43,26 +44,41 @@ namespace metaproxy_1 { class CGI::Rep { friend class CGI; std::list exec_map; + std::map env_map; + std::map children; + boost::mutex m_mutex; + public: + ~Rep(); }; } } yf::CGI::CGI() : m_p(new Rep) { + +} + +yf::CGI::Rep::~Rep() +{ + std::map::const_iterator it; + boost::mutex::scoped_lock lock(m_mutex); + + for (it = children.begin(); it != children.end(); it++) + kill(it->second, SIGTERM); } yf::CGI::~CGI() -{ // must have a destructor because of boost::scoped_ptr +{ } void yf::CGI::process(mp::Package &package) const { Z_GDU *zgdu_req = package.request().get(); Z_GDU *zgdu_res = 0; - + if (!zgdu_req) return; - + if (zgdu_req->which != Z_GDU_HTTP_Request) { package.move(); @@ -85,37 +101,105 @@ void yf::CGI::process(mp::Package &package) const metaproxy_1::odr odr; for (it = m_p->exec_map.begin(); it != m_p->exec_map.end(); it++) { - if (it->path.compare(path_info) == 0) + if (it->path.compare(0, it->path.length(), path_info) == 0) { int r; pid_t pid; int status; - int fd; - + int fds[2]; + const char *program_cstr = it->program.c_str(); + std::map::const_iterator it; + + r = pipe(fds); + if (r == -1) + { + zgdu_res = odr.create_HTTP_Response( + package.session(), zgdu_req->u.HTTP_Request, 400); + package.response() = zgdu_res; + continue; + } pid = ::fork(); switch (pid) { case 0: /* child */ - for (fd = 3; fd <= 1023; fd++) - close(fd); - setenv("PATH_INFO", path_info.c_str(), 1); + close(1); + dup(fds[1]); + setenv("SCRIPT_NAME", path_info.c_str(), 1); + setenv("PATH_INFO", "", 1); setenv("QUERY_STRING", query_string.c_str(), 1); - r = execl(it->program.c_str(), it->program.c_str(), (char *) 0); + for (it = m_p->env_map.begin(); it != m_p->env_map.end(); it++) + setenv(it->first.c_str(), it->second.c_str(), 1); + + if (1) + { + char *cp1 = xstrdup(program_cstr); + char *cp2 = strrchr(cp1, '/'); + if (cp2) + { + *cp2 = '\0'; + chdir(cp1); + + } + xfree(cp1); + } + r = execl(program_cstr, program_cstr, (char *) 0); if (r == -1) exit(1); exit(0); break; case -1: /* error */ + close(fds[0]); + close(fds[1]); zgdu_res = odr.create_HTTP_Response( package.session(), zgdu_req->u.HTTP_Request, 400); package.response() = zgdu_res; break; default: /* parent */ + close(fds[1]); + if (pid) + { + boost::mutex::scoped_lock lock(m_p->m_mutex); + m_p->children[pid] = pid; + } + WRBUF w = wrbuf_alloc(); + wrbuf_puts(w, "HTTP/1.1 200 OK\r\n"); + while (1) + { + char buf[512]; + ssize_t rd = read(fds[0], buf, sizeof buf); + if (rd <= 0) + break; + wrbuf_write(w, buf, rd); + } + close(fds[0]); waitpid(pid, &status, 0); - zgdu_res = odr.create_HTTP_Response( - package.session(), zgdu_req->u.HTTP_Request, 200); + if (pid) + { + boost::mutex::scoped_lock lock(m_p->m_mutex); + m_p->children.erase(pid); + } + ODR dec = odr_createmem(ODR_DECODE); + odr_setbuf(dec, wrbuf_buf(w), wrbuf_len(w), 0); + r = z_GDU(dec, &zgdu_res, 0, 0); + if (r && zgdu_res) + { + package.response() = zgdu_res; + } + else + { + zgdu_res = odr.create_HTTP_Response( + package.session(), zgdu_req->u.HTTP_Request, 400); + Z_HTTP_Response *hres = zgdu_res->u.HTTP_Response; + z_HTTP_header_add(odr, &hres->headers, + "Content-Type", "text/plain"); + hres->content_buf = + odr_strdup(odr, "Invalid script from script"); + hres->content_len = strlen(hres->content_buf); + } package.response() = zgdu_res; + odr_destroy(dec); + wrbuf_destroy(w); break; } return; @@ -124,8 +208,9 @@ void yf::CGI::process(mp::Package &package) const package.move(); } -void yf::CGI::configure(const xmlNode *ptr, bool test_only) +void yf::CGI::configure(const xmlNode *ptr, bool test_only, const char *path) { + yaz_log(YLOG_LOG, "cgi::configure path=%s", path); for (ptr = ptr->children; ptr; ptr = ptr->next) { if (ptr->type != XML_ELEMENT_NODE) @@ -143,15 +228,35 @@ void yf::CGI::configure(const xmlNode *ptr, bool test_only) exec.program = mp::xml::get_text(attr->children); else throw mp::filter::FilterException - ("Bad attribute " + ("Bad attribute " + std::string((const char *) attr->name) + " in cgi section"); } m_p->exec_map.push_back(exec); } + else if (!strcmp((const char *) ptr->name, "env")) + { + std::string name, value; + + const struct _xmlAttr *attr; + for (attr = ptr->properties; attr; attr = attr->next) + { + if (!strcmp((const char *) attr->name, "name")) + name = mp::xml::get_text(attr->children); + else if (!strcmp((const char *) attr->name, "value")) + value = mp::xml::get_text(attr->children); + else + throw mp::filter::FilterException + ("Bad attribute " + + std::string((const char *) attr->name) + + " in cgi section"); + } + if (name.length() > 0) + m_p->env_map[name] = value; + } else { - throw mp::filter::FilterException("Bad element " + throw mp::filter::FilterException("Bad element " + std::string((const char *) ptr->name)); }