I'm trying to write a very basic HTTP server. I want to separate the server and the service in two separate executable (similar to inetd).
So I have a generic server, that forks a service and execute its code using exec. The main loop is as follows:
while(true) {
int client_sock;
if ((client_sock = accept(server_sock, (struct sockaddr *) NULL, NULL)) < 0) {
return -1;
}
pid_t pid = fork();
if (pid == 0) {
close(server_sock);
close(0);
dup(client_sock);
close(1);
dup(client_sock);
execvp(argv[2], argv+2);
return -1;
} else if (pid < 0) {
assert(false);
}
close(client_sock);
}
The service reads on stdin, and writes on stdout and processes simple http requests. Everything seems to work fine when I connect to my server using nc. nc receives and displays the expected http reply.
However, when I connect to my server using a web browser, the browser tells me that the server closed the connection abruptly (ECONNRESET error I believe). I'm sure that my service processed the request: I checked the execution of my service with strace, all the expected "write(1,...)" are there, and there are not any close(1). Even more surprising, some times, the web browser gets the reply.
Does anything look wrong in the provided code? if not, what could cause the problem?