-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurllib.go
78 lines (66 loc) · 1.55 KB
/
urllib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package urllib
import (
"fmt";
"sort";
"utf8";
"strings";
"bytes";
)
var always_safe = strings.Bytes("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-");
func urlquoter(c int, safe string) []byte {
safe_bytes := strings.Bytes(safe);
c_bytes := make([]byte, utf8.RuneLen(c));
utf8.EncodeRune(c, c_bytes);
if bytes.Index(safe_bytes, c_bytes) != -1 || bytes.Index(always_safe, c_bytes) != -1 {
return c_bytes;
}
else {
return strings.Bytes(fmt.Sprintf("%%%02X", c));
}
panic("unreachable");
}
func Urlquote_safe(s string, safe string) string {
var qs = bytes.NewBufferString("");
for _, c := range s {
qs.Write(urlquoter(c,safe));
}
return qs.String();
}
func Urlquote(s string) string {
return Urlquote_safe(s, "");
}
func SpaceToPlus(c int) int {
if c == ' ' { return '+'; }
else { return c; }
panic("unreachable");
}
func Urlquote_plus_safe(s string, safe string) string {
if strings.Index(s, " ") != -1 {
s = Urlquote_safe(s, safe+" ");
return strings.Map(SpaceToPlus,s);
}
else {
return Urlquote_safe(s, safe);
}
panic("unreachable");
}
func Urlquote_plus(s string) string {
return Urlquote_plus_safe(s, "");
}
func Urlencode(params map[string]string) string {
keys := make([]string, len(params));
i := 0;
for k,_ := range params {
keys[i] = k;
i = i + 1;
}
sort.SortStrings(keys);
sparamss := make([]string,len(params));
j := 0;
for k := range keys {
sparamss[j] = Urlquote_plus(keys[k]) + "=" + Urlquote_plus(params[keys[k]]);
j = j + 1;
}
sparams := strings.Join(sparamss, "&");
return sparams;
}