From 15df3ed91d1f739bffbf74eec93d43256c60715b Mon Sep 17 00:00:00 2001 From: John Sully Date: Sun, 15 Mar 2020 18:47:01 -0400 Subject: [PATCH] Handle HTTP error codes with MOTD Former-commit-id: 2ec0b2cd206f2c73fabcda5d59751b013aa8cfbf --- src/motd.cpp | 13 ++++++++++++- src/motd.h | 1 + src/redis-cli.c | 1 + src/server.cpp | 1 + 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/motd.cpp b/src/motd.cpp index 8ad0b9caa..2fbef9526 100644 --- a/src/motd.cpp +++ b/src/motd.cpp @@ -23,7 +23,7 @@ static const char *szMotdCachePath() return ""; const char *homedir = pw->pw_dir; sdsMotdCachePath = sdsnew(homedir); - sdsMotdCachePath = sdscat(sdsMotdCachePath, "/.keydb-cli-motd"); + sdsMotdCachePath = sdscat(sdsMotdCachePath, motd_cache_file); return sdsMotdCachePath; } static size_t motd_write_callback(void *ptr, size_t size, size_t nmemb, sds *str) @@ -101,6 +101,17 @@ extern "C" char *fetchMOTD(int cache) sdsfree(str); str = NULL; } + else + { + long response_code; + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); + if ((response_code / 100) != 2) + { + // An error code not in the 200s implies an error + sdsfree(str); + str = NULL; + } + } /* always cleanup */ curl_easy_cleanup(curl); diff --git a/src/motd.h b/src/motd.h index 31479c9b2..c57e63726 100644 --- a/src/motd.h +++ b/src/motd.h @@ -1,6 +1,7 @@ #pragma once extern const char *motd_url; +extern const char *motd_cache_file; #ifdef __cplusplus extern "C" { diff --git a/src/redis-cli.c b/src/redis-cli.c index c8aaab6db..96f920ae1 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -94,6 +94,7 @@ int spectrum_palette_size; int g_fInCrash = 0; const char *motd_url = "http://api.keydb.dev/motd/motd_cli.txt"; +const char *motd_cache_file = "/.keydb-cli-motd"; /*------------------------------------------------------------------------------ * Utility functions diff --git a/src/server.cpp b/src/server.cpp index 1ec2c0d7a..3d7aa3a41 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -66,6 +66,7 @@ int g_fTestMode = false; const char *motd_url = "http://api.keydb.dev/motd/motd_server.txt"; +const char *motd_cache_file = "/.keydb-server-motd"; /* Our shared "common" objects */