|
server 0.0.1
basicserverinfrastructure
|
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 { 00068 serverRead(this, i); 00069 00076 { 00077 int j; 00078 HttpRequestQueue queue = 00079 ((HttpRequestParser)(this->conns)[fd].reader)->request_queue; 00080 00081 for (j=0; j<queue->nrequests; j++) { 00082 HttpRequest request = queue->requests[j]; 00083 00088 delete(&request); 00089 00093 time_t t; 00094 struct tm * tmp; 00095 char timestr[200]; 00096 00097 #define RESP_HEAD "HTTP/1.1 404 Not Found\r\n" \ 00098 "Content-Type: text/html\r\n" \ 00099 "Content-Length: %lu\r\n" \ 00100 "Date: %s\r\n" \ 00101 "Server: testserver\r\n" 00102 00103 #define RESP_DATA "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" \ 00104 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" \ 00105 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" \ 00106 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" \ 00107 "<head><title>404 - Not Found</title></head>" \ 00108 "<body><h1>404 - Not Found</h1></body>" \ 00109 "</html>" 00110 00111 t = time(NULL); 00112 tmp = localtime(&t); 00113 strftime(timestr, sizeof(timestr), "%a, %d %b %Y %T %Z", tmp); 00114 00119 sprintf((this->conns)[fd].wbuf, RESP_HEAD "\r\n" RESP_DATA, sizeof(RESP_DATA), timestr); 00120 (this->fds)[i].events = (this->fds)[i].events | POLLOUT; 00121 } 00122 00123 queue->nrequests = 0; 00124 } 00125 } 00126 } 00127 00131 if (0 != ((this->fds)[i].revents & POLLOUT)) { 00132 write( 00133 (this->fds)[i].fd, 00134 (this->conns)[fd].wbuf, 00135 strlen((this->conns)[fd].wbuf)); 00136 (this->fds)[i].events = (this->fds)[i].events & ~POLLOUT; 00137 serverCloseConn(this, i); 00138 } 00139 } 00140 } 00141 } 00142 00143 // vim: set ts=4 sw=4: