Simplify url_error function.

* src/url.c (url_error): simplify, remove url arg, return const char *.
* src/url.h (url_error): remove url arg, return const char *.
* src/html-url.c (get_urls_file): Simplify call to url_error(),
  remove call to free().
* src/http.c (metalink_from_http): Likewise.
* src/main.c (main): Likewise.
* src/metalink.c (retrieve_from_metalink, fetch_metalink_file): Likewise.
* src/recur.c (retrieve_tree): Likewise.
* src/res.c (res_retrieve_file): Likewise.
* src/retr.c (retrieve_url, retrieve_from_file): Likewise.
This commit is contained in:
Tim Rühsen 2023-02-17 19:26:03 +01:00
parent 218f6fee30
commit 77929eda1b
9 changed files with 29 additions and 55 deletions

View File

@ -941,11 +941,9 @@ get_urls_file (const char *file)
url = url_parse (url_text, &up_error_code, NULL, false);
if (!url)
{
char *error = url_error (url_text, up_error_code);
logprintf (LOG_NOTQUIET, _("%s: Invalid URL %s: %s\n"),
file, url_text, error);
file, url_text, url_error (up_error_code));
xfree (url_text);
xfree (error);
inform_exit_status (URLERROR);
continue;
}

View File

@ -2827,10 +2827,8 @@ skip_content_type:
if (!url)
{
char *error = url_error (urlstr, url_err);
logprintf (LOG_NOTQUIET, _("When downloading signature:\n"
"%s: %s.\n"), urlstr, error);
xfree (error);
"%s: %s.\n"), urlstr, url_error (url_err));
iri_free (iri);
}
else

View File

@ -2135,9 +2135,7 @@ only if outputting to a regular file.\n"));
if (!url_parsed)
{
char *error = url_error (t, url_err);
logprintf (LOG_NOTQUIET, "%s: %s.\n",t, error);
xfree (error);
logprintf (LOG_NOTQUIET, "%s: %s.\n", t, url_error (url_err));
inform_exit_status (URLERROR);
}
else

View File

@ -419,9 +419,7 @@ retrieve_from_metalink (const metalink_t* metalink)
if (!url)
{
char *error = url_error (mres->url, url_err);
logprintf (LOG_NOTQUIET, "%s: %s.\n", mres->url, error);
xfree (error);
logprintf (LOG_NOTQUIET, "%s: %s.\n", mres->url, url_error (url_err));
inform_exit_status (URLERROR);
iri_free (iri);
continue;
@ -1175,11 +1173,9 @@ fetch_metalink_file (const char *url_str,
if (!url)
{
char *error = url_error (url_str, url_err);
logprintf (LOG_NOTQUIET, "%s: %s.\n", url_str, error);
logprintf (LOG_NOTQUIET, "%s: %s.\n", url_str, url_error (url_err));
inform_exit_status (retr_err);
iri_free (iri);
xfree (error);
return retr_err;
}

View File

@ -321,9 +321,7 @@ retrieve_tree (struct url *start_url_parsed, struct iri *pi)
if (!url_parsed)
{
char *error = url_error (url, url_err);
logprintf (LOG_NOTQUIET, "%s: %s.\n",url, error);
xfree (error);
logprintf (LOG_NOTQUIET, "%s: %s.\n",url, url_error (url_err));
inform_exit_status (URLERROR);
}
else

View File

@ -561,9 +561,7 @@ res_retrieve_file (const char *url, char **file, struct iri *iri)
url_parsed = url_parse (robots_url, &url_err, i, true);
if (!url_parsed)
{
char *error = url_error (robots_url, url_err);
logprintf (LOG_NOTQUIET, "%s: %s.\n", robots_url, error);
xfree (error);
logprintf (LOG_NOTQUIET, "%s: %s.\n", robots_url, url_error (url_err));
err = URLERROR;
}
else

View File

@ -929,11 +929,9 @@ retrieve_url (struct url * orig_parsed, const char *origurl, char **file,
proxy_url = url_parse (proxy, &up_error_code, pi, true);
if (!proxy_url)
{
char *error = url_error (proxy, up_error_code);
logprintf (LOG_NOTQUIET, _("Error parsing proxy URL %s: %s.\n"),
proxy, error);
proxy, url_error (up_error_code));
xfree (url);
xfree (error);
xfree (proxy);
iri_free (pi);
RESTORE_METHOD;
@ -1052,16 +1050,14 @@ retrieve_url (struct url * orig_parsed, const char *origurl, char **file,
newloc_parsed = url_parse (mynewloc, &up_error_code, iri, true);
if (!newloc_parsed)
{
char *error = url_error (mynewloc, up_error_code);
logprintf (LOG_NOTQUIET, "%s: %s.\n", escnonprint_uri (mynewloc),
error);
url_error (up_error_code));
if (orig_parsed != u)
{
url_free (u);
}
xfree (url);
xfree (mynewloc);
xfree (error);
RESTORE_METHOD;
goto bail;
}
@ -1217,9 +1213,7 @@ retrieve_from_file (const char *file, bool html, int *count)
struct url *url_parsed = url_parse (url, &url_err, iri, true);
if (!url_parsed)
{
char *error = url_error (url, url_err);
logprintf (LOG_NOTQUIET, "%s: %s.\n", url, error);
xfree (error);
logprintf (LOG_NOTQUIET, "%s: %s.\n", url, url_error (url_err));
iri_free (iri);
return URLERROR;
}

View File

@ -673,6 +673,8 @@ init_seps (enum url_scheme scheme)
enum {
PE_NO_ERROR = 0,
PE_UNSUPPORTED_SCHEME,
PE_UNSUPPORTED_SCHEME_HTTPS,
PE_UNSUPPORTED_SCHEME_FTPS,
PE_MISSING_SCHEME,
PE_INVALID_HOST_NAME,
PE_BAD_PORT_NUMBER,
@ -684,7 +686,9 @@ enum {
static const char *parse_errors[] = {
[PE_NO_ERROR] = N_("No error"),
// PE_UNSUPPORTED_SCHEME is handled separately in url_error()
[PE_UNSUPPORTED_SCHEME] = N_("Unsupported scheme"),
[PE_UNSUPPORTED_SCHEME_HTTPS] = N_("HTTPS support not compiled in"),
[PE_UNSUPPORTED_SCHEME_FTPS] = N_("FTPS support not compiled in"),
[PE_MISSING_SCHEME] = N_("Scheme missing"),
[PE_INVALID_HOST_NAME] = N_("Invalid host name"),
[PE_BAD_PORT_NUMBER] = N_("Bad port number"),
@ -726,10 +730,14 @@ url_parse (const char *url, int *error, struct iri *iri, bool percent_encode)
scheme = url_scheme (url);
if (scheme == SCHEME_INVALID)
{
if (url_has_scheme (url))
error_code = PE_UNSUPPORTED_SCHEME;
else
if (!url_has_scheme (url))
error_code = PE_MISSING_SCHEME;
else if (!c_strncasecmp (url, "https:", 6))
error_code = PE_UNSUPPORTED_SCHEME_HTTPS;
else if (!c_strncasecmp (url, "ftps:", 5))
error_code = PE_UNSUPPORTED_SCHEME_FTPS;
else
error_code = PE_UNSUPPORTED_SCHEME;
goto error;
}
@ -993,29 +1001,15 @@ url_parse (const char *url, int *error, struct iri *iri, bool percent_encode)
/* Return the error message string from ERROR_CODE, which should have
been retrieved from url_parse. The error message is translated. */
char *
url_error (const char *url, int error_code)
const char *
url_error (int error_code)
{
assert (error_code >= 0 && ((size_t) error_code) < countof (parse_errors));
assert (error_code >= 0 && error_code < (int) countof (parse_errors));
if (error_code != PE_UNSUPPORTED_SCHEME)
return xstrdup (_(parse_errors[error_code]));
if (error_code >= 0 && error_code < (int) countof (parse_errors))
return _(parse_errors[error_code]);
assert (url_has_scheme (url));
if (!url_has_scheme (url))
return xstrdup (_("Unexpected missing scheme"));
const char *p = strchr (url, ':');
if (p)
{
if (!c_strncasecmp (url, "https", p - url))
return xstrdup (_("HTTPS support not compiled in"));
return aprintf (_("Unsupported scheme %s"), quote_mem (url, url - p));
}
return xstrdup (""); // This should never be reached
return ""; // This should never be reached
}
/* Split PATH into DIR and FILE. PATH comes from the URL and is

View File

@ -108,7 +108,7 @@ void url_unescape (char *);
void url_unescape_except_reserved (char *);
struct url *url_parse (const char *, int *, struct iri *iri, bool percent_encode);
char *url_error (const char *, int);
const char *url_error (int);
char *url_full_path (const struct url *);
void url_set_dir (struct url *, const char *);
void url_set_file (struct url *, const char *);