| 90 | } |
| 91 | |
| 92 | extern "C" char *fetchMOTD(int cache, int enable_motd) |
| 93 | { |
| 94 | sds str; |
| 95 | CURL *curl; |
| 96 | CURLcode res; |
| 97 | |
| 98 | /* Do not try the CURL if the motd is disabled*/ |
| 99 | if (!enable_motd) { |
| 100 | return NULL; |
| 101 | } |
| 102 | /* First try and get the string from the cache */ |
| 103 | if (cache) { |
| 104 | str = fetchMOTDFromCache(); |
| 105 | if (str != NULL) |
| 106 | return str; |
| 107 | } |
| 108 | |
| 109 | str = sdsnew(""); |
| 110 | curl = curl_easy_init(); |
| 111 | if(curl) { |
| 112 | curl_easy_setopt(curl, CURLOPT_URL, motd_url); |
| 113 | curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // follow redirects |
| 114 | curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2); // take no more than two seconds |
| 115 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, motd_write_callback); |
| 116 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str); |
| 117 | |
| 118 | /* Perform the request, res will get the return code */ |
| 119 | res = curl_easy_perform(curl); |
| 120 | /* Check for errors */ |
| 121 | if(res != CURLE_OK) |
| 122 | { |
| 123 | sdsfree(str); |
| 124 | str = NULL; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | long response_code; |
| 129 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); |
| 130 | if ((response_code / 100) != 2) |
| 131 | { |
| 132 | // An error code not in the 200s implies an error |
| 133 | sdsfree(str); |
| 134 | str = NULL; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* always cleanup */ |
| 139 | curl_easy_cleanup(curl); |
| 140 | |
| 141 | if (str != NULL && cache) |
| 142 | setMOTDCache(str); |
| 143 | } |
| 144 | return str; |
| 145 | } |
| 146 | |
| 147 | #else |
| 148 |
no test coverage detected