X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=src%2Ffilter_cgi.cpp;h=70b16f0e026630dc87106eee60548969f5352dc3;hb=HEAD;hp=9c08eea1b7ebc3a1b4348086ca1124b0e86650d1;hpb=6c3a29ebeff5c1113f0d778b4cf91810338aede1;p=metaproxy-moved-to-github.git diff --git a/src/filter_cgi.cpp b/src/filter_cgi.cpp index 9c08eea..70b16f0 100644 --- a/src/filter_cgi.cpp +++ b/src/filter_cgi.cpp @@ -24,8 +24,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include +#include #include #include +#include #include #include "config.hpp" @@ -47,6 +49,8 @@ namespace metaproxy_1 { std::map env_map; std::map children; boost::mutex m_mutex; + std::string documentroot; + void child(Z_HTTP_Request *, const CGI::Exec *); public: ~Rep(); }; @@ -71,21 +75,85 @@ yf::CGI::~CGI() { } +void yf::CGI::Rep::child(Z_HTTP_Request *hreq, const CGI::Exec *it) +{ + const char *path_cstr = hreq->path; + std::string path(path_cstr); + const char *program_cstr = it->program.c_str(); + std::string script_name(path, 0, it->path.length()); + std::string rest(path, it->path.length()); + std::string query_string; + std::string path_info; + size_t qpos = rest.find('?'); + if (qpos == std::string::npos) + path_info = rest; + else + { + query_string.assign(rest, qpos + 1, std::string::npos); + path_info.assign(rest, 0, qpos); + } + setenv("REQUEST_METHOD", hreq->method, 1); + setenv("REQUEST_URI", path_cstr, 1); + setenv("SCRIPT_NAME", script_name.c_str(), 1); + setenv("PATH_INFO", path_info.c_str(), 1); + setenv("QUERY_STRING", query_string.c_str(), 1); + const char *v; + v = z_HTTP_header_lookup(hreq->headers, "Cookie"); + if (v) + setenv("HTTP_COOKIE", v, 1); + v = z_HTTP_header_lookup(hreq->headers, "User-Agent"); + if (v) + setenv("HTTP_USER_AGENT", v, 1); + v = z_HTTP_header_lookup(hreq->headers, "Accept"); + if (v) + setenv("HTTP_ACCEPT", v, 1); + v = z_HTTP_header_lookup(hreq->headers, "Accept-Encoding"); + if (v) + setenv("HTTP_ACCEPT_ENCODING", v, 1); + setenv("DOCUMENT_ROOT", documentroot.c_str(), 1); + setenv("GATEWAY_INTERFACE", "CGI/1.1", 1); + + v = z_HTTP_header_lookup(hreq->headers, "Content-Type"); + if (v) + { + char tmp[40]; + sprintf(tmp, "%d", hreq->content_len); + setenv("CONTENT_LENGTH", tmp, 1); + setenv("CONTENT_TYPE", v, 1); + } + // apply user-defined environment + std::map::const_iterator it_e; + for (it_e = env_map.begin(); + it_e != env_map.end(); it_e++) + setenv(it_e->first.c_str(), it_e->second.c_str(), 1); + // change directory to configuration root + // then to CGI program directory (could be relative) + chdir(documentroot.c_str()); + char *program = xstrdup(program_cstr); + char *cp = strrchr(program, '/'); + if (cp) + { + *cp++ = '\0'; + chdir(program); + } + else + cp = program; + int r = execl(cp, cp, (char *) 0); + if (r == -1) + exit(1); + exit(0); +} + 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) + if (!zgdu_req || zgdu_req->which != Z_GDU_HTTP_Request) { package.move(); return; } - - std::list::const_iterator it; metaproxy_1::odr odr; Z_HTTP_Request *hreq = zgdu_req->u.HTTP_Request; @@ -94,83 +162,54 @@ void yf::CGI::process(mp::Package &package) const { if (strncmp(it->path.c_str(), path_cstr, it->path.length()) == 0) { - std::string path(path_cstr); - const char *program_cstr = it->program.c_str(); - std::string script_name(path, 0, it->path.length()); - std::string rest(path, it->path.length()); - std::string query_string; - std::string path_info; - size_t qpos = rest.find('?'); - if (qpos == std::string::npos) - path_info = rest; - else + int fds_response[2]; + int r = pipe(fds_response); + if (r == -1) { - query_string.assign(rest, qpos + 1, std::string::npos); - path_info.assign(rest, 0, qpos); + zgdu_res = odr.create_HTTP_Response( + package.session(), hreq, 400); + package.response() = zgdu_res; + continue; } - int fds[2]; - int r = pipe(fds); + int fds_request[2]; + r = pipe(fds_request); if (r == -1) { zgdu_res = odr.create_HTTP_Response( package.session(), hreq, 400); package.response() = zgdu_res; + close(fds_response[0]); + close(fds_response[1]); continue; } + int status; pid_t pid = ::fork(); switch (pid) { case 0: /* child */ + /* POSTed content */ + close(0); + dup(fds_request[0]); + close(fds_request[1]); + /* response */ close(1); - dup(fds[1]); - setenv("REQUEST_METHOD", hreq->method, 1); - setenv("REQUEST_URI", path_cstr, 1); - setenv("SCRIPT_NAME", script_name.c_str(), 1); - setenv("PATH_INFO", path_info.c_str(), 1); - setenv("QUERY_STRING", query_string.c_str(), 1); - { - const char *v; - v = z_HTTP_header_lookup(hreq->headers, "Cookie"); - if (v) - setenv("HTTP_COOKIE", v, 1); - v = z_HTTP_header_lookup(hreq->headers, "User-Agent"); - if (v) - setenv("HTTP_USER_AGENT", v, 1); - v = z_HTTP_header_lookup(hreq->headers, "Accept"); - if (v) - setenv("HTTP_ACCEPT", v, 1); - v = z_HTTP_header_lookup(hreq->headers, "Accept-Encoding"); - if (v) - setenv("HTTP_ACCEPT_ENCODING", v, 1); - std::map::const_iterator it; - for (it = m_p->env_map.begin(); - it != m_p->env_map.end(); it++) - setenv(it->first.c_str(), it->second.c_str(), 1); - char *program = xstrdup(program_cstr); - char *cp = strrchr(program, '/'); - if (cp) - { - *cp++ = '\0'; - chdir(program); - } - else - cp = program; - r = execl(cp, cp, (char *) 0); - } - if (r == -1) - exit(1); - exit(0); + close(fds_response[0]); + dup(fds_response[1]); + m_p->child(hreq, &(*it)); break; case -1: /* error */ - close(fds[0]); - close(fds[1]); + close(fds_request[0]); + close(fds_request[1]); + close(fds_response[0]); + close(fds_response[1]); zgdu_res = odr.create_HTTP_Response( package.session(), hreq, 400); package.response() = zgdu_res; break; default: /* parent */ - close(fds[1]); + close(fds_response[1]); + close(fds_request[0]); if (pid) { boost::mutex::scoped_lock lock(m_p->m_mutex); @@ -178,15 +217,44 @@ void yf::CGI::process(mp::Package &package) const } WRBUF w = wrbuf_alloc(); wrbuf_puts(w, "HTTP/1.1 200 OK\r\n"); + fcntl(fds_response[0], F_SETFL, O_NONBLOCK); + fcntl(fds_request[1], F_SETFL, O_NONBLOCK); + int no_write = 0; while (1) { - char buf[512]; - ssize_t rd = read(fds[0], buf, sizeof buf); - if (rd <= 0) + int num = 1; + struct yaz_poll_fd fds[2]; + fds[0].fd = fds_response[0]; + fds[0].input_mask = yaz_poll_read; + if (no_write < hreq->content_len) + { + fds[1].fd = fds_request[1]; + fds[1].input_mask = yaz_poll_write; + num = 2; + } + int r = yaz_poll(fds, num, 60, 0); + if (r <= 0) break; - wrbuf_write(w, buf, rd); + if (fds[0].output_mask & (yaz_poll_read|yaz_poll_except)) + { + char buf[512]; + ssize_t rd = read(fds_response[0], buf, sizeof buf); + if (rd <= 0) + break; + wrbuf_write(w, buf, rd); + } + if (num == 2 && fds[1].output_mask & yaz_poll_write) + { + ssize_t wd = write(fds_request[1], + hreq->content_buf + no_write, + hreq->content_len - no_write); + if (wd <= 0) + break; + no_write += wd; + } } - close(fds[0]); + close(fds_request[1]); + close(fds_response[0]); waitpid(pid, &status, 0); if (pid) @@ -269,6 +337,10 @@ void yf::CGI::configure(const xmlNode *ptr, bool test_only, const char *path) if (name.length() > 0) m_p->env_map[name] = value; } + else if (!strcmp((const char *) ptr->name, "documentroot")) + { + m_p->documentroot = path; + } else { throw mp::filter::FilterException("Bad element " @@ -276,6 +348,8 @@ void yf::CGI::configure(const xmlNode *ptr, bool test_only, const char *path) ptr->name)); } } + if (m_p->documentroot.length() == 0) + m_p->documentroot = "."; } static mp::filter::Base* filter_creator()