add: file sending capability
This commit is contained in:
parent
6178ef3c7b
commit
760cec9a20
18 changed files with 79 additions and 53 deletions
10
endpoint.c
10
endpoint.c
|
@ -5,10 +5,11 @@
|
|||
#include <endpoint.h>
|
||||
|
||||
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
|
||||
|
|
|
@ -8,10 +8,11 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
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();
|
||||
|
|
|
@ -8,9 +8,10 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
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;
|
||||
|
||||
|
|
|
@ -7,9 +7,10 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
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))
|
||||
|
|
|
@ -10,9 +10,10 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
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))
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -8,10 +8,12 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
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();
|
||||
|
|
|
@ -8,9 +8,10 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
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);
|
||||
|
|
|
@ -8,9 +8,10 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
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();
|
||||
|
|
|
@ -11,9 +11,10 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
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;
|
||||
|
||||
|
|
|
@ -8,9 +8,10 @@
|
|||
#include <models/tag.h>
|
||||
|
||||
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);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <string.h>
|
||||
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <mongoose.h>
|
||||
#include <logger.h>
|
||||
|
@ -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);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <mongoose.h>
|
||||
#include <confini.h>
|
||||
|
||||
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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -4,27 +4,27 @@
|
|||
#include <router.h>
|
||||
|
||||
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 */
|
||||
|
|
|
@ -4,9 +4,9 @@
|
|||
#include <router.h>
|
||||
|
||||
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 */
|
||||
|
|
|
@ -4,21 +4,21 @@
|
|||
#include <router.h>
|
||||
|
||||
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 */
|
||||
|
|
3
main.c
3
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 ********************/
|
||||
|
||||
|
|
Loading…
Reference in a new issue