You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
4.3 KiB
188 lines
4.3 KiB
#include <poll.h> /* for poll system call and related */
|
|
#include <string.h> /* for memset and stuff */
|
|
#include <stdlib.h> /* for exit */
|
|
#include <errno.h> /* for errno */
|
|
#include <unistd.h>
|
|
#include <ctype.h>
|
|
#include <time.h>
|
|
#include <errno.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"
|
|
|
|
//* @TODO: to be removed
|
|
#include "http/request.h"
|
|
#include "http/request/parser.h"
|
|
#include "http/request/queue.h"
|
|
#include "http/response.h"
|
|
//* until here
|
|
|
|
#undef MAX
|
|
#define MAX(x,y) ((x) > (y) ? (x) : (y))
|
|
|
|
#include "poll.c"
|
|
#include "handle_accept.c"
|
|
#include "read.c"
|
|
|
|
void
|
|
serverRun(Server this)
|
|
{
|
|
loggerLog(this->logger, LOGGER_INFO, "service started");
|
|
|
|
/**
|
|
* @TODO: 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.
|
|
*/
|
|
while (!doShutdown) /* until error or signal */
|
|
{
|
|
int events;
|
|
unsigned int i;
|
|
|
|
events = serverPoll(this);
|
|
if (doShutdown) break;
|
|
|
|
for (i=0; i < this->nfds; i++) {
|
|
int fd = (this->fds)[i].fd;
|
|
int naccs = 10, nreads = 10, nwrites = 10;
|
|
|
|
if (0 >= events) break;
|
|
|
|
if (0 != ((this->fds)[i].revents & POLLIN) && 0 < nreads) {
|
|
events--;
|
|
|
|
/**
|
|
* handle accept
|
|
*/
|
|
if (this->sock->handle == (this->fds)[i].fd) {
|
|
while(-1 != serverHandleAccept(this) && 0 < naccs) {
|
|
naccs--;
|
|
|
|
switch(errno) {
|
|
case EAGAIN:
|
|
loggerLog(this->logger,
|
|
LOGGER_DEBUG,
|
|
"server accept blocks");
|
|
break;
|
|
|
|
default:
|
|
loggerLog(this->logger,
|
|
LOGGER_DEBUG,
|
|
"server accept error");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* handle reads
|
|
*/
|
|
else {
|
|
nreads--;
|
|
/**
|
|
* do some other processing
|
|
* @TODO: actually this will hard assume that our stream reader
|
|
* is a http parser and it has its queue...think about more
|
|
* generalizing here.
|
|
*/
|
|
int size;
|
|
|
|
if (0 >= (size=serverRead(this, i))) {
|
|
serverCloseConn(this, i);
|
|
}
|
|
else {
|
|
int j;
|
|
HttpRequestQueue queue =
|
|
((HttpRequestParser)(this->conns)[fd].reader)->request_queue;
|
|
|
|
for (j=0; j<queue->nrequests; j++) {
|
|
HttpResponse response;
|
|
|
|
/**
|
|
* @TODO: for now simply remove request and send not found.
|
|
* Make this sane.
|
|
*/
|
|
response = httpResponse404();
|
|
|
|
if (httpRequestHasKeepAlive(queue->requests[j])) {
|
|
(this->conns)[fd].keep_alive = 1;
|
|
httpResponseHeaderSet(
|
|
response,
|
|
"Connection",
|
|
"Keep-Alive");
|
|
}
|
|
else {
|
|
(this->conns)[fd].keep_alive = 0;
|
|
httpResponseHeaderSet(
|
|
response,
|
|
"Connection",
|
|
"Close");
|
|
}
|
|
|
|
delete(&(queue->requests[j]));
|
|
|
|
(this->conns)[fd].wbuf = calloc(
|
|
1, httpResponseSizeGet(response) + 1);
|
|
httpResponseToString(response, (this->conns)[fd].wbuf);
|
|
|
|
delete(&response);
|
|
|
|
(this->fds)[i].events |= POLLOUT;
|
|
}
|
|
|
|
queue->nrequests = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* handle writes
|
|
*/
|
|
if (0 != ((this->fds)[i].revents & POLLOUT) && 0 < nwrites) {
|
|
int size;
|
|
|
|
events--;
|
|
nwrites--;
|
|
|
|
size = write(
|
|
(this->fds)[i].fd,
|
|
(this->conns)[fd].wbuf,
|
|
strlen((this->conns)[fd].wbuf));
|
|
|
|
if (size == strlen((this->conns)[fd].wbuf) ||
|
|
-1 == size) {
|
|
if (-1 == size) {
|
|
loggerLog(this->logger, LOGGER_ERR,
|
|
"write error, closing connection");
|
|
}
|
|
|
|
if ((this->conns)[fd].keep_alive) {
|
|
(this->fds)[i].events &= ~POLLOUT;
|
|
}
|
|
else {
|
|
serverCloseConn(this, i);
|
|
}
|
|
free((this->conns)[fd].wbuf);
|
|
(this->conns)[fd].wbuf = NULL;
|
|
}
|
|
else {
|
|
memmove((this->conns)[fd].wbuf,
|
|
(this->conns)[fd].wbuf + size,
|
|
strlen((this->conns)[fd].wbuf) - size + 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// vim: set ts=4 sw=4:
|