From 2c2576ed4e25477683045e23c20e8fec3cf226b3 Mon Sep 17 00:00:00 2001 From: oddsock Date: Fri, 11 Apr 2003 03:00:24 +0000 Subject: [PATCH] - fixed alot of yp logic. timeouts now work properly so the tolerance of the unavailability of yp servers is much much better now. - new icecast config option to specify the timeout - url encoding is now fixed so that the yp data is formatted much nicer (and is correct :)) - added url encoding for some fields that were not url-encoded - modified util_dict_urlencode() to not url-encode the key (still does the value) - new curl option (CURLOPT_NOSIGNAL) which prevents curl from using signals when timeouts are hit. This new option needs curl 7.10 at least. svn path=/trunk/icecast/; revision=4603 --- src/source.c | 148 ++++++++++++++++++++++++++++----------------------- src/util.c | 21 +++----- src/yp.c | 13 +++-- 3 files changed, 97 insertions(+), 85 deletions(-) diff --git a/src/source.c b/src/source.c index 98b724ba..a74a0182 100644 --- a/src/source.c +++ b/src/source.c @@ -298,13 +298,9 @@ void *source_main(void *arg) } } for (i=0;inum_yp_directories;i++) { - if (source->ypdata[i]->server_type) { - free(source->ypdata[i]->server_type); - } - source->ypdata[i]->server_type = malloc( - strlen(source->format->format_description) + 1); - strcpy(source->ypdata[i]->server_type, - source->format->format_description); + _add_yp_info(source, "server_type", + source->format->format_description, + YP_SERVER_TYPE); } for (i=0;inum_yp_directories;i++) { @@ -352,35 +348,26 @@ void *source_main(void *arg) if (current_time > (source->ypdata[i]->yp_last_touch + source->ypdata[i]->yp_touch_interval)) { current_song[0] = 0; - if (stats_get_value(source->mount, "artist")) { - strncat(current_song, - stats_get_value(source->mount, "artist"), + if ((s = stats_get_value(source->mount, "artist"))) { + strncat(current_song, s, sizeof(current_song) - 1); - if (strlen(current_song) + 4 < sizeof(current_song)) { + if (strlen(current_song) + 4 < + sizeof(current_song)) + { strncat(current_song, " - ", 3); } } - if (stats_get_value(source->mount, "title")) { - if (strlen(current_song) + - strlen(stats_get_value(source->mount, "title")) + if (s = stats_get_value(source->mount, "title")) { + if (strlen(current_song) + strlen(s) < sizeof(current_song) -1) { strncat(current_song, - stats_get_value(source->mount, "title"), + s, sizeof(current_song) - 1 - strlen(current_song)); } } - - if (source->ypdata[i]->current_song) { - free(source->ypdata[i]->current_song); - source->ypdata[i]->current_song = NULL; - } - - source->ypdata[i]->current_song = - malloc(strlen(current_song) + 1); - strcpy(source->ypdata[i]->current_song, current_song); - + _add_yp_info(source, "current_song", current_song, YP_CURRENT_SONG); thread_create("YP Touch Thread", yp_touch_thread, (void *)source, THREAD_DETACHED); } @@ -743,6 +730,7 @@ static int _parse_audio_info(source_t *source, char *s) static void _add_yp_info(source_t *source, char *stat_name, void *info, int type) { + char *escaped; int i; if (!info) { return; @@ -750,49 +738,69 @@ static void _add_yp_info(source_t *source, char *stat_name, for (i=0;inum_yp_directories;i++) { switch (type) { case YP_SERVER_NAME: - if (source->ypdata[i]->server_name) { - free(source->ypdata[i]->server_name); + escaped = util_url_escape(info); + if (escaped) { + if (source->ypdata[i]->server_name) { + free(source->ypdata[i]->server_name); + } + source->ypdata[i]->server_name = + malloc(strlen((char *)escaped) +1); + strcpy(source->ypdata[i]->server_name, (char *)escaped); + stats_event(source->mount, stat_name, (char *)info); + free(escaped); } - source->ypdata[i]->server_name = - malloc(strlen((char *)info) +1); - strcpy(source->ypdata[i]->server_name, (char *)info); - stats_event(source->mount, stat_name, (char *)info); break; case YP_SERVER_DESC: - if (source->ypdata[i]->server_desc) { - free(source->ypdata[i]->server_desc); + escaped = util_url_escape(info); + if (escaped) { + if (source->ypdata[i]->server_desc) { + free(source->ypdata[i]->server_desc); + } + source->ypdata[i]->server_desc = + malloc(strlen((char *)escaped) +1); + strcpy(source->ypdata[i]->server_desc, (char *)escaped); + stats_event(source->mount, stat_name, (char *)info); + free(escaped); } - source->ypdata[i]->server_desc = - malloc(strlen((char *)info) +1); - strcpy(source->ypdata[i]->server_desc, (char *)info); - stats_event(source->mount, stat_name, (char *)info); break; case YP_SERVER_GENRE: - if (source->ypdata[i]->server_genre) { - free(source->ypdata[i]->server_genre); + escaped = util_url_escape(info); + if (escaped) { + if (source->ypdata[i]->server_genre) { + free(source->ypdata[i]->server_genre); + } + source->ypdata[i]->server_genre = + malloc(strlen((char *)escaped) +1); + strcpy(source->ypdata[i]->server_genre, (char *)escaped); + stats_event(source->mount, stat_name, (char *)info); + free(escaped); } - source->ypdata[i]->server_genre = - malloc(strlen((char *)info) +1); - strcpy(source->ypdata[i]->server_genre, (char *)info); - stats_event(source->mount, stat_name, (char *)info); break; case YP_SERVER_URL: - if (source->ypdata[i]->server_url) { - free(source->ypdata[i]->server_url); + escaped = util_url_escape(info); + if (escaped) { + if (source->ypdata[i]->server_url) { + free(source->ypdata[i]->server_url); + } + source->ypdata[i]->server_url = + malloc(strlen((char *)escaped) +1); + strcpy(source->ypdata[i]->server_url, (char *)escaped); + stats_event(source->mount, stat_name, (char *)info); + free(escaped); } - source->ypdata[i]->server_url = - malloc(strlen((char *)info) +1); - strcpy(source->ypdata[i]->server_url, (char *)info); - stats_event(source->mount, stat_name, (char *)info); break; case YP_BITRATE: - if (source->ypdata[i]->bitrate) { - free(source->ypdata[i]->bitrate); + escaped = util_url_escape(info); + if (escaped) { + if (source->ypdata[i]->bitrate) { + free(source->ypdata[i]->bitrate); + } + source->ypdata[i]->bitrate = + malloc(strlen((char *)escaped) +1); + strcpy(source->ypdata[i]->bitrate, (char *)escaped); + stats_event(source->mount, stat_name, (char *)info); + free(escaped); } - source->ypdata[i]->bitrate = - malloc(strlen((char *)info) +1); - strcpy(source->ypdata[i]->bitrate, (char *)info); - stats_event(source->mount, stat_name, (char *)info); break; case YP_AUDIO_INFO: if (source->ypdata[i]->audio_info) { @@ -803,21 +811,29 @@ static void _add_yp_info(source_t *source, char *stat_name, strcpy(source->ypdata[i]->audio_info, (char *)info); break; case YP_SERVER_TYPE: - if (source->ypdata[i]->server_type) { - free(source->ypdata[i]->server_type); + escaped = util_url_escape(info); + if (escaped) { + if (source->ypdata[i]->server_type) { + free(source->ypdata[i]->server_type); + } + source->ypdata[i]->server_type = + malloc(strlen((char *)escaped) +1); + strcpy(source->ypdata[i]->server_type, (char *)escaped); + free(escaped); } - source->ypdata[i]->server_type = - malloc(strlen((char *)info) +1); - strcpy(source->ypdata[i]->server_type, (char *)info); break; case YP_CURRENT_SONG: - if (source->ypdata[i]->current_song) { - free(source->ypdata[i]->current_song); + escaped = util_url_escape(info); + if (escaped) { + if (source->ypdata[i]->current_song) { + free(source->ypdata[i]->current_song); + } + source->ypdata[i]->current_song = + malloc(strlen((char *)escaped) +1); + strcpy(source->ypdata[i]->current_song, (char *)escaped); + stats_event(source->mount, stat_name, (char *)info); + free(escaped); } - source->ypdata[i]->current_song = - malloc(strlen((char *)info) +1); - strcpy(source->ypdata[i]->current_song, (char *)info); - stats_event(source->mount, stat_name, (char *)info); break; case YP_URL_TIMEOUT: source->ypdata[i]->yp_url_timeout = (int)info; diff --git a/src/util.c b/src/util.c index d877c942..3e3d6d2f 100644 --- a/src/util.c +++ b/src/util.c @@ -519,8 +519,8 @@ int util_dict_set(util_dict *dict, const char *key, const char *val) return 1; } -/* given a dictionary, URL-encode each key and val and - stringify them in order as key=val&key=val... if val +/* given a dictionary, URL-encode each val and + stringify it in order as key=val&key=val... if val is set, or just key&key if val is NULL. TODO: Memory management needs overhaul. */ char *util_dict_urlencode(util_dict *dict, char delim) @@ -533,28 +533,19 @@ char *util_dict_urlencode(util_dict *dict, char delim) /* encode key */ if (!dict->key) continue; - if (!(enc = util_url_escape(dict->key))) { - if (res) - free(res); - return NULL; - } if (start) { - if (!(res = malloc(strlen(enc) + 1))) { - free(enc); + if (!(res = malloc(strlen(dict->key) + 1))) { return NULL; } - sprintf(res, "%s", enc); - free(enc); + sprintf(res, "%s", dict->key); start = 0; } else { - if (!(tmp = realloc(res, strlen(res) + strlen(enc) + 2))) { - free(enc); + if (!(tmp = realloc(res, strlen(res) + strlen(dict->key) + 2))) { free(res); return NULL; } else res = tmp; - sprintf(res + strlen(res), "%c%s", delim, enc); - free(enc); + sprintf(res + strlen(res), "%c%s", delim, dict->key); } /* encode value */ diff --git a/src/yp.c b/src/yp.c index dea84ad3..f3fc7bee 100644 --- a/src/yp.c +++ b/src/yp.c @@ -19,16 +19,21 @@ static int yp_submit_url(int curl_con, char *yp_url, char *url, char *type, int i) { int ret = 0; - int *timeout; + int timeout; ice_config_t *config = config_get_config(); - timeout = config->yp_url_timeout + i; + timeout = config->yp_url_timeout[i]; config_release_config(); - + /* If not specified, use a reasonable timeout + of 30 seconds */ + if (timeout == 0) { + timeout = 30; + } curl_easy_setopt(curl_get_handle(curl_con), CURLOPT_URL, yp_url); curl_easy_setopt(curl_get_handle(curl_con), CURLOPT_POSTFIELDS, url); curl_easy_setopt(curl_get_handle(curl_con), CURLOPT_TIMEOUT, timeout); - + /* This is to force libcurl to not use signals for timeouts */ + curl_easy_setopt(curl_get_handle(curl_con), CURLOPT_NOSIGNAL, 1); /* get it! */ memset(curl_get_result(curl_con), 0, sizeof(struct curl_memory_struct)); memset(curl_get_header_result(curl_con), 0,