From c64c99fbb4c3935dd18cae04b569fcb61f2e9cfe Mon Sep 17 00:00:00 2001 From: Katja Vornberger Date: Mon, 12 Feb 2024 21:12:02 +0100 Subject: [PATCH] Do not register request starting with "japi_": Use wrapper function instead of init variable --- include/japi.h | 1 - src/japi.c | 36 ++++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/include/japi.h b/include/japi.h index f30bf8f..4016106 100644 --- a/include/japi.h +++ b/include/japi.h @@ -60,7 +60,6 @@ typedef struct __japi_context { struct __japi_client *clients; /*!< Pointer to the JAPI client context */ bool include_args_in_response; /*!< Flag to include request args in response */ bool shutdown; /*!< Flag to shutdown the JAPI server */ - bool init; /*!< Flag to mark finished initialization */ } japi_context; /*! diff --git a/src/japi.c b/src/japi.c index 57af2e4..d4437c2 100644 --- a/src/japi.c +++ b/src/japi.c @@ -236,8 +236,9 @@ int japi_destroy(japi_context *ctx) return 0; } -int japi_register_request(japi_context *ctx, const char *req_name, - japi_req_handler req_handler) +static int japi_register_request_internal(japi_context *ctx, const char *req_name, + japi_req_handler req_handler, + bool allow_internal_req_name) { japi_request *req; char *bad_req_name = "japi_"; @@ -265,9 +266,11 @@ int japi_register_request(japi_context *ctx, const char *req_name, return -4; } - if (ctx->init && strncmp(req_name, bad_req_name, strlen(bad_req_name)) == 0) { - fprintf(stderr, "ERROR: Request name is not allowed.\n"); - return -6; + if (!allow_internal_req_name) { + if (strncmp(req_name, bad_req_name, strlen(bad_req_name)) == 0) { + fprintf(stderr, "ERROR: Request name is not allowed.\n"); + return -6; + } } req = (japi_request *)malloc(sizeof(japi_request)); @@ -285,6 +288,12 @@ int japi_register_request(japi_context *ctx, const char *req_name, return 0; } +int japi_register_request(japi_context *ctx, const char *req_name, + japi_req_handler req_handler) +{ + return japi_register_request_internal(ctx, req_name, req_handler, false); +} + japi_context *japi_init(void *userptr) { japi_context *ctx; @@ -295,7 +304,6 @@ japi_context *japi_init(void *userptr) return NULL; } - ctx->init = false; ctx->userptr = userptr; ctx->requests = NULL; ctx->push_services = NULL; @@ -315,16 +323,16 @@ japi_context *japi_init(void *userptr) signal(SIGPIPE, SIG_IGN); /* Register the default fallback handler */ - japi_register_request(ctx, "japi_request_not_found_handler", - &japi_request_not_found_handler); + japi_register_request_internal(ctx, "japi_request_not_found_handler", + &japi_request_not_found_handler, true); /* Register subscribe/unsubscribe service function */ - japi_register_request(ctx, "japi_pushsrv_subscribe", &japi_pushsrv_subscribe); - japi_register_request(ctx, "japi_pushsrv_unsubscribe", &japi_pushsrv_unsubscribe); + japi_register_request_internal(ctx, "japi_pushsrv_subscribe", + &japi_pushsrv_subscribe, true); + japi_register_request_internal(ctx, "japi_pushsrv_unsubscribe", + &japi_pushsrv_unsubscribe, true); /* Register list_push_service function */ - japi_register_request(ctx, "japi_pushsrv_list", &japi_pushsrv_list); - japi_register_request(ctx, "japi_cmd_list", &japi_cmd_list); - - ctx->init = true; + japi_register_request_internal(ctx, "japi_pushsrv_list", &japi_pushsrv_list, true); + japi_register_request_internal(ctx, "japi_cmd_list", &japi_cmd_list, true); return ctx; }