github.com/wangyougui/gf/v2@v2.6.5/encoding/gurl/url.go (about)

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