feat(http-post): adjust HTTP POST request

This commit is contained in:
dancingCycle 2023-03-20 15:34:10 +01:00
parent a1564f5ea1
commit 5745a3e9e1
1 changed files with 124 additions and 14 deletions

View File

@ -18,6 +18,7 @@
/*to use file2char*/
#include "file2char.h"
#define PREFIX "/test"
#define ROUTE_HELLO "/hello"
#define ROUTE_ENTITIES "/entities"
#define PREFIX "/test"
@ -29,11 +30,13 @@ int callback_get_hello (const struct _u_request * request, struct _u_response *
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);
int callback_set_entity (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_all_test_foo (const struct _u_request * request, struct _u_response * response, void * user_data);
/**
* decode a u_map into a string
@ -167,6 +170,9 @@ int main(int argc, char *argv[]) {
// 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);
@ -174,7 +180,15 @@ int main(int argc, char *argv[]) {
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, "GET", ROUTE_ENTITIES, "/update", 0, &callback_set_entity, (void *)conn);
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);
ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/param/:foo", 0, &callback_all_test_foo, "user data 1");
ulfius_add_endpoint_by_val(&instance, "POST", PREFIX, "/param/:foo", 0, &callback_all_test_foo, "user data 2");
ulfius_add_endpoint_by_val(&instance, "PUT", PREFIX, "/param/:foo", 0, &callback_all_test_foo, "user data 3");
ulfius_add_endpoint_by_val(&instance, "DELETE", PREFIX, "/param/:foo", 0, &callback_all_test_foo, "user data 4");
// default_endpoint declaration
ulfius_set_default_endpoint(&instance, &callback_default, NULL);
@ -258,7 +272,7 @@ int callback_get_entity (const struct _u_request * request, struct _u_response *
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 200, "This is the /entity/:id/info route: Param error!");
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);
@ -291,7 +305,7 @@ int callback_get_entity (const struct _u_request * request, struct _u_response *
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 200, "This is the /entity/:id/info route: Dba error!");
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/info route: Dba error!");
}else{
/*evaluate result*/
nbRecords = PQntuples(pqRes);
@ -328,14 +342,6 @@ int callback_get_entity (const struct _u_request * request, struct _u_response *
return U_CALLBACK_CONTINUE;
}
/**
* Callback function for route /entities to update an existing entity
*/
int callback_set_entity (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
//TODO
}
/**
* Callback function for route /entities to reply all entities
*/
@ -369,7 +375,7 @@ int callback_get_entities (const struct _u_request * request, struct _u_response
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 200, "This is the /entities route: Dba error!");
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_entities() Done.\n");
return U_CALLBACK_CONTINUE;
}
@ -403,3 +409,107 @@ int callback_get_entities (const struct _u_request * request, struct _u_response
printf("callback_get_entities() Done.\n");
return U_CALLBACK_CONTINUE;
}
int callback_delete_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_create_entity (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
int r;
const char **keys;
const char *value;
char *line;
int len;
int i;
PGconn *conn;
char *prepStmEntities;
PGresult *pqRes;
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]);
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);
/*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);
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_get_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;
}
/**
* Callback function that put "Hello World!" and all the data sent by the client in the response as string (http method, url, params, cookies, headers, post, json, and user specific data in the response
*/
int callback_all_test_foo (const struct _u_request * request, struct _u_response * response, void * user_data) {
char * url_params = print_map(request->map_url), * headers = print_map(request->map_header), * cookies = print_map(request->map_cookie);
char * post_params = print_map(request->map_post_body);
char * response_body;
int r=asprintf(
&response_body,
"Hello World!\n\n method is %s\n url is %s\n\n parameters from the url are \n%s\n\n cookies are \n%s\n\n headers are \n%s\n\n post parameters are \n%s\n\n user data is %s\n\nclient address is %s\n\n",
request->http_verb, request->http_url, post_params, cookies, headers, post_params, (char *)user_data, inet_ntoa(((struct sockaddr_in *)request->client_address)->sin_addr));
ulfius_set_string_body_response(response, 200, response_body);
free(post_params);
free(headers);
free(cookies);
free(post_params);
free(response_body);
return U_CALLBACK_CONTINUE;
}