From 79b346559a4b569570afb5ff1550573f2b802c9b Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Thu, 12 Sep 2013 15:20:09 +0100 Subject: [PATCH] code with abstraced application compiles again, but does not work correctly, start debugging. refs #24 --- configure.ac | 1 + include/application/adapter/http.h | 2 +- include/application/application.h | 9 +++- include/hash/hash.h | 2 + include/session.h | 4 -- src/Makefile.am | 5 +- src/application/Makefile.am | 16 ++++++ src/application/adapter/http/http.c | 8 ++- src/application/adapter/http/update.c | 38 ++++++++------ src/application/application.c | 10 +++- src/application/login.c | 42 +++++++++++++++ .../get.c => application/session_get.c} | 28 ++++------ .../add.c => application/session_start.c} | 37 +++++-------- .../delete.c => application/session_stop.c} | 22 ++++---- src/application/session_update.c | 52 +++++++++++++++++++ src/hash/delete.c | 8 +++ src/hash/get.c | 7 +++ src/http/worker/process.c | 2 + src/session/Makefile.am | 2 +- src/session/session.c | 19 ++++++- src/taskrambler.c | 17 +++--- 21 files changed, 236 insertions(+), 95 deletions(-) create mode 100644 src/application/Makefile.am create mode 100644 src/application/login.c rename src/{session/get.c => application/session_get.c} (66%) rename src/{session/add.c => application/session_start.c} (62%) rename src/{session/delete.c => application/session_stop.c} (71%) create mode 100644 src/application/session_update.c diff --git a/configure.ac b/configure.ac index 9b7fbd0..45c5842 100644 --- a/configure.ac +++ b/configure.ac @@ -64,5 +64,6 @@ AC_CONFIG_FILES([Makefile src/socket/Makefile src/stream/Makefile src/tree/Makefile + src/application/Makefile tests/Makefile]) AC_OUTPUT diff --git a/include/application/adapter/http.h b/include/application/adapter/http.h index 134b093..2708080 100644 --- a/include/application/adapter/http.h +++ b/include/application/adapter/http.h @@ -24,7 +24,7 @@ #define __APPLICATION_ADAPTER_HTTP_H__ #include "class.h" -#include "application.h" +#include "application/application.h" CLASS(ApplicationAdapterHttp) { diff --git a/include/application/application.h b/include/application/application.h index 5a7f9b9..01c51dc 100644 --- a/include/application/application.h +++ b/include/application/application.h @@ -23,14 +23,17 @@ #ifndef __APPLICATION_H__ #define __APPLICATION_H__ +#include #include "class.h" +#include "session.h" +#include "hash.h" #include "auth/credential.h" struct randval { time_t timestamp; int value; -} +}; CLASS(Application) { Hash active_sessions; @@ -40,8 +43,10 @@ CLASS(Application) { // this should return a user account....now it only return success or failure. int applicationLogin(Application, Credential); -unsigned long applicationSessionStart(Application); +unsigned long applicationSessionStart(Application, const char *, size_t); void applicationSessionStop(Application, unsigned long); +void applicationSessionUpdate( + Application, unsigned long, const char *, size_t); Session applicationSessionGet(Application, unsigned long); #endif // __HTTP_HEADER_H__ diff --git a/include/hash/hash.h b/include/hash/hash.h index 5f67a8b..dd69291 100644 --- a/include/hash/hash.h +++ b/include/hash/hash.h @@ -36,6 +36,8 @@ CLASS(Hash) { void * hashAdd(Hash, void *); void * hashDelete(Hash, const char *, size_t); void * hashGet(Hash, const char *, size_t); +void * hashDeleteByVal(Hash, unsigned long); +void * hashGetByVal(Hash, unsigned long); void hashEach(Hash, void (*)(const void*)); #endif // __HASH_HASH_H__ diff --git a/include/session.h b/include/session.h index 02f7aec..8ca5d56 100644 --- a/include/session.h +++ b/include/session.h @@ -38,10 +38,6 @@ CLASS(Session) { char * username; }; -Session sessionAdd(const Session *, Session); -Session sessionGet(const Session *, const unsigned long id); -void sessionDelete(const Session *, const unsigned long id); - #endif // __SESSION_H__ // vim: set ts=4 sw=4: diff --git a/src/Makefile.am b/src/Makefile.am index 6894e90..9d06867 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,7 +11,8 @@ UTILS = utils/hash.c \ utils/signalHandling.c \ utils/mime_type.c -LIBS = ./http/libhttp.a \ +LIBS = ./application/libapplication.a \ + ./http/libhttp.a \ ./auth/libauth.a \ ./cbuf/libcbuf.a \ ./class/libclass.a \ @@ -35,4 +36,4 @@ taskrambler_LDADD = $(LIBS) -lrt -lssl -lldap #taskrambler_LDFLAGS = $(COVERAGE_LDFLAGS) SUBDIRS = asset auth cbuf class hash queue http \ - logger server session socket stream tree + logger server session socket stream tree application diff --git a/src/application/Makefile.am b/src/application/Makefile.am new file mode 100644 index 0000000..9e9f036 --- /dev/null +++ b/src/application/Makefile.am @@ -0,0 +1,16 @@ +ACLOCAL_AMFLAGS = -I m4 +AUTOMAKE_OPTIONS = subdir-objects + +APPLICATION = application.c \ + login.c \ + session_start.c \ + session_stop.c \ + session_update.c \ + session_get.c +ADAPTERHTTP = adapter/http/http.c \ + adapter/http/update.c + +noinst_LIBRARIES = libapplication.a + +libapplication_a_SOURCES = $(APPLICATION) $(ADAPTERHTTP) +libapplication_a_CFLAGS = $(CFLAGS) -Wall -I ../../include/ diff --git a/src/application/adapter/http/http.c b/src/application/adapter/http/http.c index 346e7ff..3dfdda3 100644 --- a/src/application/adapter/http/http.c +++ b/src/application/adapter/http/http.c @@ -49,10 +49,14 @@ applicationAdapterHttpDtor(void * _this) } -void applicationAdapterHttpUpdate(ApplicationAdapterHttp, void *); +void applicationAdapterHttpUpdate(void *, void *); -INIT_IFACE(Class, applicationAdapterHttpCtor, applicationAdapterHttpDtor); +INIT_IFACE( + Class, + applicationAdapterHttpCtor, + applicationAdapterHttpDtor, + NULL); INIT_IFACE(Observer, applicationAdapterHttpUpdate); CREATE_CLASS( ApplicationAdapterHttp, diff --git a/src/application/adapter/http/update.c b/src/application/adapter/http/update.c index ebfcc88..7ca06cd 100644 --- a/src/application/adapter/http/update.c +++ b/src/application/adapter/http/update.c @@ -22,8 +22,8 @@ #define _GNU_SOURCE -#include -#include +#include +#include #include #include "class.h" @@ -57,17 +57,17 @@ getSessionId(Hash cookies) static void -loginAdapter(Application application, HttpWorker worker) +loginAdapter(Application application, HttpWorker worker, unsigned long sid) { HashValue username; - HashValue passeord; + HashValue password; Credential credential; username = hashGet( - worker->current_reqeust->post, + worker->current_request->post, CSTRA("username")); password = hashGet( - worker->current_reqeust->post, + worker->current_request->post, CSTRA("password")); if (NULL == username || NULL == password) { @@ -86,7 +86,10 @@ loginAdapter(Application application, HttpWorker worker) size_t nbuf; if (NO_SESSION_SID == sid) { - sid = applicationSessionStart(application); + sid = applicationSessionStart( + application, + (char *)(username->value), + username->nvalue); } else { applicationSessionUpdate( application, @@ -99,29 +102,30 @@ loginAdapter(Application application, HttpWorker worker) worker->current_response = (HttpMessage)httpResponseSession( - applicationSessionGet(sid)); + applicationSessionGet(application, sid)); hashAdd( worker->current_response->header, - new(HttpHeader. CSTRA("Set-Cookie"), buffer, nbuf)); + new(HttpHeader, CSTRA("Set-Cookie"), buffer, nbuf)); } else { worker->current_response = new(HttpResponse, "HTTP/1.1", 403, "Forbidden"); } - delete(credential) + delete(credential); } void -applicationAdapterHttpUpdate(ApplicationAdapterHttp this, void * subject) +applicationAdapterHttpUpdate(void * _this, void * subject) { - HttpWorker worker = (HttpWorker)subject; - unsigned long sid = getSessionId(worker->current_request->cookies); + ApplicationAdapterHttp this = _this; + HttpWorker worker = (HttpWorker)subject; + unsigned long sid = getSessionId(worker->current_request->cookies); if (0 == strcmp("POST", worker->current_request->method)) { if (0 == strcmp("/login/", worker->current_request->path)) { - loginAdapter(this->application, worker); + loginAdapter(this->application, worker, sid); return; } } @@ -130,18 +134,18 @@ applicationAdapterHttpUpdate(ApplicationAdapterHttp this, void * subject) if (0 == strcmp("/sessinfo/", worker->current_request->path)) { worker->current_response = (HttpMessage)httpResponseSession( - applicationSessionGet(sid)); + applicationSessionGet(this->application, sid)); return; } if (0 == strcmp("/sess/", worker->current_request->path)) { if (NO_SESSION_SID == sid) { - sid = applicationSessionStart(application); + sid = applicationSessionStart(this->application, NULL, 0); } worker->current_response = (HttpMessage)httpResponseSession( - applicationSessionGet(sid)); + applicationSessionGet(this->application, sid)); return; } diff --git a/src/application/application.c b/src/application/application.c index 6831004..16da56f 100644 --- a/src/application/application.c +++ b/src/application/application.c @@ -25,6 +25,7 @@ #include #include "class.h" +#include "hash.h" #include "application/application.h" #include "utils/memory.h" @@ -36,7 +37,9 @@ applicationCtor(void * _this, va_list * params) Application this = _this; this->val = va_arg(*params, struct randval *); - this->auth = va_arg(* params, void *); + this->auth = va_arg(*params, void *); + + this->active_sessions = new(Hash); return 0; } @@ -45,10 +48,13 @@ static void applicationDtor(void * _this) { + Application this = _this; + + delete(this->active_sessions); } -INIT_IFACE(Class, applicationCtor, applicationDtor); +INIT_IFACE(Class, applicationCtor, applicationDtor, NULL); CREATE_CLASS(Application, NULL, IFACE(Class)); // vim: set ts=4 sw=4: diff --git a/src/application/login.c b/src/application/login.c new file mode 100644 index 0000000..e10f5f5 --- /dev/null +++ b/src/application/login.c @@ -0,0 +1,42 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 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 . + */ + +#define _GNU_SOURCE + +#include +#include +#include + +#include "class.h" +#include "auth.h" + +#include "utils/memory.h" +#include "application/application.h" + + +int +applicationLogin(Application this, Credential credential) +{ + return authenticate(this->auth, credential); +} + +// vim: set ts=4 sw=4: diff --git a/src/session/get.c b/src/application/session_get.c similarity index 66% rename from src/session/get.c rename to src/application/session_get.c index ed3734e..46eaa10 100644 --- a/src/session/get.c +++ b/src/application/session_get.c @@ -20,32 +20,22 @@ * along with this program. If not, see . */ -#include -#include +#define _GNU_SOURCE +#include + +#include "class.h" #include "session.h" +#include "hash.h" +#include "application/application.h" +#include "utils/memory.h" -static -inline -int -sessionGetComp(const void * _a, const void * _b) -{ - unsigned long a = *(unsigned long *)_a; - Session b = (Session)_b; - return (a < b->id)? -1 : (a > b->id)? 1 : 0; -} Session -sessionGet(const Session * root, const unsigned long id) +applicationSessionGet(Application this, unsigned long sid) { - Session * found = tfind(&id, (void**)root, sessionGetComp); - - if (NULL == found) { - return NULL; - } - - return *found; + return hashGetByVal(this->active_sessions, sid); } // vim: set ts=4 sw=4: diff --git a/src/session/add.c b/src/application/session_start.c similarity index 62% rename from src/session/add.c rename to src/application/session_start.c index f6a9d2c..594e7e0 100644 --- a/src/session/add.c +++ b/src/application/session_start.c @@ -20,39 +20,26 @@ * along with this program. If not, see . */ -#include +#define _GNU_SOURCE + +#include -#include "session.h" #include "class.h" +#include "session.h" +#include "hash.h" +#include "application/application.h" +#include "utils/memory.h" -static -inline -int -sessionAddComp(const void * _a, const void * _b) -{ - Session a = (Session)_a; - Session b = (Session)_b; - return (a->id < b->id)? -1 : (a->id > b->id)? 1 : 0; -} -Session -sessionAdd(const Session * root, Session session) +unsigned long +applicationSessionStart(Application this, const char * name, size_t nname) { - Session * found = tsearch(session, (void**)root, sessionAddComp); - - if (NULL == found) { - return NULL; - } + Session session = new(Session, name, nname); - if (*found != session) { - /** - * \todo this should not happen, so do some logging here. - */ - delete(session); - } + hashAdd(this->active_sessions, session); - return *found; + return session->id; } // vim: set ts=4 sw=4: diff --git a/src/session/delete.c b/src/application/session_stop.c similarity index 71% rename from src/session/delete.c rename to src/application/session_stop.c index 33d203d..0b04928 100644 --- a/src/session/delete.c +++ b/src/application/session_stop.c @@ -20,26 +20,22 @@ * along with this program. If not, see . */ -#include +#define _GNU_SOURCE + +#include -#include "session.h" #include "class.h" +#include "session.h" +#include "hash.h" +#include "application/application.h" +#include "utils/memory.h" -static -inline -int -sessionDeleteComp(const void * _a, const void * _b) -{ - unsigned long a = *(unsigned long *)_a; - Session b = (Session)_b; - return (a < b->id)? -1 : (a > b->id)? 1 : 0; -} void -sessionDelete(const Session * root, const unsigned long id) +applicationSessionStop(Application this, unsigned long sid) { - tdelete(&id, (void**)root, sessionDeleteComp); + hashDeleteByVal(this->active_sessions, sid); } // vim: set ts=4 sw=4: diff --git a/src/application/session_update.c b/src/application/session_update.c new file mode 100644 index 0000000..cc85032 --- /dev/null +++ b/src/application/session_update.c @@ -0,0 +1,52 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 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 . + */ + +#define _GNU_SOURCE + +#include +#include + +#include "class.h" +#include "session.h" +#include "hash.h" +#include "application/application.h" + +#include "utils/memory.h" + + +void +applicationSessionUpdate( + Application this, + unsigned long sid, + const char * name, + size_t nname) +{ + Session session = hashGetByVal(this->active_sessions, sid); + + MEM_FREE(session->username); + + session->username = memMalloc(nname + 1); + session->username[nname] = 0; + memcpy(session->username, name, nname); +} + +// vim: set ts=4 sw=4: diff --git a/src/hash/delete.c b/src/hash/delete.c index 503e164..5c4fdd7 100644 --- a/src/hash/delete.c +++ b/src/hash/delete.c @@ -55,4 +55,12 @@ hashDelete(Hash this, const char * search, size_t nsearch) return found; } +void * +hashDeleteByVal(Hash this, unsigned long hash) +{ + void * found = treeDelete(&(this->root), &hash, hashDeleteComp); + + return found; +} + // vim: set ts=4 sw=4: diff --git a/src/hash/get.c b/src/hash/get.c index 29a0096..dd88354 100644 --- a/src/hash/get.c +++ b/src/hash/get.c @@ -56,4 +56,11 @@ hashGet(Hash this, const char * search, size_t nsearch) return found; } +void * +hashGetByVal(Hash this, unsigned long hash) +{ + void * found = treeFind(this->root, &hash, hashGetComp); + + return found; +} // vim: set ts=4 sw=4: diff --git a/src/http/worker/process.c b/src/http/worker/process.c index 05b8c1d..e3c5780 100644 --- a/src/http/worker/process.c +++ b/src/http/worker/process.c @@ -41,6 +41,8 @@ #include "http/response.h" #include "http/parser.h" +#include "interface/subject.h" + #include "utils/memory.h" #include "utils/mime_type.h" #include "commons.h" diff --git a/src/session/Makefile.am b/src/session/Makefile.am index a3d1d2e..462b4e8 100644 --- a/src/session/Makefile.am +++ b/src/session/Makefile.am @@ -3,5 +3,5 @@ AUTOMAKE_OPTIONS = subdir-objects noinst_LIBRARIES = libsession.a -libsession_a_SOURCES = session.c add.c get.c delete.c +libsession_a_SOURCES = session.c libsession_a_CFLAGS = $(CFLAGS) -Wall -I ../../include/ diff --git a/src/session/session.c b/src/session/session.c index 512be4b..fe2975c 100644 --- a/src/session/session.c +++ b/src/session/session.c @@ -28,6 +28,7 @@ #include #include "session.h" +#include "hash.h" #include "class.h" #include "utils/hash.h" @@ -61,7 +62,23 @@ sessionDtor(void * _this) MEM_FREE(this->username); } +static +unsigned long +sessionGetId(void * _this) +{ + Session this = _this; + + return this->id; +} + +static +void +sessionHandleDouble(void * _this, void * _doub) +{ +} + INIT_IFACE(Class, sessionCtor, sessionDtor, NULL); -CREATE_CLASS(Session, NULL, IFACE(Class)); +INIT_IFACE(Hashable, sessionGetId, sessionHandleDouble); +CREATE_CLASS(Session, NULL, IFACE(Class), IFACE(Hashable)); // vim: set ts=4 sw=4: diff --git a/src/taskrambler.c b/src/taskrambler.c index 80569bd..6020e92 100644 --- a/src/taskrambler.c +++ b/src/taskrambler.c @@ -39,6 +39,9 @@ #include "logger.h" #include "http/worker.h" #include "auth.h" +#include "application/application.h" +#include "application/adapter/http.h" +#include "interface/subject.h" #include "class.h" #include "logger.h" @@ -135,8 +138,8 @@ main() default: { AuthLdap auth; - //Application application; - //ApplicationAdapterHttp adapterHttp; + Application application; + ApplicationAdapterHttp adapterHttp; HttpWorker worker; Server server; @@ -199,10 +202,12 @@ main() } } while (!WIFEXITED(status) && !WIFSIGNALED(status)); - if (NULL != server) delete(server); - if (NULL != worker) delete(worker); - if (NULL != auth) delete(auth); - if (NULL != logger) delete(logger); + delete(server); + delete(worker); + delete(adapterHttp); + delete(application); + delete(auth); + delete(logger); clearMimeTypes(); assetPoolCleanup();