uri.c 907 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
  2. // SPDX-FileCopyrightText: 2019-2022 Badwolf Authors <https://hacktivis.me/projects/badwolf>
  3. // SPDX-License-Identifier: BSD-3-Clause
  4. #include "uri.h"
  5. #include <glib.h> /* g_strcmp0(), g_uri_parse_scheme(), g_strdup_printf */
  6. #include <stdlib.h> /* realpath(), free() */
  7. #include <unistd.h> /* access() */
  8. const gchar *
  9. badwolf_ensure_uri_scheme(const gchar *text, gboolean try_file)
  10. {
  11. const gchar *fallback = "about:blank";
  12. char *path = NULL;
  13. if(g_strcmp0(text, "") <= 0) return fallback;
  14. if(g_uri_parse_scheme(text)) return text;
  15. if(try_file)
  16. {
  17. /* flawfinder: ignore. `path` is allocated by realpath itself */
  18. path = realpath(text, NULL);
  19. gchar *f = NULL;
  20. if(path != NULL)
  21. {
  22. f = g_strdup_printf("file://%s", path);
  23. free(path);
  24. return f;
  25. }
  26. }
  27. return g_strdup_printf("http://%s", text);
  28. }