20 #include <metaproxy/package.hpp>
21 #include <metaproxy/util.hpp>
36 namespace yf = mp::filter;
49 std::map<std::string,std::string>
env_map;
60 yf::CGI::CGI() : m_p(new
Rep)
67 std::map<pid_t,pid_t>::const_iterator it;
68 boost::mutex::scoped_lock lock(m_mutex);
70 for (it = children.begin(); it != children.end(); it++)
71 kill(it->second, SIGTERM);
78 void yf::CGI::Rep::child(Z_HTTP_Request *hreq,
const CGI::Exec *it)
80 const char *path_cstr = hreq->path;
81 std::string path(path_cstr);
82 const char *program_cstr = it->
program.c_str();
83 std::string script_name(path, 0, it->
path.length());
84 std::string rest(path, it->
path.length());
85 std::string query_string;
86 std::string path_info;
87 size_t qpos = rest.find(
'?');
88 if (qpos == std::string::npos)
92 query_string.assign(rest, qpos + 1, std::string::npos);
93 path_info.assign(rest, 0, qpos);
95 setenv(
"REQUEST_METHOD", hreq->method, 1);
96 setenv(
"REQUEST_URI", path_cstr, 1);
97 setenv(
"SCRIPT_NAME", script_name.c_str(), 1);
98 setenv(
"PATH_INFO", path_info.c_str(), 1);
99 setenv(
"QUERY_STRING", query_string.c_str(), 1);
101 v = z_HTTP_header_lookup(hreq->headers,
"Cookie");
103 setenv(
"HTTP_COOKIE", v, 1);
104 v = z_HTTP_header_lookup(hreq->headers,
"User-Agent");
106 setenv(
"HTTP_USER_AGENT", v, 1);
107 v = z_HTTP_header_lookup(hreq->headers,
"Accept");
109 setenv(
"HTTP_ACCEPT", v, 1);
110 v = z_HTTP_header_lookup(hreq->headers,
"Accept-Encoding");
112 setenv(
"HTTP_ACCEPT_ENCODING", v, 1);
113 setenv(
"DOCUMENT_ROOT", documentroot.c_str(), 1);
114 setenv(
"GATEWAY_INTERFACE",
"CGI/1.1", 1);
116 v = z_HTTP_header_lookup(hreq->headers,
"Content-Type");
120 sprintf(tmp,
"%d", hreq->content_len);
121 setenv(
"CONTENT_LENGTH", tmp, 1);
122 setenv(
"CONTENT_TYPE", v, 1);
125 std::map<std::string,std::string>::const_iterator it_e;
126 for (it_e = env_map.begin();
127 it_e != env_map.end(); it_e++)
128 setenv(it_e->first.c_str(), it_e->second.c_str(), 1);
131 chdir(documentroot.c_str());
132 char *program = xstrdup(program_cstr);
133 char *cp = strrchr(program,
'/');
141 int r = execl(cp, cp, (
char *) 0);
147 void yf::CGI::process(mp::Package &package)
const
149 Z_GDU *zgdu_req = package.request().get();
152 if (!zgdu_req || zgdu_req->which != Z_GDU_HTTP_Request)
157 std::list<CGI::Exec>::const_iterator it;
158 metaproxy_1::odr odr;
159 Z_HTTP_Request *hreq = zgdu_req->u.HTTP_Request;
160 const char *path_cstr = hreq->path;
161 for (it = m_p->exec_map.begin(); it != m_p->exec_map.end(); it++)
163 if (strncmp(it->path.c_str(), path_cstr, it->path.length()) == 0)
166 int r = pipe(fds_response);
169 zgdu_res = odr.create_HTTP_Response(
170 package.session(), hreq, 400);
171 package.response() = zgdu_res;
175 r = pipe(fds_request);
178 zgdu_res = odr.create_HTTP_Response(
179 package.session(), hreq, 400);
180 package.response() = zgdu_res;
181 close(fds_response[0]);
182 close(fds_response[1]);
187 pid_t pid = ::fork();
194 close(fds_request[1]);
197 close(fds_response[0]);
198 dup(fds_response[1]);
199 m_p->child(hreq, &(*it));
202 close(fds_request[0]);
203 close(fds_request[1]);
204 close(fds_response[0]);
205 close(fds_response[1]);
206 zgdu_res = odr.create_HTTP_Response(
207 package.session(), hreq, 400);
208 package.response() = zgdu_res;
211 close(fds_response[1]);
212 close(fds_request[0]);
215 boost::mutex::scoped_lock lock(m_p->m_mutex);
216 m_p->children[pid] = pid;
218 WRBUF w = wrbuf_alloc();
219 wrbuf_puts(w,
"HTTP/1.1 200 OK\r\n");
220 fcntl(fds_response[0], F_SETFL, O_NONBLOCK);
221 fcntl(fds_request[1], F_SETFL, O_NONBLOCK);
226 struct yaz_poll_fd fds[2];
227 fds[0].fd = fds_response[0];
228 fds[0].input_mask = yaz_poll_read;
229 if (no_write < hreq->content_len)
231 fds[1].fd = fds_request[1];
232 fds[1].input_mask = yaz_poll_write;
235 int r = yaz_poll(fds, num, 60, 0);
238 if (fds[0].output_mask & (yaz_poll_read|yaz_poll_except))
241 ssize_t rd = read(fds_response[0], buf,
sizeof buf);
244 wrbuf_write(w, buf, rd);
246 if (num == 2 && fds[1].output_mask & yaz_poll_write)
248 ssize_t wd = write(fds_request[1],
249 hreq->content_buf + no_write,
250 hreq->content_len - no_write);
256 close(fds_request[1]);
257 close(fds_response[0]);
258 waitpid(pid, &status, 0);
262 boost::mutex::scoped_lock lock(m_p->m_mutex);
263 m_p->children.erase(pid);
265 ODR dec = odr_createmem(ODR_DECODE);
266 odr_setbuf(dec, wrbuf_buf(w), wrbuf_len(w), 0);
267 r = z_GDU(dec, &zgdu_res, 0, 0);
270 package.response() = zgdu_res;
274 zgdu_res = odr.create_HTTP_Response(
275 package.session(), zgdu_req->u.HTTP_Request, 400);
276 Z_HTTP_Response *hres = zgdu_res->u.HTTP_Response;
277 z_HTTP_header_add(odr, &hres->headers,
278 "Content-Type",
"text/plain");
280 odr_strdup(odr,
"Invalid script from script");
281 hres->content_len = strlen(hres->content_buf);
283 package.response() = zgdu_res;
294 void yf::CGI::configure(
const xmlNode *ptr,
bool test_only,
const char *path)
296 yaz_log(YLOG_LOG,
"cgi::configure path=%s", path);
297 for (ptr = ptr->children; ptr; ptr = ptr->next)
299 if (ptr->type != XML_ELEMENT_NODE)
301 if (!strcmp((
const char *) ptr->name,
"map"))
305 const struct _xmlAttr *attr;
306 for (attr = ptr->properties; attr; attr = attr->next)
308 if (!strcmp((
const char *) attr->name,
"path"))
309 exec.
path = mp::xml::get_text(attr->children);
310 else if (!strcmp((
const char *) attr->name,
"exec"))
311 exec.
program = mp::xml::get_text(attr->children);
313 throw mp::filter::FilterException
315 + std::string((
const char *) attr->name)
316 +
" in cgi section");
318 m_p->exec_map.push_back(exec);
320 else if (!strcmp((
const char *) ptr->name,
"env"))
322 std::string name, value;
324 const struct _xmlAttr *attr;
325 for (attr = ptr->properties; attr; attr = attr->next)
327 if (!strcmp((
const char *) attr->name,
"name"))
328 name = mp::xml::get_text(attr->children);
329 else if (!strcmp((
const char *) attr->name,
"value"))
330 value = mp::xml::get_text(attr->children);
332 throw mp::filter::FilterException
334 + std::string((
const char *) attr->name)
335 +
" in cgi section");
337 if (name.length() > 0)
338 m_p->env_map[name] = value;
340 else if (!strcmp((
const char *) ptr->name,
"documentroot"))
342 m_p->documentroot = path;
346 throw mp::filter::FilterException(
"Bad element "
347 + std::string((
const char *)
351 if (m_p->documentroot.length() == 0)
352 m_p->documentroot =
".";
357 return new mp::filter::CGI;
struct metaproxy_1_filter_struct metaproxy_1_filter_cgi
static mp::filter::Base * filter_creator()