2020-05-30 22:23:57 +00:00
|
|
|
#include <logger.h>
|
|
|
|
#include <cJSON.h>
|
|
|
|
#include <mongoose.h>
|
|
|
|
#include <macros.h>
|
|
|
|
#include <endpoint.h>
|
|
|
|
|
|
|
|
void
|
2020-05-31 00:07:25 +00:00
|
|
|
endpoint_func_index(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
2020-05-30 22:23:57 +00:00
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
(void)hm;
|
2020-05-31 00:07:25 +00:00
|
|
|
(void)nc;
|
2020-06-02 22:47:49 +00:00
|
|
|
(void)response;
|
2020-05-30 22:23:57 +00:00
|
|
|
|
2020-06-02 22:47:49 +00:00
|
|
|
response->status_code = 0;
|
2020-05-31 00:07:25 +00:00
|
|
|
|
2020-11-12 20:58:01 +00:00
|
|
|
mg_serve_http(nc, hm, global_config->http_server_opts);
|
2020-05-30 22:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-05-31 00:07:25 +00:00
|
|
|
endpoint_func_not_found(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
2020-05-30 22:23:57 +00:00
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
(void)hm;
|
2020-05-31 00:07:25 +00:00
|
|
|
(void)nc;
|
2020-06-02 22:47:49 +00:00
|
|
|
|
2020-11-12 20:58:01 +00:00
|
|
|
if(access(global_config->not_found_file, R_OK) != -1)
|
2020-06-02 22:47:49 +00:00
|
|
|
{
|
2020-11-12 20:58:01 +00:00
|
|
|
struct mg_str mime_type = mg_mk_str(global_config->not_found_file_type);
|
2020-06-02 22:47:49 +00:00
|
|
|
response->status_code = 0;
|
2020-11-12 20:58:01 +00:00
|
|
|
mg_http_serve_file(nc, hm, global_config->not_found_file, mime_type, mg_mk_str(""));
|
2020-06-02 22:47:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-26 19:00:05 +00:00
|
|
|
LOGGER_DEBUG("404 file not found\n");
|
2020-11-15 15:36:44 +00:00
|
|
|
|
|
|
|
endpoint_response_free_content(response);
|
|
|
|
|
2020-06-02 22:47:49 +00:00
|
|
|
response->status_code = 404;
|
2020-11-12 20:58:01 +00:00
|
|
|
response->content_type = global_config->not_found_content_type;
|
|
|
|
response->content_length = strlen(global_config->not_found_content);
|
|
|
|
response->content = global_config->not_found_content;
|
2020-06-02 22:47:49 +00:00
|
|
|
response->alloced_content = false;
|
|
|
|
}
|
2020-05-30 22:23:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-13 22:20:07 +00:00
|
|
|
void
|
|
|
|
endpoint_response_msg(endpoint_response_t *response, int status_code, const char *content, int content_length)
|
|
|
|
{
|
2020-11-15 15:36:44 +00:00
|
|
|
endpoint_response_free_content(response);
|
|
|
|
|
2020-11-13 22:20:07 +00:00
|
|
|
cJSON *json;
|
|
|
|
|
|
|
|
json = cJSON_CreateObject();
|
|
|
|
cJSON *json_msg;
|
|
|
|
|
|
|
|
if(content_length)
|
|
|
|
{
|
|
|
|
json_msg = cJSON_CreateStringReference(content);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
json_msg = cJSON_CreateString(content);
|
|
|
|
}
|
|
|
|
if(json_msg == NULL)
|
|
|
|
{
|
|
|
|
endpoint_response_text(response, status_code, content, content_length);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cJSON_AddItemToObject(json, "msg", json_msg);
|
|
|
|
|
|
|
|
endpoint_response_json(response, status_code, json);
|
2020-11-15 15:36:44 +00:00
|
|
|
|
|
|
|
cJSON_Delete(json);
|
2020-11-13 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 22:23:57 +00:00
|
|
|
void
|
|
|
|
endpoint_response_text(endpoint_response_t *response, int status_code, const char *content, int content_length)
|
|
|
|
{
|
2020-11-15 15:36:44 +00:00
|
|
|
endpoint_response_free_content(response);
|
2020-05-30 22:23:57 +00:00
|
|
|
if(content == NULL)
|
|
|
|
{
|
|
|
|
content = "";
|
|
|
|
content_length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
response->status_code = status_code;
|
|
|
|
response->content_type = "text/plain";
|
2020-09-03 22:28:49 +00:00
|
|
|
if(content_length)
|
2020-05-30 22:23:57 +00:00
|
|
|
{
|
|
|
|
response->content_length = content_length;
|
|
|
|
response->alloced_content = false;
|
2020-11-15 15:36:44 +00:00
|
|
|
response->content = (char*)content;
|
2020-05-30 22:23:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response->content_length = strlen(content);
|
|
|
|
response->alloced_content = true;
|
2020-11-15 15:36:44 +00:00
|
|
|
|
2020-11-19 22:36:02 +00:00
|
|
|
response->content = malloc(sizeof(char) * (response->content_length + 1));
|
|
|
|
strcpy(response->content, content);
|
|
|
|
response->content[response->content_length] = '\0';
|
2020-05-30 22:23:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
endpoint_response_json(endpoint_response_t *response, int status_code, const cJSON *json_root)
|
|
|
|
{
|
2020-11-15 15:36:44 +00:00
|
|
|
endpoint_response_free_content(response);
|
2020-05-30 22:23:57 +00:00
|
|
|
if(json_root != NULL)
|
|
|
|
{
|
|
|
|
char *json_str = cJSON_Print(json_root);
|
|
|
|
if (json_str != NULL)
|
|
|
|
{
|
|
|
|
response->status_code = status_code;
|
|
|
|
response->content_type = "application/json";
|
|
|
|
response->content_length = strlen(json_str);
|
|
|
|
response->content = json_str;
|
|
|
|
response->alloced_content = true;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 22:20:07 +00:00
|
|
|
M_RESPONSE_MSG(LOGGER_ERR, response, 500, "failed to print json");
|
2020-05-30 22:23:57 +00:00
|
|
|
}
|
2020-11-15 15:36:44 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
endpoint_response_free_content(endpoint_response_t *response)
|
|
|
|
{
|
|
|
|
if(response->alloced_content)
|
|
|
|
{
|
|
|
|
free(response->content);
|
|
|
|
response->content = NULL;
|
|
|
|
response->alloced_content = false;
|
|
|
|
}
|
|
|
|
}
|