diff options
Diffstat (limited to 'src/manager/lib')
-rw-r--r-- | src/manager/lib/controller.h | 21 | ||||
-rw-r--r-- | src/manager/lib/dispatcher.c | 23 | ||||
-rw-r--r-- | src/manager/lib/dispatcher.h | 7 | ||||
-rw-r--r-- | src/manager/lib/response.c | 10 | ||||
-rw-r--r-- | src/manager/lib/response.h | 7 | ||||
-rw-r--r-- | src/manager/lib/session.c | 20 | ||||
-rw-r--r-- | src/manager/lib/session.h | 2 | ||||
-rw-r--r-- | src/manager/lib/template.c | 2 | ||||
-rw-r--r-- | src/manager/lib/template.h | 4 |
9 files changed, 71 insertions, 25 deletions
diff --git a/src/manager/lib/controller.h b/src/manager/lib/controller.h index 92968d8a6..fe6177513 100644 --- a/src/manager/lib/controller.h +++ b/src/manager/lib/controller.h @@ -59,13 +59,24 @@ struct controller_t { char* (*get_name)(controller_t *this); /** - * @brief Get the controllers handler function for an action name. + * @brief Handle a HTTP request for that controller. * - * @param name name of the action - * @return controllers handler + * Request URLs are parsed in the form + * controller_name/p1/p2/p3/p4/p5 with a maximum of 5 parameters. Each + * parameter not found in the request URL is set to NULL. + * + * @param request HTTP request + * @param response HTTP response + * @param p1 first parameter + * @param p2 second parameter + * @param p3 third parameter + * @param p4 forth parameter + * @param p5 fifth parameter + * @return */ - controller_handler_t (*get_handler)(controller_t *this, char *name); - + void (*handle)(controller_t *this, request_t *request, response_t *response, + char *a1, char *a2, char *a3, char *a4, char *a5); + /** * @brief Destroy the controller instance. */ diff --git a/src/manager/lib/dispatcher.c b/src/manager/lib/dispatcher.c index 018122e6f..db99110c3 100644 --- a/src/manager/lib/dispatcher.c +++ b/src/manager/lib/dispatcher.c @@ -146,6 +146,7 @@ static session_entry_t *session_entry_create(private_dispatcher_t *this) entry->waiting = 1; pthread_cond_init(&entry->cond, NULL); entry->session = load_session(this); + entry->used = time(NULL); return entry; } @@ -208,16 +209,19 @@ static void dispatch(private_dispatcher_t *this) iterator = this->sessions->create_iterator_locked(this->sessions, &this->mutex); while (iterator->iterate(iterator, (void**)¤t)) { - if (sid && streq(current->session->get_sid(current->session), sid)) - { - found = current; - found->waiting++; - } - else if (current->waiting == 0 && - current->used + this->timeout > now) + /* check all sessions for timeout */ + if (current->waiting == 0 && + current->used < now - this->timeout) { iterator->remove(iterator); session_entry_destroy(current); + continue; + } + if (!found && sid && + streq(current->session->get_sid(current->session), sid)) + { + found = current; + found->waiting++; } } iterator->destroy(iterator); @@ -319,7 +323,8 @@ static void destroy(private_dispatcher_t *this) /* * see header file */ -dispatcher_t *dispatcher_create(context_constructor_t constructor, void *param) +dispatcher_t *dispatcher_create(int timeout, context_constructor_t constructor, + void *param) { private_dispatcher_t *this = malloc_thing(private_dispatcher_t); @@ -334,7 +339,7 @@ dispatcher_t *dispatcher_create(context_constructor_t constructor, void *param) pthread_mutex_init(&this->mutex, NULL); this->param = param; this->fd = 0; - this->timeout = 180; + this->timeout = timeout; FCGX_Init(); diff --git a/src/manager/lib/dispatcher.h b/src/manager/lib/dispatcher.h index 5119a1eab..f46e5f32d 100644 --- a/src/manager/lib/dispatcher.h +++ b/src/manager/lib/dispatcher.h @@ -40,6 +40,9 @@ struct dispatcher_t { /** * @brief Register a controller to the dispatcher. * + * The first controller added serves as default controller. Client's + * get redirected to it if no other controller matches. + * * @param constructor constructor function to the conntroller * @param param param to pass to constructor */ @@ -70,9 +73,11 @@ struct dispatcher_t { * The context constructor is invoked to create a session context for * each session. * + * @param timeout session timeout * @param constructor construction function for session context * @param param parameter to supply to context constructor */ -dispatcher_t *dispatcher_create(context_constructor_t constructor, void *param); +dispatcher_t *dispatcher_create(int timeout, + context_constructor_t constructor, void *param); #endif /* DISPATCHER_H_ */ diff --git a/src/manager/lib/response.c b/src/manager/lib/response.c index ae74ab6e5..be933792f 100644 --- a/src/manager/lib/response.c +++ b/src/manager/lib/response.c @@ -186,6 +186,15 @@ static void redirect(private_response_t *this, char *location) *location == '/' ? "" : "/", location); } + +/** + * Implementation of response_t.get_base. + */ +static char* get_base(private_response_t *this) +{ + return FCGX_GetParam("SCRIPT_NAME", this->req->envp); +} + /** * Implementation of response_t.destroy */ @@ -210,6 +219,7 @@ response_t *response_create(FCGX_Request *request) this->public.set_content_type = (void(*)(response_t*, char *type))set_content_type; this->public.add_cookie = (void(*)(response_t*, char *name, char *value))add_cookie; this->public.redirect = (void(*)(response_t*, char *location))redirect; + this->public.get_base = (char*(*)(response_t*))get_base; this->public.destroy = (void(*)(response_t*))destroy; this->req = request; diff --git a/src/manager/lib/response.h b/src/manager/lib/response.h index e3be2cf43..50d0eacc1 100644 --- a/src/manager/lib/response.h +++ b/src/manager/lib/response.h @@ -78,6 +78,13 @@ struct response_t { * @param location location to redirect to */ void (*redirect)(response_t *this, char *location); + + /** + * @brief Get the base path of the application. + * + * @return base path + */ + char* (*get_base)(response_t *this); /** * @brief Destroy a response_t. diff --git a/src/manager/lib/session.c b/src/manager/lib/session.c index be25f2737..7520c3226 100644 --- a/src/manager/lib/session.c +++ b/src/manager/lib/session.c @@ -91,7 +91,6 @@ static void process(private_session_t *this, char *pos, *path, *controller, *action; iterator_t *iterator; bool handled = FALSE; - controller_handler_t handler; controller_t *current; if (this->sid == NULL) @@ -126,12 +125,8 @@ static void process(private_session_t *this, { if (streq(current->get_name(current), controller)) { - handler = current->get_handler(current, action); - if (handler) - { - handler(current, request, response); - handled = TRUE; - } + current->handle(current, request, response, action, NULL, NULL, NULL, NULL); + handled = TRUE; break; } } @@ -140,8 +135,15 @@ static void process(private_session_t *this, free(action); if (!handled) { - response->add_header(response, "Status", "400 Not Found"); - response->printf(response, "<html><body><h1>Not Found</h1></body></html>\n"); + if (this->controllers->get_first(this->controllers, + (void**)¤t) == SUCCESS) + { + response->redirect(response, current->get_name(current)); + } + else + { + response->printf(response, "No controllers loaded!\n"); + } } } diff --git a/src/manager/lib/session.h b/src/manager/lib/session.h index baaacd098..a66b1a8e2 100644 --- a/src/manager/lib/session.h +++ b/src/manager/lib/session.h @@ -68,7 +68,7 @@ struct session_t { /** * @brief Create a session. * - * @param context user defined session context instance + * @param context user defined session context instance */ session_t *session_create(context_t *context); diff --git a/src/manager/lib/template.c b/src/manager/lib/template.c index 3ae7c87a3..36a4d294e 100644 --- a/src/manager/lib/template.c +++ b/src/manager/lib/template.c @@ -66,6 +66,8 @@ static void render(private_template_t *this, response_t *response) NEOERR* err; CSPARSE *parse; + hdf_set_value(this->hdf, "base", response->get_base(response)); + err = cs_init(&parse, this->hdf); if (!err) { diff --git a/src/manager/lib/template.h b/src/manager/lib/template.h index 1a8c2f7b7..6e17177a1 100644 --- a/src/manager/lib/template.h +++ b/src/manager/lib/template.h @@ -55,6 +55,10 @@ struct template_t { /** * @brief Render a template to a response object. * + * The render() function additionally sets a clearsilver variable "base" + * which points to the root of the web application and allows to point to + * other targets without to worry about path location. + * * @param response response to render to * @return rendered template string */ |