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