run_8c.tex
9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
\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: