mirror of
https://github.com/simon987/sist2.git
synced 2025-12-12 23:18:51 +00:00
SQLite search backend
This commit is contained in:
344
src/web/serve.c
344
src/web/serve.c
@@ -5,11 +5,24 @@
|
||||
#include "src/index/web.h"
|
||||
#include "src/auth0/auth0_c_api.h"
|
||||
#include "src/web/web_util.h"
|
||||
#include "src/cli.h"
|
||||
#include <time.h>
|
||||
|
||||
#include <src/ctx.h>
|
||||
|
||||
#define HTTP_TEXT_TYPE_HEADER "Content-Type: text/plain;charset=utf-8\r\n"
|
||||
#define HTTP_REPLY_NOT_FOUND mg_http_reply(nc, 404, HTTP_SERVER_HEADER HTTP_TEXT_TYPE_HEADER, "Not found");
|
||||
void fts_search_paths(struct mg_connection *nc, struct mg_http_message *hm);
|
||||
|
||||
void fts_search_mimetypes(struct mg_connection *nc, struct mg_http_message *hm);
|
||||
|
||||
void fts_search_summary_stats(struct mg_connection *nc, struct mg_http_message *hm);
|
||||
|
||||
void fts_search(struct mg_connection *nc, struct mg_http_message *hm);
|
||||
|
||||
void fts_get_document(struct mg_connection *nc, struct mg_http_message *hm);
|
||||
|
||||
void fts_suggest_tag(struct mg_connection *nc, struct mg_http_message *hm);
|
||||
|
||||
void fts_get_tags(struct mg_connection *nc, struct mg_http_message *hm);
|
||||
|
||||
static struct mg_http_serve_opts DefaultServeOpts = {
|
||||
.fs = NULL,
|
||||
@@ -47,12 +60,8 @@ void stats_files(struct mg_connection *nc, struct mg_http_message *hm) {
|
||||
}
|
||||
|
||||
cJSON *json = database_get_stats(db, stat_type);
|
||||
char *json_str = cJSON_PrintUnformatted(json);
|
||||
mg_send_json(nc, json);
|
||||
|
||||
web_send_headers(nc, 200, strlen(json_str), "Content-Type: application/json");
|
||||
mg_send(nc, json_str, strlen(json_str));
|
||||
|
||||
free(json_str);
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
@@ -93,7 +102,7 @@ void serve_chunk_vendors_css(struct mg_connection *nc, struct mg_http_message *h
|
||||
}
|
||||
|
||||
void serve_thumbnail(struct mg_connection *nc, struct mg_http_message *hm, const char *arg_index,
|
||||
const char *arg_doc_id, int arg_num) {
|
||||
const char *arg_doc_id, int arg_num) {
|
||||
|
||||
database_t *db = web_get_database(arg_index);
|
||||
if (db == NULL) {
|
||||
@@ -252,6 +261,10 @@ void serve_file_from_disk(cJSON *json, index_t *idx, struct mg_connection *nc, s
|
||||
}
|
||||
|
||||
void cache_es_version() {
|
||||
if (WebCtx.search_backend == SQLITE_SEARCH_BACKEND) {
|
||||
return;
|
||||
}
|
||||
|
||||
static int is_cached = FALSE;
|
||||
|
||||
if (is_cached == TRUE) {
|
||||
@@ -321,6 +334,12 @@ void index_info(struct mg_connection *nc) {
|
||||
cJSON_AddItemToArray(arr, idx_json);
|
||||
}
|
||||
|
||||
if (WebCtx.search_backend == SQLITE_SEARCH_BACKEND) {
|
||||
cJSON_AddStringToObject(json, "searchBackend", "sqlite");
|
||||
} else {
|
||||
cJSON_AddStringToObject(json, "searchBackend", "elasticsearch");
|
||||
}
|
||||
|
||||
char *json_str = cJSON_PrintUnformatted(json);
|
||||
|
||||
web_send_headers(nc, 200, strlen(json_str), "Content-Type: application/json");
|
||||
@@ -329,54 +348,63 @@ void index_info(struct mg_connection *nc) {
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
cJSON *get_root_document_by_id(const char *index_id, const char *doc_id) {
|
||||
|
||||
database_t *db = web_get_database(index_id);
|
||||
if (!db) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char next_id[SIST_DOC_ID_LEN];
|
||||
strcpy(next_id, doc_id);
|
||||
|
||||
while (TRUE) {
|
||||
cJSON *doc = database_get_document(db, next_id);
|
||||
|
||||
if (doc == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cJSON *parent = cJSON_GetObjectItem(doc, "parent");
|
||||
if (parent == NULL || cJSON_IsNull(parent)) {
|
||||
return doc;
|
||||
}
|
||||
|
||||
strcpy(next_id, parent->valuestring);
|
||||
cJSON_Delete(parent);
|
||||
}
|
||||
}
|
||||
|
||||
void file(struct mg_connection *nc, struct mg_http_message *hm) {
|
||||
|
||||
if (hm->uri.len != SIST_DOC_ID_LEN + 2) {
|
||||
if (hm->uri.len != SIST_INDEX_ID_LEN + SIST_DOC_ID_LEN + 2) {
|
||||
LOG_DEBUGF("serve.c", "Invalid file path: %.*s", (int) hm->uri.len, hm->uri.ptr);
|
||||
HTTP_REPLY_NOT_FOUND
|
||||
return;
|
||||
}
|
||||
|
||||
char arg_doc_id[SIST_DOC_ID_LEN];
|
||||
memcpy(arg_doc_id, hm->uri.ptr + 3, SIST_DOC_ID_LEN);
|
||||
char arg_index[SIST_INDEX_ID_LEN];
|
||||
|
||||
memcpy(arg_index, hm->uri.ptr + 3, SIST_INDEX_ID_LEN);
|
||||
*(arg_index + SIST_INDEX_ID_LEN - 1) = '\0';
|
||||
memcpy(arg_doc_id, hm->uri.ptr + 3 + SIST_INDEX_ID_LEN, SIST_DOC_ID_LEN);
|
||||
*(arg_doc_id + SIST_DOC_ID_LEN - 1) = '\0';
|
||||
|
||||
const char *next = arg_doc_id;
|
||||
cJSON *doc = NULL;
|
||||
cJSON *index_id = NULL;
|
||||
cJSON *source = NULL;
|
||||
|
||||
while (true) {
|
||||
doc = elastic_get_document(next);
|
||||
source = cJSON_GetObjectItem(doc, "_source");
|
||||
index_id = cJSON_GetObjectItem(source, "index");
|
||||
if (index_id == NULL) {
|
||||
cJSON_Delete(doc);
|
||||
HTTP_REPLY_NOT_FOUND
|
||||
return;
|
||||
}
|
||||
cJSON *parent = cJSON_GetObjectItem(source, "parent");
|
||||
if (parent == NULL) {
|
||||
break;
|
||||
}
|
||||
next = parent->valuestring;
|
||||
}
|
||||
|
||||
index_t *idx = web_get_index_by_id(index_id->valuestring);
|
||||
|
||||
index_t *idx = web_get_index_by_id(arg_index);
|
||||
if (idx == NULL) {
|
||||
cJSON_Delete(doc);
|
||||
HTTP_REPLY_NOT_FOUND
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *source = get_root_document_by_id(arg_index, arg_doc_id);
|
||||
|
||||
if (strlen(idx->desc.rewrite_url) == 0) {
|
||||
serve_file_from_disk(source, idx, nc, hm);
|
||||
} else {
|
||||
serve_file_from_url(source, idx, nc);
|
||||
}
|
||||
cJSON_Delete(doc);
|
||||
cJSON_Delete(source);
|
||||
}
|
||||
|
||||
void status(struct mg_connection *nc) {
|
||||
@@ -398,6 +426,10 @@ typedef struct {
|
||||
|
||||
tag_req_t *parse_tag_request(cJSON *json) {
|
||||
|
||||
if (json == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!cJSON_IsObject(json)) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -425,115 +457,101 @@ tag_req_t *parse_tag_request(cJSON *json) {
|
||||
return req;
|
||||
}
|
||||
|
||||
subreq_ctx_t *elastic_delete_tag(const tag_req_t *req) {
|
||||
char *buf = malloc(sizeof(char) * 8192);
|
||||
snprintf(buf, 8192,
|
||||
"{"
|
||||
" \"script\" : {"
|
||||
" \"source\": \"if (ctx._source.tag.contains(params.tag)) { ctx._source.tag.remove(ctx._source.tag.indexOf(params.tag)) }\","
|
||||
" \"lang\": \"painless\","
|
||||
" \"params\" : {"
|
||||
" \"tag\" : \"%s\""
|
||||
" }"
|
||||
" }"
|
||||
"}", req->name
|
||||
);
|
||||
|
||||
char url[4096];
|
||||
snprintf(url, sizeof(url), "%s/%s/_update/%s", WebCtx.es_url, WebCtx.es_index, req->doc_id);
|
||||
|
||||
return web_post_async(url, buf, WebCtx.es_insecure_ssl);
|
||||
}
|
||||
|
||||
subreq_ctx_t *elastic_write_tag(const tag_req_t *req) {
|
||||
char *buf = malloc(sizeof(char) * 8192);
|
||||
snprintf(buf, 8192,
|
||||
"{"
|
||||
" \"script\" : {"
|
||||
" \"source\": \"if(ctx._source.tag == null) {ctx._source.tag = new ArrayList()} ctx._source.tag.add(params.tag)\","
|
||||
" \"lang\": \"painless\","
|
||||
" \"params\" : {"
|
||||
" \"tag\" : \"%s\""
|
||||
" }"
|
||||
" }"
|
||||
"}", req->name
|
||||
);
|
||||
|
||||
char url[4096];
|
||||
snprintf(url, sizeof(url), "%s/%s/_update/%s", WebCtx.es_url, WebCtx.es_index, req->doc_id);
|
||||
return web_post_async(url, buf, WebCtx.es_insecure_ssl);
|
||||
}
|
||||
|
||||
void tag(struct mg_connection *nc, struct mg_http_message *hm) {
|
||||
// if (hm->uri.len != SIST_INDEX_ID_LEN + 4) {
|
||||
// LOG_DEBUGF("serve.c", "Invalid tag path: %.*s", (int) hm->uri.len, hm->uri.ptr)
|
||||
// HTTP_REPLY_NOT_FOUND
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// char arg_index[SIST_INDEX_ID_LEN];
|
||||
// memcpy(arg_index, hm->uri.ptr + 5, SIST_INDEX_ID_LEN);
|
||||
// *(arg_index + SIST_INDEX_ID_LEN - 1) = '\0';
|
||||
//
|
||||
// if (hm->body.len < 2 || hm->method.len != 4 || memcmp(&hm->method, "POST", 4) == 0) {
|
||||
// LOG_DEBUG("serve.c", "Invalid tag request")
|
||||
// HTTP_REPLY_NOT_FOUND
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// store_t *store = get_tag_store(arg_index);
|
||||
// if (store == NULL) {
|
||||
// LOG_DEBUGF("serve.c", "Could not get tag store for index: %s", arg_index)
|
||||
// HTTP_REPLY_NOT_FOUND
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// char *body = malloc(hm->body.len + 1);
|
||||
// memcpy(body, hm->body.ptr, hm->body.len);
|
||||
// *(body + hm->body.len) = '\0';
|
||||
// cJSON *json = cJSON_Parse(body);
|
||||
//
|
||||
// tag_req_t *arg_req = parse_tag_request(json);
|
||||
// if (arg_req == NULL) {
|
||||
// LOG_DEBUGF("serve.c", "Could not parse tag request", arg_index)
|
||||
// cJSON_Delete(json);
|
||||
// free(body);
|
||||
// mg_http_reply(nc, 400, "", "Invalid request");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// cJSON *arr = NULL;
|
||||
//
|
||||
// size_t data_len = 0;
|
||||
// const char *data = store_read(store, arg_req->doc_id, SIST_DOC_ID_LEN, &data_len);
|
||||
// if (data_len == 0) {
|
||||
// arr = cJSON_CreateArray();
|
||||
// } else {
|
||||
// arr = cJSON_Parse(data);
|
||||
// }
|
||||
//
|
||||
// if (arg_req->delete) {
|
||||
//
|
||||
// if (data_len > 0) {
|
||||
// cJSON *element = NULL;
|
||||
// int i = 0;
|
||||
// cJSON_ArrayForEach(element, arr) {
|
||||
// if (strcmp(element->valuestring, arg_req->name) == 0) {
|
||||
// cJSON_DeleteItemFromArray(arr, i);
|
||||
// break;
|
||||
// }
|
||||
// i++;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// char *buf = malloc(sizeof(char) * 8192);
|
||||
// snprintf(buf, 8192,
|
||||
// "{"
|
||||
// " \"script\" : {"
|
||||
// " \"source\": \"if (ctx._source.tag.contains(params.tag)) { ctx._source.tag.remove(ctx._source.tag.indexOf(params.tag)) }\","
|
||||
// " \"lang\": \"painless\","
|
||||
// " \"params\" : {"
|
||||
// " \"tag\" : \"%s\""
|
||||
// " }"
|
||||
// " }"
|
||||
// "}", arg_req->name
|
||||
// );
|
||||
//
|
||||
// char url[4096];
|
||||
// snprintf(url, sizeof(url), "%s/%s/_update/%s", WebCtx.es_url, WebCtx.es_index, arg_req->doc_id);
|
||||
// nc->fn_data = web_post_async(url, buf, WebCtx.es_insecure_ssl);
|
||||
//
|
||||
// } else {
|
||||
// cJSON_AddItemToArray(arr, cJSON_CreateString(arg_req->name));
|
||||
//
|
||||
// char *buf = malloc(sizeof(char) * 8192);
|
||||
// snprintf(buf, 8192,
|
||||
// "{"
|
||||
// " \"script\" : {"
|
||||
// " \"source\": \"if(ctx._source.tag == null) {ctx._source.tag = new ArrayList()} ctx._source.tag.add(params.tag)\","
|
||||
// " \"lang\": \"painless\","
|
||||
// " \"params\" : {"
|
||||
// " \"tag\" : \"%s\""
|
||||
// " }"
|
||||
// " }"
|
||||
// "}", arg_req->name
|
||||
// );
|
||||
//
|
||||
// char url[4096];
|
||||
// snprintf(url, sizeof(url), "%s/%s/_update/%s", WebCtx.es_url, WebCtx.es_index, arg_req->doc_id);
|
||||
// nc->fn_data = web_post_async(url, buf, WebCtx.es_insecure_ssl);
|
||||
// }
|
||||
//
|
||||
// char *json_str = cJSON_PrintUnformatted(arr);
|
||||
// store_write(store, arg_req->doc_id, SIST_DOC_ID_LEN, json_str, strlen(json_str) + 1);
|
||||
// store_flush(store);
|
||||
//
|
||||
// free(arg_req);
|
||||
// free(json_str);
|
||||
// cJSON_Delete(json);
|
||||
// cJSON_Delete(arr);
|
||||
// free(body);
|
||||
if (hm->uri.len != SIST_INDEX_ID_LEN + 4) {
|
||||
LOG_DEBUGF("serve.c", "Invalid tag path: %.*s", (int) hm->uri.len, hm->uri.ptr);
|
||||
HTTP_REPLY_NOT_FOUND
|
||||
return;
|
||||
}
|
||||
|
||||
char arg_index[SIST_INDEX_ID_LEN];
|
||||
memcpy(arg_index, hm->uri.ptr + 5, SIST_INDEX_ID_LEN);
|
||||
*(arg_index + SIST_INDEX_ID_LEN - 1) = '\0';
|
||||
|
||||
char *body = malloc(hm->body.len + 1);
|
||||
memcpy(body, hm->body.ptr, hm->body.len);
|
||||
*(body + hm->body.len) = '\0';
|
||||
cJSON *json = cJSON_Parse(body);
|
||||
free(body);
|
||||
|
||||
if (json == NULL) {
|
||||
HTTP_REPLY_BAD_REQUEST
|
||||
return;
|
||||
}
|
||||
|
||||
database_t *db = web_get_database(arg_index);
|
||||
if (db == NULL) {
|
||||
LOG_DEBUGF("serve.c", "Could not get database for index: %s", arg_index);
|
||||
HTTP_REPLY_NOT_FOUND
|
||||
return;
|
||||
}
|
||||
|
||||
tag_req_t *req = parse_tag_request(json);
|
||||
cJSON_Delete(json);
|
||||
if (req == NULL) {
|
||||
LOG_DEBUGF("serve.c", "Could not parse tag request", arg_index);
|
||||
HTTP_REPLY_BAD_REQUEST
|
||||
return;
|
||||
}
|
||||
|
||||
if (req->delete) {
|
||||
database_delete_tag(db, req->doc_id, req->name);
|
||||
if (WebCtx.search_backend == SQLITE_SEARCH_BACKEND) {
|
||||
database_delete_tag(WebCtx.search_db, req->doc_id, req->name);
|
||||
HTTP_REPLY_OK
|
||||
} else {
|
||||
nc->fn_data = elastic_delete_tag(req);
|
||||
}
|
||||
} else {
|
||||
database_write_tag(db, req->doc_id, req->name);
|
||||
if (WebCtx.search_backend == SQLITE_SEARCH_BACKEND) {
|
||||
database_write_tag(WebCtx.search_db, req->doc_id, req->name);
|
||||
HTTP_REPLY_OK
|
||||
} else {
|
||||
nc->fn_data = elastic_write_tag(req);
|
||||
}
|
||||
}
|
||||
|
||||
free(req);
|
||||
}
|
||||
|
||||
int validate_auth(struct mg_connection *nc, struct mg_http_message *hm) {
|
||||
@@ -631,11 +649,39 @@ static void ev_router(struct mg_connection *nc, int ev, void *ev_data, UNUSED(vo
|
||||
return;
|
||||
}
|
||||
|
||||
if (mg_http_match_uri(hm, "/es")) {
|
||||
search(nc, hm);
|
||||
} else if (mg_http_match_uri(hm, "/status")) {
|
||||
if (WebCtx.search_backend == SQLITE_SEARCH_BACKEND) {
|
||||
if (mg_http_match_uri(hm, "/fts/paths")) {
|
||||
fts_search_paths(nc, hm);
|
||||
return;
|
||||
} else if (mg_http_match_uri(hm, "/fts/mimetypes")) {
|
||||
fts_search_mimetypes(nc, hm);
|
||||
return;
|
||||
} else if (mg_http_match_uri(hm, "/fts/dateRange")) {
|
||||
fts_search_summary_stats(nc, hm);
|
||||
return;
|
||||
} else if (mg_http_match_uri(hm, "/fts/search")) {
|
||||
fts_search(nc, hm);
|
||||
return;
|
||||
} else if (mg_http_match_uri(hm, "/fts/d/*")) {
|
||||
fts_get_document(nc, hm);
|
||||
return;
|
||||
} else if (mg_http_match_uri(hm, "/fts/suggestTags")) {
|
||||
fts_suggest_tag(nc, hm);
|
||||
return;
|
||||
} else if (mg_http_match_uri(hm, "/fts/tags")) {
|
||||
fts_get_tags(nc, hm);
|
||||
return;
|
||||
}
|
||||
} else if (WebCtx.search_backend == ES_SEARCH_BACKEND) {
|
||||
if (mg_http_match_uri(hm, "/es")) {
|
||||
search(nc, hm);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (mg_http_match_uri(hm, "/status")) {
|
||||
status(nc);
|
||||
} else if (mg_http_match_uri(hm, "/f/*")) {
|
||||
} else if (mg_http_match_uri(hm, "/f/*/*")) {
|
||||
file(nc, hm);
|
||||
} else if (mg_http_match_uri(hm, "/t/*/*/*")) {
|
||||
thumbnail_with_num(nc, hm);
|
||||
@@ -702,14 +748,12 @@ void serve(const char *listen_address) {
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr);
|
||||
|
||||
int ok = 1;
|
||||
|
||||
struct mg_connection *nc = mg_http_listen(&mgr, listen_address, ev_router, NULL);
|
||||
if (nc == NULL) {
|
||||
LOG_FATALF("serve.c", "Couldn't bind web server on address %s", listen_address);
|
||||
}
|
||||
|
||||
while (ok) {
|
||||
while (TRUE) {
|
||||
mg_mgr_poll(&mgr, 10);
|
||||
}
|
||||
mg_mgr_free(&mgr);
|
||||
|
||||
@@ -3,6 +3,11 @@
|
||||
|
||||
#include "src/sist.h"
|
||||
|
||||
#define HTTP_TEXT_TYPE_HEADER "Content-Type: text/plain;charset=utf-8\r\n"
|
||||
#define HTTP_REPLY_NOT_FOUND mg_http_reply(nc, 404, HTTP_SERVER_HEADER HTTP_TEXT_TYPE_HEADER, "Not found");
|
||||
#define HTTP_REPLY_BAD_REQUEST mg_http_reply(nc, 400, HTTP_SERVER_HEADER HTTP_TEXT_TYPE_HEADER, "Invalid request");
|
||||
#define HTTP_REPLY_OK mg_http_reply(nc, 200, HTTP_SERVER_HEADER HTTP_TEXT_TYPE_HEADER, "ok");
|
||||
|
||||
void serve(const char *listen_address);
|
||||
|
||||
#endif
|
||||
|
||||
378
src/web/web_fts.c
Normal file
378
src/web/web_fts.c
Normal file
@@ -0,0 +1,378 @@
|
||||
#include "serve.h"
|
||||
#include <mongoose.h>
|
||||
#include "src/web/web_util.h"
|
||||
|
||||
typedef struct {
|
||||
char *index_id;
|
||||
char *prefix;
|
||||
int min_depth;
|
||||
int max_depth;
|
||||
} fts_search_paths_req_t;
|
||||
|
||||
typedef struct {
|
||||
cJSON *val;
|
||||
int invalid;
|
||||
} json_value;
|
||||
|
||||
typedef struct {
|
||||
char *query;
|
||||
char *path;
|
||||
fts_sort_t sort;
|
||||
double size_min;
|
||||
double size_max;
|
||||
double date_min;
|
||||
double date_max;
|
||||
int page_size;
|
||||
char **index_ids;
|
||||
char **mime_types;
|
||||
char **tags;
|
||||
int sort_asc;
|
||||
int seed;
|
||||
char **after;
|
||||
int fetch_aggregations;
|
||||
int highlight;
|
||||
int highlight_context_size;
|
||||
} fts_search_req_t;
|
||||
|
||||
fts_sort_t get_sort_mode(const cJSON *req_sort) {
|
||||
if (strcmp(req_sort->valuestring, "score") == 0) {
|
||||
return FTS_SORT_SCORE;
|
||||
} else if (strcmp(req_sort->valuestring, "size") == 0) {
|
||||
return FTS_SORT_SIZE;
|
||||
} else if (strcmp(req_sort->valuestring, "mtime") == 0) {
|
||||
return FTS_SORT_MTIME;
|
||||
} else if (strcmp(req_sort->valuestring, "random") == 0) {
|
||||
return FTS_SORT_RANDOM;
|
||||
} else if (strcmp(req_sort->valuestring, "name") == 0) {
|
||||
return FTS_SORT_NAME;
|
||||
}
|
||||
|
||||
return FTS_SORT_INVALID;
|
||||
}
|
||||
|
||||
|
||||
static json_value get_json_string(cJSON *object, const char *name) {
|
||||
|
||||
cJSON *item = cJSON_GetObjectItem(object, name);
|
||||
if (item == NULL || cJSON_IsNull(item)) {
|
||||
return (json_value) {NULL, FALSE};
|
||||
}
|
||||
if (!cJSON_IsString(item)) {
|
||||
return (json_value) {NULL, TRUE};
|
||||
}
|
||||
|
||||
return (json_value) {item, FALSE};
|
||||
}
|
||||
|
||||
static json_value get_json_number(cJSON *object, const char *name) {
|
||||
|
||||
cJSON *item = cJSON_GetObjectItem(object, name);
|
||||
if (item == NULL || cJSON_IsNull(item)) {
|
||||
return (json_value) {NULL, FALSE};
|
||||
}
|
||||
if (!cJSON_IsNumber(item)) {
|
||||
return (json_value) {NULL, TRUE};
|
||||
}
|
||||
|
||||
return (json_value) {item, FALSE};
|
||||
}
|
||||
|
||||
static json_value get_json_bool(cJSON *object, const char *name) {
|
||||
cJSON *item = cJSON_GetObjectItem(object, name);
|
||||
if (item == NULL || cJSON_IsNull(item)) {
|
||||
return (json_value) {NULL, FALSE};
|
||||
}
|
||||
if (!cJSON_IsBool(item)) {
|
||||
return (json_value) {NULL, TRUE};
|
||||
}
|
||||
|
||||
return (json_value) {item, FALSE};
|
||||
}
|
||||
|
||||
static json_value get_json_array(cJSON *object, const char *name) {
|
||||
cJSON *item = cJSON_GetObjectItem(object, name);
|
||||
if (item == NULL || cJSON_IsNull(item)) {
|
||||
return (json_value) {NULL, FALSE};
|
||||
}
|
||||
if (!cJSON_IsArray(item) || cJSON_GetArraySize(item) == 0) {
|
||||
return (json_value) {NULL, TRUE};
|
||||
}
|
||||
|
||||
cJSON *elem;
|
||||
cJSON_ArrayForEach(elem, item) {
|
||||
if (!cJSON_IsString(elem)) {
|
||||
return (json_value) {NULL, TRUE};
|
||||
}
|
||||
}
|
||||
|
||||
return (json_value) {item, FALSE};
|
||||
}
|
||||
|
||||
char **json_array_to_c_array(cJSON *json) {
|
||||
|
||||
cJSON *element;
|
||||
char **arr = calloc(cJSON_GetArraySize(json) + 1, sizeof(char *));
|
||||
int i = 0;
|
||||
cJSON_ArrayForEach(element, json) {
|
||||
arr[i++] = strdup(element->valuestring);
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
#define DEFAULT_HIGHLIGHT_CONTEXT_SIZE 20
|
||||
|
||||
fts_search_req_t *get_search_req(struct mg_http_message *hm) {
|
||||
cJSON *json = web_get_json_body(hm);
|
||||
|
||||
if (json == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
json_value req_query, req_path, req_size_min, req_size_max, req_date_min, req_date_max, req_page_size,
|
||||
req_index_ids, req_mime_types, req_tags, req_sort_asc, req_sort, req_seed, req_after,
|
||||
req_fetch_aggregations, req_highlight, req_highlight_context_size;
|
||||
|
||||
if (!cJSON_IsObject(json) ||
|
||||
(req_query = get_json_string(json, "query")).invalid ||
|
||||
(req_path = get_json_string(json, "path")).invalid ||
|
||||
(req_sort = get_json_string(json, "sort")).val == NULL ||
|
||||
(req_size_min = get_json_number(json, "sizeMin")).invalid ||
|
||||
(req_size_max = get_json_number(json, "sizeMax")).invalid ||
|
||||
(req_date_min = get_json_number(json, "dateMin")).invalid ||
|
||||
(req_date_max = get_json_number(json, "dateMax")).invalid ||
|
||||
(req_page_size = get_json_number(json, "pageSize")).val == NULL ||
|
||||
(req_after = get_json_array(json, "after")).invalid ||
|
||||
(req_seed = get_json_number(json, "seed")).invalid ||
|
||||
(req_fetch_aggregations = get_json_bool(json, "fetchAggregations")).invalid ||
|
||||
(req_sort_asc = get_json_bool(json, "sortAsc")).invalid ||
|
||||
(req_index_ids = get_json_array(json, "indexIds")).invalid ||
|
||||
(req_mime_types = get_json_array(json, "mimeTypes")).invalid ||
|
||||
(req_highlight = get_json_bool(json, "highlight")).invalid ||
|
||||
(req_highlight_context_size = get_json_number(json, "highlightContextSize")).invalid ||
|
||||
(req_tags = get_json_array(json, "tags")).invalid) {
|
||||
cJSON_Delete(json);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int index_id_count = cJSON_GetArraySize(req_index_ids.val);
|
||||
if (index_id_count > 999) {
|
||||
cJSON_Delete(json);
|
||||
return NULL;
|
||||
}
|
||||
int mime_count = req_mime_types.val ? 0 : cJSON_GetArraySize(req_mime_types.val);
|
||||
if (mime_count > 999) {
|
||||
cJSON_Delete(json);
|
||||
return NULL;
|
||||
}
|
||||
int tag_count = req_tags.val ? 0 : cJSON_GetArraySize(req_tags.val);
|
||||
if (tag_count > 9999) {
|
||||
cJSON_Delete(json);
|
||||
return NULL;
|
||||
}
|
||||
if (req_path.val && (strstr(req_path.val->valuestring, "*") || strlen(req_path.val) >= PATH_MAX)) {
|
||||
cJSON_Delete(json);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fts_sort_t sort = get_sort_mode(req_sort.val);
|
||||
if (sort == FTS_SORT_INVALID) {
|
||||
cJSON_Delete(json);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (req_after.val && cJSON_GetArraySize(req_after.val) != 2) {
|
||||
cJSON_Delete(json);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (req_page_size.val->valueint > 1000 || req_page_size.val->valueint < 0) {
|
||||
cJSON_Delete(json);
|
||||
return NULL;
|
||||
}
|
||||
if (req_highlight_context_size.val->valueint < 0) {
|
||||
cJSON_Delete(json);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fts_search_req_t *req = malloc(sizeof(fts_search_req_t));
|
||||
|
||||
req->sort = sort;
|
||||
req->query = req_query.val ? strdup(req_query.val->valuestring) : NULL;
|
||||
req->path = req_path.val ? strdup(req_path.val->valuestring) : NULL;
|
||||
req->size_min = req_size_min.val ? req_size_min.val->valuedouble : 0;
|
||||
req->size_max = req_size_max.val ? req_size_max.val->valuedouble : 0;
|
||||
req->seed = (int) (req_seed.val ? req_seed.val->valuedouble : 0);
|
||||
req->date_min = req_date_min.val ? req_date_min.val->valuedouble : 0;
|
||||
req->date_max = req_date_max.val ? req_date_max.val->valuedouble : 0;
|
||||
req->page_size = (int) req_page_size.val->valuedouble;
|
||||
req->sort_asc = req_sort_asc.val ? req_sort_asc.val->valueint : TRUE;
|
||||
req->index_ids = req_index_ids.val ? json_array_to_c_array(req_index_ids.val) : NULL;
|
||||
req->after = req_after.val ? json_array_to_c_array(req_after.val) : NULL;
|
||||
req->mime_types = req_mime_types.val ? json_array_to_c_array(req_mime_types.val) : NULL;
|
||||
req->tags = req_tags.val ? json_array_to_c_array(req_tags.val) : NULL;
|
||||
req->fetch_aggregations = req_fetch_aggregations.val ? req_fetch_aggregations.val->valueint : FALSE;
|
||||
req->highlight = req_highlight.val ? req_highlight.val->valueint : FALSE;
|
||||
req->highlight_context_size = req_highlight_context_size.val
|
||||
? req_highlight_context_size.val->valueint
|
||||
: DEFAULT_HIGHLIGHT_CONTEXT_SIZE;
|
||||
|
||||
cJSON_Delete(json);
|
||||
|
||||
return req;
|
||||
}
|
||||
|
||||
void destroy_array(char **array) {
|
||||
if (array == NULL) {
|
||||
return;
|
||||
}
|
||||
array_foreach(array) { free(array[i]); }
|
||||
free(array);
|
||||
}
|
||||
|
||||
void destroy_search_req(fts_search_req_t *req) {
|
||||
free(req->query);
|
||||
free(req->path);
|
||||
|
||||
destroy_array(req->index_ids);
|
||||
destroy_array(req->mime_types);
|
||||
destroy_array(req->tags);
|
||||
|
||||
free(req);
|
||||
}
|
||||
|
||||
fts_search_paths_req_t *get_search_paths_req(struct mg_http_message *hm) {
|
||||
cJSON *json = web_get_json_body(hm);
|
||||
|
||||
if (json == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
json_value req_index_id, req_min_depth, req_max_depth, req_prefix;
|
||||
|
||||
if (!cJSON_IsObject(json) ||
|
||||
(req_index_id = get_json_string(json, "indexId")).invalid ||
|
||||
(req_prefix = get_json_string(json, "prefix")).invalid ||
|
||||
(req_min_depth = get_json_number(json, "minDepth")).val == NULL ||
|
||||
(req_max_depth = get_json_number(json, "maxDepth")).val == NULL) {
|
||||
cJSON_Delete(json);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fts_search_paths_req_t *req = malloc(sizeof(fts_search_paths_req_t));
|
||||
|
||||
req->index_id = req_index_id.val ? strdup(req_index_id.val->valuestring) : NULL;
|
||||
req->min_depth = req_min_depth.val->valueint;
|
||||
req->max_depth = req_max_depth.val->valueint;
|
||||
req->prefix = req_prefix.val ? strdup(req_prefix.val->valuestring) : NULL;
|
||||
|
||||
cJSON_Delete(json);
|
||||
return req;
|
||||
}
|
||||
|
||||
void destroy_search_paths_req(fts_search_paths_req_t *req) {
|
||||
if (req->index_id) {
|
||||
free(req->index_id);
|
||||
}
|
||||
if (req->prefix) {
|
||||
free(req->prefix);
|
||||
}
|
||||
free(req);
|
||||
}
|
||||
|
||||
void fts_search_paths(struct mg_connection *nc, struct mg_http_message *hm) {
|
||||
|
||||
fts_search_paths_req_t *req = get_search_paths_req(hm);
|
||||
if (req == NULL) {
|
||||
HTTP_REPLY_BAD_REQUEST
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json = database_fts_get_paths(WebCtx.search_db, req->index_id, req->min_depth,
|
||||
req->max_depth, req->prefix, req->max_depth == 10000);
|
||||
|
||||
destroy_search_paths_req(req);
|
||||
mg_send_json(nc, json);
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
void fts_search_mimetypes(struct mg_connection *nc, struct mg_http_message *hm) {
|
||||
|
||||
cJSON *json = database_fts_get_mimetypes(WebCtx.search_db);
|
||||
|
||||
mg_send_json(nc, json);
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
void fts_search_summary_stats(struct mg_connection *nc, UNUSED(struct mg_http_message *hm)) {
|
||||
|
||||
database_summary_stats_t stats = database_fts_get_date_range(WebCtx.search_db);
|
||||
|
||||
cJSON *json = cJSON_CreateObject();
|
||||
|
||||
cJSON_AddNumberToObject(json, "dateMin", stats.date_min);
|
||||
cJSON_AddNumberToObject(json, "dateMax", stats.date_max);
|
||||
|
||||
mg_send_json(nc, json);
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
void fts_search(struct mg_connection *nc, struct mg_http_message *hm) {
|
||||
|
||||
fts_search_req_t *req = get_search_req(hm);
|
||||
if (req == NULL) {
|
||||
HTTP_REPLY_BAD_REQUEST
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json = database_fts_search(WebCtx.search_db, req->query, req->path,
|
||||
(long) req->size_min, (long) req->size_max,
|
||||
(long) req->date_min, (long) req->date_max,
|
||||
req->page_size, req->index_ids, req->mime_types,
|
||||
req->tags, req->sort_asc, req->sort, req->seed,
|
||||
req->after, req->fetch_aggregations, req->highlight,
|
||||
req->highlight_context_size);
|
||||
|
||||
destroy_search_req(req);
|
||||
mg_send_json(nc, json);
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
void fts_get_document(struct mg_connection *nc, struct mg_http_message *hm) {
|
||||
|
||||
char doc_id[SIST_DOC_ID_LEN];
|
||||
memcpy(doc_id, hm->uri.ptr + 7, SIST_INDEX_ID_LEN);
|
||||
*(doc_id + SIST_INDEX_ID_LEN - 1) = '\0';
|
||||
|
||||
cJSON *json = database_fts_get_document(WebCtx.search_db, doc_id);
|
||||
|
||||
if (!json) {
|
||||
HTTP_REPLY_NOT_FOUND
|
||||
return;
|
||||
}
|
||||
|
||||
mg_send_json(nc, json);
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
void fts_suggest_tag(struct mg_connection *nc, struct mg_http_message *hm) {
|
||||
char *body = web_get_string_body(hm);
|
||||
|
||||
if (body == NULL) {
|
||||
HTTP_REPLY_BAD_REQUEST
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json = database_fts_suggest_tag(WebCtx.search_db, body);
|
||||
|
||||
mg_send_json(nc, json);
|
||||
cJSON_Delete(json);
|
||||
free(body);
|
||||
}
|
||||
|
||||
void fts_get_tags(struct mg_connection *nc, struct mg_http_message *hm) {
|
||||
cJSON *json = database_fts_get_tags(WebCtx.search_db);
|
||||
|
||||
mg_send_json(nc, json);
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
@@ -61,3 +61,38 @@ void web_send_headers(struct mg_connection *nc, int status_code, size_t length,
|
||||
extra_headers
|
||||
);
|
||||
}
|
||||
cJSON *web_get_json_body(struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *body = malloc(hm->body.len + 1);
|
||||
memcpy(body, hm->body.ptr, hm->body.len);
|
||||
*(body + hm->body.len) = '\0';
|
||||
cJSON *json = cJSON_Parse(body);
|
||||
free(body);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
char *web_get_string_body(struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *body = malloc(hm->body.len + 1);
|
||||
memcpy(body, hm->body.ptr, hm->body.len);
|
||||
*(body + hm->body.len) = '\0';
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
void mg_send_json(struct mg_connection *nc, const cJSON *json) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,9 @@ database_t *web_get_database(const char *index_id);
|
||||
|
||||
__always_inline
|
||||
static char *web_address_to_string(struct mg_addr *addr) {
|
||||
return "TODO";
|
||||
// static char address_to_string_buf[INET6_ADDRSTRLEN];
|
||||
//
|
||||
// return mg_ntoa(addr, address_to_string_buf, sizeof(address_to_string_buf));
|
||||
static char address_to_string_buf[INET6_ADDRSTRLEN];
|
||||
|
||||
return mg_ntoa(addr, address_to_string_buf, sizeof(address_to_string_buf));
|
||||
}
|
||||
|
||||
void web_send_headers(struct mg_connection *nc, int status_code, size_t length, char *extra_headers);
|
||||
@@ -29,4 +28,8 @@ void web_serve_asset_favicon_ico(struct mg_connection *nc);
|
||||
void web_serve_asset_style_css(struct mg_connection *nc);
|
||||
void web_serve_asset_chunk_vendors_css(struct mg_connection *nc);
|
||||
|
||||
cJSON *web_get_json_body(struct mg_http_message *hm);
|
||||
char *web_get_string_body(struct mg_http_message *hm);
|
||||
void mg_send_json(struct mg_connection *nc, const cJSON *json);
|
||||
|
||||
#endif //SIST2_WEB_UTIL_H
|
||||
|
||||
Reference in New Issue
Block a user