diff --git a/endpoint.c b/endpoint.c index 7354d3e..f7a0c90 100644 --- a/endpoint.c +++ b/endpoint.c @@ -5,10 +5,11 @@ #include void -endpoint_func_index(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +endpoint_func_index(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)args; (void)hm; + (void)nc; static const char content[] = "Emgauwa"; response->status_code = 200; @@ -16,13 +17,16 @@ endpoint_func_index(struct http_message *hm, endpoint_args_t *args, endpoint_res response->content_length = STRLEN(content);; response->content = content; response->alloced_content = false; + + mg_serve_http(nc, hm, global_config.http_server_opts); } void -endpoint_func_not_found(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +endpoint_func_not_found(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)args; (void)hm; + (void)nc; static const char content[] = "404 - NOT FOUND"; response->status_code = 404; @@ -30,6 +34,8 @@ endpoint_func_not_found(struct http_message *hm, endpoint_args_t *args, endpoint response->content_length = STRLEN(content);; response->content = content; response->alloced_content = false; + + mg_serve_http(nc, hm, global_config.http_server_opts); } void diff --git a/endpoints/api_v1_controllers.c b/endpoints/api_v1_controllers.c index 1e4de44..f233bec 100644 --- a/endpoints/api_v1_controllers.c +++ b/endpoints/api_v1_controllers.c @@ -8,10 +8,11 @@ #include void -api_v1_controllers_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_controllers_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)args; (void)hm; + (void)nc; controller_t** all_controllers = controller_get_all(); cJSON *json = cJSON_CreateArray(); diff --git a/endpoints/api_v1_controllers_STR.c b/endpoints/api_v1_controllers_STR.c index acd3252..6e3fffd 100644 --- a/endpoints/api_v1_controllers_STR.c +++ b/endpoints/api_v1_controllers_STR.c @@ -8,9 +8,10 @@ #include void -api_v1_controllers_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_controllers_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; + (void)nc; uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) @@ -41,9 +42,10 @@ api_v1_controllers_STR_GET(struct http_message *hm, endpoint_args_t *args, endpo } void -api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; + (void)nc; uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) @@ -134,9 +136,10 @@ api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpo } void -api_v1_controllers_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; + (void)nc; const char *target_uid_str = args[0].value.v_str; diff --git a/endpoints/api_v1_controllers_STR_relays.c b/endpoints/api_v1_controllers_STR_relays.c index 20a8c03..d00c61f 100644 --- a/endpoints/api_v1_controllers_STR_relays.c +++ b/endpoints/api_v1_controllers_STR_relays.c @@ -7,9 +7,10 @@ #include void -api_v1_controllers_STR_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_controllers_STR_relays_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; + (void)nc; uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) diff --git a/endpoints/api_v1_controllers_STR_relays_INT.c b/endpoints/api_v1_controllers_STR_relays_INT.c index 0c86094..956d71f 100644 --- a/endpoints/api_v1_controllers_STR_relays_INT.c +++ b/endpoints/api_v1_controllers_STR_relays_INT.c @@ -10,9 +10,10 @@ #include void -api_v1_controllers_STR_relays_INT_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; + (void)nc; uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) @@ -55,9 +56,10 @@ api_v1_controllers_STR_relays_INT_GET(struct http_message *hm, endpoint_args_t * } void -api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; + (void)nc; uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) diff --git a/endpoints/api_v1_controllers_discover.c b/endpoints/api_v1_controllers_discover.c index 5a4f33c..589c1e8 100644 --- a/endpoints/api_v1_controllers_discover.c +++ b/endpoints/api_v1_controllers_discover.c @@ -105,8 +105,10 @@ send_udp_broadcast(const char *addr, uint16_t port, void *message, size_t length } void -api_v1_controllers_discover_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { + (void)nc; + (void)hm; int discover_server_socket = bind_tcp_server("0.0.0.0", "0", 20); int discover_server_port = get_server_port(discover_server_socket); @@ -303,5 +305,5 @@ api_v1_controllers_discover_POST(struct http_message *hm, endpoint_args_t *args, } controller_free_list(known_controllers); - api_v1_controllers_GET(hm, args, response); + api_v1_controllers_GET(nc, hm, args, response); } diff --git a/endpoints/api_v1_relays.c b/endpoints/api_v1_relays.c index 355fe4c..b56d5da 100644 --- a/endpoints/api_v1_relays.c +++ b/endpoints/api_v1_relays.c @@ -8,10 +8,12 @@ #include void -api_v1_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_relays_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)args; (void)hm; + (void)nc; + relay_t** all_relays = relay_get_all(); cJSON *json = cJSON_CreateArray(); diff --git a/endpoints/api_v1_relays_tag_STR.c b/endpoints/api_v1_relays_tag_STR.c index 4d465b7..d21a47b 100644 --- a/endpoints/api_v1_relays_tag_STR.c +++ b/endpoints/api_v1_relays_tag_STR.c @@ -8,9 +8,10 @@ #include void -api_v1_relays_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_relays_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; + (void)nc; int tag_id = tag_get_id(args[0].value.v_str); int *relays_ids = junction_tag_get_relays_for_tag_id(tag_id); diff --git a/endpoints/api_v1_schedules.c b/endpoints/api_v1_schedules.c index 5d0e65e..3dab5dc 100644 --- a/endpoints/api_v1_schedules.c +++ b/endpoints/api_v1_schedules.c @@ -8,9 +8,10 @@ #include void -api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)args; + (void)nc; cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len); if(json == NULL) @@ -133,10 +134,12 @@ api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_r } void -api_v1_schedules_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_schedules_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)args; (void)hm; + (void)nc; + schedule_t** all_schedules = schedule_get_all(); cJSON *json = cJSON_CreateArray(); diff --git a/endpoints/api_v1_schedules_STR.c b/endpoints/api_v1_schedules_STR.c index f0d0286..0c5c369 100644 --- a/endpoints/api_v1_schedules_STR.c +++ b/endpoints/api_v1_schedules_STR.c @@ -11,9 +11,10 @@ #include void -api_v1_schedules_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_schedules_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; + (void)nc; uuid_t target_uid; if(schedule_uid_parse(args[0].value.v_str, target_uid)) @@ -44,9 +45,10 @@ api_v1_schedules_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoin } void -api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; + (void)nc; uuid_t target_uid; if(schedule_uid_parse(args[0].value.v_str, target_uid)) @@ -183,9 +185,10 @@ api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoin } void -api_v1_schedules_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; + (void)nc; const char *target_uid_str = args[0].value.v_str; diff --git a/endpoints/api_v1_schedules_tag_STR.c b/endpoints/api_v1_schedules_tag_STR.c index e5c5338..0cc38ab 100644 --- a/endpoints/api_v1_schedules_tag_STR.c +++ b/endpoints/api_v1_schedules_tag_STR.c @@ -8,9 +8,10 @@ #include void -api_v1_schedules_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) +api_v1_schedules_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; + (void)nc; int tag_id = tag_get_id(args[0].value.v_str); int *schedules_ids = junction_tag_get_schedules_for_tag_id(tag_id); diff --git a/handlers/connection.c b/handlers/connection.c index 2ea71c7..21c7770 100644 --- a/handlers/connection.c +++ b/handlers/connection.c @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -12,11 +13,8 @@ // -2 for "%s" -1 for \0 #define HEADERS_FMT_LEN (sizeof(HEADERS_FMT) - 3) -#define STD_RESPONSE_CONTENT "the server did not create a response" -#define STD_RESPONSE_CONTENT_LEN (sizeof(STD_RESPONSE_CONTENT) - 1) - void -handler_connection(struct mg_connection *c, int ev, void *p) +handler_connection(struct mg_connection *nc, int ev, void *p) { if (ev == MG_EV_HTTP_REQUEST) { @@ -30,19 +28,17 @@ handler_connection(struct mg_connection *c, int ev, void *p) if(endpoint->func) { endpoint_response_t response; - response.status_code = 500; - response.content_type = "text/plain"; - response.content_length = STD_RESPONSE_CONTENT_LEN; - response.content = STD_RESPONSE_CONTENT; - response.alloced_content = false; - endpoint->func(p, endpoint->args, &response); + static const char content[] = "the server did not create a response"; + endpoint_response_text(&response, 500, content, STRLEN(content)); + + endpoint->func(nc, hm, endpoint->args, &response); char *response_headers = malloc(sizeof(char) * (HEADERS_FMT_LEN + strlen(response.content_type) + 1)); sprintf(response_headers, HEADERS_FMT, response.content_type); - mg_send_head(c, response.status_code, response.content_length, response_headers); - mg_printf(c, "%s", response.content); + mg_send_head(nc, response.status_code, response.content_length, response_headers); + mg_printf(nc, "%s", response.content); free(response_headers); @@ -70,17 +66,17 @@ handler_connection(struct mg_connection *c, int ev, void *p) endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "", endpoint->options & HTTP_METHOD_DELETE ? ", DELETE" : "" ); - mg_send_head(c, 204, 0, options_header); + mg_send_head(nc, 204, 0, options_header); } else { - mg_send_head(c, 501, 0, "Content-Type: text/plain"); + mg_send_head(nc, 501, 0, "Content-Type: text/plain"); } } } else { - mg_send_head(c, 500, 0, "Content-Type: text/plain"); + mg_send_head(nc, 500, 0, "Content-Type: text/plain"); } //mg_printf(c, "%.*s", (int)hm->message.len, hm->message.p); diff --git a/include/config.h b/include/config.h index 80e9007..53b4772 100644 --- a/include/config.h +++ b/include/config.h @@ -3,6 +3,7 @@ #include +#include #include typedef enum @@ -29,6 +30,7 @@ typedef struct run_type_t run_type; char server_port[6]; uint16_t discovery_port; + struct mg_serve_http_opts http_server_opts; } config_t; extern config_t global_config; diff --git a/include/endpoint.h b/include/endpoint.h index fbb4b10..6447487 100644 --- a/include/endpoint.h +++ b/include/endpoint.h @@ -28,7 +28,7 @@ typedef struct int alloced_content; } endpoint_response_t; -typedef void (*endpoint_func_f)(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +typedef void (*endpoint_func_f)(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); typedef struct { @@ -48,10 +48,10 @@ typedef struct } endpoint_t; void -endpoint_func_index(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +endpoint_func_index(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -endpoint_func_not_found(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +endpoint_func_not_found(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void endpoint_response_text(endpoint_response_t *response, int status_code, const char *content, int content_length); diff --git a/include/endpoints/api_v1_controllers.h b/include/endpoints/api_v1_controllers.h index 32f5f69..d487e58 100644 --- a/include/endpoints/api_v1_controllers.h +++ b/include/endpoints/api_v1_controllers.h @@ -4,27 +4,27 @@ #include void -api_v1_controllers_discover_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_controllers_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_controllers_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_controllers_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_controllers_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_controllers_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_controllers_STR_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_controllers_STR_relays_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_controllers_STR_relays_INT_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); #endif /* CORE_ENDPOINTS_API_V1_CONTROLLERS_H */ diff --git a/include/endpoints/api_v1_relays.h b/include/endpoints/api_v1_relays.h index 79377de..c37f1e4 100644 --- a/include/endpoints/api_v1_relays.h +++ b/include/endpoints/api_v1_relays.h @@ -4,9 +4,9 @@ #include void -api_v1_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_relays_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_relays_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_relays_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); #endif /* CORE_ENDPOINTS_API_V1_RELAYS_H */ diff --git a/include/endpoints/api_v1_schedules.h b/include/endpoints/api_v1_schedules.h index 9ee3765..46483c3 100644 --- a/include/endpoints/api_v1_schedules.h +++ b/include/endpoints/api_v1_schedules.h @@ -4,21 +4,21 @@ #include void -api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_schedules_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_schedules_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_schedules_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_schedules_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_schedules_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); void -api_v1_schedules_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +api_v1_schedules_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); #endif /* CORE_ENDPOINTS_API_V1_SCHEDULES_H */ diff --git a/main.c b/main.c index c5b1a12..f077cf4 100644 --- a/main.c +++ b/main.c @@ -69,6 +69,9 @@ main(int argc, const char** argv) fclose(ini_file); + global_config.http_server_opts.document_root = "."; // Serve current directory + global_config.http_server_opts.enable_directory_listing = "yes"; + /******************** SETUP DATABASE ********************/