Another abandoned server code base... this is kind of an ancestor of taskrambler.
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.
 
 
 
 
 
 

162 lines
4.2 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 <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"
//* @TODO: to be removed
#include "http/request.h"
#include "http/request/parser.h"
#include "http/request/queue.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 < events; i++) {
int fd = (this->fds)[i].fd;
//int nreads = 0, nwrites = 0;
if (0 != ((this->fds)[i].revents & POLLIN)) {
/**
* handle accept
*/
if (this->sock->handle == (this->fds)[i].fd) {
serverHandleAccept(this);
}
/**
* handle reads
*/
else {
/**
* 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))) {
int j;
HttpRequestQueue queue =
((HttpRequestParser)(this->conns)[fd].reader)->request_queue;
for (j=0; j<queue->nrequests; j++) {
HttpRequest request = queue->requests[j];
//if (NULL != request->body) {
// puts("==REQUEST BODY==");
// puts(request->body);
//}
/**
* @TODO: for now simply remove request and send not found.
* Make this sane.
*/
delete(&request);
/**
* @TODO: the complete response stuff have to be removed here.
*/
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);
/**
* @TODO: just to send an answer and be able to make some
* apache benchs i do it here...this definetly MUST BE moved
*/
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;
}
}
}
/**
* handle writes
*/
if (0 != ((this->fds)[i].revents & POLLOUT)) {
int size;
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");
}
serverCloseConn(this, i);
}
else {
memmove((this->conns)[fd].wbuf,
(this->conns)[fd].wbuf + size,
strlen((this->conns)[fd].wbuf) - size + 1);
}
}
}
}
}
// vim: set ts=4 sw=4: