URL escape and unescape

本文探讨了Golang中net/url包的使用方法,特别是URL字符串的转义与反转义过程。通过具体示例代码,展示了如何正确处理URL中的特殊字符。

Recently, I study the package  net/url of Golang.

I was puzzled about the escape and unescape of url string.

then I find a clear and accurate answer at: http://www.sislands.com/coin70/week6/encoder.htm

 

the code in golang:

 1 // unescape unescapes a string; the mode specifies
 2 // which section of the URL string is being unescaped.
 3 func unescape(s string, mode encoding) (string, error) {
 4     // Count %, check that they're well-formed.
 5     n := 0
 6     hasPlus := false
 7     for i := 0; i < len(s); {
 8         switch s[i] {
 9         case '%':
10             n++
11             if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
12                 s = s[i:]
13                 if len(s) > 3 {
14                     s = s[0:3]
15                 }
16                 return "", EscapeError(s)
17             }
18             i += 3
19         case '+':
20             hasPlus = mode == encodeQueryComponent
21             i++
22         default:
23             i++
24         }
25     }
26 
27     if n == 0 && !hasPlus {
28         return s, nil
29     }
30 
31     t := make([]byte, len(s)-2*n)
32     j := 0
33     for i := 0; i < len(s); {
34         switch s[i] {
35         case '%':
36             t[j] = unhex(s[i+1])<<4 | unhex(s[i+2])
37             j++
38             i += 3
39         case '+':
40             if mode == encodeQueryComponent {
41                 t[j] = ' '
42             } else {
43                 t[j] = '+'
44             }
45             j++
46             i++
47         default:
48             t[j] = s[i]
49             j++
50             i++
51         }
52     }
53     return string(t), nil
54 }

 

 

After a period of home work,  Now I am confident about implimenting the project: gocrawel by myself.

转载于:https://www.cnblogs.com/harrysun/p/3933843.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值