|
|
|
@ -0,0 +1,774 @@
|
|
|
|
|
/*to create API*/
|
|
|
|
|
#include <ulfius.h>
|
|
|
|
|
/*to connect to Postgresql*/
|
|
|
|
|
#include <libpq-fe.h>
|
|
|
|
|
/* to use JSON in response body*/
|
|
|
|
|
#include <jansson.h>
|
|
|
|
|
/*to use strcat*/
|
|
|
|
|
#include <string.h>
|
|
|
|
|
/*to use inet_ntoa*/
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
/*to use print*/
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
/*to use sleep*/
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
/*to use struct pq_confo*/
|
|
|
|
|
#include "pqconfo.h"
|
|
|
|
|
/*to use file2char*/
|
|
|
|
|
#include "file2char.h"
|
|
|
|
|
/* to use pthread_mutex_t type*/
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
|
|
#define ROUTE_HELLO "/hello"
|
|
|
|
|
#define ROUTE_ENTITIES "/entities"
|
|
|
|
|
|
|
|
|
|
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* callback functions declaration
|
|
|
|
|
*/
|
|
|
|
|
int callback_get_hello (const struct _u_request * request, struct _u_response * response, void * user_data);
|
|
|
|
|
|
|
|
|
|
int callback_default (const struct _u_request * request, struct _u_response * response, void * user_data);
|
|
|
|
|
|
|
|
|
|
int callback_create_entity (const struct _u_request * request, struct _u_response * response, void * user_data);
|
|
|
|
|
int callback_get_entities (const struct _u_request * request, struct _u_response * response, void * user_data);
|
|
|
|
|
int callback_get_entity (const struct _u_request * request, struct _u_response * response, void * user_data);
|
|
|
|
|
int callback_update_entity (const struct _u_request * request, struct _u_response * response, void * user_data);
|
|
|
|
|
int callback_delete_entity (const struct _u_request * request, struct _u_response * response, void * user_data);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* decode a u_map into a string
|
|
|
|
|
*/
|
|
|
|
|
char * print_map(const struct _u_map * map) {
|
|
|
|
|
/*declaration*/
|
|
|
|
|
char * line, * to_return = NULL;
|
|
|
|
|
const char **keys, * value;
|
|
|
|
|
int len, i;
|
|
|
|
|
|
|
|
|
|
if (map != NULL) {
|
|
|
|
|
keys = u_map_enum_keys(map);
|
|
|
|
|
for (i=0; keys[i] != NULL; i++) {
|
|
|
|
|
value = u_map_get(map, keys[i]);
|
|
|
|
|
len = snprintf(NULL, 0, "key is %s, value is %s", keys[i], value);
|
|
|
|
|
line = malloc((size_t)(len+1));
|
|
|
|
|
snprintf(line, (size_t)(len+1), "key is %s, value is %s", keys[i], value);
|
|
|
|
|
if (to_return != NULL) {
|
|
|
|
|
len = (int)(strlen(to_return) + strlen(line) + 1);
|
|
|
|
|
to_return = realloc(to_return, (size_t)(len+1));
|
|
|
|
|
if (strlen(to_return) > 0) {
|
|
|
|
|
//concatenate strings
|
|
|
|
|
strcat(to_return, "\n");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
to_return = malloc((strlen(line) + 1));
|
|
|
|
|
to_return[0] = 0;
|
|
|
|
|
}
|
|
|
|
|
strcat(to_return, line);
|
|
|
|
|
free(line);
|
|
|
|
|
}
|
|
|
|
|
return to_return;
|
|
|
|
|
} else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
/*declaration*/
|
|
|
|
|
struct _u_instance instance;
|
|
|
|
|
int frameworkRet, apiPort, apiPortRet, cx;
|
|
|
|
|
|
|
|
|
|
/*TODO How is const defined?*/
|
|
|
|
|
const int pqConInfoSize = 100;
|
|
|
|
|
char pqConInfo[pqConInfoSize];
|
|
|
|
|
int pqConfoRet;
|
|
|
|
|
const struct pq_confo * pqConfo;
|
|
|
|
|
|
|
|
|
|
printf("main() Started...\n");
|
|
|
|
|
if (argc != 3) {
|
|
|
|
|
fprintf(stderr, "Usage: ./main <API port> <path to config file>\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("main() argv[1]: %s\n", argv[1]);
|
|
|
|
|
printf("main() argv[2]: %s\n", argv[2]);
|
|
|
|
|
|
|
|
|
|
/*get Postgresql connection info from config*/
|
|
|
|
|
/*TODO Validate argv!*/
|
|
|
|
|
pqConfoRet=get_pq_confo(argv[2],&pqConfo);
|
|
|
|
|
if(pqConfoRet){
|
|
|
|
|
fprintf(stderr, "Postgresql connection information are NOT valid\n");
|
|
|
|
|
return 2;
|
|
|
|
|
}else{
|
|
|
|
|
fprintf(stdout,"pqConfoRet: %d\n",pqConfoRet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*create pq connection info*/
|
|
|
|
|
cx = snprintf(pqConInfo,pqConInfoSize,"postgresql://%s:%s@%s:%d/%s",pqConfo->user,pqConfo->secret,pqConfo->host,pqConfo->port,pqConfo->db);
|
|
|
|
|
if(0>cx && cx>=pqConInfoSize){
|
|
|
|
|
fprintf(stderr,"main() error creating pqConInfo\n");
|
|
|
|
|
/*free Postgresql connection info*/
|
|
|
|
|
pqConfoRet=free_pq_confo(&pqConfo);
|
|
|
|
|
if(pqConfoRet){
|
|
|
|
|
fprintf(stderr, "Postgresql connection information are NOT free\n");
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*free Postgresql connection info*/
|
|
|
|
|
pqConfoRet=free_pq_confo(&pqConfo);
|
|
|
|
|
if(pqConfoRet){
|
|
|
|
|
fprintf(stderr, "Postgresql connection information are NOT free\n");
|
|
|
|
|
return 5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*store command line argument in variable*/
|
|
|
|
|
/*validate user input*/
|
|
|
|
|
/*omit injection*/
|
|
|
|
|
/*stream:argv*/
|
|
|
|
|
apiPortRet = sscanf(argv[1], "%d", &apiPort);
|
|
|
|
|
/*valid user input:1 successfully filled item*/
|
|
|
|
|
if (apiPortRet != 1) {
|
|
|
|
|
fprintf(stderr, "The argument must be an integer\n");
|
|
|
|
|
return 6;
|
|
|
|
|
}
|
|
|
|
|
if (apiPort < 0) {
|
|
|
|
|
fprintf(stderr, "Error passing a negative port\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
printf("main() port: %d\n", apiPort);
|
|
|
|
|
|
|
|
|
|
/*TODO Is mem leaked when connection to pq failes?*/
|
|
|
|
|
|
|
|
|
|
if (ulfius_init_instance(&instance, apiPort, NULL, NULL) != U_OK) {
|
|
|
|
|
printf("main() Error ulfius_init_instance, abort");
|
|
|
|
|
return 7;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*connect to database*/
|
|
|
|
|
//TODO This is a synchronous call! Shall we block the rest?
|
|
|
|
|
PGconn *conn = PQconnectdb(pqConInfo);
|
|
|
|
|
if (PQstatus(conn) == CONNECTION_BAD) {
|
|
|
|
|
fprintf(stderr, "Connection to database failed: %s\n",
|
|
|
|
|
PQerrorMessage(conn));
|
|
|
|
|
PQfinish(conn);
|
|
|
|
|
return 8;
|
|
|
|
|
}else if(PQstatus(conn)==CONNECTION_OK){
|
|
|
|
|
printf("main() connected to database\n");
|
|
|
|
|
}else{
|
|
|
|
|
printf("main() connection status NOT known\n");
|
|
|
|
|
return 9;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO Why?
|
|
|
|
|
u_map_put(instance.default_headers, "Access-Control-Allow-Origin", "*");
|
|
|
|
|
|
|
|
|
|
// Maximum body size sent by the client is 1 Kb
|
|
|
|
|
instance.max_post_body_size = 1024;
|
|
|
|
|
|
|
|
|
|
// Endpoint list declaration
|
|
|
|
|
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_HELLO, NULL, 0, &callback_get_hello, NULL);
|
|
|
|
|
|
|
|
|
|
//user_data: pass pq connection information to callback
|
|
|
|
|
ulfius_add_endpoint_by_val(&instance, "POST", ROUTE_ENTITIES, "/create", 0, &callback_create_entity, (void *)conn);
|
|
|
|
|
|
|
|
|
|
//user_data: pass pq connection information to callback
|
|
|
|
|
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_ENTITIES, "/info", 0, &callback_get_entities, (void *)conn);
|
|
|
|
|
|
|
|
|
|
//user_data: pass pq connection information to callback
|
|
|
|
|
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_ENTITIES, "/:id/info", 0, &callback_get_entity, (void *)conn);
|
|
|
|
|
|
|
|
|
|
//user_data: pass pq connection information to callback
|
|
|
|
|
ulfius_add_endpoint_by_val(&instance, "POST", ROUTE_ENTITIES, "/:id/update", 0, &callback_update_entity, (void *)conn);
|
|
|
|
|
|
|
|
|
|
//user_data: pass pq connection information to callback
|
|
|
|
|
ulfius_add_endpoint_by_val(&instance, "DELETE", ROUTE_ENTITIES, "/:id/delete", 0, &callback_delete_entity, (void *)conn);
|
|
|
|
|
|
|
|
|
|
// default_endpoint declaration
|
|
|
|
|
ulfius_set_default_endpoint(&instance, &callback_default, NULL);
|
|
|
|
|
|
|
|
|
|
// Start the framework
|
|
|
|
|
// Open an http connection
|
|
|
|
|
frameworkRet = ulfius_start_framework(&instance);
|
|
|
|
|
|
|
|
|
|
if (frameworkRet == U_OK) {
|
|
|
|
|
printf("main() Start framework on port %d\n", instance.port);
|
|
|
|
|
/*wait*/
|
|
|
|
|
while(1){
|
|
|
|
|
sleep(3600);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
printf("main() Error starting framework; frameworkRet: %d\n",frameworkRet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("main() End framework\n");
|
|
|
|
|
|
|
|
|
|
/*clean up API*/
|
|
|
|
|
ulfius_stop_framework(&instance);
|
|
|
|
|
ulfius_clean_instance(&instance);
|
|
|
|
|
|
|
|
|
|
/*clean up Postgresql connection*/
|
|
|
|
|
PQfinish(conn);
|
|
|
|
|
|
|
|
|
|
printf("main() Done.\n");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function that put a "Hello World!" string in the response
|
|
|
|
|
*/
|
|
|
|
|
int callback_get_hello (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
|
|
|
|
printf("callback_get_hello() Started...\n");
|
|
|
|
|
sleep(1);
|
|
|
|
|
ulfius_set_string_body_response(response, 200, "Hello World!");
|
|
|
|
|
printf("callback_get_hello() Done.\n");
|
|
|
|
|
return U_CALLBACK_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Default callback function called if no endpoint has a match
|
|
|
|
|
*/
|
|
|
|
|
int callback_default (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
|
|
|
|
printf("callback_default() Started...\n");
|
|
|
|
|
ulfius_set_string_body_response(response, 404, "Page not found, do what you want");
|
|
|
|
|
printf("callback_default() Done.\n");
|
|
|
|
|
return U_CALLBACK_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function for route /entities to reply the entity at question
|
|
|
|
|
*/
|
|
|
|
|
int callback_get_entity (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
|
|
|
|
/*declarations*/
|
|
|
|
|
char *url_params;
|
|
|
|
|
char *response_body;
|
|
|
|
|
int r;
|
|
|
|
|
const char **keys;
|
|
|
|
|
const char *value;
|
|
|
|
|
char *line;
|
|
|
|
|
int len;
|
|
|
|
|
int i;
|
|
|
|
|
PGconn *conn;
|
|
|
|
|
char *prepStmEntities;
|
|
|
|
|
PGresult *pqRes;
|
|
|
|
|
int nbRecords,nbFields;
|
|
|
|
|
json_t *arrayRecords, *arrayFields, *stringField;
|
|
|
|
|
|
|
|
|
|
printf("callback_get_entity() Started...\n");
|
|
|
|
|
url_params = print_map(request->map_url);
|
|
|
|
|
r=asprintf(
|
|
|
|
|
&response_body,
|
|
|
|
|
"parameters from the url are \n%s\n\n",
|
|
|
|
|
url_params);
|
|
|
|
|
//printf("callback_get_entity() %d characters generated\n",r);
|
|
|
|
|
|
|
|
|
|
if(request->map_url != NULL){
|
|
|
|
|
keys=u_map_enum_keys(request->map_url);
|
|
|
|
|
if(keys[0] == NULL){
|
|
|
|
|
//TODO error
|
|
|
|
|
printf("callback_get_entity() param NOT available\n");
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/info route: Param error!");
|
|
|
|
|
}else{
|
|
|
|
|
value = u_map_get(request->map_url, keys[0]);
|
|
|
|
|
//printf("callback_get_entity() key is %s, value is %s\n", keys[0], value);
|
|
|
|
|
|
|
|
|
|
conn = (PGconn *)user_data;
|
|
|
|
|
|
|
|
|
|
/*define prepared statement*/
|
|
|
|
|
len=snprintf(NULL,0,"SELECT * FROM entities WHERE id=%s;",value);
|
|
|
|
|
prepStmEntities=malloc((size_t)(len+1));
|
|
|
|
|
//TODO Release prepStmEntities!
|
|
|
|
|
snprintf(prepStmEntities,(size_t)(len+1),"SELECT * FROM entities WHERE id=%s;",value);
|
|
|
|
|
//printf("callback_get_entity() prepStmEntities: %s\n",prepStmEntities);
|
|
|
|
|
|
|
|
|
|
//lock PGconn mutex
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
|
|
|
|
|
|
/*create prepared statement*/
|
|
|
|
|
/*conn:connection*/
|
|
|
|
|
/*stm:statement*/
|
|
|
|
|
/*0:number of passed parameters*/
|
|
|
|
|
/*NULL: server figures out parameter type*/
|
|
|
|
|
/*paramValues:pointer of an array of strings containing parameters*/
|
|
|
|
|
/*NULL:relevant for binary parameters*/
|
|
|
|
|
/*NULL:relevant for binary parameters*/
|
|
|
|
|
/*0:obtain result in text format*/
|
|
|
|
|
//TODO How to pass params?
|
|
|
|
|
pqRes = PQexecParams(conn, prepStmEntities, 0, NULL, NULL, NULL, NULL, 0);
|
|
|
|
|
|
|
|
|
|
//unlock PGconn mutex
|
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
|
|
|
|
|
|
if(PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
|
|
|
|
|
/*handle error*/
|
|
|
|
|
printf("callback_get_entity() No data retrieved\n");
|
|
|
|
|
/*clean up result*/
|
|
|
|
|
PQclear(pqRes);
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/info route: Dba error!");
|
|
|
|
|
}else{
|
|
|
|
|
/*evaluate result*/
|
|
|
|
|
nbRecords = PQntuples(pqRes);
|
|
|
|
|
//printf("callback_get_entity() nbRecords: %d\n",nbRecords);
|
|
|
|
|
nbFields = PQnfields(pqRes);
|
|
|
|
|
arrayRecords=json_array();
|
|
|
|
|
for(int i=0;i<nbRecords;i++){
|
|
|
|
|
arrayFields=json_array();
|
|
|
|
|
for(int j=0;j<nbFields;j++){
|
|
|
|
|
stringField = json_string(PQgetvalue(pqRes,i,j));
|
|
|
|
|
json_array_append(arrayFields, stringField);
|
|
|
|
|
json_decref(stringField);
|
|
|
|
|
}
|
|
|
|
|
json_array_append(arrayRecords, arrayFields);
|
|
|
|
|
json_decref(arrayFields);
|
|
|
|
|
}
|
|
|
|
|
/*clean up result*/
|
|
|
|
|
PQclear(pqRes);
|
|
|
|
|
/*set response body*/
|
|
|
|
|
ulfius_set_json_body_response(response, 200, arrayRecords);
|
|
|
|
|
/*clean up JSON*/
|
|
|
|
|
json_decref(arrayRecords);
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
printf("callback_delete_entity() HTTP GET request error!\n");
|
|
|
|
|
ulfius_set_string_body_response(response, 400, "This is the /entity/:id/info route: HTTP GET request error!");
|
|
|
|
|
}
|
|
|
|
|
/*clean up remainings*/
|
|
|
|
|
free(url_params);
|
|
|
|
|
free(response_body);
|
|
|
|
|
printf("callback_get_entity() Done.\n");
|
|
|
|
|
return U_CALLBACK_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function to reply all entities
|
|
|
|
|
*/
|
|
|
|
|
int callback_get_entities (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
|
|
|
|
/*declarations*/
|
|
|
|
|
PGconn *conn;
|
|
|
|
|
char *prepStmEntities;
|
|
|
|
|
PGresult *pqRes;
|
|
|
|
|
int nbRecords,nbFields;
|
|
|
|
|
json_t *arrayRecords, *arrayFields, *stringField;
|
|
|
|
|
|
|
|
|
|
printf("callback_get_entities() Started...\n");
|
|
|
|
|
conn = (PGconn *)user_data;
|
|
|
|
|
|
|
|
|
|
/*define prepared statement*/
|
|
|
|
|
prepStmEntities = "SELECT * FROM entities;";
|
|
|
|
|
|
|
|
|
|
//lock PGconn mutex
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
|
|
|
|
|
|
/*create prepared statement*/
|
|
|
|
|
/*conn:connection*/
|
|
|
|
|
/*stm:statement*/
|
|
|
|
|
/*1:number of passed parameters*/
|
|
|
|
|
/*NULL: server figures out parameter type*/
|
|
|
|
|
/*paramValues:pointer of an array of strings containing parameters*/
|
|
|
|
|
/*NULL:relevant for binary parameters*/
|
|
|
|
|
/*NULL:relevant for binary parameters*/
|
|
|
|
|
/*0:obtain result in text format*/
|
|
|
|
|
pqRes = PQexecParams(conn, prepStmEntities, 0, NULL, NULL, NULL, NULL, 0);
|
|
|
|
|
|
|
|
|
|
//unlock PGconn mutex
|
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
|
|
|
|
|
|
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
|
|
|
|
|
printf("callback_get_entities() No data retrieved\n");
|
|
|
|
|
/*clean up result*/
|
|
|
|
|
PQclear(pqRes);
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
|
|
|
|
|
printf("callback_get_entities() Done.\n");
|
|
|
|
|
return U_CALLBACK_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*evaluate result*/
|
|
|
|
|
nbRecords = PQntuples(pqRes);
|
|
|
|
|
nbFields = PQnfields(pqRes);
|
|
|
|
|
arrayRecords=json_array();
|
|
|
|
|
for(int i=0;i<nbRecords;i++){
|
|
|
|
|
arrayFields=json_array();
|
|
|
|
|
for(int j=0;j<nbFields;j++){
|
|
|
|
|
/*not password, verification_token_id, created_account*/
|
|
|
|
|
if(j!=6&&j!=8&&j!=10){
|
|
|
|
|
stringField = json_string(PQgetvalue(pqRes,i,j));
|
|
|
|
|
json_array_append(arrayFields, stringField);
|
|
|
|
|
json_decref(stringField);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
json_array_append(arrayRecords, arrayFields);
|
|
|
|
|
json_decref(arrayFields);
|
|
|
|
|
}
|
|
|
|
|
/*clean up result*/
|
|
|
|
|
PQclear(pqRes);
|
|
|
|
|
/*set response body*/
|
|
|
|
|
ulfius_set_json_body_response(response, 200, arrayRecords);
|
|
|
|
|
/*clean up JSON*/
|
|
|
|
|
json_decref(arrayRecords);
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
printf("callback_get_entities() Done.\n");
|
|
|
|
|
return U_CALLBACK_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int query_db (const PGconn * conn, const char ** queryStr, const char ** valueStr){
|
|
|
|
|
/*declarations*/
|
|
|
|
|
int res;
|
|
|
|
|
PGresult *pqRes;
|
|
|
|
|
char *prepStmEntities;
|
|
|
|
|
int len;
|
|
|
|
|
int nbRecords;
|
|
|
|
|
printf("query_db() Started...\n");
|
|
|
|
|
/*define prepared statement*/
|
|
|
|
|
len=snprintf(NULL,0,*queryStr,*valueStr);
|
|
|
|
|
prepStmEntities=malloc((size_t)(len+1));
|
|
|
|
|
//TODO Release prepStmEntities!
|
|
|
|
|
snprintf(prepStmEntities,(size_t)(len+1),*queryStr,*valueStr);
|
|
|
|
|
//printf("query_db() prepStmEntities: %s\n",prepStmEntities);
|
|
|
|
|
|
|
|
|
|
//lock PGconn mutex
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
|
|
|
|
|
|
/*create prepared statement*/
|
|
|
|
|
/*conn:connection*/
|
|
|
|
|
/*stm:statement*/
|
|
|
|
|
/*0:number of passed parameters*/
|
|
|
|
|
/*NULL: server figures out parameter type*/
|
|
|
|
|
/*paramValues:pointer of an array of strings containing parameters*/
|
|
|
|
|
/*NULL:relevant for binary parameters*/
|
|
|
|
|
/*NULL:relevant for binary parameters*/
|
|
|
|
|
/*0:obtain result in text format*/
|
|
|
|
|
//TODO How to pass params?
|
|
|
|
|
pqRes = PQexecParams((PGconn *)conn, prepStmEntities, 0, NULL, NULL, NULL, NULL, 0);
|
|
|
|
|
|
|
|
|
|
//unlock PGconn mutex
|
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
|
|
|
|
|
|
if(PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
|
|
|
|
|
/*handle error*/
|
|
|
|
|
printf("query_db() No data retrieved\n");
|
|
|
|
|
/*clean up result*/
|
|
|
|
|
PQclear(pqRes);
|
|
|
|
|
res=1;
|
|
|
|
|
}else{
|
|
|
|
|
/*evaluate result*/
|
|
|
|
|
nbRecords = PQntuples(pqRes);
|
|
|
|
|
//printf("query_db() nbRecords: %d\n",nbRecords);
|
|
|
|
|
/*clean up result*/
|
|
|
|
|
PQclear(pqRes);
|
|
|
|
|
res=0;
|
|
|
|
|
}
|
|
|
|
|
printf("query_db() Done.\n");
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function to delete a particular entity
|
|
|
|
|
*/
|
|
|
|
|
int callback_delete_entity (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
|
|
|
|
/*declarations*/
|
|
|
|
|
int r;
|
|
|
|
|
//TODO Does this mem need to be deallocated?
|
|
|
|
|
const char **keys;
|
|
|
|
|
//TODO Does this mem need to be deallocated?
|
|
|
|
|
const char *value;
|
|
|
|
|
|
|
|
|
|
printf("callback_delete_entity() Started...\n");
|
|
|
|
|
|
|
|
|
|
if(request->map_url != NULL){
|
|
|
|
|
keys=u_map_enum_keys(request->map_url);
|
|
|
|
|
if(keys[0] == NULL){
|
|
|
|
|
//TODO error
|
|
|
|
|
printf("callback_delete_entity() param NOT available\n");
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/delete route: Param error!");
|
|
|
|
|
}else{
|
|
|
|
|
value = u_map_get(request->map_url, keys[0]);
|
|
|
|
|
//printf("callback_delete_entity() key is %s, value is %s\n", keys[0], value);
|
|
|
|
|
|
|
|
|
|
//TODO delete from entity
|
|
|
|
|
const char * queryFromEntity="DELETE FROM relations WHERE from_entity=%s RETURNING *;";
|
|
|
|
|
|
|
|
|
|
int delRes=query_db((PGconn *)user_data,&queryFromEntity,&value);
|
|
|
|
|
if(delRes!=0){
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/delete route: Dba from_entity error!");
|
|
|
|
|
}else{
|
|
|
|
|
//TODO delete to entity
|
|
|
|
|
const char * queryToEntity="DELETE FROM relations WHERE to_entity=%s RETURNING *;";
|
|
|
|
|
delRes=query_db((PGconn *)user_data,&queryToEntity,&value);
|
|
|
|
|
if(delRes!=0){
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/delete route: Dba to_entity error!");
|
|
|
|
|
}else{
|
|
|
|
|
//TODO delete entity
|
|
|
|
|
const char * queryEntities="DELETE FROM entities WHERE id=%s RETURNING *;";
|
|
|
|
|
delRes=query_db((PGconn *)user_data,&queryEntities,&value);
|
|
|
|
|
if(delRes!=0){
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/delete route: Dba entity error!");
|
|
|
|
|
}else{
|
|
|
|
|
//printf("callback_delete_entity() Juhu! Everything deleted!\n");
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 200, "Done.\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
printf("callback_delete_entity() HTTP DELETE request error!\n");
|
|
|
|
|
ulfius_set_string_body_response(response, 400, "This is the /entity/:id/delete route: HTTP DELETE request error!");
|
|
|
|
|
}
|
|
|
|
|
/*clean up remainings*/
|
|
|
|
|
printf("callback_delete_entity() Done.\n");
|
|
|
|
|
return U_CALLBACK_CONTINUE;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function to update a particular entity
|
|
|
|
|
*/
|
|
|
|
|
int callback_update_entity (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
|
|
|
|
/*declarations*/
|
|
|
|
|
int r;
|
|
|
|
|
const char **keys=NULL;
|
|
|
|
|
const char **keysUrl=NULL;
|
|
|
|
|
const char *value=NULL;
|
|
|
|
|
const char *valueUrl=NULL;
|
|
|
|
|
char *line=NULL;
|
|
|
|
|
int len;
|
|
|
|
|
int i;
|
|
|
|
|
PGconn *conn=NULL;
|
|
|
|
|
char *prepStmEntities=NULL;
|
|
|
|
|
PGresult *pqRes=NULL;
|
|
|
|
|
|
|
|
|
|
printf("callback_update_entity() Started...\n");
|
|
|
|
|
|
|
|
|
|
//parse url
|
|
|
|
|
if(request->map_url != NULL){
|
|
|
|
|
keysUrl=u_map_enum_keys(request->map_url);
|
|
|
|
|
if(keysUrl[0] == NULL){
|
|
|
|
|
//TODO error
|
|
|
|
|
printf("callback_update_entity() param NOT available\n");
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/update route: Param error!");
|
|
|
|
|
}else{
|
|
|
|
|
valueUrl = u_map_get(request->map_url, keysUrl[0]);
|
|
|
|
|
//printf("callback_update_entity() URL: key is %s, value is %s\n", keysUrl[0], valueUrl);
|
|
|
|
|
|
|
|
|
|
//parse body
|
|
|
|
|
if(request->map_post_body != NULL){
|
|
|
|
|
keys=u_map_enum_keys(request->map_post_body);
|
|
|
|
|
if(keys[0] == NULL){
|
|
|
|
|
//TODO error
|
|
|
|
|
printf("callback_update_entity() param NOT available\n");
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/update route: HTTP POST data error!");
|
|
|
|
|
}else{
|
|
|
|
|
//printf("callback_update_entity() param available\n");
|
|
|
|
|
value = u_map_get(request->map_post_body, keys[0]);
|
|
|
|
|
//printf("callback_update_entity() BODY: key is %s, value is %s\n", keys[0], value);
|
|
|
|
|
if(value==NULL){
|
|
|
|
|
//TODO error
|
|
|
|
|
printf("callback_update_entity() key NOT available in request map\n");
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/update route: HTTP POST key error!");
|
|
|
|
|
}else{
|
|
|
|
|
//printf("callback_update_entity() key is %s, value is %s\n", keys[0], value);
|
|
|
|
|
conn = (PGconn *)user_data;
|
|
|
|
|
/*define prepared statement*/
|
|
|
|
|
len=snprintf(NULL,0,
|
|
|
|
|
"UPDATE entities SET name='%s' WHERE id=%s RETURNING *;"
|
|
|
|
|
,value,valueUrl);
|
|
|
|
|
prepStmEntities=malloc((size_t)(len+1));
|
|
|
|
|
//TODO Release prepStmEntities!
|
|
|
|
|
snprintf(prepStmEntities,(size_t)(len+1),
|
|
|
|
|
"UPDATE entities SET name='%s' WHERE id=%s RETURNING *;"
|
|
|
|
|
,value,valueUrl);
|
|
|
|
|
//printf("callback_update_entity() prepStmEntities: %s\n",prepStmEntities);
|
|
|
|
|
|
|
|
|
|
//lock PGconn mutex
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
|
|
|
|
|
|
/*create prepared statement*/
|
|
|
|
|
/*conn:connection*/
|
|
|
|
|
/*stm:statement*/
|
|
|
|
|
/*0:number of passed parameters*/
|
|
|
|
|
/*NULL: server figures out parameter type*/
|
|
|
|
|
/*paramValues:pointer of an array of strings containing parameters*/
|
|
|
|
|
/*NULL:relevant for binary parameters*/
|
|
|
|
|
/*NULL:relevant for binary parameters*/
|
|
|
|
|
/*0:obtain result in text format*/
|
|
|
|
|
//TODO How to pass params?
|
|
|
|
|
pqRes = PQexecParams(conn, prepStmEntities, 0, NULL, NULL, NULL, NULL, 0);
|
|
|
|
|
|
|
|
|
|
//unlock PGconn mutex
|
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
|
|
|
|
|
|
if(PQresultStatus(pqRes) != PGRES_TUPLES_OK){
|
|
|
|
|
/*handle error*/
|
|
|
|
|
printf("callback_update_entity() No data retrieved\n");
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/update route: Dba error!");
|
|
|
|
|
/*clean up result*/
|
|
|
|
|
PQclear(pqRes);
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
}else{
|
|
|
|
|
/*evaluate result*/
|
|
|
|
|
int nbRecords=PQntuples(pqRes);
|
|
|
|
|
//printf("callback_update_entity() nbRecords: %d\n",nbRecords);
|
|
|
|
|
/*clean up result*/
|
|
|
|
|
PQclear(pqRes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
printf("callback_update_entity() HTTP POST request error!\n");
|
|
|
|
|
ulfius_set_string_body_response(response, 400, "This is the /entity/:id/update route: HTTP POST request error!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
printf("callback_update_entity() HTTP POST request error!\n");
|
|
|
|
|
ulfius_set_string_body_response(response, 400, "This is the /entity/:id/update route: HTTP POST request error!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*clean up remainings*/
|
|
|
|
|
printf("callback_update_entity() Done.\n");
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 200, "Done.\n");
|
|
|
|
|
return U_CALLBACK_CONTINUE;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function to create a particular entity
|
|
|
|
|
*/
|
|
|
|
|
int callback_create_entity (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
|
|
|
|
/*declarations*/
|
|
|
|
|
int r;
|
|
|
|
|
const char **keys=NULL;
|
|
|
|
|
const char *value=NULL;
|
|
|
|
|
char *line=NULL;
|
|
|
|
|
int len;
|
|
|
|
|
int i;
|
|
|
|
|
PGconn *conn=NULL;
|
|
|
|
|
char *prepStmEntities=NULL;
|
|
|
|
|
PGresult *pqRes=NULL;
|
|
|
|
|
|
|
|
|
|
printf("callback_create_entity() Started...\n");
|
|
|
|
|
|
|
|
|
|
if(request->map_post_body != NULL){
|
|
|
|
|
keys=u_map_enum_keys(request->map_post_body);
|
|
|
|
|
if(keys[0] == NULL){
|
|
|
|
|
//TODO error
|
|
|
|
|
printf("callback_create_entity() param NOT available\n");
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/create route: HTTP POST data error!");
|
|
|
|
|
}else{
|
|
|
|
|
value = u_map_get(request->map_post_body, keys[0]);
|
|
|
|
|
if(value==NULL){
|
|
|
|
|
//TODO error
|
|
|
|
|
printf("callback_create_entity() key NOT available in request map\n");
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/create route: HTTP POST key error!");
|
|
|
|
|
}else{
|
|
|
|
|
//printf("callback_create_entity() key is %s, value is %s\n", keys[0], value);
|
|
|
|
|
conn = (PGconn *)user_data;
|
|
|
|
|
/*define prepared statement*/
|
|
|
|
|
len=snprintf(NULL,0,
|
|
|
|
|
"INSERT INTO entities (name) VALUES ('%s') RETURNING *;"
|
|
|
|
|
,value);
|
|
|
|
|
prepStmEntities=malloc((size_t)(len+1));
|
|
|
|
|
//TODO Release prepStmEntities!
|
|
|
|
|
snprintf(prepStmEntities,(size_t)(len+1),
|
|
|
|
|
"INSERT INTO entities (name) VALUES ('%s') RETURNING *;"
|
|
|
|
|
,value);
|
|
|
|
|
//printf("callback_create_entity() prepStmEntities: %s\n",prepStmEntities);
|
|
|
|
|
|
|
|
|
|
//lock PGconn mutex
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
|
|
|
|
|
|
/*create prepared statement*/
|
|
|
|
|
/*conn:connection*/
|
|
|
|
|
/*stm:statement*/
|
|
|
|
|
/*0:number of passed parameters*/
|
|
|
|
|
/*NULL: server figures out parameter type*/
|
|
|
|
|
/*paramValues:pointer of an array of strings containing parameters*/
|
|
|
|
|
/*NULL:relevant for binary parameters*/
|
|
|
|
|
/*NULL:relevant for binary parameters*/
|
|
|
|
|
/*0:obtain result in text format*/
|
|
|
|
|
//TODO How to pass params?
|
|
|
|
|
pqRes = PQexecParams(conn, prepStmEntities, 0, NULL, NULL, NULL, NULL, 0);
|
|
|
|
|
|
|
|
|
|
//unlock PGconn mutex
|
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
|
|
|
|
|
|
if(PQresultStatus(pqRes) != PGRES_TUPLES_OK){
|
|
|
|
|
/*handle error*/
|
|
|
|
|
printf("callback_create_entity() No data retrieved\n");
|
|
|
|
|
ulfius_set_string_body_response(response, 500, "This is the /entity/create route: Dba error!");
|
|
|
|
|
/*clean up result*/
|
|
|
|
|
PQclear(pqRes);
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
}else{
|
|
|
|
|
/*evaluate result*/
|
|
|
|
|
int nbRecords=PQntuples(pqRes);
|
|
|
|
|
//printf("callback_create_entity() nbRecords: %d\n",nbRecords);
|
|
|
|
|
/*clean up result*/
|
|
|
|
|
PQclear(pqRes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
printf("callback_create_entity() HTTP POST request error!\n");
|
|
|
|
|
ulfius_set_string_body_response(response, 400, "This is the /entity/create route: HTTP POST request error!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*clean up remainings*/
|
|
|
|
|
printf("callback_create_entity() Done.\n");
|
|
|
|
|
//TODO Why this casting?
|
|
|
|
|
(void)(request);
|
|
|
|
|
(void)(user_data);
|
|
|
|
|
ulfius_set_string_body_response(response, 200, "Done.\n");
|
|
|
|
|
return U_CALLBACK_CONTINUE;
|
|
|
|
|
|
|
|
|
|
}
|