+#if ENABLE_FEATURE_HTTPD_CGI || ENABLE_FEATURE_HTTPD_PROXY
+
+/* gcc 4.2.1 fares better with NOINLINE */
+static NOINLINE void cgi_io_loop_and_exit(int fromCgi_rd, int toCgi_wr, int post_len) NORETURN;
+static NOINLINE void cgi_io_loop_and_exit(int fromCgi_rd, int toCgi_wr, int post_len)
+{
+ enum { FROM_CGI = 1, TO_CGI = 2 }; /* indexes in pfd[] */
+ struct pollfd pfd[3];
+ int out_cnt; /* we buffer a bit of initial CGI output */
+ int count;
+
+ /* iobuf is used for CGI -> network data,
+ * hdr_buf is for network -> CGI data (POSTDATA) */
+
+ /* If CGI dies, we still want to correctly finish reading its output
+ * and send it to the peer. So please no SIGPIPEs! */
+ signal(SIGPIPE, SIG_IGN);
+
+ // We inconsistently handle a case when more POSTDATA from network
+ // is coming than we expected. We may give *some part* of that
+ // extra data to CGI.
+
+ //if (hdr_cnt > post_len) {
+ // /* We got more POSTDATA from network than we expected */
+ // hdr_cnt = post_len;
+ //}
+ post_len -= hdr_cnt;
+ /* post_len - number of POST bytes not yet read from network */
+
+ /* NB: breaking out of this loop jumps to log_and_exit() */
+ out_cnt = 0;
+ while (1) {
+ memset(pfd, 0, sizeof(pfd));
+
+ pfd[FROM_CGI].fd = fromCgi_rd;
+ pfd[FROM_CGI].events = POLLIN;
+
+ if (toCgi_wr) {
+ pfd[TO_CGI].fd = toCgi_wr;
+ if (hdr_cnt > 0) {
+ pfd[TO_CGI].events = POLLOUT;
+ } else if (post_len > 0) {
+ pfd[0].events = POLLIN;
+ } else {
+ /* post_len <= 0 && hdr_cnt <= 0:
+ * no more POST data to CGI,
+ * let CGI see EOF on CGI's stdin */
+ if (toCgi_wr != fromCgi_rd)
+ close(toCgi_wr);
+ toCgi_wr = 0;
+ }
+ }
+
+ /* Now wait on the set of sockets */
+ count = safe_poll(pfd, toCgi_wr ? TO_CGI+1 : FROM_CGI+1, -1);
+ if (count <= 0) {
+#if 0
+ if (safe_waitpid(pid, &status, WNOHANG) <= 0) {
+ /* Weird. CGI didn't exit and no fd's
+ * are ready, yet poll returned?! */
+ continue;
+ }
+ if (DEBUG && WIFEXITED(status))
+ bb_error_msg("CGI exited, status=%d", WEXITSTATUS(status));
+ if (DEBUG && WIFSIGNALED(status))
+ bb_error_msg("CGI killed, signal=%d", WTERMSIG(status));
+#endif
+ break;
+ }
+
+ if (pfd[TO_CGI].revents) {
+ /* hdr_cnt > 0 here due to the way pfd[TO_CGI].events set */
+ /* Have data from peer and can write to CGI */
+ count = safe_write(toCgi_wr, hdr_ptr, hdr_cnt);
+ /* Doesn't happen, we dont use nonblocking IO here
+ *if (count < 0 && errno == EAGAIN) {
+ * ...
+ *} else */
+ if (count > 0) {
+ hdr_ptr += count;
+ hdr_cnt -= count;
+ } else {
+ /* EOF/broken pipe to CGI, stop piping POST data */
+ hdr_cnt = post_len = 0;
+ }
+ }
+
+ if (pfd[0].revents) {
+ /* post_len > 0 && hdr_cnt == 0 here */
+ /* We expect data, prev data portion is eaten by CGI
+ * and there *is* data to read from the peer
+ * (POSTDATA) */
+ //count = post_len > (int)sizeof(hdr_buf) ? (int)sizeof(hdr_buf) : post_len;
+ //count = safe_read(STDIN_FILENO, hdr_buf, count);
+ count = safe_read(STDIN_FILENO, hdr_buf, sizeof(hdr_buf));
+ if (count > 0) {
+ hdr_cnt = count;
+ hdr_ptr = hdr_buf;
+ post_len -= count;
+ } else {
+ /* no more POST data can be read */
+ post_len = 0;
+ }
+ }
+
+ if (pfd[FROM_CGI].revents) {
+ /* There is something to read from CGI */
+ char *rbuf = iobuf;
+
+ /* Are we still buffering CGI output? */
+ if (out_cnt >= 0) {
+ /* HTTP_200[] has single "\r\n" at the end.
+ * According to http://hoohoo.ncsa.uiuc.edu/cgi/out.html,
+ * CGI scripts MUST send their own header terminated by
+ * empty line, then data. That's why we have only one
+ * <cr><lf> pair here. We will output "200 OK" line
+ * if needed, but CGI still has to provide blank line
+ * between header and body */
+
+ /* Must use safe_read, not full_read, because
+ * CGI may output a few first bytes and then wait
+ * for POSTDATA without closing stdout.
+ * With full_read we may wait here forever. */
+ count = safe_read(fromCgi_rd, rbuf + out_cnt, PIPE_BUF - 8);
+ if (count <= 0) {
+ /* eof (or error) and there was no "HTTP",
+ * so write it, then write received data */
+ if (out_cnt) {
+ full_write(STDOUT_FILENO, HTTP_200, sizeof(HTTP_200)-1);
+ full_write(STDOUT_FILENO, rbuf, out_cnt);
+ }
+ break; /* CGI stdout is closed, exiting */
+ }
+ out_cnt += count;
+ count = 0;
+ /* "Status" header format is: "Status: 302 Redirected\r\n" */
+ if (out_cnt >= 8 && memcmp(rbuf, "Status: ", 8) == 0) {
+ /* send "HTTP/1.0 " */
+ if (full_write(STDOUT_FILENO, HTTP_200, 9) != 9)
+ break;
+ rbuf += 8; /* skip "Status: " */
+ count = out_cnt - 8;
+ out_cnt = -1; /* buffering off */
+ } else if (out_cnt >= 4) {
+ /* Did CGI add "HTTP"? */
+ if (memcmp(rbuf, HTTP_200, 4) != 0) {
+ /* there is no "HTTP", do it ourself */
+ if (full_write(STDOUT_FILENO, HTTP_200, sizeof(HTTP_200)-1) != sizeof(HTTP_200)-1)
+ break;
+ }
+ /* Commented out:
+ if (!strstr(rbuf, "ontent-")) {
+ full_write(s, "Content-type: text/plain\r\n\r\n", 28);
+ }
+ * Counter-example of valid CGI without Content-type:
+ * echo -en "HTTP/1.0 302 Found\r\n"
+ * echo -en "Location: http://www.busybox.net\r\n"
+ * echo -en "\r\n"
+ */
+ count = out_cnt;
+ out_cnt = -1; /* buffering off */
+ }
+ } else {
+ count = safe_read(fromCgi_rd, rbuf, PIPE_BUF);
+ if (count <= 0)
+ break; /* eof (or error) */
+ }
+ if (full_write(STDOUT_FILENO, rbuf, count) != count)
+ break;
+ if (DEBUG)
+ fprintf(stderr, "cgi read %d bytes: '%.*s'\n", count, count, rbuf);
+ } /* if (pfd[FROM_CGI].revents) */
+ } /* while (1) */
+ log_and_exit();
+}
+#endif
+