move the fallback text for about handler into messages handler

This commit is contained in:
Vincent Sanders 2019-10-30 21:26:53 +00:00
parent 4b0c3f0efe
commit 4eb06ad2cf
6 changed files with 127 additions and 122 deletions

View File

@ -79,25 +79,6 @@ struct about_handlers {
bool hidden; /**< If entry should be hidden in listing */
};
/**
* authentication query description if messages fails to retrieve usable text
*/
static const char *authentication_description_fallback = "The site %s is requesting your username and password. The realm is \"%s\"";
/**
* privacy query description if messages fails to retrieve usable text
*/
static const char *privacy_description_fallback = "A privacy error occurred while communicating with %s this may be a site configuration error or an attempt to steal private information (passwords, messages or credit cards)";
/**
* timeout query description if messages fails to retrieve usable text
*/
static const char *timeout_description_fallback = "A connection to %s could not be established. The site may be temporarily unavailable or too busy to respond.";
/**
* fetcherror query description if messages fails to retrieve usable text
*/
static const char *fetcherror_description_fallback = "An error occoured when connecting to %s";
/**
* issue fetch callbacks with locking
@ -740,7 +721,6 @@ get_authentication_description(struct nsurl *url,
char *url_s;
size_t url_l;
char *str = NULL;
int slen;
const char *key;
res = nsurl_get(url, NSURL_HOST, &url_s, &url_l);
@ -755,23 +735,46 @@ get_authentication_description(struct nsurl *url,
}
str = messages_get_buff(key, url_s, realm);
NSLOG(netsurf, INFO,
"key:%s url:%s realm:%s str:%s", key, url_s, realm, str);
if ((str != NULL) && (strcmp(key, str) != 0)) {
if (str != NULL) {
NSLOG(netsurf, INFO,
"key:%s url:%s realm:%s str:%s",
key, url_s, realm, str);
*out_str = str;
} else {
/* no message so fallback */
slen = snprintf(str, 0, authentication_description_fallback,
url_s, realm) + 1;
str = malloc(slen);
if (str == NULL) {
res = NSERROR_NOMEM;
} else {
snprintf(str, slen, authentication_description_fallback,
url_s, realm);
*out_str = str;
}
res = NSERROR_NOMEM;
}
free(url_s);
return res;
}
/**
* generate a generic query description
*/
static nserror
get_query_description(struct nsurl *url,
const char *key,
char **out_str)
{
nserror res;
char *url_s;
size_t url_l;
char *str = NULL;
/* get the host in question */
res = nsurl_get(url, NSURL_HOST, &url_s, &url_l);
if (res != NSERROR_OK) {
return res;
}
/* obtain the description with the url substituted */
str = messages_get_buff(key, url_s);
if (str == NULL) {
res = NSERROR_NOMEM;
} else {
*out_str = str;
}
free(url_s);
@ -948,56 +951,6 @@ fetch_about_query_auth_handler_aborted:
}
/**
* generate a query description
*/
static nserror
get_query_description(struct nsurl *url,
const char *key,
const char *fallback,
char **out_str)
{
nserror res;
char *url_s;
size_t url_l;
char *str = NULL;
/* get the host in question */
res = nsurl_get(url, NSURL_HOST, &url_s, &url_l);
if (res != NSERROR_OK) {
return res;
}
/* obtain the description with the url substituted */
str = messages_get_buff(key, url_s);
if ((str != NULL) && (strcmp(key, str) == 0)) {
/* the returned string was simply the key */
free(str);
str = NULL;
}
if (str == NULL) {
/* failed to get suitable translated message text so
* fall back to basic english.
*/
int slen;
slen = snprintf(str, 0, fallback, url_s) + 1;
str = malloc(slen);
if (str != NULL) {
snprintf(str, slen, fallback, url_s);
}
}
if (str == NULL) {
res = NSERROR_NOMEM;
} else {
*out_str = str;
}
free(url_s);
return res;
}
/**
* Handler to generate about scheme privacy query page
*
@ -1064,7 +1017,6 @@ static bool fetch_about_query_privacy_handler(struct fetch_about_context *ctx)
res = get_query_description(siteurl,
"PrivacyDescription",
privacy_description_fallback,
&description);
if (res == NSERROR_OK) {
res = ssenddataf(ctx, "<div><p>%s</p></div>", description);
@ -1187,7 +1139,6 @@ static bool fetch_about_query_timeout_handler(struct fetch_about_context *ctx)
res = get_query_description(siteurl,
"TimeoutDescription",
timeout_description_fallback,
&description);
if (res == NSERROR_OK) {
res = ssenddataf(ctx, "<div><p>%s</p></div>", description);
@ -1311,7 +1262,6 @@ fetch_about_query_fetcherror_handler(struct fetch_about_context *ctx)
res = get_query_description(siteurl,
"FetchErrorDescription",
fetcherror_description_fallback,
&description);
if (res == NSERROR_OK) {
res = ssenddataf(ctx, "<div><p>%s</p></div>", description);

View File

@ -25,7 +25,7 @@
#
# The split-messages tool requires keys for all languages to be
# grouped together but language order is not important. If a key for a
# specific language is ommited the default language value will be used
# specific language is omitted the default language value will be used
# instead (currently en)
#
# If you find something tagged 'all', but it is only relevant to a specific
@ -1099,8 +1099,8 @@ en.all.TryAgain: Try Again
# Fetch error interface
# =======================
#
en.all.FetchErrorTitle:Error occured fetching page
en.all.FetchErrorDescription:An error occoured when connecting to %s
en.all.FetchErrorTitle:Error occurred fetching page
en.all.FetchErrorDescription:An error occurred when connecting to %s
# SSL certificate viewer

View File

@ -118,8 +118,7 @@ START_TEST(message_get_buff_test)
ck_assert_int_eq(res, NSERROR_OK);
buf = messages_get_buff("DefinitelyNotAKey");
ck_assert_str_eq(buf, "DefinitelyNotAKey");
free(buf);
ck_assert(buf == NULL);
buf = messages_get_buff("NoMemory");
ck_assert_str_eq(buf, "NetSurf is running out of memory. Please free some memory and try again.");

View File

@ -347,10 +347,12 @@ const char *hash_get(struct hash_table *ht, const char *key)
h = hash_string_fnv(key, &key_length);
c = h % ht->nchains;
for (e = ht->chain[c]; e; e = e->next)
for (e = ht->chain[c]; e; e = e->next) {
if ((key_length == e->key_length) &&
(memcmp(key, e->pairing, key_length) == 0))
(memcmp(key, e->pairing, key_length) == 0)) {
return e->pairing + key_length + 1;
}
}
return NULL;
}

View File

@ -42,10 +42,68 @@
/** Messages are stored in a fixed-size hash table. */
#define HASH_SIZE 101
/** The hash table used to store the standard Messages file for the old API */
/**
* The hash table used to store the standard Messages file for the old API
*/
static struct hash_table *messages_hash = NULL;
/**
* Create a message context
*
* generate a message context populated with english fallbacks for
* some formatted messages.
*/
static struct hash_table *messages_create_ctx(int hash_size)
{
struct hash_table *nctx;
const struct {
const char *key;
const char *value;
} fallback[] = {
{ "LoginDescription",
"The site %s is requesting your username and password. "
"The realm is \"%s\""},
{ "PrivacyDescription",
"A privacy error occurred while communicating with %s this "
"may be a site configuration error or an attempt to steal "
"private information (passwords, messages or credit cards)"},
{ "TimeoutDescription",
"A connection to %s could not be established. The site may "
"be temporarily unavailable or too busy to respond."},
{ "FetchErrorDescription",
"An error occurred when connecting to %s"},
{ NULL, NULL}
};
nctx = hash_create(hash_size);
if (nctx != NULL) {
int floop;
for (floop = 0; fallback[floop].key != NULL; floop++) {
hash_add(nctx,
fallback[floop].key,
fallback[floop].value);
}
}
return nctx;
}
/**
* Free memory used by a messages hash.
* The context will not be valid after this function returns.
*
* \param ctx context of messages file to free
*/
static void messages_destroy_ctx(struct hash_table *ctx)
{
if (ctx == NULL)
return;
hash_destroy(ctx);
}
/**
* Read keys and values from messages file.
*
@ -66,7 +124,7 @@ static nserror messages_load_ctx(const char *path, struct hash_table **ctx)
return hash_add_file(*ctx, path);
}
nctx = hash_create(HASH_SIZE);
nctx = messages_create_ctx(HASH_SIZE);
if (nctx == NULL) {
NSLOG(netsurf, INFO,
"Unable to create hash table for messages file %s",
@ -115,21 +173,6 @@ messages_get_ctx(const char *key, struct hash_table *ctx)
}
/**
* Free memory used by a messages hash.
* The context will not be valid after this function returns.
*
* \param ctx context of messages file to free
*/
static void messages_destroy_ctx(struct hash_table *ctx)
{
if (ctx == NULL)
return;
hash_destroy(ctx);
}
/* exported interface documented in messages.h */
nserror messages_add_from_file(const char *path)
{
@ -148,7 +191,7 @@ nserror messages_add_from_inline(const uint8_t *data, size_t size)
{
/* ensure the hash table is initialised */
if (messages_hash == NULL) {
messages_hash = hash_create(HASH_SIZE);
messages_hash = messages_create_ctx(HASH_SIZE);
}
if (messages_hash == NULL) {
NSLOG(netsurf, INFO, "Unable to create hash table");
@ -157,6 +200,7 @@ nserror messages_add_from_inline(const uint8_t *data, size_t size)
return hash_add_inline(messages_hash, data, size);
}
/* exported interface documented in messages.h */
char *messages_get_buff(const char *key, ...)
{
@ -165,7 +209,17 @@ char *messages_get_buff(const char *key, ...)
int buff_len = 0;
va_list ap;
msg_fmt = messages_get_ctx(key, messages_hash);
assert(key != NULL);
if (messages_hash == NULL) {
return NULL;
}
msg_fmt = hash_get(messages_hash, key);
if (msg_fmt == NULL) {
return NULL;
}
va_start(ap, key);
buff_len = vsnprintf(buff, buff_len, msg_fmt, ap);

View File

@ -16,7 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file
/**
* \file
* Localised message support (interface).
*
* The messages module loads a file of keys and associated strings, and
@ -30,8 +31,8 @@
* file table. Use the _ctx versions of the functions to do this.
*/
#ifndef _NETSURF_UTILS_MESSAGES_H_
#define _NETSURF_UTILS_MESSAGES_H_
#ifndef NETSURF_UTILS_MESSAGES_H_
#define NETSURF_UTILS_MESSAGES_H_
#include <stdint.h>
@ -90,13 +91,12 @@ const char *messages_get_sslcode(ssl_cert_err code);
/**
* Formatted message from a key in the global message hash.
*
* \param key key of message
* \param key key of message
* \param ... message parameters
* \return buffer containing formatted message text or NULL if memory
* is unavailable. The caller owns the returned buffer and is
* responsible for freeing it.
* \return buffer containing formatted message text or NULL if key is
* unavailable or memory allocation failed. The caller owns the
* returned buffer and is responsible for freeing it.
*/
char *messages_get_buff(const char *key, ...);
/**