Since Friday afternoon, I have been working on JayantHTTPD trying to get it to serve up pages threaded style, using a worker thread model. It took research on Friday night and all day Saturday to get it going, but I have succeeded.
The first main issue I had was figuring out how to dynamically allocate an array for the number of threads set in the configuration file. Since I’m used to C++, we never malloc’d our arrays, we used things like std::vector. The solution to this issue was amazingly simplistic. Just a threadinfo_t* workers = (threadinfo_t*)malloc((sizeof(thread_info_t)*server->maxthreads));
The next bug I found coming from the old crappy threading system was that it closed stdin, stdout, and stderr… I have no clue how I didn’t see this, nor why I’m even mentioning this embarrassing, n00by mistake, but I am.
Then once I had all of this handled, I had the file descriptors getting completely owned. I’m still not 100% sure what was up with them, but for instance, changing the following:
if( (fd = accept(mast_fd, (struct sockaddr *)&their_addr, &sin_size)) != -1)
{
dprintf("fd %d accepted from fd %d\n",fd,mast_fd);
add_request(fd, &request_mutex, &got_request);
}
to
fd = accept(mast_fd, (struct sockaddr *)&their_addr, &sin_size);
if(fd != -1)
{
dprintf("fd %d accepted from fd %d\n",fd,mast_fd);
add_request(fd, &request_mutex, &got_request);
}
Fixed one issue. The first code snippet will always return fd 1. No idea why, which caused some interesting actions when the thread went to send the page.
After this was fixed, I had to go through and convert some library calls to be thread safe, but this sadly still didn’t fix the handling of the requests.
As of this very moment, I’m still working on the actual handling of the requests. Right now it sends a 200 OK then dies before another if statement. Example:
if(send(fd, "HTTP/1.1 200 OK\n", 16, 0) == -1)
{
dprintf("Failed to send Status 200 OK\n");
fclose(in);
goto cleanup;
}
dprintf("Send 200 OK\n");
/* Dies here for some reason... */
sprintf(tempstring,"Server: %s\n",server->signature);
So this is how I spent my weekend. You can see my active social life gleeming through, eh?

