78void 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 int r = chdir(documentroot.c_str());
134 yaz_log(YLOG_LOG,
"CGI chdir(%s) failed: %s",
135 documentroot.c_str(), strerror(errno));
138 char *program = xstrdup(program_cstr);
139 char *cp = strrchr(program,
'/');
146 yaz_log(YLOG_FATAL,
"CGI chdir(%s) failed: %s",
147 program, strerror(errno));
153 r = execl(cp, cp, (
char *) 0);
156 yaz_log(YLOG_FATAL,
"CGI execl(%s) failed: %s",
157 program, strerror(errno));
164void yf::CGI::process(mp::Package &package)
const
166 Z_GDU *zgdu_req = package.request().get();
169 if (!zgdu_req || zgdu_req->which != Z_GDU_HTTP_Request)
174 std::list<CGI::Exec>::const_iterator it;
175 metaproxy_1::odr odr;
176 Z_HTTP_Request *hreq = zgdu_req->u.HTTP_Request;
177 const char *path_cstr = hreq->path;
178 for (it = m_p->exec_map.begin(); it != m_p->exec_map.end(); it++)
180 if (strncmp(it->path.c_str(), path_cstr, it->path.length()) == 0)
183 int r = pipe(fds_response);
186 zgdu_res = odr.create_HTTP_Response(
187 package.session(), hreq, 400);
188 package.response() = zgdu_res;
192 r = pipe(fds_request);
195 zgdu_res = odr.create_HTTP_Response(
196 package.session(), hreq, 400);
197 package.response() = zgdu_res;
198 close(fds_response[0]);
199 close(fds_response[1]);
204 pid_t pid = ::fork();
210 r = dup(fds_request[0]);
213 zgdu_res = odr.create_HTTP_Response(
214 package.session(), hreq, 500);
215 package.response() = zgdu_res;
218 close(fds_request[1]);
221 close(fds_response[0]);
222 r = dup(fds_response[1]);
225 zgdu_res = odr.create_HTTP_Response(
226 package.session(), hreq, 500);
227 package.response() = zgdu_res;
230 m_p->child(hreq, &(*it));
233 close(fds_request[0]);
234 close(fds_request[1]);
235 close(fds_response[0]);
236 close(fds_response[1]);
237 zgdu_res = odr.create_HTTP_Response(
238 package.session(), hreq, 400);
239 package.response() = zgdu_res;
242 close(fds_response[1]);
243 close(fds_request[0]);
246 boost::mutex::scoped_lock lock(m_p->m_mutex);
247 m_p->children[pid] = pid;
249 WRBUF w = wrbuf_alloc();
250 wrbuf_puts(w,
"HTTP/1.1 200 OK\r\n");
251 fcntl(fds_response[0], F_SETFL, O_NONBLOCK);
252 fcntl(fds_request[1], F_SETFL, O_NONBLOCK);
257 struct yaz_poll_fd fds[2];
258 fds[0].fd = fds_response[0];
259 fds[0].input_mask = yaz_poll_read;
260 if (no_write < hreq->content_len)
262 fds[1].fd = fds_request[1];
263 fds[1].input_mask = yaz_poll_write;
266 int r = yaz_poll(fds, num, 60, 0);
269 if (fds[0].output_mask & (yaz_poll_read|yaz_poll_except))
272 ssize_t rd = read(fds_response[0], buf,
sizeof buf);
275 wrbuf_write(w, buf, rd);
277 if (num == 2 && fds[1].output_mask & yaz_poll_write)
279 ssize_t wd = write(fds_request[1],
280 hreq->content_buf + no_write,
281 hreq->content_len - no_write);
287 close(fds_request[1]);
288 close(fds_response[0]);
289 waitpid(pid, &status, 0);
293 boost::mutex::scoped_lock lock(m_p->m_mutex);
294 m_p->children.erase(pid);
296 ODR dec = odr_createmem(ODR_DECODE);
297 odr_setbuf(dec, wrbuf_buf(w), wrbuf_len(w), 0);
298 r = z_GDU(dec, &zgdu_res, 0, 0);
301 package.response() = zgdu_res;
305 zgdu_res = odr.create_HTTP_Response(
306 package.session(), zgdu_req->u.HTTP_Request, 400);
307 Z_HTTP_Response *hres = zgdu_res->u.HTTP_Response;
308 z_HTTP_header_add(odr, &hres->headers,
309 "Content-Type",
"text/plain");
311 odr_strdup(odr,
"Invalid script from script");
312 hres->content_len = strlen(hres->content_buf);
314 package.response() = zgdu_res;
325void yf::CGI::configure(
const xmlNode *ptr,
bool test_only,
const char *path)
327 yaz_log(YLOG_LOG,
"cgi::configure path=%s", path);
328 for (ptr = ptr->children; ptr; ptr = ptr->next)
330 if (ptr->type != XML_ELEMENT_NODE)
332 if (!strcmp((
const char *) ptr->name,
"map"))
336 const struct _xmlAttr *attr;
337 for (attr = ptr->properties; attr; attr = attr->next)
339 if (!strcmp((
const char *) attr->name,
"path"))
340 exec.
path = mp::xml::get_text(attr->children);
341 else if (!strcmp((
const char *) attr->name,
"exec"))
342 exec.
program = mp::xml::get_text(attr->children);
344 throw mp::filter::FilterException
346 + std::string((
const char *) attr->name)
347 +
" in cgi section");
349 m_p->exec_map.push_back(exec);
351 else if (!strcmp((
const char *) ptr->name,
"env"))
353 std::string name, value;
355 const struct _xmlAttr *attr;
356 for (attr = ptr->properties; attr; attr = attr->next)
358 if (!strcmp((
const char *) attr->name,
"name"))
359 name = mp::xml::get_text(attr->children);
360 else if (!strcmp((
const char *) attr->name,
"value"))
361 value = mp::xml::get_text(attr->children);
363 throw mp::filter::FilterException
365 + std::string((
const char *) attr->name)
366 +
" in cgi section");
368 if (name.length() > 0)
369 m_p->env_map[name] = value;
371 else if (!strcmp((
const char *) ptr->name,
"documentroot"))
373 m_p->documentroot = path;
377 throw mp::filter::FilterException(
"Bad element "
378 + std::string((
const char *)
382 if (m_p->documentroot.length() == 0)
383 m_p->documentroot =
".";