url.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "fmt"
  6. "net/url"
  7. "strings"
  8. )
  9. // IsAbsoluteURL returns true if the link is absolute.
  10. func IsAbsoluteURL(link string) bool {
  11. u, err := url.Parse(link)
  12. if err != nil {
  13. return false
  14. }
  15. return u.IsAbs()
  16. }
  17. // GetAbsoluteURL returns the absolute form of `input` if possible, as well as its parsed form.
  18. func GetAbsoluteURL(input string) (string, *url.URL, error) {
  19. if strings.HasPrefix(input, "//") {
  20. return "https:" + input, nil, nil
  21. }
  22. if strings.HasPrefix(input, "https://") || strings.HasPrefix(input, "http://") {
  23. return input, nil, nil
  24. }
  25. u, err := url.Parse(input)
  26. if err != nil {
  27. return "", nil, fmt.Errorf("unable to parse input URL: %v", err)
  28. }
  29. if u.IsAbs() {
  30. return u.String(), u, nil
  31. }
  32. return "", u, nil
  33. }
  34. // AbsoluteURL converts the input URL as absolute URL if necessary.
  35. func AbsoluteURL(baseURL, input string) (string, error) {
  36. absURL, u, err := GetAbsoluteURL(input)
  37. if err != nil {
  38. return "", err
  39. }
  40. if absURL != "" {
  41. return absURL, nil
  42. }
  43. base, err := url.Parse(baseURL)
  44. if err != nil {
  45. return "", fmt.Errorf("unable to parse base URL: %v", err)
  46. }
  47. return base.ResolveReference(u).String(), nil
  48. }
  49. // RootURL returns absolute URL without the path.
  50. func RootURL(websiteURL string) string {
  51. if strings.HasPrefix(websiteURL, "//") {
  52. websiteURL = "https://" + websiteURL[2:]
  53. }
  54. absoluteURL, err := AbsoluteURL(websiteURL, "")
  55. if err != nil {
  56. return websiteURL
  57. }
  58. u, err := url.Parse(absoluteURL)
  59. if err != nil {
  60. return absoluteURL
  61. }
  62. return u.Scheme + "://" + u.Host + "/"
  63. }
  64. // IsHTTPS returns true if the URL is using HTTPS.
  65. func IsHTTPS(websiteURL string) bool {
  66. parsedURL, err := url.Parse(websiteURL)
  67. if err != nil {
  68. return false
  69. }
  70. return strings.EqualFold(parsedURL.Scheme, "https")
  71. }
  72. // Domain returns only the domain part of the given URL.
  73. func Domain(websiteURL string) string {
  74. parsedURL, err := url.Parse(websiteURL)
  75. if err != nil {
  76. return websiteURL
  77. }
  78. return parsedURL.Host
  79. }
  80. // DomainWithoutWWW returns only the domain part of the given URL, with the "www." prefix removed if present.
  81. func DomainWithoutWWW(websiteURL string) string {
  82. return strings.TrimPrefix(Domain(websiteURL), "www.")
  83. }
  84. // JoinBaseURLAndPath returns a URL string with the provided path elements joined together.
  85. func JoinBaseURLAndPath(baseURL, path string) (string, error) {
  86. if baseURL == "" {
  87. return "", fmt.Errorf("empty base URL")
  88. }
  89. if path == "" {
  90. return "", fmt.Errorf("empty path")
  91. }
  92. _, err := url.Parse(baseURL)
  93. if err != nil {
  94. return "", fmt.Errorf("invalid base URL: %w", err)
  95. }
  96. finalURL, err := url.JoinPath(baseURL, path)
  97. if err != nil {
  98. return "", fmt.Errorf("unable to join base URL %s and path %s: %w", baseURL, path, err)
  99. }
  100. return finalURL, nil
  101. }