From 7a7a0686c20b0884e98bc72588e2290b13534af3 Mon Sep 17 00:00:00 2001 From: simon987 Date: Wed, 3 Apr 2024 14:26:54 -0400 Subject: [PATCH] Fixes for new mongoose version --- src/web/serve.c | 11 +++++------ src/web/web_util.c | 7 +++++++ src/web/web_util.h | 19 +++++++++++++++++-- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/web/serve.c b/src/web/serve.c index 723817b..7ab7fab 100644 --- a/src/web/serve.c +++ b/src/web/serve.c @@ -108,7 +108,6 @@ void stats_files(struct mg_connection *nc, struct mg_http_message *hm) { cJSON *json = database_get_stats(db, stat_type); mg_send_json(nc, json); - cJSON_Delete(json); } @@ -169,6 +168,7 @@ void serve_thumbnail(struct mg_connection *nc, struct mg_http_message *hm, int i "Cache-Control: max-age=31536000" ); mg_send(nc, data, data_len); + nc->is_resp = 0; free(data); } else { HTTP_REPLY_NOT_FOUND @@ -217,6 +217,7 @@ void search(struct mg_connection *nc, struct mg_http_message *hm) { snprintf(url, 4096, "%s/%s/_search", WebCtx.es_url, WebCtx.es_index); nc->fn_data = web_post_async(url, body, WebCtx.es_insecure_ssl); + nc->is_resp = 1; } void serve_file_from_url(cJSON *json, index_t *idx, struct mg_connection *nc) { @@ -382,11 +383,7 @@ void index_info(struct mg_connection *nc) { cJSON_AddStringToObject(json, "searchBackend", "elasticsearch"); } - char *json_str = cJSON_PrintUnformatted(json); - - web_send_headers(nc, 200, strlen(json_str), "Content-Type: application/json"); - mg_send(nc, json_str, strlen(json_str)); - free(json_str); + mg_send_json(nc, json); cJSON_Delete(json); } @@ -450,6 +447,7 @@ void status(struct mg_connection *nc) { } free(status); + nc->is_resp = 0; } typedef struct { @@ -738,6 +736,7 @@ static void ev_router(struct mg_connection *nc, int ev, void *ev_data) { if (r->status_code == 200) { web_send_headers(nc, 200, r->size, "Content-Type: application/json"); mg_send(nc, r->body, r->size); + nc->is_resp = 0; } else if (r->status_code == 0) { sist_log("serve.c", LOG_SIST_ERROR, "Could not connect to elasticsearch!"); diff --git a/src/web/web_util.c b/src/web/web_util.c index 07ede77..186c767 100644 --- a/src/web/web_util.c +++ b/src/web/web_util.c @@ -5,31 +5,37 @@ void web_serve_asset_index_html(struct mg_connection *nc) { web_send_headers(nc, 200, sizeof(index_html), HTTP_CROSS_ORIGIN_HEADERS "Content-Type: text/html"); mg_send(nc, index_html, sizeof(index_html)); + nc->is_resp = 0; } void web_serve_asset_index_js(struct mg_connection *nc) { web_send_headers(nc, 200, sizeof(index_js), "Content-Type: application/javascript"); mg_send(nc, index_js, sizeof(index_js)); + nc->is_resp = 0; } void web_serve_asset_chunk_vendors_js(struct mg_connection *nc) { web_send_headers(nc, 200, sizeof(chunk_vendors_js), "Content-Type: application/javascript"); mg_send(nc, chunk_vendors_js, sizeof(chunk_vendors_js)); + nc->is_resp = 0; } void web_serve_asset_favicon_ico(struct mg_connection *nc) { web_send_headers(nc, 200, sizeof(favicon_ico), "Content-Type: image/x-icon"); mg_send(nc, favicon_ico, sizeof(favicon_ico)); + nc->is_resp = 0; } void web_serve_asset_style_css(struct mg_connection *nc) { web_send_headers(nc, 200, sizeof(index_css), "Content-Type: text/css"); mg_send(nc, index_css, sizeof(index_css)); + nc->is_resp = 0; } void web_serve_asset_chunk_vendors_css(struct mg_connection *nc) { web_send_headers(nc, 200, sizeof(chunk_vendors_css), "Content-Type: text/css"); mg_send(nc, chunk_vendors_css, sizeof(chunk_vendors_css)); + nc->is_resp = 0; } index_t *web_get_index_by_id(int index_id) { @@ -92,6 +98,7 @@ void mg_send_json(struct mg_connection *nc, const cJSON *json) { web_send_headers(nc, 200, strlen(json_str), "Content-Type: application/json"); mg_send(nc, json_str, strlen(json_str)); + nc->is_resp = 0; free(json_str); } diff --git a/src/web/web_util.h b/src/web/web_util.h index b226f9e..75ce78a 100644 --- a/src/web/web_util.h +++ b/src/web/web_util.h @@ -16,9 +16,24 @@ database_t *web_get_database(int index_id); __always_inline static char *web_address_to_string(struct mg_addr *addr) { - static char address_to_string_buf[INET6_ADDRSTRLEN]; + static char address_to_string_buf[64]; - mg_snprintf(address_to_string_buf, sizeof(address_to_string_buf), "%I", addr); + if (addr->is_ip6) { + snprintf(address_to_string_buf, sizeof(address_to_string_buf), + "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", + addr->ip[0], addr->ip[1], + addr->ip[2], addr->ip[3], + addr->ip[4], addr->ip[5], + addr->ip[6], addr->ip[7], + addr->ip[8], addr->ip[9], + addr->ip[10], addr->ip[11], + addr->ip[12], addr->ip[13], + addr->ip[14], addr->ip[15]); + } else { + snprintf(address_to_string_buf, sizeof(address_to_string_buf), + "%d.%d.%d.%d", + addr->ip[0], addr->ip[1], addr->ip[2], addr->ip[3]); + } return address_to_string_buf; }