server 0.0.1
basicserverinfrastructure

src/server/run.c File Reference

#include <poll.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#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"
#include "http/request.h"
#include "http/request_parser.h"
#include "http/request_queue.h"
#include "poll.c"
#include "handle_accept.c"
#include "read.c"
Include dependency graph for run.c:

Go to the source code of this file.

Defines

#define MAX(x, y)   ((x) > (y) ? (x) : (y))
#define RESP_HEAD
#define RESP_DATA

Functions

void serverRun (Server this)

Define Documentation

#define MAX (   x,
 
)    ((x) > (y) ? (x) : (y))

Definition at line 23 of file run.c.

#define RESP_DATA
Value:
"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" \
                                                        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" \
                                                        " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" \
                                                        "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" \
                                                        "<head><title>404 - Not Found</title></head>" \
                                                        "<body><h1>404 - Not Found</h1></body>" \
                                                        "</html>"
#define RESP_HEAD
Value:
"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"

Function Documentation

void serverRun ( Server  this)

: 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.

handle accept

handle reads

do some other processing : actually this will hard assume that our stream reader is a http parser and it has its queue...think about more generalizing here.

: for now simply remove request and send not found. Make this sane.

: the complete response stuff have to be removed here.

: just to send an answer and be able to make some apache benchs i do it here...this definetly MUST BE moved

handle writes

Definition at line 30 of file run.c.

{
    loggerLog(this->logger, LOGGER_INFO, "service started");

    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)) {
                                if (this->sock->handle == (this->fds)[i].fd) {
                                        serverHandleAccept(this);
                                }

                                else {
                                        serverRead(this, i);

                                        {
                                                int              j;
                                                HttpRequestQueue queue =
                                                        ((HttpRequestParser)(this->conns)[fd].reader)->request_queue;

                                                for (j=0; j<queue->nrequests; j++) {
                                                        HttpRequest request = queue->requests[j];

                                                        delete(&request);

                                                        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 "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" \
                                                        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" \
                                                        " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" \
                                                        "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" \
                                                        "<head><title>404 - Not Found</title></head>" \
                                                        "<body><h1>404 - Not Found</h1></body>" \
                                                        "</html>"

                                                        t = time(NULL);
                                                        tmp = localtime(&t);
                                                        strftime(timestr, sizeof(timestr), "%a, %d %b %Y %T %Z", tmp);

                                                        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;
                                        }
                                }
                        }

                        if (0 != ((this->fds)[i].revents & POLLOUT)) {
                                write(
                                                (this->fds)[i].fd,
                                                (this->conns)[fd].wbuf,
                                                strlen((this->conns)[fd].wbuf));
                                (this->fds)[i].events = (this->fds)[i].events & ~POLLOUT;
                                serverCloseConn(this, i);
                        }
                }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines