url.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gurl provides useful API for URL handling.
  7. package gurl
  8. import (
  9. "net/url"
  10. "strings"
  11. "github.com/gogf/gf/v2/errors/gerror"
  12. )
  13. // Encode escapes the string so it can be safely placed
  14. // inside a URL query.
  15. func Encode(str string) string {
  16. return url.QueryEscape(str)
  17. }
  18. // Decode does the inverse transformation of Encode,
  19. // converting each 3-byte encoded substring of the form "%AB" into the
  20. // hex-decoded byte 0xAB.
  21. // It returns an error if any % is not followed by two hexadecimal
  22. // digits.
  23. func Decode(str string) (string, error) {
  24. return url.QueryUnescape(str)
  25. }
  26. // RawEncode does encode the given string according
  27. // URL-encode according to RFC 3986.
  28. // See http://php.net/manual/en/function.rawurlencode.php.
  29. func RawEncode(str string) string {
  30. return strings.ReplaceAll(url.QueryEscape(str), "+", "%20")
  31. }
  32. // RawDecode does decode the given string
  33. // Decode URL-encoded strings.
  34. // See http://php.net/manual/en/function.rawurldecode.php.
  35. func RawDecode(str string) (string, error) {
  36. return url.QueryUnescape(strings.ReplaceAll(str, "%20", "+"))
  37. }
  38. // BuildQuery Generate URL-encoded query string.
  39. // See http://php.net/manual/en/function.http-build-query.php.
  40. func BuildQuery(queryData url.Values) string {
  41. return queryData.Encode()
  42. }
  43. // ParseURL Parse a URL and return its components.
  44. // -1: all; 1: scheme; 2: host; 4: port; 8: user; 16: pass; 32: path; 64: query; 128: fragment.
  45. // See http://php.net/manual/en/function.parse-url.php.
  46. func ParseURL(str string, component int) (map[string]string, error) {
  47. u, err := url.Parse(str)
  48. if err != nil {
  49. err = gerror.Wrapf(err, `url.Parse failed for URL "%s"`, str)
  50. return nil, err
  51. }
  52. if component == -1 {
  53. component = 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128
  54. }
  55. var components = make(map[string]string)
  56. if (component & 1) == 1 {
  57. components["scheme"] = u.Scheme
  58. }
  59. if (component & 2) == 2 {
  60. components["host"] = u.Hostname()
  61. }
  62. if (component & 4) == 4 {
  63. components["port"] = u.Port()
  64. }
  65. if (component & 8) == 8 {
  66. components["user"] = u.User.Username()
  67. }
  68. if (component & 16) == 16 {
  69. components["pass"], _ = u.User.Password()
  70. }
  71. if (component & 32) == 32 {
  72. components["path"] = u.Path
  73. }
  74. if (component & 64) == 64 {
  75. components["query"] = u.RawQuery
  76. }
  77. if (component & 128) == 128 {
  78. components["fragment"] = u.Fragment
  79. }
  80. return components, nil
  81. }