api: Abstract datum_api_create_empty_mhd_response

This commit is contained in:
Luke Dashjr 2025-03-11 20:25:21 +00:00
parent ad96281588
commit f0aa212ec4
No known key found for this signature in database
GPG Key ID: A291A2C45D0C504A

View File

@ -68,6 +68,10 @@ const char *cbnames[] = {
"Antmain2" "Antmain2"
}; };
static struct MHD_Response *datum_api_create_empty_mhd_response() {
return MHD_create_response_from_buffer(0, "", MHD_RESPMEM_PERSISTENT);
}
static void html_leading_zeros(char * const buffer, const size_t buffer_size, const char * const numstr) { static void html_leading_zeros(char * const buffer, const size_t buffer_size, const char * const numstr) {
int zeros = 0; int zeros = 0;
while (numstr[zeros] == '0') { while (numstr[zeros] == '0') {
@ -398,7 +402,7 @@ int datum_api_submit_uncached_response(struct MHD_Connection * const connection,
} }
int datum_api_do_error(struct MHD_Connection * const connection, const unsigned int status_code) { int datum_api_do_error(struct MHD_Connection * const connection, const unsigned int status_code) {
struct MHD_Response *response = MHD_create_response_from_buffer(0, "", MHD_RESPMEM_PERSISTENT); struct MHD_Response *response = datum_api_create_empty_mhd_response();
return datum_api_submit_uncached_response(connection, status_code, response); return datum_api_submit_uncached_response(connection, status_code, response);
} }
@ -424,7 +428,7 @@ bool datum_api_check_admin_password_httponly(struct MHD_Connection * const conne
} }
if (ret != MHD_YES) { if (ret != MHD_YES) {
DLOG_DEBUG("Wrong password in HTTP authentication"); DLOG_DEBUG("Wrong password in HTTP authentication");
struct MHD_Response *response = MHD_create_response_from_buffer(0, "", MHD_RESPMEM_PERSISTENT); struct MHD_Response *response = datum_api_create_empty_mhd_response();
ret = MHD_queue_auth_fail_response2(connection, realm, datum_config.api_csrf_token, response, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO, MHD_DIGEST_ALG_SHA256); ret = MHD_queue_auth_fail_response2(connection, realm, datum_config.api_csrf_token, response, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO, MHD_DIGEST_ALG_SHA256);
MHD_destroy_response(response); MHD_destroy_response(response);
return false; return false;
@ -606,7 +610,7 @@ int datum_api_cmd(struct MHD_Connection *connection, char *post, int len) {
datum_api_cmd_kill_client2(data, size, &redirect); datum_api_cmd_kill_client2(data, size, &redirect);
} }
response = MHD_create_response_from_buffer(0, "", MHD_RESPMEM_PERSISTENT); response = datum_api_create_empty_mhd_response();
MHD_add_response_header(response, "Location", redirect); MHD_add_response_header(response, "Location", redirect);
return datum_api_submit_uncached_response(connection, MHD_HTTP_FOUND, response); return datum_api_submit_uncached_response(connection, MHD_HTTP_FOUND, response);
} }