implemented proper argument parsing code

This commit is contained in:
Martin Willi
2007-09-28 06:43:59 +00:00
parent a57ab4d690
commit 780050cbc3
+17 -24
View File
@@ -87,51 +87,44 @@ static void create_sid(private_session_t *this, request_t *request)
*/ */
static void process(private_session_t *this, request_t *request) static void process(private_session_t *this, request_t *request)
{ {
char *pos, *path, *controller, *action; char *pos, *start, *param[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
iterator_t *iterator; iterator_t *iterator;
bool handled = FALSE; bool handled = FALSE;
controller_t *current; controller_t *current;
int i = 0;
if (this->sid == NULL) if (this->sid == NULL)
{ {
create_sid(this, request); create_sid(this, request);
} }
path = request->get_path(request); start = request->get_path(request);
if (*path == '/') path++; if (start)
pos = strchr(path, '/');
if (pos == NULL)
{ {
controller = strdup(path); if (*start == '/') start++;
action = strdup(""); while ((pos = strchr(start, '/')) != NULL && i < 5)
}
else
{ {
controller = strndup(path, pos - path); param[i++] = strndup(start, pos - start);
path = pos + 1; start = pos + 1;
pos = strchr(path, '/');
if (pos == NULL)
{
action = strdup(path);
}
else
{
action = strndup(path, pos - path);
}
} }
param[i] = strdup(start);
iterator = this->controllers->create_iterator(this->controllers, TRUE); iterator = this->controllers->create_iterator(this->controllers, TRUE);
while (iterator->iterate(iterator, (void**)&current)) while (iterator->iterate(iterator, (void**)&current))
{ {
if (streq(current->get_name(current), controller)) if (streq(current->get_name(current), param[0]))
{ {
current->handle(current, request, action, NULL, NULL, NULL, NULL); current->handle(current, request, param[1], param[2], param[3],
param[4], param[5]);
handled = TRUE; handled = TRUE;
break; break;
} }
} }
iterator->destroy(iterator); iterator->destroy(iterator);
free(controller); for (i = 0; i < 6; i++)
free(action); {
free(param[i]);
}
}
if (!handled) if (!handled)
{ {
if (this->controllers->get_first(this->controllers, if (this->controllers->get_first(this->controllers,