2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2019-01-15 23:27:47 +08:00
|
|
|
// Package gurl provides useful API for URL handling.
|
2017-11-23 10:21:28 +08:00
|
|
|
package gurl
|
|
|
|
|
2019-01-30 21:27:03 +08:00
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2021-12-21 22:59:14 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2019-01-30 21:27:03 +08:00
|
|
|
)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
2020-04-18 13:30:49 +08:00
|
|
|
// Encode escapes the string so it can be safely placed
|
|
|
|
// inside a URL query.
|
2017-11-23 10:21:28 +08:00
|
|
|
func Encode(str string) string {
|
2019-06-19 09:06:52 +08:00
|
|
|
return url.QueryEscape(str)
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2020-04-18 13:30:49 +08:00
|
|
|
// Decode does the inverse transformation of Encode,
|
|
|
|
// converting each 3-byte encoded substring of the form "%AB" into the
|
|
|
|
// hex-decoded byte 0xAB.
|
|
|
|
// It returns an error if any % is not followed by two hexadecimal
|
|
|
|
// digits.
|
2017-11-23 10:21:28 +08:00
|
|
|
func Decode(str string) (string, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
return url.QueryUnescape(str)
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
2019-01-30 21:27:03 +08:00
|
|
|
|
2021-11-16 00:59:15 +08:00
|
|
|
// RawEncode does encode the given string according
|
2021-11-16 00:56:14 +08:00
|
|
|
// URL-encode according to RFC 3986.
|
2019-01-30 21:27:03 +08:00
|
|
|
// See http://php.net/manual/en/function.rawurlencode.php.
|
|
|
|
func RawEncode(str string) string {
|
2022-11-01 20:12:21 +08:00
|
|
|
return strings.ReplaceAll(url.QueryEscape(str), "+", "%20")
|
2019-01-30 21:27:03 +08:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:59:15 +08:00
|
|
|
// RawDecode does decode the given string
|
2021-11-16 00:56:14 +08:00
|
|
|
// Decode URL-encoded strings.
|
2019-01-30 21:27:03 +08:00
|
|
|
// See http://php.net/manual/en/function.rawurldecode.php.
|
|
|
|
func RawDecode(str string) (string, error) {
|
2022-11-01 20:12:21 +08:00
|
|
|
return url.QueryUnescape(strings.ReplaceAll(str, "%20", "+"))
|
2019-01-30 21:27:03 +08:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:26:10 +08:00
|
|
|
// BuildQuery Generate URL-encoded query string.
|
2019-01-30 21:27:03 +08:00
|
|
|
// See http://php.net/manual/en/function.http-build-query.php.
|
|
|
|
func BuildQuery(queryData url.Values) string {
|
2019-06-19 09:06:52 +08:00
|
|
|
return queryData.Encode()
|
2019-01-30 21:27:03 +08:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:26:10 +08:00
|
|
|
// ParseURL Parse a URL and return its components.
|
2019-01-30 21:27:03 +08:00
|
|
|
// -1: all; 1: scheme; 2: host; 4: port; 8: user; 16: pass; 32: path; 64: query; 128: fragment.
|
|
|
|
// See http://php.net/manual/en/function.parse-url.php.
|
|
|
|
func ParseURL(str string, component int) (map[string]string, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
u, err := url.Parse(str)
|
|
|
|
if err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrapf(err, `url.Parse failed for URL "%s"`, str)
|
2019-06-19 09:06:52 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if component == -1 {
|
|
|
|
component = 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128
|
|
|
|
}
|
|
|
|
var components = make(map[string]string)
|
|
|
|
if (component & 1) == 1 {
|
|
|
|
components["scheme"] = u.Scheme
|
|
|
|
}
|
|
|
|
if (component & 2) == 2 {
|
|
|
|
components["host"] = u.Hostname()
|
|
|
|
}
|
|
|
|
if (component & 4) == 4 {
|
|
|
|
components["port"] = u.Port()
|
|
|
|
}
|
|
|
|
if (component & 8) == 8 {
|
|
|
|
components["user"] = u.User.Username()
|
|
|
|
}
|
|
|
|
if (component & 16) == 16 {
|
|
|
|
components["pass"], _ = u.User.Password()
|
|
|
|
}
|
|
|
|
if (component & 32) == 32 {
|
|
|
|
components["path"] = u.Path
|
|
|
|
}
|
|
|
|
if (component & 64) == 64 {
|
|
|
|
components["query"] = u.RawQuery
|
|
|
|
}
|
|
|
|
if (component & 128) == 128 {
|
|
|
|
components["fragment"] = u.Fragment
|
|
|
|
}
|
|
|
|
return components, nil
|
2019-01-30 21:27:03 +08:00
|
|
|
}
|