* src/main.c (get_hsts_database): Switch to using XDG-compliant location for
    hsts database, unless current installation is already using ~/.wget-hsts.
---
 doc/wget.texi | 13 +++++++------
 src/main.c    | 39 ++++++++++++++++++++++++++++++++++++---
 2 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/doc/wget.texi b/doc/wget.texi
index 04df4591..8d93b61c 100644
--- a/doc/wget.texi
+++ b/doc/wget.texi
@@ -1961,11 +1961,12 @@ consequence, Wget would ignore all the 
@code{Strict-Transport-Security}
 headers, and would not enforce any existing HSTS policy.
 
 @item --hsts-file=@var{file}
-By default, Wget stores its HSTS database in @file{~/.wget-hsts}.
-You can use @samp{--hsts-file} to override this. Wget will use
-the supplied file as the HSTS database. Such file must conform to the
-correct HSTS database format used by Wget. If Wget cannot parse the provided
-file, the behaviour is unspecified.
+By default, Wget stores its HSTS database in @file{~/.wget-hsts} if it already
+exist or creates @file{wget/hsts} in @file{${XDG_DATA_HOME:-~/.local/share}}.
+You can use @samp{--hsts-file} to override this. Wget will use the supplied
+file as the HSTS database. Such file must conform to the correct HSTS database
+format used by Wget. If Wget cannot parse the provided file, the behaviour is
+unspecified.
 
 The Wget's HSTS database is a plain text file. Each line contains an HSTS entry
 (ie. a site that has issued a @code{Strict-Transport-Security} header and that
@@ -2006,7 +2007,7 @@ it effectively updates the HSTS database by rewriting the 
database file with the
 If the supplied file does not exist, Wget will create one. This file will 
contain the new HSTS
 entries. If no HSTS entries were generated (no 
@code{Strict-Transport-Security} headers
 were sent by any of the servers) then no file will be created, not even an 
empty one. This
-behaviour applies to the default database file (@file{~/.wget-hsts}) as well: 
it will not be
+behaviour applies to the default database file as well: it will not be
 created until some server enforces an HSTS policy.
 
 Care is taken not to override possible changes made by other Wget processes at
diff --git a/src/main.c b/src/main.c
index 6858d2da..46a520b3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -173,16 +173,49 @@ hsts_store_t hsts_store;
 static char*
 get_hsts_database (void)
 {
+  char *file = NULL;
+  char *base = NULL;
+  char *dir = NULL;
+  int err;
+
   if (opt.hsts_file)
     return xstrdup (opt.hsts_file);
 
   if (opt.homedir)
     {
-      char *dir = ajoin_dir_file(opt.homedir, ".wget-hsts");
-      return dir;
+      file = ajoin_dir_file(opt.homedir, ".wget-hsts");
+    }
+
+  // Backward compatibilty. If current installation already has ~/.wget-hsts, 
keep using it.
+  // Moving it to XDG-compatible location is too complicated and non-intuitive.
+  if (file_exists_p(file, NULL))
+    return file;
+  xfree (file);
+
+  base = getenv("XDG_DATA_HOME");
+  if (!(base && *base))
+    {
+      if (!opt.homedir)
+        return NULL;
+
+      file = ajoin_dir_file(opt.homedir, ".local/share/wget/hsts");
+    }
+  else
+    {
+      file = ajoin_dir_file(base, "wget/hsts");
+    }
+
+  if (!file)
+    return NULL;
+
+  err = mkalldirs(file);
+  if (err != 0)
+    {
+      xfree (file);
+      return NULL;
     }
 
-  return NULL;
+  return file;
 }
 
 static void
-- 
2.47.0


Reply via email to