mirror of
https://github.com/minetest/minetest.git
synced 2024-08-18 20:44:16 +02:00
Fix curl compatibility issues with colorize_url (#14615)
Also move the escape code safety check to guiOpenURL.
This commit is contained in:
parent
780543f0a2
commit
59bf1d8cd9
@ -42,6 +42,19 @@ GUIOpenURLMenu::GUIOpenURLMenu(gui::IGUIEnvironment* env,
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static std::string maybe_colorize_url(const std::string &url)
|
||||||
|
{
|
||||||
|
// Forbid escape codes in URL
|
||||||
|
if (url.find('\x1b') != std::string::npos) {
|
||||||
|
throw std::runtime_error("URL contains escape codes");
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_COLORIZE_URL
|
||||||
|
return colorize_url(url);
|
||||||
|
#else
|
||||||
|
return url;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void GUIOpenURLMenu::regenerateGui(v2u32 screensize)
|
void GUIOpenURLMenu::regenerateGui(v2u32 screensize)
|
||||||
{
|
{
|
||||||
@ -70,16 +83,12 @@ void GUIOpenURLMenu::regenerateGui(v2u32 screensize)
|
|||||||
*/
|
*/
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
std::string text;
|
std::string text;
|
||||||
#ifdef USE_CURL
|
|
||||||
try {
|
try {
|
||||||
text = colorize_url(url);
|
text = maybe_colorize_url(url);
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
text = std::string(e.what()) + " (url = " + url + ")";
|
text = std::string(e.what()) + " (url = " + url + ")";
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
text = url;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Add stuff
|
Add stuff
|
||||||
|
@ -728,16 +728,18 @@ void TestUtilities::testIsBlockInSight()
|
|||||||
|
|
||||||
void TestUtilities::testColorizeURL()
|
void TestUtilities::testColorizeURL()
|
||||||
{
|
{
|
||||||
#if USE_CURL
|
#ifdef HAVE_COLORIZE_URL
|
||||||
#define RED COLOR_CODE("#faa")
|
#define RED COLOR_CODE("#faa")
|
||||||
#define GREY COLOR_CODE("#aaa")
|
#define GREY COLOR_CODE("#aaa")
|
||||||
#define WHITE COLOR_CODE("#fff")
|
#define WHITE COLOR_CODE("#fff")
|
||||||
|
|
||||||
std::string result = colorize_url("http://example.com/");
|
std::string result = colorize_url("http://example.com/");
|
||||||
UASSERT(result == (GREY "http://" WHITE "example.com" GREY "/"));
|
UASSERTEQ(auto, result, (GREY "http://" WHITE "example.com" GREY "/"));
|
||||||
|
|
||||||
result = colorize_url(u8"https://u:p@wikipedi\u0430.org:1234/heIIoll?a=b#c");
|
result = colorize_url(u8"https://u:p@wikipedi\u0430.org:1234/heIIoll?a=b#c");
|
||||||
UASSERT(result ==
|
UASSERTEQ(auto, result,
|
||||||
(GREY "https://u:p@" WHITE "wikipedi" RED "%d0%b0" WHITE ".org" GREY ":1234/heIIoll?a=b#c"));
|
(GREY "https://u:p@" WHITE "wikipedi" RED "%d0%b0" WHITE ".org" GREY ":1234/heIIoll?a=b#c"));
|
||||||
|
#else
|
||||||
|
warningstream << "Test skipped." << std::endl;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||||||
|
|
||||||
#include "colorize.h"
|
#include "colorize.h"
|
||||||
|
|
||||||
#if USE_CURL
|
#ifdef HAVE_COLORIZE_URL
|
||||||
|
|
||||||
#include <curl/urlapi.h>
|
#include <curl/urlapi.h>
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
@ -26,18 +26,13 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||||||
|
|
||||||
std::string colorize_url(const std::string &url)
|
std::string colorize_url(const std::string &url)
|
||||||
{
|
{
|
||||||
// Forbid escape codes in URL
|
|
||||||
if (url.find('\x1b') != std::string::npos) {
|
|
||||||
throw std::runtime_error("Unable to open URL as it contains escape codes");
|
|
||||||
}
|
|
||||||
|
|
||||||
auto urlHandleRAII = std::unique_ptr<CURLU, decltype(&curl_url_cleanup)>(
|
auto urlHandleRAII = std::unique_ptr<CURLU, decltype(&curl_url_cleanup)>(
|
||||||
curl_url(), curl_url_cleanup);
|
curl_url(), curl_url_cleanup);
|
||||||
CURLU *urlHandle = urlHandleRAII.get();
|
CURLU *urlHandle = urlHandleRAII.get();
|
||||||
|
|
||||||
auto rc = curl_url_set(urlHandle, CURLUPART_URL, url.c_str(), 0);
|
auto rc = curl_url_set(urlHandle, CURLUPART_URL, url.c_str(), 0);
|
||||||
if (rc != CURLUE_OK) {
|
if (rc != CURLUE_OK) {
|
||||||
throw std::runtime_error("Unable to open URL as it is not valid");
|
throw std::runtime_error("URL is not valid");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto url_get = [&] (CURLUPart what) -> std::string {
|
auto url_get = [&] (CURLUPart what) -> std::string {
|
||||||
@ -56,7 +51,11 @@ std::string colorize_url(const std::string &url)
|
|||||||
auto path = url_get(CURLUPART_PATH);
|
auto path = url_get(CURLUPART_PATH);
|
||||||
auto query = url_get(CURLUPART_QUERY);
|
auto query = url_get(CURLUPART_QUERY);
|
||||||
auto fragment = url_get(CURLUPART_FRAGMENT);
|
auto fragment = url_get(CURLUPART_FRAGMENT);
|
||||||
|
#if LIBCURL_VERSION_NUM >= 0x074100
|
||||||
auto zoneid = url_get(CURLUPART_ZONEID);
|
auto zoneid = url_get(CURLUPART_ZONEID);
|
||||||
|
#else
|
||||||
|
std::string zoneid;
|
||||||
|
#endif
|
||||||
|
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
|
|
||||||
@ -75,9 +74,7 @@ std::string colorize_url(const std::string &url)
|
|||||||
// Print hostname, escaping unsafe characters
|
// Print hostname, escaping unsafe characters
|
||||||
os << white;
|
os << white;
|
||||||
bool was_alphanum = true;
|
bool was_alphanum = true;
|
||||||
std::string host_s = host;
|
for (char c : host) {
|
||||||
for (size_t i = 0; i < host_s.size(); i++) {
|
|
||||||
char c = host_s[i];
|
|
||||||
bool is_alphanum = isalnum(c) || ispunct(c);
|
bool is_alphanum = isalnum(c) || ispunct(c);
|
||||||
if (is_alphanum == was_alphanum) {
|
if (is_alphanum == was_alphanum) {
|
||||||
// skip
|
// skip
|
||||||
|
@ -23,11 +23,19 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||||||
#define COLOR_CODE(color) "\x1b(c@" color ")"
|
#define COLOR_CODE(color) "\x1b(c@" color ")"
|
||||||
|
|
||||||
#if USE_CURL
|
#if USE_CURL
|
||||||
|
#include <curl/curlver.h>
|
||||||
|
// curl_url functions since 7.62.0
|
||||||
|
#if LIBCURL_VERSION_NUM >= 0x073e00
|
||||||
|
#define HAVE_COLORIZE_URL
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_COLORIZE_URL
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Colorize URL to highlight the hostname and any unsafe characters
|
* Colorize URL to highlight the hostname and any unsafe characters.
|
||||||
*
|
*
|
||||||
* Throws an exception if the url is invalid
|
* Throws an exception if the url is invalid.
|
||||||
*/
|
*/
|
||||||
std::string colorize_url(const std::string &url);
|
std::string colorize_url(const std::string &url);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user