mirror of https://github.com/nealey/eris.git
POST working: passing regression tests now
This commit is contained in:
parent
03d532240a
commit
4536afe4a6
81
eris.c
81
eris.c
|
@ -448,51 +448,63 @@ start_cgi(int nph, const char *pathinfo, const char *const *envp)
|
|||
int pid;
|
||||
char cgiheader[BUFFER_SIZE];
|
||||
size_t cgiheaderlen = BUFFER_SIZE;
|
||||
int fd[2],
|
||||
df[2];
|
||||
FILE *cin;
|
||||
int cin[2];
|
||||
int cout[2];
|
||||
FILE *cinf;
|
||||
|
||||
if (pipe(fd) || pipe(df) || !(cin = fdopen(fd[0], "r"))) {
|
||||
if (pipe(cin) || pipe(cout) || !(cinf = fdopen(cin[0], "rb"))) {
|
||||
badrequest(500, "Internal Server Error",
|
||||
"Server Resource problem.");
|
||||
}
|
||||
|
||||
if ((pid = fork())) {
|
||||
if (pid > 0) {
|
||||
int nfds;
|
||||
fd_set rfds, wfds;
|
||||
pid = fork();
|
||||
if (-1 == pid) {
|
||||
badrequest(500, "Internal Server Error",
|
||||
"Unable to fork.");
|
||||
}
|
||||
if (pid) {
|
||||
/* Parent */
|
||||
int passthru = nph;
|
||||
|
||||
fcntl(fd[0], F_SETFL, O_NONBLOCK);
|
||||
fcntl(cin[0], F_SETFL, O_NONBLOCK);
|
||||
signal(SIGCHLD, cgi_child);
|
||||
signal(SIGPIPE, SIG_IGN); /* NO! no signal! */
|
||||
|
||||
close(df[0]);
|
||||
close(fd[1]);
|
||||
close(cin[1]);
|
||||
close(cout[0]);
|
||||
|
||||
alarm(CGI_TIMEOUT);
|
||||
|
||||
while (1) {
|
||||
int nfds;
|
||||
fd_set rfds, wfds;
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_ZERO(&wfds);
|
||||
FD_SET(fd[0], &rfds);
|
||||
nfds = fd[0];
|
||||
FD_SET(cin[0], &rfds);
|
||||
nfds = cin[0];
|
||||
|
||||
if (post_len) {
|
||||
/* have post data */
|
||||
FD_SET(df[0], &wfds);
|
||||
if (df[0] > nfds) {
|
||||
nfds = df[0];
|
||||
FD_SET(cout[1], &wfds);
|
||||
if (cout[1] > nfds) {
|
||||
nfds = cout[1];
|
||||
}
|
||||
} else if (df[1] >= 0) {
|
||||
close(df[1]); /* no post data */
|
||||
df[1] = -1;
|
||||
} else if (cout[1] >= 0) {
|
||||
close(cout[1]); /* no post data */
|
||||
cout[1] = -1;
|
||||
}
|
||||
|
||||
while (select(nfds+1, &rfds, &wfds, NULL, NULL) != -1) {
|
||||
if (FD_ISSET(fd[0], &rfds)) {
|
||||
if (-1 == select(nfds+1, &rfds, &wfds, NULL, NULL)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (FD_ISSET(cin[0], &rfds)) {
|
||||
if (passthru) {
|
||||
size_t len;
|
||||
|
||||
/* Re-use this big buffer */
|
||||
len = fread(cgiheader, 1, sizeof cgiheader, cin);
|
||||
len = fread(cgiheader, 1, sizeof cgiheader, cinf);
|
||||
if (0 == len) {
|
||||
/* CGI is done */
|
||||
break;
|
||||
|
@ -502,7 +514,7 @@ start_cgi(int nph, const char *pathinfo, const char *const *envp)
|
|||
} else {
|
||||
int ret;
|
||||
|
||||
ret = read_header(cin, cgiheader, &cgiheaderlen);
|
||||
ret = read_header(cinf, cgiheader, &cgiheaderlen);
|
||||
if (0 == ret) {
|
||||
/* Call read_header again */
|
||||
} else if (-1 == ret) {
|
||||
|
@ -534,7 +546,7 @@ start_cgi(int nph, const char *pathinfo, const char *const *envp)
|
|||
fwrite(cgiheader, 1, cgiheaderlen, stdout);
|
||||
}
|
||||
}
|
||||
} else if (FD_ISSET(df[1], &wfds)) {
|
||||
} else if (FD_ISSET(cout[1], &wfds)) {
|
||||
/*
|
||||
* write to cgi the post data
|
||||
*/
|
||||
|
@ -548,12 +560,13 @@ start_cgi(int nph, const char *pathinfo, const char *const *envp)
|
|||
break;
|
||||
}
|
||||
post_len -= len;
|
||||
write(df[1], buf, len);
|
||||
write(cout[1], buf, len);
|
||||
} else {
|
||||
close(df[1]);
|
||||
close(cout[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
alarm(0);
|
||||
|
||||
fflush(stdout);
|
||||
dolog(size);
|
||||
|
@ -563,18 +576,18 @@ start_cgi(int nph, const char *pathinfo, const char *const *envp)
|
|||
setsockopt(1, IPPROTO_TCP, TCP_CORK, &zero, sizeof(zero));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
close(df[1]);
|
||||
close(fd[0]);
|
||||
/* Child */
|
||||
|
||||
dup2(df[0], 0);
|
||||
dup2(fd[1], 1);
|
||||
close(cout[1]);
|
||||
close(cin[0]);
|
||||
|
||||
close(df[0]);
|
||||
close(fd[1]);
|
||||
dup2(cout[0], 0);
|
||||
dup2(cin[1], 1);
|
||||
|
||||
close(cout[0]);
|
||||
close(cin[1]);
|
||||
|
||||
alarm(CGI_TIMEOUT);
|
||||
do_cgi(pathinfo, envp);
|
||||
}
|
||||
exit(0);
|
||||
|
|
|
@ -70,6 +70,7 @@ class BasicTests(LinesTests):
|
|||
so, se = self.p.communicate(h.encode('utf-8'))
|
||||
return (so, se)
|
||||
|
||||
|
||||
class DirTests(BasicTests):
|
||||
args = ['-d']
|
||||
|
||||
|
@ -106,7 +107,7 @@ class CGITests(BasicTests):
|
|||
|
||||
def testPost(self):
|
||||
so, se = self.post('/cgi/set.cgi', 'default', 'a=1&b=2&c=3')
|
||||
self.assertLinesEqual(se, b'10.1.2.3 200 330 default (null) (null) /cgi/set.cgi\n')
|
||||
self.assertLinesEqual(se, b'10.1.2.3 200 306 default (null) (null) /cgi/set.cgi\n')
|
||||
self.assertLinesEqual(so, b'HTTP/1.0 200 OK\r\nServer: eris/2.0\r\nPragma: no-cache\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\nGATEWAY_INTERFACE:CGI/1.1\nSERVER_PROTOCOL:HTTP/1.0\nSERVER_SOFTWARE:eris/2.0\nSERVER_NAME:default\nSERVER_PORT:80\nREQUEST_METHOD:POST\nREQUEST_URI:/cgi/set.cgi\nSCRIPT_NAME:/cgi/set.cgi\nREMOTE_ADDR:10.1.2.3\nREMOTE_PORT:5858\nCONTENT_TYPE:application/x-www-form-urlencoded\nCONTENT_LENGTH:11\nForm data: a=1&b=2&c=3')
|
||||
|
||||
# XXX: Test posting to static html with keepalive
|
||||
|
|
Loading…
Reference in New Issue