| 885 | } |
| 886 | |
| 887 | static void handle_ui_log_post(cbm_http_conn_t *c, const cbm_http_req_t *req) { |
| 888 | if (req->body_len > UI_LOG_BODY_MAX) { |
| 889 | cbm_http_replyf(c, 413, g_cors_json, "{\"error\":\"body exceeds %d bytes\"}", |
| 890 | UI_LOG_BODY_MAX); |
| 891 | return; |
| 892 | } |
| 893 | if (req->body_len == 0) { |
| 894 | cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"invalid body\"}"); |
| 895 | return; |
| 896 | } |
| 897 | yyjson_doc *doc = yyjson_read(req->body, req->body_len, 0); |
| 898 | if (!doc) { |
| 899 | cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"invalid json\"}"); |
| 900 | return; |
| 901 | } |
| 902 | yyjson_val *root = yyjson_doc_get_root(doc); |
| 903 | yyjson_val *entries = root ? yyjson_obj_get(root, "entries") : NULL; |
| 904 | if (!root || !yyjson_is_obj(root) || !entries || !yyjson_is_arr(entries)) { |
| 905 | yyjson_doc_free(doc); |
| 906 | cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"entries[] required\"}"); |
| 907 | return; |
| 908 | } |
| 909 | |
| 910 | ui_log_scratch_t *s = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*s)); |
| 911 | char path[CBM_SZ_1K]; |
| 912 | if (!s || !cbm_ui_log_file_path(path, sizeof(path))) { |
| 913 | cbm_free(CBM_MEM_CLASS_OTHER, s); |
| 914 | yyjson_doc_free(doc); |
| 915 | cbm_http_replyf(c, 500, g_cors_json, "{\"error\":\"no log path\"}"); |
| 916 | return; |
| 917 | } |
| 918 | |
| 919 | /* Page and session are the same for every entry of a post. */ |
| 920 | yyjson_val *v_page = yyjson_obj_get(root, "page"); |
| 921 | yyjson_val *v_session = yyjson_obj_get(root, "session"); |
| 922 | ui_log_copy_field(s->field, sizeof(s->field), |
| 923 | (v_page && yyjson_is_str(v_page)) ? yyjson_get_str(v_page) : "", 256); |
| 924 | cbm_json_escape(s->page, (int)sizeof(s->page), s->field); |
| 925 | ui_log_copy_field(s->field, sizeof(s->field), |
| 926 | (v_session && yyjson_is_str(v_session)) ? yyjson_get_str(v_session) : "", |
| 927 | 128); |
| 928 | cbm_json_escape(s->session, (int)sizeof(s->session), s->field); |
| 929 | |
| 930 | char received[40]; |
| 931 | ui_log_utc_now(received, sizeof(received)); |
| 932 | |
| 933 | int accepted = 0; |
| 934 | int duplicates = 0; |
| 935 | int dropped = 0; |
| 936 | bool file_error = false, storage_error = false; |
| 937 | |
| 938 | cbm_ui_log_init(); |
| 939 | cbm_mutex_lock(&g_log_mutex); |
| 940 | sqlite3 *db = activity_db_locked(); |
| 941 | if (!db) { |
| 942 | cbm_mutex_unlock(&g_log_mutex); |
| 943 | cbm_free(CBM_MEM_CLASS_OTHER, s); |
| 944 | yyjson_doc_free(doc); |
no test coverage detected