gf/encoding/gurl/url.go

91 lines
2.6 KiB
Go
Raw Normal View History

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,
// 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
import (
2019-06-19 09:06:52 +08:00
"net/url"
"strings"
"github.com/gogf/gf/v2/errors/gerror"
)
2017-11-23 10:21:28 +08:00
// Encode escapes the string so it can be safely placed
// inside an 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
}
// 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
}
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.
// See http://php.net/manual/en/function.rawurlencode.php.
func RawEncode(str string) string {
return strings.ReplaceAll(url.QueryEscape(str), "+", "%20")
}
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.
// See http://php.net/manual/en/function.rawurldecode.php.
func RawDecode(str string) (string, error) {
return url.QueryUnescape(strings.ReplaceAll(str, "%20", "+"))
}
// BuildQuery Generate URL-encoded query string.
// 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()
}
// ParseURL Parse an URL and return its components.
// -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 {
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
}