From aa321374eccacfb36ae9bb9aaa3cca221fc8408b Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Sun, 10 Jun 2018 10:18:07 +0000 Subject: [PATCH] Update: Added client_protocol_from_string() and client_protocol_to_string() --- src/admin.c | 12 +----------- src/client.c | 39 +++++++++++++++++++++++++++++++++++++++ src/client.h | 3 +++ 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/admin.c b/src/admin.c index 9a75b3bc..7924839e 100644 --- a/src/admin.c +++ b/src/admin.c @@ -688,17 +688,7 @@ static inline xmlNodePtr __add_listener(client_t *client, xmlNewTextChild(node, NULL, XMLSTR("tls"), XMLSTR(client->con->tls ? "true" : "false")); - switch (client->protocol) { - case ICECAST_PROTOCOL_HTTP: - xmlNewTextChild(node, NULL, XMLSTR("protocol"), XMLSTR("http")); - break; - case ICECAST_PROTOCOL_SHOUTCAST: - xmlNewTextChild(node, NULL, XMLSTR("protocol"), XMLSTR("icy")); - break; - case ICECAST_PROTOCOL_GOPHER: - xmlNewTextChild(node, NULL, XMLSTR("protocol"), XMLSTR("gopher")); - break; - } + xmlNewTextChild(node, NULL, XMLSTR("protocol"), XMLSTR(client_protocol_to_string(client->protocol))); return node; } diff --git a/src/client.c b/src/client.c index 6e0e39de..409db5e0 100644 --- a/src/client.c +++ b/src/client.c @@ -58,6 +58,45 @@ static inline void client_send_500(client_t *client, const char *message); +/* This returns the protocol ID based on the string. + * If the string is invalid for any reason we return ICECAST_PROTOCOL_HTTP. + */ +protocol_t client_protocol_from_string(const char *str) +{ + if (!str) { + ICECAST_LOG_ERROR("No protocol string given. Returning ICECAST_PROTOCOL_HTTP."); + return ICECAST_PROTOCOL_HTTP; + } + + if (strcasecmp(str, "http") == 0) { + return ICECAST_PROTOCOL_HTTP; + } else if (strcasecmp(str, "icy") == 0 || strcasecmp(str, "shoutcast") == 0) { + return ICECAST_PROTOCOL_SHOUTCAST; + } else if (strcasecmp(str, "gopher") == 0) { + return ICECAST_PROTOCOL_GOPHER; + } else { + ICECAST_LOG_ERROR("Unknown protocol \"%H\" string given. Returning ICECAST_PROTOCOL_HTTP.", str); + return ICECAST_PROTOCOL_HTTP; + } +} + +const char * client_protocol_to_string(protocol_t protocol) +{ + switch (protocol) { + case ICECAST_PROTOCOL_HTTP: + return "http"; + break; + case ICECAST_PROTOCOL_SHOUTCAST: + return "icy"; + break; + case ICECAST_PROTOCOL_GOPHER: + return "gopher"; + break; + } + + return NULL; +} + /* create a client_t with the provided connection and parser details. Return * 0 on success, -1 if server limit has been reached. In either case a * client_t is returned just in case a message needs to be returned. Should diff --git a/src/client.h b/src/client.h index e071ea08..48a381ed 100644 --- a/src/client.h +++ b/src/client.h @@ -128,6 +128,9 @@ struct _client_tag { int (*check_buffer)(source_t *source, client_t *client); }; +protocol_t client_protocol_from_string(const char *str); +const char * client_protocol_to_string(protocol_t protocol); + int client_create (client_t **c_ptr, connection_t *con, http_parser_t *parser); void client_destroy(client_t *client); void client_send_error_by_id(client_t *client, icecast_error_id_t id);