url.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package urllib // import "miniflux.app/v2/internal/urllib"
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/url"
  8. "strings"
  9. )
  10. // IsRelativePath returns true if the link is a relative path.
  11. func IsRelativePath(link string) bool {
  12. if link == "" {
  13. return false
  14. }
  15. if parsedURL, err := url.Parse(link); err == nil {
  16. // Only allow relative paths (not scheme-relative URLs like //example.org)
  17. // and ensure the URL doesn't have a host component
  18. if !parsedURL.IsAbs() && parsedURL.Host == "" && parsedURL.Scheme == "" {
  19. return true
  20. }
  21. }
  22. return false
  23. }
  24. // IsAbsoluteURL returns true if the link is absolute.
  25. func IsAbsoluteURL(link string) bool {
  26. u, err := url.Parse(link)
  27. if err != nil {
  28. return false
  29. }
  30. return u.IsAbs()
  31. }
  32. // GetAbsoluteURL returns the absolute form of `input` if possible, as well as its parsed form.
  33. func GetAbsoluteURL(input string) (string, *url.URL, error) {
  34. if strings.HasPrefix(input, "//") {
  35. return "https:" + input, nil, nil
  36. }
  37. if strings.HasPrefix(input, "https://") || strings.HasPrefix(input, "http://") {
  38. return input, nil, nil
  39. }
  40. u, err := url.Parse(input)
  41. if err != nil {
  42. return "", nil, fmt.Errorf("unable to parse input URL: %v", err)
  43. }
  44. if u.IsAbs() {
  45. return u.String(), u, nil
  46. }
  47. return "", u, nil
  48. }
  49. // AbsoluteURL converts the input URL as absolute URL if necessary.
  50. func AbsoluteURL(baseURL, input string) (string, error) {
  51. absURL, u, err := GetAbsoluteURL(input)
  52. if err != nil {
  53. return "", err
  54. }
  55. if absURL != "" {
  56. return absURL, nil
  57. }
  58. base, err := url.Parse(baseURL)
  59. if err != nil {
  60. return "", fmt.Errorf("unable to parse base URL: %v", err)
  61. }
  62. return base.ResolveReference(u).String(), nil
  63. }
  64. // RootURL returns absolute URL without the path.
  65. func RootURL(websiteURL string) string {
  66. if strings.HasPrefix(websiteURL, "//") {
  67. websiteURL = "https://" + websiteURL[2:]
  68. }
  69. absoluteURL, err := AbsoluteURL(websiteURL, "")
  70. if err != nil {
  71. return websiteURL
  72. }
  73. u, err := url.Parse(absoluteURL)
  74. if err != nil {
  75. return absoluteURL
  76. }
  77. return u.Scheme + "://" + u.Host + "/"
  78. }
  79. // IsHTTPS returns true if the URL is using HTTPS.
  80. func IsHTTPS(websiteURL string) bool {
  81. parsedURL, err := url.Parse(websiteURL)
  82. if err != nil {
  83. return false
  84. }
  85. return strings.EqualFold(parsedURL.Scheme, "https")
  86. }
  87. // Domain returns only the domain part of the given URL.
  88. func Domain(websiteURL string) string {
  89. parsedURL, err := url.Parse(websiteURL)
  90. if err != nil {
  91. return websiteURL
  92. }
  93. return parsedURL.Host
  94. }
  95. // DomainWithoutWWW returns only the domain part of the given URL, with the "www." prefix removed if present.
  96. func DomainWithoutWWW(websiteURL string) string {
  97. return strings.TrimPrefix(Domain(websiteURL), "www.")
  98. }
  99. // JoinBaseURLAndPath returns a URL string with the provided path elements joined together.
  100. func JoinBaseURLAndPath(baseURL, path string) (string, error) {
  101. if baseURL == "" {
  102. return "", errors.New("empty base URL")
  103. }
  104. if path == "" {
  105. return "", errors.New("empty path")
  106. }
  107. _, err := url.Parse(baseURL)
  108. if err != nil {
  109. return "", fmt.Errorf("invalid base URL: %w", err)
  110. }
  111. finalURL, err := url.JoinPath(baseURL, path)
  112. if err != nil {
  113. return "", fmt.Errorf("unable to join base URL %s and path %s: %w", baseURL, path, err)
  114. }
  115. return finalURL, nil
  116. }