Browse Source
added a generic router. This is a REST router that maps urls to specific functions which it tries to find via dlsym. That way the only thing to do to get a new REST functionality is to add a function that fits into the namesheme. All existing functionality is moved accordingly
v0.1.8
added a generic router. This is a REST router that maps urls to specific functions which it tries to find via dlsym. That way the only thing to do to get a new REST functionality is to add a function that fits into the namesheme. All existing functionality is moved accordingly
v0.1.8
23 changed files with 841 additions and 257 deletions
-
14assets/js/init.js
-
1configure.ac
-
2include/application/adapter/http.h
-
17include/http/request.h
-
59include/router.h
-
11src/Makefile.am
-
9src/application/Makefile.am
-
2src/application/adapter/http/http.c
-
245src/application/adapter/http/update.c
-
75src/application/controller/authenticate/create.c
-
44src/application/controller/authenticate/delete.c
-
58src/application/controller/currentuser/read.c
-
63src/application/controller/randval/read.c
-
59src/application/controller/sessinfo/read.c
-
98src/application/controller/user/create.c
-
49src/application/controller/version/read.c
-
2src/http/Makefile.am
-
12src/http/request.c
-
15src/http/request/get_method_id.c
-
11src/router/Makefile.am
-
181src/router/route.c
-
66src/router/router.c
-
5src/taskrambler.c
@ -0,0 +1,59 @@ |
|||||
|
/** |
||||
|
* \file |
||||
|
* A generic REST router that is able to map a URL to a specific |
||||
|
* function. It uses dlsym to get the address of the function to |
||||
|
* be called. |
||||
|
* The functions need to have a common interface for sure....This will |
||||
|
* work out while I am working on this. |
||||
|
* After a function is found it's address will be stored in a hash |
||||
|
* so that further lookups might be faster. |
||||
|
* By it's nature I think this is part of the HttpApplicationAdapter. |
||||
|
* |
||||
|
* \author Georg Hopp |
||||
|
* |
||||
|
* \copyright |
||||
|
* Copyright © 2013 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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#ifndef __ROUTER_H__ |
||||
|
#define __ROUTER_H__ |
||||
|
|
||||
|
#include <dlfcn.h> |
||||
|
|
||||
|
#include "class.h" |
||||
|
#include "hash.h" |
||||
|
#include "session.h" |
||||
|
#include "http/request.h" |
||||
|
#include "http/response.h" |
||||
|
#include "application/application.h" |
||||
|
|
||||
|
typedef char * (* fptr_routable)(Application, Session, Hash); |
||||
|
|
||||
|
CLASS(Router) { |
||||
|
Hash functions; |
||||
|
Application application; |
||||
|
|
||||
|
void * handle; |
||||
|
char * prefix; |
||||
|
size_t nprefix; |
||||
|
|
||||
|
}; |
||||
|
|
||||
|
HttpResponse routerRoute(Router, HttpRequest, Session); |
||||
|
|
||||
|
#endif // __ROUTER_H__ |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -0,0 +1,75 @@ |
|||||
|
/** |
||||
|
* \file |
||||
|
* |
||||
|
* \author Georg Hopp |
||||
|
* |
||||
|
* \copyright |
||||
|
* Copyright © 2013 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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#define _GNU_SOURCE |
||||
|
|
||||
|
#include "class.h" |
||||
|
#include "application/application.h" |
||||
|
#include "session.h" |
||||
|
#include "hash.h" |
||||
|
#include "auth/credential.h" |
||||
|
#include "user.h" |
||||
|
|
||||
|
#include "utils/memory.h" |
||||
|
|
||||
|
char * controllerCurrentuserRead(Application, Session, Hash); |
||||
|
|
||||
|
char * |
||||
|
controllerAuthenticateCreate( |
||||
|
Application application, |
||||
|
Session session, |
||||
|
Hash args) |
||||
|
{ |
||||
|
HashValue username; |
||||
|
HashValue password; |
||||
|
Credential credential; |
||||
|
|
||||
|
char * response_data; |
||||
|
|
||||
|
username = hashGet(args, CSTRA("username")); |
||||
|
password = hashGet(args, CSTRA("password")); |
||||
|
|
||||
|
if (NULL == username) { |
||||
|
username = hashGet(args, CSTRA("email")); |
||||
|
} |
||||
|
|
||||
|
if (NULL == username || NULL == password) { |
||||
|
return NULL; |
||||
|
} |
||||
|
|
||||
|
credential = new(Credential, |
||||
|
CRED_PASSWORD, |
||||
|
(char *)(username->value), username->nvalue, |
||||
|
(char *)(password->value), password->nvalue); |
||||
|
|
||||
|
if (! applicationLogin(application, credential, session)) { |
||||
|
response_data = NULL; |
||||
|
} else { |
||||
|
response_data = controllerCurrentuserRead(application, session, NULL); |
||||
|
} |
||||
|
|
||||
|
delete(credential); |
||||
|
|
||||
|
return response_data; |
||||
|
} |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -0,0 +1,44 @@ |
|||||
|
/** |
||||
|
* \file |
||||
|
* |
||||
|
* \author Georg Hopp |
||||
|
* |
||||
|
* \copyright |
||||
|
* Copyright © 2013 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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#define _GNU_SOURCE |
||||
|
|
||||
|
#include "class.h" |
||||
|
#include "application/application.h" |
||||
|
#include "session.h" |
||||
|
#include "hash.h" |
||||
|
|
||||
|
#include "utils/memory.h" |
||||
|
|
||||
|
char * controllerCurrentuserRead(Application, Session, Hash); |
||||
|
|
||||
|
char * |
||||
|
controllerAuthenticateDelete( |
||||
|
Application application, |
||||
|
Session session, |
||||
|
Hash args) |
||||
|
{ |
||||
|
applicationLogout(application, session); |
||||
|
return controllerCurrentuserRead(application, session, NULL); |
||||
|
} |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -0,0 +1,58 @@ |
|||||
|
/** |
||||
|
* \file |
||||
|
* |
||||
|
* \author Georg Hopp |
||||
|
* |
||||
|
* \copyright |
||||
|
* Copyright © 2013 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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#define _GNU_SOURCE |
||||
|
|
||||
|
#include <sys/types.h> |
||||
|
#include <stdio.h> |
||||
|
|
||||
|
#include "class.h" |
||||
|
#include "application/application.h" |
||||
|
#include "session.h" |
||||
|
#include "hash.h" |
||||
|
|
||||
|
#include "utils/memory.h" |
||||
|
|
||||
|
|
||||
|
#define USER_JSON \ |
||||
|
"{\"email\":\"%s\",\"firstname\":\"%s\",\"surname\":\"%s\"}" |
||||
|
|
||||
|
char * |
||||
|
controllerCurrentuserRead(Application app, Session sess, Hash args) |
||||
|
{ |
||||
|
char * buffer; |
||||
|
size_t nbuffer; |
||||
|
|
||||
|
nbuffer = snprintf(NULL, 0, USER_JSON, |
||||
|
(NULL != sess->user)? sess->user->email : "", |
||||
|
(NULL != sess->user)? sess->user->firstname : "", |
||||
|
(NULL != sess->user)? sess->user->surname : ""); |
||||
|
buffer = memMalloc(nbuffer); |
||||
|
nbuffer = sprintf(buffer, USER_JSON, |
||||
|
(NULL != sess->user)? sess->user->email : "", |
||||
|
(NULL != sess->user)? sess->user->firstname : "", |
||||
|
(NULL != sess->user)? sess->user->surname : ""); |
||||
|
|
||||
|
return buffer; |
||||
|
} |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -0,0 +1,63 @@ |
|||||
|
/** |
||||
|
* \file |
||||
|
* |
||||
|
* \author Georg Hopp |
||||
|
* |
||||
|
* \copyright |
||||
|
* Copyright © 2013 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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#define _GNU_SOURCE |
||||
|
|
||||
|
#include <sys/types.h> |
||||
|
#include <stdio.h> |
||||
|
|
||||
|
#include "application/application.h" |
||||
|
#include "session.h" |
||||
|
#include "hash.h" |
||||
|
|
||||
|
#include "utils/memory.h" |
||||
|
|
||||
|
#define RANDVAL_JSON "{\"ctime\":%ld,\"vnext\":%ld,\"value\":\"%02d\"}" |
||||
|
|
||||
|
char * |
||||
|
controllerRandvalRead(Application app, Session sess, Hash args) |
||||
|
{ |
||||
|
char * buffer; |
||||
|
size_t nbuffer; |
||||
|
time_t remaining; |
||||
|
|
||||
|
remaining = 10 - (time(NULL) - app->val->timestamp); |
||||
|
|
||||
|
nbuffer = snprintf( |
||||
|
NULL, |
||||
|
0, |
||||
|
RANDVAL_JSON, |
||||
|
app->val->timestamp, |
||||
|
remaining, |
||||
|
app->val->value); |
||||
|
buffer = memMalloc(nbuffer); |
||||
|
sprintf( |
||||
|
buffer, |
||||
|
RANDVAL_JSON, |
||||
|
app->val->timestamp, |
||||
|
remaining, |
||||
|
app->val->value); |
||||
|
|
||||
|
return buffer; |
||||
|
} |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -0,0 +1,59 @@ |
|||||
|
/** |
||||
|
* \file |
||||
|
* |
||||
|
* \author Georg Hopp |
||||
|
* |
||||
|
* \copyright |
||||
|
* Copyright © 2013 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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#define _GNU_SOURCE |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
#include <sys/types.h> |
||||
|
|
||||
|
#include "application/application.h" |
||||
|
#include "session.h" |
||||
|
#include "hash.h" |
||||
|
|
||||
|
#include "utils/memory.h" |
||||
|
|
||||
|
#define SESSION_JSON "{\"id\":\"%s\",\"timeout\":%d,\"timeleft\":%ld}" |
||||
|
|
||||
|
|
||||
|
char * |
||||
|
controllerSessinfoRead( |
||||
|
Application application, |
||||
|
Session session, |
||||
|
Hash args) |
||||
|
{ |
||||
|
char * buffer; |
||||
|
size_t nbuffer; |
||||
|
|
||||
|
nbuffer = snprintf(NULL, 0, SESSION_JSON, |
||||
|
(NULL != session)? session->id : "", |
||||
|
(NULL != session)? SESSION_LIVETIME : 0, |
||||
|
(NULL != session)? session->livetime - time(NULL) : 0); |
||||
|
buffer = memMalloc(nbuffer); |
||||
|
sprintf(buffer, SESSION_JSON, |
||||
|
(NULL != session)? session->id : "", |
||||
|
(NULL != session)? SESSION_LIVETIME : 0, |
||||
|
(NULL != session)? session->livetime - time(NULL) : 0); |
||||
|
|
||||
|
return buffer; |
||||
|
} |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -0,0 +1,98 @@ |
|||||
|
/** |
||||
|
* \file |
||||
|
* |
||||
|
* \author Georg Hopp |
||||
|
* |
||||
|
* \copyright |
||||
|
* Copyright © 2013 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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#define _GNU_SOURCE |
||||
|
|
||||
|
#include "application/application.h" |
||||
|
#include "session.h" |
||||
|
#include "hash.h" |
||||
|
#include "auth/credential.h" |
||||
|
#include "user.h" |
||||
|
|
||||
|
#include "utils/memory.h" |
||||
|
|
||||
|
char * controllerCurrentuserRead(Application, Session, Hash); |
||||
|
|
||||
|
char * |
||||
|
controllerUserCreate( |
||||
|
Application application, |
||||
|
Session session, |
||||
|
Hash args) |
||||
|
{ |
||||
|
HashValue email; |
||||
|
HashValue password; |
||||
|
HashValue pwrepeat; |
||||
|
HashValue firstname; |
||||
|
HashValue surname; |
||||
|
|
||||
|
Credential credential; |
||||
|
User user; |
||||
|
|
||||
|
char * response_data; |
||||
|
|
||||
|
email = hashGet(args, CSTRA("email")); |
||||
|
password = hashGet(args, CSTRA("password")); |
||||
|
pwrepeat = hashGet(args, CSTRA("pwrepeat")); |
||||
|
firstname = hashGet(args, CSTRA("firstname")); |
||||
|
surname = hashGet(args, CSTRA("surname")); |
||||
|
|
||||
|
if ( |
||||
|
NULL == email || |
||||
|
NULL == password || |
||||
|
NULL == pwrepeat || |
||||
|
NULL == firstname || |
||||
|
NULL == surname) |
||||
|
{ |
||||
|
return NULL; |
||||
|
} |
||||
|
|
||||
|
if ( |
||||
|
password->nvalue != pwrepeat->nvalue || |
||||
|
0 != memcmp(password->value, pwrepeat->value, password->nvalue)) |
||||
|
{ |
||||
|
return NULL; |
||||
|
} |
||||
|
|
||||
|
credential = new(Credential, |
||||
|
CRED_PASSWORD, |
||||
|
(char *)(email->value), email->nvalue, |
||||
|
(char *)(password->value), password->nvalue); |
||||
|
|
||||
|
user = new(User, |
||||
|
(char *)(email->value), email->nvalue, |
||||
|
(char *)(firstname->value), firstname->nvalue, |
||||
|
(char *)(surname->value), surname->nvalue); |
||||
|
|
||||
|
if (! applicationSignup(application, credential, user, session)) { |
||||
|
response_data = NULL; |
||||
|
} else { |
||||
|
applicationLogin(application, credential, session); |
||||
|
response_data = controllerCurrentuserRead(application, session, NULL); |
||||
|
} |
||||
|
|
||||
|
delete(credential); |
||||
|
delete(user); |
||||
|
|
||||
|
return response_data; |
||||
|
} |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -0,0 +1,49 @@ |
|||||
|
/** |
||||
|
* \file |
||||
|
* |
||||
|
* \author Georg Hopp |
||||
|
* |
||||
|
* \copyright |
||||
|
* Copyright © 2013 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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#define _GNU_SOURCE |
||||
|
|
||||
|
#include <sys/types.h> |
||||
|
#include <stdio.h> |
||||
|
|
||||
|
#include "application/application.h" |
||||
|
#include "hash.h" |
||||
|
#include "session.h" |
||||
|
|
||||
|
#include "utils/memory.h" |
||||
|
|
||||
|
#define VERSION_JSON "{\"version\":\"%s\"}" |
||||
|
|
||||
|
char * |
||||
|
controllerVersionRead(Application app, Session sess, Hash args) |
||||
|
{ |
||||
|
char * buffer; |
||||
|
size_t nbuffer; |
||||
|
|
||||
|
nbuffer = snprintf(NULL, 0, VERSION_JSON, app->version? app->version : ""); |
||||
|
buffer = memMalloc(nbuffer); |
||||
|
sprintf(buffer, VERSION_JSON, app->version? app->version : ""); |
||||
|
|
||||
|
return buffer; |
||||
|
} |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -0,0 +1,11 @@ |
|||||
|
ACLOCAL_AMFLAGS = -I m4 |
||||
|
AUTOMAKE_OPTIONS = subdir-objects |
||||
|
|
||||
|
ROUTER = router.c route.c |
||||
|
|
||||
|
AM_CFLAGS += -I../../include/ |
||||
|
|
||||
|
noinst_LTLIBRARIES = librouter.la |
||||
|
|
||||
|
librouter_la_SOURCES = $(ROUTER) |
||||
|
librouter_la_CFLAGS = $(AM_CFLAGS) |
||||
@ -0,0 +1,181 @@ |
|||||
|
/** |
||||
|
* \file |
||||
|
* This is the generic application router.... |
||||
|
* Here RBAC can take place as every resource is always requested |
||||
|
* via an HTTP request. |
||||
|
* |
||||
|
* \author Georg Hopp |
||||
|
* |
||||
|
* \copyright |
||||
|
* Copyright © 2013 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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
// for strchr and others. |
||||
|
#include <string.h> |
||||
|
|
||||
|
// for size_t |
||||
|
#include <sys/types.h> |
||||
|
|
||||
|
// for dlopen, dlsym |
||||
|
#include <dlfcn.h> |
||||
|
|
||||
|
// for toupper |
||||
|
#include <ctype.h> |
||||
|
|
||||
|
#include "router.h" |
||||
|
#include "hash.h" |
||||
|
#include "session.h" |
||||
|
#include "http/request.h" |
||||
|
#include "http/response.h" |
||||
|
#include "application/application.h" |
||||
|
|
||||
|
#include "utils/memory.h" |
||||
|
#include "commons.h" |
||||
|
|
||||
|
|
||||
|
#define COMMAND_LEN 128 |
||||
|
|
||||
|
|
||||
|
HttpResponse |
||||
|
routerRoute( |
||||
|
Router this, |
||||
|
HttpRequest request, |
||||
|
Session sess) |
||||
|
{ |
||||
|
char functionName[COMMAND_LEN + this->nprefix * 10]; |
||||
|
Hash args = NULL; |
||||
|
fptr_routable function; |
||||
|
|
||||
|
char * tmp; |
||||
|
char * command; |
||||
|
size_t ncommand; |
||||
|
char * response_data; |
||||
|
HttpResponse response; |
||||
|
|
||||
|
if ('/' != request->uri[0]) { |
||||
|
/* |
||||
|
* we only support absolute paths within our |
||||
|
* application |
||||
|
*/ |
||||
|
return NULL; |
||||
|
} |
||||
|
|
||||
|
command = &(request->uri[1]); |
||||
|
command[0] = toupper(command[0]); |
||||
|
|
||||
|
/* |
||||
|
* find end of command |
||||
|
*/ |
||||
|
tmp = strchr(command, '/'); |
||||
|
if (NULL == tmp) { |
||||
|
ncommand = strlen(command); |
||||
|
} else { |
||||
|
ncommand = tmp - command; |
||||
|
} |
||||
|
|
||||
|
memcpy(functionName, this->prefix, this->nprefix); |
||||
|
memcpy(&(functionName[this->nprefix]), |
||||
|
command, MIN(COMMAND_LEN, ncommand)); |
||||
|
|
||||
|
/** |
||||
|
* \todo |
||||
|
* now get all arguments if we have some |
||||
|
*/ |
||||
|
|
||||
|
/* |
||||
|
* following the crud pattern we map the first part |
||||
|
* of the uri and the request method to according |
||||
|
* function names. |
||||
|
*/ |
||||
|
switch (request->method_id) { |
||||
|
case HTTP_GET: |
||||
|
args = new(Hash); |
||||
|
strcpy(&(functionName[this->nprefix + ncommand]), "Read"); |
||||
|
break; |
||||
|
|
||||
|
case HTTP_POST: |
||||
|
args = request->post; |
||||
|
strcpy(&(functionName[this->nprefix + ncommand]), "Create"); |
||||
|
break; |
||||
|
|
||||
|
case HTTP_PUT: |
||||
|
strcpy(&(functionName[this->nprefix + ncommand]), "Update"); |
||||
|
break; |
||||
|
|
||||
|
case HTTP_DELETE: |
||||
|
strcpy(&(functionName[this->nprefix + ncommand]), "Delete"); |
||||
|
break; |
||||
|
|
||||
|
default: |
||||
|
/* other methods are not subject of REST */ |
||||
|
return NULL; |
||||
|
} |
||||
|
|
||||
|
/* |
||||
|
* \todo for the moment I don't cache the found symbol... |
||||
|
* I don't even check if there was an error...the only thing |
||||
|
* I do is checking a NULL symbol and in that case don't |
||||
|
* handle the request here. |
||||
|
*/ |
||||
|
dlerror(); |
||||
|
function = dlsym(this->handle, functionName); |
||||
|
|
||||
|
/** |
||||
|
* \todo somewhere here or above access control have to take place |
||||
|
*/ |
||||
|
|
||||
|
if (NULL == function) { |
||||
|
/** |
||||
|
* nothing there to handle the request ... so leave it to the |
||||
|
* caller... |
||||
|
*/ |
||||
|
char * error; |
||||
|
|
||||
|
if (NULL != (error = dlerror())) { |
||||
|
/** |
||||
|
* \todo add logging...maybe. |
||||
|
*/ |
||||
|
} |
||||
|
|
||||
|
return NULL; |
||||
|
} |
||||
|
|
||||
|
/* |
||||
|
* function has to allocate the memory for reponse_date by using |
||||
|
* memMalloc. |
||||
|
*/ |
||||
|
response_data = function(this->application, sess, args); |
||||
|
|
||||
|
switch (request->method_id) { |
||||
|
case HTTP_GET: |
||||
|
delete(args); |
||||
|
break; |
||||
|
|
||||
|
case HTTP_POST: |
||||
|
case HTTP_PUT: |
||||
|
case HTTP_DELETE: |
||||
|
default: |
||||
|
/* other methods are not subject of REST */ |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
response = httpResponseJson(response_data, strlen(response_data)); |
||||
|
MEM_FREE(response_data); |
||||
|
|
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -0,0 +1,66 @@ |
|||||
|
/** |
||||
|
* \file |
||||
|
* |
||||
|
* \author Georg Hopp |
||||
|
* |
||||
|
* \copyright |
||||
|
* Copyright © 2013 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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#include <stdarg.h> |
||||
|
|
||||
|
// for dlopen, dlsym |
||||
|
#include <dlfcn.h> |
||||
|
|
||||
|
#include "class.h" |
||||
|
#include "router.h" |
||||
|
#include "hash.h" |
||||
|
#include "application/application.h" |
||||
|
|
||||
|
#define PREFIX "controller" |
||||
|
|
||||
|
static |
||||
|
int |
||||
|
routerCtor(void * _this, va_list * params) |
||||
|
{ |
||||
|
Router this = _this; |
||||
|
|
||||
|
this->application = va_arg(*params, Application); |
||||
|
this->functions = new(Hash); |
||||
|
this->handle = dlopen(NULL, RTLD_LAZY); |
||||
|
this->prefix = PREFIX; |
||||
|
this->nprefix = sizeof(PREFIX) - 1; |
||||
|
|
||||
|
if (NULL == this->handle) { |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
static |
||||
|
void |
||||
|
routerDtor(void * _this) { |
||||
|
Router this = _this; |
||||
|
|
||||
|
delete(this->functions); |
||||
|
dlclose(this->handle); |
||||
|
} |
||||
|
|
||||
|
INIT_IFACE(Class, routerCtor, routerDtor, NULL); |
||||
|
CREATE_CLASS(Router, NULL, IFACE(Class)); |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue