mirror of
https://github.com/fumiama/simple-http-server.git
synced 2026-06-05 00:30:23 +08:00
fix fail loop in cgi
This commit is contained in:
25
server.c
25
server.c
@@ -258,15 +258,18 @@ static void execute_cgi(int client, int content_length, const HTTP_REQUEST* requ
|
|||||||
if(cnt > 0) {
|
if(cnt > 0) {
|
||||||
write(cgi_input[1], buf, cnt);
|
write(cgi_input[1], buf, cnt);
|
||||||
i += cnt;
|
i += cnt;
|
||||||
|
} else {
|
||||||
|
cannot_execute(client);
|
||||||
|
goto CGI_CLOSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
int len = 0;
|
int len = 0;
|
||||||
while(len < content_length) {
|
while(len < content_length) {
|
||||||
int delta = splice(client, NULL, cgi_input[1], NULL, content_length - len, SPLICE_F_GIFT);
|
int delta = splice(client, NULL, cgi_input[1], NULL, content_length - len, SPLICE_F_GIFT);
|
||||||
if(delta < 0) {
|
if(delta <= 0) {
|
||||||
cannot_execute(client);
|
cannot_execute(client);
|
||||||
break;
|
goto CGI_CLOSE;
|
||||||
}
|
}
|
||||||
len += delta;
|
len += delta;
|
||||||
}
|
}
|
||||||
@@ -278,7 +281,10 @@ static void execute_cgi(int client, int content_length, const HTTP_REQUEST* requ
|
|||||||
while(p - (char*)&cnt < sizeof(uint32_t)) {
|
while(p - (char*)&cnt < sizeof(uint32_t)) {
|
||||||
int offset = read(cgi_output[0], p, sizeof(uint32_t));
|
int offset = read(cgi_output[0], p, sizeof(uint32_t));
|
||||||
if(offset > 0) p += offset;
|
if(offset > 0) p += offset;
|
||||||
else cannot_execute(client);
|
else {
|
||||||
|
cannot_execute(client);
|
||||||
|
goto CGI_CLOSE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
printf("CGI msg len: %u bytes.\n", cnt);
|
printf("CGI msg len: %u bytes.\n", cnt);
|
||||||
if(cnt > 0) {
|
if(cnt > 0) {
|
||||||
@@ -286,22 +292,29 @@ static void execute_cgi(int client, int content_length, const HTTP_REQUEST* requ
|
|||||||
#if __APPLE__
|
#if __APPLE__
|
||||||
char* data = malloc(cnt);
|
char* data = malloc(cnt);
|
||||||
while(len < cnt) {
|
while(len < cnt) {
|
||||||
len += read(cgi_output[0], data, cnt);
|
int n = read(cgi_output[0], data, cnt);
|
||||||
|
if(n <= 0) {
|
||||||
|
if(data) free(data);
|
||||||
|
cannot_execute(client);
|
||||||
|
goto CGI_CLOSE;
|
||||||
|
}
|
||||||
|
len += n;
|
||||||
send(client, data, len, 0);
|
send(client, data, len, 0);
|
||||||
}
|
}
|
||||||
if(data) free(data);
|
if(data) free(data);
|
||||||
#else
|
#else
|
||||||
while(len < cnt) {
|
while(len < cnt) {
|
||||||
int delta = splice(cgi_output[0], NULL, client, NULL, cnt - len, SPLICE_F_GIFT);
|
int delta = splice(cgi_output[0], NULL, client, NULL, cnt - len, SPLICE_F_GIFT);
|
||||||
if(delta < 0) {
|
if(delta <= 0) {
|
||||||
cannot_execute(client);
|
cannot_execute(client);
|
||||||
break;
|
goto CGI_CLOSE;
|
||||||
}
|
}
|
||||||
len += delta;
|
len += delta;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
printf("CGI send %d bytes\n", len);
|
printf("CGI send %d bytes\n", len);
|
||||||
}
|
}
|
||||||
|
CGI_CLOSE:
|
||||||
close(cgi_output[0]);
|
close(cgi_output[0]);
|
||||||
close(cgi_input[1]);
|
close(cgi_input[1]);
|
||||||
waitpid(pid, &i, 0);
|
waitpid(pid, &i, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user