run_8c.tex 9.19 KB
\hypertarget{run_8c}{
\section{src/server/run.c File Reference}
\label{run_8c}\index{src/server/run.c@{src/server/run.c}}
}
{\ttfamily \#include $<$poll.h$>$}\par
{\ttfamily \#include $<$string.h$>$}\par
{\ttfamily \#include $<$stdlib.h$>$}\par
{\ttfamily \#include $<$errno.h$>$}\par
{\ttfamily \#include $<$unistd.h$>$}\par
{\ttfamily \#include $<$time.h$>$}\par
{\ttfamily \#include \char`\"{}server.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}socket.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}logger.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}signalHandling.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}interface/class.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}interface/stream\_\-reader.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}interface/logger.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}http/request.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}http/request\_\-parser.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}http/request\_\-queue.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}poll.c\char`\"{}}\par
{\ttfamily \#include \char`\"{}handle\_\-accept.c\char`\"{}}\par
{\ttfamily \#include \char`\"{}read.c\char`\"{}}\par
Include dependency graph for run.c:
\subsection*{Defines}
\begin{DoxyCompactItemize}
\item 
\#define \hyperlink{run_8c_aacc3ee1a7f283f8ef65cea31f4436a95}{MAX}(x, y)~((x) $>$ (y) ? (x) : (y))
\item 
\#define \hyperlink{run_8c_a62de8b169a3e2f03f0e793aee87b3758}{RESP\_\-HEAD}
\item 
\#define \hyperlink{run_8c_a668b468f247abfce706ce6eaf6caecc9}{RESP\_\-DATA}
\end{DoxyCompactItemize}
\subsection*{Functions}
\begin{DoxyCompactItemize}
\item 
void \hyperlink{run_8c_a67a4d9073b80d8ae1459fb11ddd23675}{serverRun} (\hyperlink{structServer}{Server} this)
\end{DoxyCompactItemize}


\subsection{Define Documentation}
\hypertarget{run_8c_aacc3ee1a7f283f8ef65cea31f4436a95}{
\index{run.c@{run.c}!MAX@{MAX}}
\index{MAX@{MAX}!run.c@{run.c}}
\subsubsection[{MAX}]{\setlength{\rightskip}{0pt plus 5cm}\#define MAX(
\begin{DoxyParamCaption}
\item[{}]{x, }
\item[{}]{y}
\end{DoxyParamCaption}
)~((x) $>$ (y) ? (x) : (y))}}
\label{run_8c_aacc3ee1a7f283f8ef65cea31f4436a95}


Definition at line 23 of file run.c.

\hypertarget{run_8c_a668b468f247abfce706ce6eaf6caecc9}{
\index{run.c@{run.c}!RESP\_\-DATA@{RESP\_\-DATA}}
\index{RESP\_\-DATA@{RESP\_\-DATA}!run.c@{run.c}}
\subsubsection[{RESP\_\-DATA}]{\setlength{\rightskip}{0pt plus 5cm}\#define RESP\_\-DATA}}
\label{run_8c_a668b468f247abfce706ce6eaf6caecc9}
{\bfseries Value:}
\begin{DoxyCode}
"<?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 F
      ound</title></head>" \
                                                        "<body><h1>404 - Not Foun
      d</h1></body>" \
                                                        "</html>"
\end{DoxyCode}
\hypertarget{run_8c_a62de8b169a3e2f03f0e793aee87b3758}{
\index{run.c@{run.c}!RESP\_\-HEAD@{RESP\_\-HEAD}}
\index{RESP\_\-HEAD@{RESP\_\-HEAD}!run.c@{run.c}}
\subsubsection[{RESP\_\-HEAD}]{\setlength{\rightskip}{0pt plus 5cm}\#define RESP\_\-HEAD}}
\label{run_8c_a62de8b169a3e2f03f0e793aee87b3758}
{\bfseries Value:}
\begin{DoxyCode}
"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"
\end{DoxyCode}


\subsection{Function Documentation}
\hypertarget{run_8c_a67a4d9073b80d8ae1459fb11ddd23675}{
\index{run.c@{run.c}!serverRun@{serverRun}}
\index{serverRun@{serverRun}!run.c@{run.c}}
\subsubsection[{serverRun}]{\setlength{\rightskip}{0pt plus 5cm}void serverRun (
\begin{DoxyParamCaption}
\item[{{\bf Server}}]{this}
\end{DoxyParamCaption}
)}}
\label{run_8c_a67a4d9073b80d8ae1459fb11ddd23675}


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


\begin{DoxyCode}
{
    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 = que
      ue->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 F
      ound</title></head>" \
                                                        "<body><h1>404 - Not Foun
      d</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);
                        }
                }
    }
}
\end{DoxyCode}


Here is the call graph for this function:




Here is the caller graph for this function: