-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_url.go
104 lines (87 loc) · 1.81 KB
/
http_url.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package purr
import (
"net/url"
lua "github.com/yuin/gopher-lua"
)
type purrURL struct {
*url.URL
values url.Values
}
var purrURLMethods = map[string]lua.LGFunction{
"query_add": queryAdd,
"query_del": queryDel,
"query_set": querySet,
"query_get": queryGet,
"encode": urlEncode,
}
const purrURLTypeName = "url"
func constructURLUD(L *lua.LState, url *purrURL) *lua.LUserData {
ud := L.NewUserData()
ud.Value = url
L.SetMetatable(ud, L.GetTypeMetatable(purrURLTypeName))
return ud
}
func checkURL(L *lua.LState) *purrURL {
ud := L.CheckUserData(1)
if v, ok := ud.Value.(*purrURL); ok {
return v
}
L.ArgError(1, purrURLTypeName+" expected")
return nil
}
//static methods
func newURL(L *lua.LState) int {
path := L.CheckString(1)
pu := new(purrURL)
pu.values = url.Values{}
pu.URL = new(url.URL)
pu.Path = path
ud := constructURLUD(L, pu)
L.Push(ud)
return 1
}
func parseRawURL(L *lua.LState) int {
urlStr := L.CheckString(1)
rawURL, err := url.Parse(urlStr)
if err != nil {
L.ArgError(1, err.Error())
return 0
}
pu := purrURL{URL: rawURL, values: url.Values{}}
ud := constructURLUD(L, &pu)
L.Push(ud)
return 1
}
func queryAdd(L *lua.LState) int {
pu := checkURL(L)
key := L.CheckString(2)
value := L.CheckString(3)
pu.values.Add(key, value)
return 0
}
func queryDel(L *lua.LState) int {
pu := checkURL(L)
key := L.CheckString(2)
pu.values.Del(key)
return 0
}
func queryGet(L *lua.LState) int {
pu := checkURL(L)
key := L.CheckString(2)
value := pu.values.Get(key)
L.Push(lua.LString(value))
return 1
}
func querySet(L *lua.LState) int {
pu := checkURL(L)
key := L.CheckString(2)
value := L.CheckString(3)
pu.values.Set(key, value)
return 0
}
func urlEncode(L *lua.LState) int {
pu := checkURL(L)
pu.RawQuery = pu.values.Encode()
L.Push(lua.LString(pu.String()))
return 1
}