server 0.0.1
basicserverinfrastructure

src/server/run.c

Go to the documentation of this file.
00001 #include <poll.h> /* for poll system call and related */
00002 #include <string.h>     /* for memset and stuff */
00003 #include <stdlib.h>     /* for exit */
00004 #include <errno.h>      /* for errno */
00005 #include <unistd.h>
00006 #include <time.h>
00007 
00008 #include "server.h"
00009 #include "socket.h"
00010 #include "logger.h"
00011 #include "signalHandling.h"
00012 #include "interface/class.h"
00013 #include "interface/stream_reader.h"
00014 #include "interface/logger.h"
00015 
00016 //* @TODO: to be removed
00017 #include "http/request.h"
00018 #include "http/request_parser.h"
00019 #include "http/request_queue.h"
00020 //* until here
00021 
00022 #undef  MAX
00023 #define MAX(x,y) ((x) > (y) ? (x) : (y))
00024 
00025 #include "poll.c"
00026 #include "handle_accept.c"
00027 #include "read.c"
00028 
00029 void
00030 serverRun(Server this)
00031 {
00032     loggerLog(this->logger, LOGGER_INFO, "service started");
00033 
00044     while (!doShutdown) /* until error or signal  */
00045     {
00046                 int          events;
00047                 unsigned int i;
00048 
00049                 events = serverPoll(this);
00050                 if (doShutdown) break;
00051 
00052                 for (i=0; i < events; i++) {
00053                         int fd = (this->fds)[i].fd;
00054                         //int nreads = 0, nwrites = 0;
00055 
00056                         if (0 != ((this->fds)[i].revents & POLLIN)) {
00060                                 if (this->sock->handle == (this->fds)[i].fd) {
00061                                         serverHandleAccept(this);
00062                                 }
00063 
00067                                 else {
00074                                         int size;
00075                                         if (0 < (size=serverRead(this, i))) {
00076                                                 int              j;
00077                                                 HttpRequestQueue queue =
00078                                                         ((HttpRequestParser)(this->conns)[fd].reader)->request_queue;
00079 
00080                                                 for (j=0; j<queue->nrequests; j++) {
00081                                                         HttpRequest request = queue->requests[j];
00082 
00083                                                         //if (NULL != request->body) {
00084                                                         //      puts("==REQUEST BODY==");
00085                                                         //      puts(request->body);
00086                                                         //}
00087 
00092                                                         delete(&request);
00093 
00097                                                         time_t t;
00098                                                         struct tm * tmp;
00099                                                         char timestr[200];
00100 
00101 #define RESP_HEAD "HTTP/1.1 404 Not Found\r\n" \
00102                                                         "Content-Type: text/html\r\n" \
00103                                                         "Content-Length: %lu\r\n" \
00104                                                         "Date: %s\r\n" \
00105                                                         "Server: testserver\r\n"
00106 
00107 #define RESP_DATA "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" \
00108                                                         "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" \
00109                                                         " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" \
00110                                                         "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" \
00111                                                         "<head><title>404 - Not Found</title></head>" \
00112                                                         "<body><h1>404 - Not Found</h1></body>" \
00113                                                         "</html>"
00114 
00115                                                         t = time(NULL);
00116                                                         tmp = localtime(&t);
00117                                                         strftime(timestr, sizeof(timestr), "%a, %d %b %Y %T %Z", tmp);
00118 
00123                                                         sprintf((this->conns)[fd].wbuf, RESP_HEAD "\r\n" RESP_DATA, sizeof(RESP_DATA), timestr);
00124                                                         (this->fds)[i].events = (this->fds)[i].events | POLLOUT;
00125                                                 }
00126 
00127                                                 queue->nrequests = 0;
00128                                         }
00129                                 }
00130                         }
00131 
00135                         if (0 != ((this->fds)[i].revents & POLLOUT)) {
00136                                 int size;
00137 
00138                                 size = write(
00139                                                 (this->fds)[i].fd,
00140                                                 (this->conns)[fd].wbuf,
00141                                                 strlen((this->conns)[fd].wbuf));
00142 
00143                                 if (size == strlen((this->conns)[fd].wbuf) ||
00144                                                 -1 == size) {
00145                                         if (-1 == size) {
00146                                                 loggerLog(this->logger, LOGGER_ERR,
00147                                                                 "write error, closing connection");
00148                                         }
00149 
00150                                         serverCloseConn(this, i);
00151                                 }
00152                                 else {
00153                                         memmove((this->conns)[fd].wbuf,
00154                                                         (this->conns)[fd].wbuf + size,
00155                                                         strlen((this->conns)[fd].wbuf) - size + 1);
00156                                 }
00157                         }
00158                 }
00159     }
00160 }
00161 
00162 // vim: set ts=4 sw=4:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines