uri.c 871 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
  2. // Copyright © 2019-2021 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. path = realpath(text, NULL);
  18. gchar *f = NULL;
  19. if(path != NULL)
  20. {
  21. if(access(path, R_OK) == 0)
  22. {
  23. f = g_strdup_printf("file://%s", path);
  24. }
  25. free(path);
  26. return f;
  27. }
  28. }
  29. return g_strdup_printf("http://%s", text);
  30. }