diff --git a/src/gsad_gmp.c b/src/gsad_gmp.c index 5268a89b9..3e3cf5f31 100644 --- a/src/gsad_gmp.c +++ b/src/gsad_gmp.c @@ -268,62 +268,19 @@ typedef struct /** * @brief Wrap some XML in an envelope. * - * @param[in] connection Connection to manager - * @param[in] credentials Username and password for authentication. - * @param[in] params HTTP request params (UNUSED) - * @param[in] xml XML string. Freed before exit. - * @param[out] response_data Extra data return for the HTTP response or NULL. + * @param[in] connection Connection to manager + * @param[in] credentials Username and password for authentication. + * @param[in] params HTTP request params (UNUSED) + * @param[in] xml XML string. Freed before exit. + * @param[in,out] response_data Extra data return for the HTTP response. * - * @return Enveloped GMP XML object. + * @return Enveloped GMP XML. */ static char * envelope_gmp (gvm_connection_t *connection, gsad_credentials_t *credentials, params_t *params, gchar *xml, cmd_response_data_t *response_data) { - gchar *res; - GString *string; - - assert (credentials); - - gsad_user_t *user = gsad_credentials_get_user (credentials); - const gchar *timezone = gsad_user_get_timezone (user); - const gchar *jwt = gsad_user_get_jwt (user); - - string = g_string_new (""); - - res = g_markup_printf_escaped ( - "" - "%s" - "%s" - "%s" - "%s" - "%ld" - "%s" - "%s", - GSAD_VERSION, gsad_user_get_token (user), timezone ? timezone : "", - gsad_user_get_username (user), gsad_user_session_get_timeout (user), - gsad_credentials_get_language (credentials), - gsad_user_get_client_address (user)); - - g_string_append (string, res); - g_free (res); - - if (jwt) - { - gchar *jwt_elem; - jwt_elem = g_markup_printf_escaped ("" - "%s" - "", - jwt); - g_string_append (string, jwt_elem); - g_free (jwt_elem); - } - - g_string_append_printf (string, "%s", xml); - g_free (xml); - - cmd_response_data_set_content_type (response_data, GSAD_CONTENT_TYPE_APP_XML); - return g_string_free (string, FALSE); + return gsad_envelope (credentials, xml, response_data); } /** diff --git a/src/gsad_http.c b/src/gsad_http.c index 2788ebcf9..ff8f6680a 100644 --- a/src/gsad_http.c +++ b/src/gsad_http.c @@ -10,11 +10,12 @@ #include "gsad_http.h" -#include "gsad_base.h" /* for ctime_r_strip_newline */ -#include "gsad_i18n.h" /* for accept_language_to_env_fmt */ -#include "gsad_params_mhd.h" /* for params_mhd_append */ -#include "gsad_settings.h" -#include "gsad_utils.h" /* for str_equal */ +#include "gsad_base.h" /* for ctime_r_strip_newline */ +#include "gsad_i18n.h" /* for accept_language_to_env_fmt */ +#include "gsad_params_mhd.h" /* for params_mhd_append */ +#include "gsad_settings.h" /* for gsad_settings_get_global_settings */ +#include "gsad_user_session.h" /* for gsad_user_session_timeout */ +#include "gsad_utils.h" /* for str_equal */ #include /* for asset */ #include /* for sockaddr_as_str */ @@ -960,6 +961,62 @@ serve_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, return MHD_NO; } +/** + * @brief Wrap some XML in an envelope. + * + * @param[in] credentials Credentials to get user information from. + * @param[in] xml XML string. Freed before exit. + * @param[in,out] response_data Extra data return for the HTTP response. + * + * @return Enveloped GMP XML. + */ +char * +gsad_envelope (gsad_credentials_t *credentials, gchar *xml, + cmd_response_data_t *response_data) +{ + assert (credentials); + + gsad_user_t *user = gsad_credentials_get_user (credentials); + const gchar *timezone = gsad_user_get_timezone (user); + const gchar *jwt = gsad_user_get_jwt (user); + + GString *string = g_string_new (""); + + xml_string_append ( + string, + "" + "%s" + "%s" + "%s" + "%s" + "%ld" + "%s" + "%s", + GSAD_VERSION, gsad_user_get_token (user), timezone ? timezone : "", + gsad_user_get_username (user), gsad_user_session_get_timeout (user), + gsad_user_get_language (user), gsad_user_get_client_address (user)); + + if (jwt) + { + gchar *jwt_elem = g_markup_printf_escaped ("" + "%s" + "", + jwt); + g_string_append (string, jwt_elem); + g_free (jwt_elem); + } + g_string_append (string, xml); + g_string_append (string, ""); + g_free (xml); + + gchar *envelope = g_string_free (string, FALSE); + + cmd_response_data_set_content_length (response_data, strlen (envelope)); + cmd_response_data_set_content_type (response_data, GSAD_CONTENT_TYPE_APP_XML); + + return envelope; +} + /** * @brief Handles fatal errors. * @@ -979,64 +1036,26 @@ gsad_message (gsad_credentials_t *credentials, const char *title, const char *function, int line, const char *msg, cmd_response_data_t *response_data) { - gchar *xml, *xmltitle; - - if (function) - { - xmltitle = g_strdup_printf ("%s: %s:%i (GSA %s)", title, - function, line, GSAD_VERSION); - } - else - { - xmltitle = - g_strdup_printf ("%s (GSA %s)", title, GSAD_VERSION); - } + gchar *gsad_response = g_markup_printf_escaped ("" + "%s" + "", + msg ? msg : ""); if (credentials) { - gchar *pre; - gsad_user_t *user = gsad_credentials_get_user (credentials); - - pre = g_markup_printf_escaped ( - "" - "%s" - "%s" - "%s" - "%s" - "%s", - GSAD_VERSION, gsad_user_get_token (user), gsad_user_get_username (user), - gsad_credentials_get_language (credentials), - gsad_user_get_client_address (user)); - - xml = g_strdup_printf ("%s" - "" - "%s" - "%s" - "" - "%s" - "", - pre, xmltitle, msg ? msg : "", - gsad_user_get_capabilities (user)); - - g_free (pre); - } - else - { - xml = g_strdup_printf ("" - "%s" - "" - "%s" - "%s" - "" - "" - "", - GSAD_VERSION, xmltitle, msg ? msg : ""); + return gsad_envelope (credentials, gsad_response, response_data); } - g_free (xmltitle); + gchar *envelope = g_strdup_printf ("" + "%s" + "" + "%s" + "", + GSAD_VERSION, gsad_response); + g_free (gsad_response); + cmd_response_data_set_content_length (response_data, strlen (envelope)); cmd_response_data_set_content_type (response_data, GSAD_CONTENT_TYPE_APP_XML); - cmd_response_data_set_content_length (response_data, strlen (xml)); - return xml; + return envelope; } diff --git a/src/gsad_http.h b/src/gsad_http.h index 19ad3fa14..0e0955aa4 100644 --- a/src/gsad_http.h +++ b/src/gsad_http.h @@ -206,4 +206,7 @@ gchar * gsad_message (gsad_credentials_t *, const gchar *, const gchar *, int, const gchar *, cmd_response_data_t *); +gchar * +gsad_envelope (gsad_credentials_t *, gchar *, cmd_response_data_t *); + #endif /* _GSAD_HTTP_H */