From 3e7d32a58ed28c74f1361dc38908e271d6d17d7b Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 18 Sep 2023 15:33:31 +0200 Subject: [PATCH 1/4] Clean: declare variables in one place --- src/gsad_http_handler.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/gsad_http_handler.c b/src/gsad_http_handler.c index 98f49ea32..9a683bb97 100644 --- a/src/gsad_http_handler.c +++ b/src/gsad_http_handler.c @@ -756,9 +756,10 @@ handle_static_config (http_connection_t *connection, const char *method, http_handler_t * init_http_handlers () { - http_handler_t *method_router; - http_handler_t *gmp_post_handler; - http_handler_t *url_handlers; + http_handler_t *method_router, *gmp_post_handler, *url_handlers; + http_handler_t *gmp_handler, *gmp_url_handler; + http_handler_t *system_report_handler, *system_report_url_handler; + http_handler_t *logout_handler, *logout_url_handler; http_validator = gvm_validator_new (); gvm_validator_add (http_validator, "slave_id", SLAVE_ID_REGEXP); @@ -780,23 +781,22 @@ init_http_handlers () handle_static_file); url_handler_add_func (url_handlers, "^/manual/.+$", handle_static_file); - http_handler_t *gmp_handler = http_handler_new (handle_setup_user); + gmp_handler = http_handler_new (handle_setup_user); http_handler_add (gmp_handler, http_handler_new (handle_setup_credentials)); http_handler_add (gmp_handler, http_handler_new (handle_gmp_get)); - http_handler_t *gmp_url_handler = url_handler_new ("^/gmp$", gmp_handler); + gmp_url_handler = url_handler_new ("^/gmp$", gmp_handler); - http_handler_t *system_report_handler = http_handler_new (handle_setup_user); + system_report_handler = http_handler_new (handle_setup_user); http_handler_add (system_report_handler, http_handler_new (handle_setup_credentials)); http_handler_add (system_report_handler, http_handler_new (handle_system_report)); - http_handler_t *system_report_url_handler = - url_handler_new ("^/system_report/.+$", system_report_handler); + system_report_url_handler = url_handler_new ("^/system_report/.+$", + system_report_handler); - http_handler_t *logout_handler = http_handler_new (handle_get_user); + logout_handler = http_handler_new (handle_get_user); http_handler_add (logout_handler, http_handler_new (handle_logout)); - http_handler_t *logout_url_handler = - url_handler_new ("^/logout/?$", logout_handler); + logout_url_handler = url_handler_new ("^/logout/?$", logout_handler); http_handler_add (url_handlers, gmp_url_handler); http_handler_add (url_handlers, system_report_url_handler); From 3fe5f159e36bd55633d8e8fa29fe4da02931f153 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 18 Sep 2023 15:43:36 +0200 Subject: [PATCH 2/4] Clean: move URL handlers to new function --- src/gsad_http_handler.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/gsad_http_handler.c b/src/gsad_http_handler.c index 9a683bb97..b4004b70b 100644 --- a/src/gsad_http_handler.c +++ b/src/gsad_http_handler.c @@ -753,25 +753,14 @@ handle_static_config (http_connection_t *connection, const char *method, NULL); } -http_handler_t * -init_http_handlers () +static http_handler_t * +make_url_handlers () { - http_handler_t *method_router, *gmp_post_handler, *url_handlers; + http_handler_t *url_handlers; http_handler_t *gmp_handler, *gmp_url_handler; http_handler_t *system_report_handler, *system_report_url_handler; http_handler_t *logout_handler, *logout_url_handler; - http_validator = gvm_validator_new (); - gvm_validator_add (http_validator, "slave_id", SLAVE_ID_REGEXP); - gvm_validator_add (http_validator, "token", TOKEN_REGEXP); - - handlers = http_handler_new (handle_validate); - - method_router = method_router_new (); - gmp_post_handler = http_handler_new (handle_gmp_post); - - http_handler_add (handlers, method_router); - url_handlers = url_handler_new ("^/(img|js|css|locales)/.+$", http_handler_new (handle_static_file)); url_handler_add_func (url_handlers, "^/robots\\.txt$", handle_static_file); @@ -804,6 +793,27 @@ init_http_handlers () http_handler_add (url_handlers, http_handler_new (handle_index)); + return url_handlers; +} + +http_handler_t * +init_http_handlers () +{ + http_handler_t *method_router, *gmp_post_handler, *url_handlers; + + http_validator = gvm_validator_new (); + gvm_validator_add (http_validator, "slave_id", SLAVE_ID_REGEXP); + gvm_validator_add (http_validator, "token", TOKEN_REGEXP); + + handlers = http_handler_new (handle_validate); + + method_router = method_router_new (); + gmp_post_handler = http_handler_new (handle_gmp_post); + + http_handler_add (handlers, method_router); + + url_handlers = make_url_handlers (); + method_router_set_get_handler (method_router, url_handlers); method_router_set_post_handler (method_router, gmp_post_handler); From d2a3dfd66a65361dcf59c0b6aaa9792b88fc509c Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 18 Sep 2023 19:32:45 +0200 Subject: [PATCH 3/4] Clean: add a few comments. --- src/gsad_http_handler.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/gsad_http_handler.c b/src/gsad_http_handler.c index b4004b70b..7d4fba5dd 100644 --- a/src/gsad_http_handler.c +++ b/src/gsad_http_handler.c @@ -763,6 +763,9 @@ make_url_handlers () url_handlers = url_handler_new ("^/(img|js|css|locales)/.+$", http_handler_new (handle_static_file)); + + // Add simpler handlers. + url_handler_add_func (url_handlers, "^/robots\\.txt$", handle_static_file); url_handler_add_func (url_handlers, "^/config\\.*js$", handle_static_config); @@ -770,11 +773,15 @@ make_url_handlers () handle_static_file); url_handler_add_func (url_handlers, "^/manual/.+$", handle_static_file); + // Create /gmp handler. + gmp_handler = http_handler_new (handle_setup_user); http_handler_add (gmp_handler, http_handler_new (handle_setup_credentials)); http_handler_add (gmp_handler, http_handler_new (handle_gmp_get)); gmp_url_handler = url_handler_new ("^/gmp$", gmp_handler); + // Create /system_report handler. + system_report_handler = http_handler_new (handle_setup_user); http_handler_add (system_report_handler, http_handler_new (handle_setup_credentials)); @@ -783,10 +790,14 @@ make_url_handlers () system_report_url_handler = url_handler_new ("^/system_report/.+$", system_report_handler); + // Create /logout handler. + logout_handler = http_handler_new (handle_get_user); http_handler_add (logout_handler, http_handler_new (handle_logout)); logout_url_handler = url_handler_new ("^/logout/?$", logout_handler); + // Add the handlers. + http_handler_add (url_handlers, gmp_url_handler); http_handler_add (url_handlers, system_report_url_handler); http_handler_add (url_handlers, logout_url_handler); From 3e0feba898ab4d4b7de5775bb2192a0e87543e26 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 18 Sep 2023 19:39:13 +0200 Subject: [PATCH 4/4] Clean: format clang style --- src/gsad_http_handler.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gsad_http_handler.c b/src/gsad_http_handler.c index 7d4fba5dd..2c50ac867 100644 --- a/src/gsad_http_handler.c +++ b/src/gsad_http_handler.c @@ -787,8 +787,8 @@ make_url_handlers () http_handler_new (handle_setup_credentials)); http_handler_add (system_report_handler, http_handler_new (handle_system_report)); - system_report_url_handler = url_handler_new ("^/system_report/.+$", - system_report_handler); + system_report_url_handler = + url_handler_new ("^/system_report/.+$", system_report_handler); // Create /logout handler.