From 7f688412eac190e43f86501a6b95fd22a88a858d Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Mon, 20 Feb 2012 21:36:55 +0100 Subject: [PATCH] some code cleanups...no changes in the logic --- ChangeLog | 6 +++- include/hash.h | 8 ----- include/server.h | 1 - .../message/helper.c => include/utils/hash.h | 31 +++---------------- include/utils/memory.h | 28 +++++++++++++++++ include/{ => utils}/signalHandling.h | 6 ++-- src/Makefile.am | 6 ++-- src/http/header.c | 4 +-- src/http/header/add.c | 3 +- src/http/header/get.c | 2 +- src/http/message.c | 14 +++++++-- src/http/request.c | 6 ++-- src/http/request/parser.c | 3 +- src/http/request/parser/get_header.c | 10 +++--- src/http/request/parser/get_request_line.c | 3 +- src/http/request/parser/parse.c | 3 -- src/http/response.c | 4 +-- src/http/response/304.c | 2 -- src/http/response/image.c | 2 -- src/http/response/writer.c | 3 +- src/http/response/writer/write.c | 5 --- src/server.c | 4 --- src/server/handle_accept.c | 16 ++-------- src/server/poll.c | 6 +++- src/server/read.c | 5 ++- src/server/run.c | 14 ++++----- src/testserver.c | 4 +-- src/{ => utils}/daemonize.c | 0 src/{ => utils}/hash.c | 2 +- src/utils/memory.c | 14 +++++++++ src/{ => utils}/signalHandling.c | 0 31 files changed, 107 insertions(+), 108 deletions(-) delete mode 100644 include/hash.h rename src/http/message/helper.c => include/utils/hash.h (70%) create mode 100644 include/utils/memory.h rename include/{ => utils}/signalHandling.h (89%) rename src/{ => utils}/daemonize.c (100%) rename src/{ => utils}/hash.c (97%) create mode 100644 src/utils/memory.c rename src/{ => utils}/signalHandling.c (100%) diff --git a/ChangeLog b/ChangeLog index 7b9672d..aa58a80 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,10 @@ +2012-02-20 21:36:55 +0100 Georg Hopp + + * some code cleanups...no changes in the logic (HEAD, master) + 2012-02-20 18:08:23 +0100 Georg Hopp - * move sdbm implementation in one file. (HEAD, master) + * move sdbm implementation in one file. 2012-02-20 17:16:44 +0100 Georg Hopp diff --git a/include/hash.h b/include/hash.h deleted file mode 100644 index 6ee4af2..0000000 --- a/include/hash.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __HASH_H__ -#define __HASH_H__ - -unsigned long sdbm(const unsigned char *); - -#endif // __HASH_H__ - -// vim: set ts=4 sw=4: diff --git a/include/server.h b/include/server.h index ce11c54..40d8817 100644 --- a/include/server.h +++ b/include/server.h @@ -63,7 +63,6 @@ CLASS(Server) { }; void serverRun(Server this); -void serverCloseConn(Server this, unsigned int handle); #endif // __SERVER_H__ diff --git a/src/http/message/helper.c b/include/utils/hash.h similarity index 70% rename from src/http/message/helper.c rename to include/utils/hash.h index 7d0ef1b..cd2345d 100644 --- a/src/http/message/helper.c +++ b/include/utils/hash.h @@ -1,5 +1,6 @@ /** * \file + * Functions to handle varios signals send to the application. * * \author Georg Hopp * @@ -20,33 +21,11 @@ * along with this program. If not, see . */ -#ifndef __HTTP_MESSAGE_HELPER_C__ -#define __HTTP_MESSAGE_HELPER_C__ +#ifndef __UTILS_HASH_H__ +#define __UTILS_HASH_H__ -#include +unsigned long sdbm(const unsigned char *); -#include "class.h" -#include "interface/class.h" - - -static -inline -void -_free(void ** data) -{ - if (NULL != *data) { - free(*data); - } -} - -static -inline -void -tDelete(void * node) -{ - delete(&node); -} - -#endif // __HTTP_MESSAGE_HELPER_C__ +#endif // __UTILS_HASH_H__ // vim: set ts=4 sw=4: diff --git a/include/utils/memory.h b/include/utils/memory.h new file mode 100644 index 0000000..9ea0643 --- /dev/null +++ b/include/utils/memory.h @@ -0,0 +1,28 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright (C) 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __UTILS_MEMORY_H__ +#define __UTILS_MEMORY_H__ + +void ffree(void **); + +#endif // __UTILS_MEMORY_H__ diff --git a/include/signalHandling.h b/include/utils/signalHandling.h similarity index 89% rename from include/signalHandling.h rename to include/utils/signalHandling.h index 6eaa3d9..269d1fe 100644 --- a/include/signalHandling.h +++ b/include/utils/signalHandling.h @@ -21,13 +21,13 @@ * along with this program. If not, see . */ -#ifndef __SIGNAL_HANDLING_H__ -#define __SIGNAL_HANDLING_H__ +#ifndef __UTILS_SIGNAL_HANDLING_H__ +#define __UTILS_SIGNAL_HANDLING_H__ extern volatile int doShutdown; void terminate(int signum); void init_signals(void); -#endif // __SIGNAL_HANDLING_H__ +#endif // __UTILS_SIGNAL_HANDLING_H__ diff --git a/src/Makefile.am b/src/Makefile.am index 846ef43..9553921 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,7 +7,8 @@ IFACE = interface/class.c interface/stream_reader.c interface/logger.c \ CLASS = class.c interface.c RB = ringbuffer.c ringbuffer/rb_read.c SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c -SERVER = server.c server/run.c server/close_conn.c +SERVER = server.c server/run.c server/close_conn.c server/poll.c \ + server/handle_accept.c server/read.c LOGGER = logger.c logger/stderr.c logger/syslog.c CB = cbuf.c cbuf/read.c cbuf/write.c \ cbuf/get_line.c cbuf/set_data.c cbuf/get_data.c \ @@ -31,6 +32,7 @@ HEADER = http/header.c http/header/get.c http/header/add.c \ PARSER = http/request/parser.c http/request/parser/get_header.c \ http/request/parser/parse.c http/request/parser/get_request_line.c \ http/request/parser/get_body.c +UTILS = utils/hash.c utils/memory.c utils/daemonize.c utils/signalHandling.c AM_CFLAGS = -Wall -I ../include/ @@ -40,6 +42,6 @@ bin_PROGRAMS = testserver testserver_SOURCES = testserver.c \ $(IFACE) $(CLASS) $(SOCKET) $(SERVER) $(LOGGER) $(MSG) $(REQ) \ $(WRITER) $(RESP) $(HEADER) $(PARSER) $(WORKER) $(CB) \ - signalHandling.c daemonize.c hash.c + $(UTILS) testserver_CFLAGS = -Wall -I ../include/ testserver_LDFLAGS = -lrt diff --git a/src/http/header.c b/src/http/header.c index 5ae3f5e..f85e060 100644 --- a/src/http/header.c +++ b/src/http/header.c @@ -24,12 +24,12 @@ #include #include -#include "hash.h" #include "class.h" #include "interface/class.h" - #include "http/header.h" +#include "utils/hash.h" + static void ctor(void * _this, va_list * params) { diff --git a/src/http/header/add.c b/src/http/header/add.c index d995ba9..a257faa 100644 --- a/src/http/header/add.c +++ b/src/http/header/add.c @@ -21,13 +21,12 @@ */ #include -#include #include -#include "hash.h" #include "class.h" #include "interface/class.h" #include "http/header.h" +#include "utils/hash.h" static inline diff --git a/src/http/header/get.c b/src/http/header/get.c index 3ef57b1..49f1b00 100644 --- a/src/http/header/get.c +++ b/src/http/header/get.c @@ -25,8 +25,8 @@ #include #include -#include "hash.h" #include "http/header.h" +#include "utils/hash.h" static inline diff --git a/src/http/message.c b/src/http/message.c index 23fcb9e..f767c8f 100644 --- a/src/http/message.c +++ b/src/http/message.c @@ -34,9 +34,17 @@ #include "interface/class.h" #include "http/message.h" -#include "message/helper.c" +#include "utils/memory.h" +static +inline +void +tDelete(void * node) +{ + delete(&node); +} + static void ctor(void * _this, va_list * params) @@ -54,7 +62,7 @@ dtor(void * _this) { HttpMessage this = _this; - _free((void **)&(this->version)); + ffree((void **)&(this->version)); /** * this is a GNU extension...anyway on most non @@ -65,7 +73,7 @@ dtor(void * _this) switch (this->type) { case HTTP_MESSAGE_BUFFERED: - _free((void **)&(this->body)); + ffree((void **)&(this->body)); break; case HTTP_MESSAGE_PIPED: diff --git a/src/http/request.c b/src/http/request.c index f34a9bc..bc7ac34 100644 --- a/src/http/request.c +++ b/src/http/request.c @@ -30,7 +30,7 @@ #include "interface/http_intro.h" #include "http/request.h" -#include "message/helper.c" +#include "utils/memory.h" static @@ -43,8 +43,8 @@ dtor(void * _this) { HttpRequest this = _this; - _free((void **)&(this->uri)); - _free((void **)&(this->method)); + ffree((void **)&(this->uri)); + ffree((void **)&(this->method)); PARENTCALL(_this, Class, dtor); } diff --git a/src/http/request/parser.c b/src/http/request/parser.c index 6c21d7d..327b388 100644 --- a/src/http/request/parser.c +++ b/src/http/request/parser.c @@ -20,10 +20,9 @@ * along with this program. If not, see . */ -#include -#include #include #include +#include #include "class.h" #include "interface/class.h" diff --git a/src/http/request/parser/get_header.c b/src/http/request/parser/get_header.c index 2e9fb80..9bf2696 100644 --- a/src/http/request/parser/get_header.c +++ b/src/http/request/parser/get_header.c @@ -20,9 +20,7 @@ * along with this program. If not, see . */ -#include -#include -#include +#include #include "class.h" #include "interface/class.h" @@ -31,8 +29,8 @@ #include "http/request/parser.h" #include "ringbuffer.h" -ssize_t -httpRequestParserGetHeader(HttpRequestParser this, char * cr) +void +httpRequestParserGetHeader(HttpMessage message, char * line) { HttpMessage message = (HttpMessage)this->cur_request; char * value; @@ -53,7 +51,7 @@ httpRequestParserGetHeader(HttpRequestParser this, char * cr) httpHeaderAdd(&(message->header), new(HttpHeader, name, value)); - return 1; //* @TODO: return something useful here + httpHeaderAdd(&(message->header), new(HttpHeader, name, value)); } // vim: set ts=4 sw=4: diff --git a/src/http/request/parser/get_request_line.c b/src/http/request/parser/get_request_line.c index b9e33a7..538b2df 100644 --- a/src/http/request/parser/get_request_line.c +++ b/src/http/request/parser/get_request_line.c @@ -25,8 +25,7 @@ #include "http/message.h" #include "http/request.h" -#include "http/request/parser.h" -#include "ringbuffer.h" +#include "http/message.h" #define MAX(x,y) ((x) > (y) ? (x) : (y)) #define MIN(x,y) ((x) < (y) ? (x) : (y)) diff --git a/src/http/request/parser/parse.c b/src/http/request/parser/parse.c index 78481f0..709a06e 100644 --- a/src/http/request/parser/parse.c +++ b/src/http/request/parser/parse.c @@ -21,9 +21,6 @@ */ #include -#include -#include -#include #include "http/request.h" #include "http/message.h" diff --git a/src/http/response.c b/src/http/response.c index a113564..a9bf21a 100644 --- a/src/http/response.c +++ b/src/http/response.c @@ -31,7 +31,7 @@ #include "interface/http_intro.h" #include "http/response.h" -#include "message/helper.c" +#include "utils/memory.h" static @@ -56,7 +56,7 @@ dtor(void * _this) { HttpResponse this = _this; - _free((void **)&(this->reason)); + ffree((void **)&(this->reason)); PARENTCALL(_this, Class, dtor); } diff --git a/src/http/response/304.c b/src/http/response/304.c index ed19591..8c8ccd0 100644 --- a/src/http/response/304.c +++ b/src/http/response/304.c @@ -20,8 +20,6 @@ * along with this program. If not, see . */ -#include -#include #include #include #include diff --git a/src/http/response/image.c b/src/http/response/image.c index ccb9315..3b0aeb1 100644 --- a/src/http/response/image.c +++ b/src/http/response/image.c @@ -20,8 +20,6 @@ * along with this program. If not, see . */ -#include -#include #include #include #include diff --git a/src/http/response/writer.c b/src/http/response/writer.c index b10d4a8..be7aaa6 100644 --- a/src/http/response/writer.c +++ b/src/http/response/writer.c @@ -20,8 +20,7 @@ * along with this program. If not, see . */ -#include -#include +#include #include "class.h" #include "interface/class.h" diff --git a/src/http/response/writer/write.c b/src/http/response/writer/write.c index 7aec1bb..3ea45fd 100644 --- a/src/http/response/writer/write.c +++ b/src/http/response/writer/write.c @@ -20,13 +20,8 @@ * along with this program. If not, see . */ -#include #include -#include -#include -#include #include -#include #include "class.h" #include "interface/class.h" diff --git a/src/server.c b/src/server.c index 9f4d40b..a9be60d 100644 --- a/src/server.c +++ b/src/server.c @@ -20,10 +20,6 @@ * along with this program. If not, see . */ -#include /* for select system call and related */ -#include /* for memset and stuff */ -#include /* for getopt */ -#include #include #include "class.h" diff --git a/src/server/handle_accept.c b/src/server/handle_accept.c index 74de04d..2cb008c 100644 --- a/src/server/handle_accept.c +++ b/src/server/handle_accept.c @@ -25,11 +25,10 @@ #include #include "http/worker.h" +#include "server.h" +#include "interface/class.h" +#include "interface/logger.h" - -#include "http/worker.h" - -static int serverHandleAccept(Server this) { @@ -45,15 +44,6 @@ serverHandleAccept(Server this) // clone worker (this->conns)[acc->handle].worker = clone(this->worker); - /** - * \todo - * set worker id---this would better be accomplished - * as a worker method. - * The same is true for server information stuff only - * that this should become an interface. - * At a second thought both has to be an interface. - */ - (this->fds)[this->nfds].fd = acc->handle; (this->fds)[this->nfds].events = POLLIN; this->nfds++; diff --git a/src/server/poll.c b/src/server/poll.c index 2dd5600..0b5a775 100644 --- a/src/server/poll.c +++ b/src/server/poll.c @@ -23,10 +23,14 @@ #include #include +#include "server.h" +#include "interface/logger.h" + +#include "utils/signalHandling.h" + #define POLLFD(ptr) ((struct pollfd *)(ptr)) #define SWAP(a, b) ((a)^=(b),(b)^=(a),(a)^=(b)) -static int serverPoll(Server this) { int events; diff --git a/src/server/read.c b/src/server/read.c index 0ea561b..4bc350f 100644 --- a/src/server/read.c +++ b/src/server/read.c @@ -20,7 +20,10 @@ * along with this program. If not, see . */ -static +#include "server.h" +#include "interface/logger.h" +#include "interface/stream_reader.h" + ssize_t serverRead(Server this, unsigned int i) { diff --git a/src/server/run.c b/src/server/run.c index d2c64e5..992b3be 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -21,20 +21,18 @@ */ #include "server.h" -#include "socket.h" -#include "logger.h" -#include "signalHandling.h" -#include "interface/class.h" -#include "interface/stream_reader.h" #include "interface/stream_writer.h" #include "interface/logger.h" +#include "utils/signalHandling.h" + #undef MAX #define MAX(x,y) ((x) > (y) ? (x) : (y)) -#include "poll.c" -#include "handle_accept.c" -#include "read.c" +int serverPoll(Server); +int serverHandleAccept(Server); +void serverCloseConn(Server, unsigned int); +ssize_t serverRead(Server, unsigned int); void serverRun(Server this) diff --git a/src/testserver.c b/src/testserver.c index 4e2cf9b..ce3aec2 100644 --- a/src/testserver.c +++ b/src/testserver.c @@ -31,10 +31,10 @@ #include "logger.h" #include "http/worker.h" -#include "signalHandling.h" - #include "interface/class.h" +#include "utils/signalHandling.h" + void daemonize(void); int diff --git a/src/daemonize.c b/src/utils/daemonize.c similarity index 100% rename from src/daemonize.c rename to src/utils/daemonize.c diff --git a/src/hash.c b/src/utils/hash.c similarity index 97% rename from src/hash.c rename to src/utils/hash.c index 0a35a7c..0dfeb78 100644 --- a/src/hash.c +++ b/src/utils/hash.c @@ -1,6 +1,6 @@ #include -#include "hash.h" +#include "utils/hash.h" /** * SDBM hashing algorithm: diff --git a/src/utils/memory.c b/src/utils/memory.c new file mode 100644 index 0000000..853b613 --- /dev/null +++ b/src/utils/memory.c @@ -0,0 +1,14 @@ +#include + +#include "utils/memory.h" + +void +ffree(void ** data) +{ + if (NULL != *data) { + free(*data); + *data = NULL; + } +} + +// vim: set ts=4 sw=4: diff --git a/src/signalHandling.c b/src/utils/signalHandling.c similarity index 100% rename from src/signalHandling.c rename to src/utils/signalHandling.c