diff --git a/src/libfast/dispatcher.c b/src/libfast/dispatcher.c index 7690230d3..8cfad0fd3 100644 --- a/src/libfast/dispatcher.c +++ b/src/libfast/dispatcher.c @@ -180,14 +180,12 @@ static session_entry_t *session_entry_create(private_dispatcher_t *this, { session_entry_t *entry; - entry = malloc_thing(session_entry_t); - entry->in_use = FALSE; - entry->closed = FALSE; - entry->cond = condvar_create(CONDVAR_TYPE_DEFAULT); - entry->session = load_session(this); - entry->used = time_monotonic(NULL); - entry->host = strdup(host); - + INIT(entry, + .cond = condvar_create(CONDVAR_TYPE_DEFAULT), + .session = load_session(this), + .used = time_monotonic(NULL), + .host = strdup(host), + ); return entry; } @@ -202,29 +200,28 @@ static void session_entry_destroy(session_entry_t *entry) free(entry); } -/** - * Implementation of dispatcher_t.add_controller. - */ -static void add_controller(private_dispatcher_t *this, - controller_constructor_t constructor, void *param) +METHOD(dispatcher_t, add_controller, void, + private_dispatcher_t *this, controller_constructor_t constructor, + void *param) { - controller_entry_t *entry = malloc_thing(controller_entry_t); + controller_entry_t *entry; - entry->constructor = constructor; - entry->param = param; + INIT(entry, + .constructor = constructor, + .param = param, + ); this->controllers->insert_last(this->controllers, entry); } -/** - * Implementation of dispatcher_t.add_filter. - */ -static void add_filter(private_dispatcher_t *this, - filter_constructor_t constructor, void *param) +METHOD(dispatcher_t, add_filter, void, + private_dispatcher_t *this, filter_constructor_t constructor, void *param) { - filter_entry_t *entry = malloc_thing(filter_entry_t); + filter_entry_t *entry; - entry->constructor = constructor; - entry->param = param; + INIT(entry, + .constructor = constructor, + .param = param, + ); this->filters->insert_last(this->filters, entry); } @@ -349,10 +346,8 @@ static void dispatch(private_dispatcher_t *this) } } -/** - * Implementation of dispatcher_t.run. - */ -static void run(private_dispatcher_t *this, int threads) +METHOD(dispatcher_t, run, void, + private_dispatcher_t *this, int threads) { this->thread_count = threads; this->threads = malloc(sizeof(thread_t*) * threads); @@ -367,10 +362,8 @@ static void run(private_dispatcher_t *this, int threads) } } -/** - * Implementation of dispatcher_t.waitsignal. - */ -static void waitsignal(private_dispatcher_t *this) +METHOD(dispatcher_t, waitsignal, void, + private_dispatcher_t *this) { sigset_t set; int sig; @@ -383,10 +376,8 @@ static void waitsignal(private_dispatcher_t *this) sigwait(&set, &sig); } -/** - * Implementation of dispatcher_t.destroy - */ -static void destroy(private_dispatcher_t *this) +METHOD(dispatcher_t, destroy, void, + private_dispatcher_t *this) { char *sid; session_entry_t *entry; @@ -419,26 +410,27 @@ static void destroy(private_dispatcher_t *this) dispatcher_t *dispatcher_create(char *socket, bool debug, int timeout, context_constructor_t constructor, void *param) { - private_dispatcher_t *this = malloc_thing(private_dispatcher_t); + private_dispatcher_t *this; - this->public.add_controller = (void(*)(dispatcher_t*, controller_constructor_t, void*))add_controller; - this->public.add_filter = (void(*)(dispatcher_t*,filter_constructor_t constructor, void *param))add_filter; - this->public.run = (void(*)(dispatcher_t*, int threads))run; - this->public.waitsignal = (void(*)(dispatcher_t*))waitsignal; - this->public.destroy = (void(*)(dispatcher_t*))destroy; - - this->sessions = hashtable_create((void*)session_hash, - (void*)session_equals, 4096); - this->controllers = linked_list_create(); - this->filters = linked_list_create(); - this->context_constructor = constructor; - this->mutex = mutex_create(MUTEX_TYPE_DEFAULT); - this->param = param; - this->fd = 0; - this->timeout = timeout; - this->last_cleanup = time_monotonic(NULL); - this->debug = debug; - this->threads = NULL; + INIT(this, + .public = { + .add_controller = _add_controller, + .add_filter = _add_filter, + .run = _run, + .waitsignal = _waitsignal, + .destroy = _destroy, + }, + .sessions = hashtable_create((void*)session_hash, + (void*)session_equals, 4096), + .controllers = linked_list_create(), + .filters = linked_list_create(), + .context_constructor = constructor, + .mutex = mutex_create(MUTEX_TYPE_DEFAULT), + .param = param, + .timeout = timeout, + .last_cleanup = time_monotonic(NULL), + .debug = debug, + ); FCGX_Init(); diff --git a/src/libfast/request.c b/src/libfast/request.c index a3db70e82..2ab873377 100644 --- a/src/libfast/request.c +++ b/src/libfast/request.c @@ -160,86 +160,66 @@ static int iterenv_cb(void *null, int num, char **key, char **value) return 0; } -/** - * Implementation of request_t.get_cookie. - */ -static char* get_cookie(private_request_t *this, char *name) +METHOD(request_t, get_cookie, char*, + private_request_t *this, char *name) { return hdf_get_valuef(this->hdf, "Cookie.%s", name); } -/** - * Implementation of request_t.get_path. - */ -static char* get_path(private_request_t *this) +METHOD(request_t, get_path, char*, + private_request_t *this) { char * path = FCGX_GetParam("PATH_INFO", this->req.envp); return path ? path : ""; } -/** - * Implementation of request_t.get_host. - */ -static char* get_host(private_request_t *this) +METHOD(request_t, get_host, char*, + private_request_t *this) { char *addr = FCGX_GetParam("REMOTE_ADDR", this->req.envp); return addr ? addr : ""; } -/** - * Implementation of request_t.get_user_agent. - */ -static char* get_user_agent(private_request_t *this) +METHOD(request_t, get_user_agent, char*, + private_request_t *this) { char *agent = FCGX_GetParam("HTTP_USER_AGENT", this->req.envp); return agent ? agent : ""; } -/** - * Implementation of request_t.get_post_data. - */ -static char* get_query_data(private_request_t *this, char *name) +METHOD(request_t, get_query_data, char*, + private_request_t *this, char *name) { return hdf_get_valuef(this->hdf, "Query.%s", name); } -/** - * Implementation of request_t.get_env_var. - */ -static char* get_env_var(private_request_t *this, char *name) +METHOD(request_t, get_env_var, char*, + private_request_t *this, char *name) { return FCGX_GetParam(name, this->req.envp); } -/** - * Implementation of request_t.read_data. - */ -static int read_data(private_request_t *this, char *buf, int len) +METHOD(request_t, read_data, int, + private_request_t *this, char *buf, int len) { return FCGX_GetStr(buf, len, this->req.in); } -/** - * Implementation of request_t.get_base. - */ -static char* get_base(private_request_t *this) +METHOD(request_t, get_base, char*, + private_request_t *this) { return FCGX_GetParam("SCRIPT_NAME", this->req.envp); } -/** - * Implementation of request_t.add_cookie. - */ -static void add_cookie(private_request_t *this, char *name, char *value) +METHOD(request_t, add_cookie, void, + private_request_t *this, char *name, char *value) { thread_this->set(thread_this, this); cgi_cookie_set (this->cgi, name, value, get_base(this), NULL, NULL, 0, 0); } -/** - * Implementation of request_t.redirect. - */ -static void redirect(private_request_t *this, char *fmt, ...) +METHOD(request_t, redirect, void, + private_request_t *this, char *fmt, ...) { va_list args; @@ -252,18 +232,14 @@ static void redirect(private_request_t *this, char *fmt, ...) FCGX_FPrintF(this->req.out, "\n\n"); } -/** - * Implementation of request_t.get_referer. - */ -static char* get_referer(private_request_t *this) +METHOD(request_t, get_referer, char*, + private_request_t *this) { return FCGX_GetParam("HTTP_REFERER", this->req.envp); } -/** - * Implementation of request_t.to_referer. - */ -static void to_referer(private_request_t *this) +METHOD(request_t, to_referer, void, + private_request_t *this) { char *referer; @@ -279,36 +255,28 @@ static void to_referer(private_request_t *this) } } -/** - * Implementation of request_t.session_closed. - */ -static bool session_closed(private_request_t *this) +METHOD(request_t, session_closed, bool, + private_request_t *this) { return this->closed; } -/** - * Implementation of request_t.close_session. - */ -static void close_session(private_request_t *this) +METHOD(request_t, close_session, void, + private_request_t *this) { this->closed = TRUE; } -/** - * Implementation of request_t.serve. - */ -static void serve(private_request_t *this, char *headers, chunk_t chunk) +METHOD(request_t, serve, void, + private_request_t *this, char *headers, chunk_t chunk) { FCGX_FPrintF(this->req.out, "%s\n\n", headers); FCGX_PutStr(chunk.ptr, chunk.len, this->req.out); } -/** - * Implementation of request_t.render. - */ -static void render(private_request_t *this, char *template) +METHOD(request_t, render, void, + private_request_t *this, char *template) { NEOERR* err; @@ -319,13 +287,10 @@ static void render(private_request_t *this, char *template) cgi_neo_error(this->cgi, err); nerr_log_error(err); } - return; } -/** - * Implementation of request_t.streamf. - */ -static int streamf(private_request_t *this, char *format, ...) +METHOD(request_t, streamf, int, + private_request_t *this, char *format, ...) { va_list args; int written; @@ -341,18 +306,14 @@ static int streamf(private_request_t *this, char *format, ...) return written; } -/** - * Implementation of request_t.set. - */ -static void set(private_request_t *this, char *key, char *value) +METHOD(request_t, set, void, + private_request_t *this, char *key, char *value) { hdf_set_value(this->hdf, key, value); } -/** - * Implementation of request_t.setf. - */ -static void setf(private_request_t *this, char *format, ...) +METHOD(request_t, setf, void, + private_request_t *this, char *format, ...) { va_list args; @@ -361,19 +322,15 @@ static void setf(private_request_t *this, char *format, ...) va_end(args); } -/** - * Implementation of request_t.get_ref. - */ -static request_t* get_ref(private_request_t *this) +METHOD(request_t, get_ref, request_t*, + private_request_t *this) { ref_get(&this->ref); return &this->public; } -/** - * Implementation of request_t.destroy - */ -static void destroy(private_request_t *this) +METHOD(request_t, destroy, void, + private_request_t *this) { if (ref_put(&this->ref)) { @@ -401,9 +358,36 @@ static void init(void) request_t *request_create(int fd, bool debug) { NEOERR* err; - private_request_t *this = malloc_thing(private_request_t); + private_request_t *this; bool failed = FALSE; + INIT(this, + .public = { + .get_path = _get_path, + .get_base = _get_base, + .get_host = _get_host, + .get_user_agent = _get_user_agent, + .add_cookie = _add_cookie, + .get_cookie = _get_cookie, + .get_query_data = _get_query_data, + .get_env_var = _get_env_var, + .read_data = _read_data, + .session_closed = _session_closed, + .close_session = _close_session, + .redirect = _redirect, + .get_referer = _get_referer, + .to_referer = _to_referer, + .render = _render, + .streamf = _streamf, + .serve = _serve, + .set = _set, + .setf = _setf, + .get_ref = _get_ref, + .destroy = _destroy, + }, + .ref = 1, + ); + thread_cleanup_push(free, this); if (FCGX_InitRequest(&this->req, fd, 0) != 0 || FCGX_Accept_r(&this->req) != 0) @@ -416,34 +400,9 @@ request_t *request_create(int fd, bool debug) return NULL; } - this->public.get_path = (char*(*)(request_t*))get_path; - this->public.get_base = (char*(*)(request_t*))get_base; - this->public.get_host = (char*(*)(request_t*))get_host; - this->public.get_user_agent = (char*(*)(request_t*))get_user_agent; - this->public.add_cookie = (void(*)(request_t*, char *name, char *value))add_cookie; - this->public.get_cookie = (char*(*)(request_t*,char*))get_cookie; - this->public.get_query_data = (char*(*)(request_t*, char *name))get_query_data; - this->public.get_env_var = (char*(*)(request_t*, char *name))get_env_var; - this->public.read_data = (int(*)(request_t*, char*, int))read_data; - this->public.session_closed = (bool(*)(request_t*))session_closed; - this->public.close_session = (void(*)(request_t*))close_session; - this->public.redirect = (void(*)(request_t*, char *fmt,...))redirect; - this->public.get_referer = (char*(*)(request_t*))get_referer; - this->public.to_referer = (void(*)(request_t*))to_referer; - this->public.render = (void(*)(request_t*,char*))render; - this->public.streamf = (int(*)(request_t*, char *format, ...))streamf; - this->public.serve = (void(*)(request_t*,char*,chunk_t))serve; - this->public.set = (void(*)(request_t*, char *, char*))set; - this->public.setf = (void(*)(request_t*, char *format, ...))setf; - this->public.get_ref = (request_t*(*)(request_t*))get_ref; - this->public.destroy = (void(*)(request_t*))destroy; - pthread_once(&once, init); thread_this->set(thread_this, this); - this->ref = 1; - this->closed = FALSE; - this->req_env_len = 0; while (this->req.envp[this->req_env_len] != NULL) { this->req_env_len++; diff --git a/src/libfast/session.c b/src/libfast/session.c index 7c4548ee5..1d9ed0107 100644 --- a/src/libfast/session.c +++ b/src/libfast/session.c @@ -63,18 +63,14 @@ struct private_session_t { context_t *context; }; -/** - * Implementation of session_t.add_controller. - */ -static void add_controller(private_session_t *this, controller_t *controller) +METHOD(session_t, add_controller, void, + private_session_t *this, controller_t *controller) { this->controllers->insert_last(this->controllers, controller); } -/** - * Implementation of session_t.add_filter. - */ -static void add_filter(private_session_t *this, filter_t *filter) +METHOD(session_t, add_filter, void, + private_session_t *this, filter_t *filter) { this->filters->insert_last(this->filters, filter); } @@ -120,10 +116,8 @@ static bool run_filter(private_session_t *this, request_t *request, char *p0, return TRUE; } -/** - * Implementation of session_t.process. - */ -static void process(private_session_t *this, request_t *request) +METHOD(session_t, process, void, + private_session_t *this, request_t *request) { char *pos, *start, *param[6] = {NULL, NULL, NULL, NULL, NULL, NULL}; enumerator_t *enumerator; @@ -184,18 +178,14 @@ static void process(private_session_t *this, request_t *request) } } -/** - * Implementation of session_t.get_sid. - */ -static char* get_sid(private_session_t *this) +METHOD(session_t, get_sid, char*, + private_session_t *this) { return this->sid; } -/** - * Implementation of session_t.destroy - */ -static void destroy(private_session_t *this) +METHOD(session_t, destroy, void, + private_session_t *this) { this->controllers->destroy_offset(this->controllers, offsetof(controller_t, destroy)); this->filters->destroy_offset(this->filters, offsetof(filter_t, destroy)); @@ -208,19 +198,21 @@ static void destroy(private_session_t *this) */ session_t *session_create(context_t *context) { - private_session_t *this = malloc_thing(private_session_t); - - this->public.add_controller = (void(*)(session_t*, controller_t*))add_controller; - this->public.add_filter = (void(*)(session_t*, filter_t*))add_filter; - this->public.process = (void(*)(session_t*,request_t*))process; - this->public.get_sid = (char*(*)(session_t*))get_sid; - this->public.destroy = (void(*)(session_t*))destroy; + private_session_t *this; + INIT(this, + .public = { + .add_controller = _add_controller, + .add_filter = _add_filter, + .process = _process, + .get_sid = _get_sid, + .destroy = _destroy, + }, + .controllers = linked_list_create(), + .filters = linked_list_create(), + .context = context, + ); create_sid(this); - this->cookie_sent = FALSE; - this->controllers = linked_list_create(); - this->filters = linked_list_create(); - this->context = context; return &this->public; }