Skip to content

Commit

Permalink
完善URI编码
Browse files Browse the repository at this point in the history
  • Loading branch information
wangluozhe committed Mar 27, 2022
1 parent 7a1a777 commit 79002e6
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 15 deletions.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# requests
[![Gitee link address](https://img.shields.io/badge/gitee-reference-red?logo=gitee&logoColor=red&labelColor=white)](https://gitee.com/leegene/requests)[![Github link address](https://img.shields.io/badge/github-reference-blue?logo=github&logoColor=black&labelColor=white&color=black)](https://github.com/wangluozhe/requests)[![Go Version](https://img.shields.io/badge/Go%20Version-1.15.6-blue?logo=go&logoColor=white&labelColor=gray)]()[![Release Version](https://img.shields.io/badge/release-v1.0.02-blue)]()
[![Gitee link address](https://img.shields.io/badge/gitee-reference-red?logo=gitee&logoColor=red&labelColor=white)](https://gitee.com/leegene/requests)[![Github link address](https://img.shields.io/badge/github-reference-blue?logo=github&logoColor=black&labelColor=white&color=black)](https://github.com/wangluozhe/requests)[![Go Version](https://img.shields.io/badge/Go%20Version-1.15.6-blue?logo=go&logoColor=white&labelColor=gray)]()[![Release Version](https://img.shields.io/badge/release-v1.0.03-blue)]()

requests支持以下新特性:

Expand All @@ -23,7 +23,7 @@ go get github.com/wangluozhe/requests
## 下载指定版本

```bash
go get github.com/wangluozhe/[email protected].02
go get github.com/wangluozhe/[email protected].03
```


Expand Down Expand Up @@ -854,8 +854,6 @@ https://www.baidu.com
## URI编码
```go
Expand All @@ -867,15 +865,27 @@ import (
)

func main() {
url := "https://www.baidu.com"
url := "https://www.baidu.com?page=10&abc=123&name=你好啊"
encode := utils.EncodeURIComponent(url)
fmt.Println(encode)
decode := utils.DecodeURIComponent(encode)
fmt.Println(decode)
}

https%3A%2F%2Fwww.baidu.com
https://www.baidu.com
encode1 := utils.EncodeURI(url)
fmt.Println(encode1)
decode1 := utils.DecodeURI(encode1)
fmt.Println(decode1)
escape := utils.Escape(url)
fmt.Println(escape)
unescape := utils.UnEscape(escape)
fmt.Println(unescape)
}

https%3A%2F%2Fwww.baidu.com%3Fpage%3D10%26abc%3D123%26name%3D%E4%BD%A0%E5%A5%BD%E5%95%8A
https://www.baidu.com?page=10&abc=123&name=你好啊
https://www.baidu.com?page=10&abc=123&name=%E4%BD%A0%E5%A5%BD%E5%95%8A
https://www.baidu.com?page=10&abc=123&name=你好啊
https%3A//www.baidu.com%3Fpage%3D10%26abc%3D123%26name%3D%u4f60%u597d%u554a
https://www.baidu.com?page=10&abc=123&name=你好啊
```
Expand Down
10 changes: 9 additions & 1 deletion examples/encode-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func main() {
url := "https://www.baidu.com"
url := "https://www.baidu.com?page=10&abc=123&name=你好啊"
hexen := utils.HexEncode(url)
fmt.Println(hexen)
fmt.Println(string(hexen))
Expand All @@ -17,6 +17,10 @@ func main() {
fmt.Println(encode)
decode := utils.DecodeURIComponent(encode)
fmt.Println(decode)
encode1 := utils.EncodeURI(url)
fmt.Println(encode1)
decode1 := utils.DecodeURI(encode1)
fmt.Println(decode1)
base64en := utils.Base64Encode(url)
fmt.Println(base64en)
base64de := utils.Base64Decode(base64en)
Expand All @@ -25,4 +29,8 @@ func main() {
fmt.Println(btoa)
atob := utils.Atob(btoa)
fmt.Println(atob)
escape := utils.Escape(url)
fmt.Println(escape)
unescape := utils.UnEscape(escape)
fmt.Println(unescape)
}
92 changes: 87 additions & 5 deletions utils/code.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package utils

import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
"net/url"
"regexp"
"strconv"
"strings"
)

// 只接受string和[]byte类型
Expand Down Expand Up @@ -43,16 +48,54 @@ func HexDecode(s interface{}) []byte {
}

// URI编码
func EncodeURIComponent(s string) string {
return url.QueryEscape(s)
func EncodeURIComponent(s interface{}) string {
byte_s := stringAndByte(s)
es := url.QueryEscape(string(byte_s))
es = strings.ReplaceAll(es, "+", "%20")
es = strings.ReplaceAll(es, "%21", "!")
es = strings.ReplaceAll(es, "%27", "'")
es = strings.ReplaceAll(es, "%28", "(")
es = strings.ReplaceAll(es, "%29", ")")
es = strings.ReplaceAll(es, "%2A", "*")
return es
}

// URI解码
func DecodeURIComponent(s string) string {
t, _ := url.QueryUnescape(s)
func DecodeURIComponent(s interface{}) string {
byte_s := stringAndByte(s)
t, err := url.QueryUnescape(strings.ReplaceAll(strings.ReplaceAll(string(byte_s), "+", "%2B"), "%20", "+"))
if err != nil {
panic(err)
}
return t
}

// URI编码
func EncodeURI(s interface{}) string {
byte_s := stringAndByte(s)
es := EncodeURIComponent(string(byte_s))
ss := "!#$&'()*+,-./:=?@_~"
for i := 0; i < len(ss); i++ {
es = strings.ReplaceAll(es, "%"+strings.ToUpper(string(HexEncode(string(ss[i])))), string(ss[i]))
}
return strings.ReplaceAll(es, "%3B", ";")
}

// URI解码
func DecodeURI(s interface{}) string {
byte_s := stringAndByte(s)
es := string(byte_s)
ss := "!#$&'()*+,-./:=?@_~"
for i := 0; i < len(ss); i++ {
es = strings.ReplaceAll(es, "%"+strings.ToUpper(string(HexEncode(string(ss[i])))), "$"+"%"+strings.ToUpper(string(HexEncode(string(ss[i]))))+"$")
}
es = DecodeURIComponent(es)
for i := 0; i < len(ss); i++ {
es = strings.ReplaceAll(es, "$"+string(ss[i])+"$", "%"+strings.ToUpper(string(HexEncode(string(ss[i])))))
}
return es
}

// Base64编码
func Btoa(s interface{}) string {
byte_s := stringAndByte(s)
Expand All @@ -69,7 +112,7 @@ func Base64Encode(s interface{}) string {
func Atob(s interface{}) string {
byte_s := stringAndByte(s)
str, err := base64.StdEncoding.DecodeString(string(byte_s))
if err != nil{
if err != nil {
panic(err)
}
return string(str)
Expand All @@ -80,3 +123,42 @@ func Base64Decode(s interface{}) string {
byte_s := stringAndByte(s)
return Atob(byte_s)
}

// 中文转Unicode
func Escape(s interface{}) string {
byte_s := stringAndByte(s)
str := string(byte_s)
es := ""
for _, s := range str {
switch {
case s >= '0' && s <= '9':
es += string(s)
case s >= 'a' && s <= 'z':
es += string(s)
case s >= 'A' && s <= 'Z':
es += string(s)
case strings.Contains("*+-./@_", string(s)):
es += string(s)
case int(s) <= 127:
es += "%" + strings.ToUpper(string(HexEncode(string(s))))
case int(s) >= 128:
es += strings.ReplaceAll(strings.ReplaceAll(strconv.QuoteToASCII(string(s)), "\"", ""), "\\u", "%u")
}
}
return es
}

// Unicode转中文
func UnEscape(s interface{}) string {
byte_s := stringAndByte(s)
str := string(byte_s)
re, _ := regexp.Compile("(%u)[0-9a-zA-Z]{4}")
str = re.ReplaceAllStringFunc(str, func(st string) string {
bs, _ := hex.DecodeString(strings.ReplaceAll(st, "%u", ""))
r := uint16(0)
binary.Read(bytes.NewReader(bs), binary.BigEndian, &r)
return string(r)
})
str = DecodeURIComponent(str)
return str
}

0 comments on commit 79002e6

Please sign in to comment.