#include /* for poll system call and related */ #include /* for memset and stuff */ #include /* for exit */ #include /* for errno */ #include #include #include "server.h" #include "socket.h" #include "logger.h" #include "signalHandling.h" #include "interface/class.h" #include "interface/stream_reader.h" #include "interface/logger.h" //* @TODO: to be removed #include "http/request.h" #include "http/request/parser.h" #include "http/request/queue.h" //* until here #undef MAX #define MAX(x,y) ((x) > (y) ? (x) : (y)) #include "poll.c" #include "handle_accept.c" #include "read.c" void serverRun(Server this) { loggerLog(this->logger, LOGGER_INFO, "service started"); /** * @TODO: actually this is the main loop of my server. When * stuff becomes more complicated it might be feasabible to * split stuff into separate processes. This will definetly * involve some IPC and syncing. Right now as this is actually * only a simple HTTP server implementation we go on with * this single process. * What we can first do to get some processing between read/write * cicles is to use the poll timeout. */ while (!doShutdown) /* until error or signal */ { int events; unsigned int i; events = serverPoll(this); if (doShutdown) break; for (i=0; i < events; i++) { int fd = (this->fds)[i].fd; //int nreads = 0, nwrites = 0; if (0 != ((this->fds)[i].revents & POLLIN)) { /** * handle accept */ if (this->sock->handle == (this->fds)[i].fd) { serverHandleAccept(this); } /** * handle reads */ else { /** * do some other processing * @TODO: actually this will hard assume that our stream reader * is a http parser and it has its queue...think about more * generalizing here. */ int size; if (0 < (size=serverRead(this, i))) { int j; HttpRequestQueue queue = ((HttpRequestParser)(this->conns)[fd].reader)->request_queue; for (j=0; jnrequests; j++) { HttpRequest request = queue->requests[j]; //if (NULL != request->body) { // puts("==REQUEST BODY=="); // puts(request->body); //} /** * @TODO: for now simply remove request and send not found. * Make this sane. */ delete(&request); /** * @TODO: the complete response stuff have to be removed here. */ time_t t; struct tm * tmp; char timestr[200]; #define RESP_HEAD "HTTP/1.1 404 Not Found\r\n" \ "Content-Type: text/html\r\n" \ "Content-Length: %lu\r\n" \ "Date: %s\r\n" \ "Server: testserver\r\n" #define RESP_DATA "\n" \ "\n" \ "\n" \ "404 - Not Found" \ "

404 - Not Found

" \ "" t = time(NULL); tmp = localtime(&t); strftime(timestr, sizeof(timestr), "%a, %d %b %Y %T %Z", tmp); /** * @TODO: just to send an answer and be able to make some * apache benchs i do it here...this definetly MUST BE moved */ sprintf((this->conns)[fd].wbuf, RESP_HEAD "\r\n" RESP_DATA, sizeof(RESP_DATA), timestr); (this->fds)[i].events = (this->fds)[i].events | POLLOUT; } queue->nrequests = 0; } } } /** * handle writes */ if (0 != ((this->fds)[i].revents & POLLOUT)) { int size; size = write( (this->fds)[i].fd, (this->conns)[fd].wbuf, strlen((this->conns)[fd].wbuf)); if (size == strlen((this->conns)[fd].wbuf) || -1 == size) { if (-1 == size) { loggerLog(this->logger, LOGGER_ERR, "write error, closing connection"); } serverCloseConn(this, i); } else { memmove((this->conns)[fd].wbuf, (this->conns)[fd].wbuf + size, strlen((this->conns)[fd].wbuf) - size + 1); } } } } } // vim: set ts=4 sw=4: