forked from cjoudrey/gluahttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
responsetype.go
145 lines (131 loc) · 3.85 KB
/
responsetype.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package gluahttp
import (
"io/ioutil"
"net/http"
"strings"
"github.com/yuin/gopher-lua"
)
func getResp(L *lua.LState, resp *http.Response) *lua.LTable {
luaResp := makeResp(L, resp)
luaResp.RawSetString("history", getHistory(L, resp))
return luaResp
}
func makeResp(L *lua.LState, resp *http.Response) *lua.LTable {
luaResp := L.NewTable()
if resp != nil {
luaResp.RawSetString("status_code", lua.LNumber(resp.StatusCode))
body := getRespBody(resp)
luaResp.RawSetString("body", lua.LString(body))
luaResp.RawSetString("body_size", lua.LNumber(len(body)))
luaResp.RawSetString("headers", getHeaders(L, resp.Header))
luaResp.RawSetString("raw_headers", rawHeaders(resp.Header))
luaResp.RawSetString("cookies", getCookies(L, resp.Cookies()))
luaResp.RawSetString("raw_cookies", rawCookies(resp.Cookies()))
luaResp.RawSetString("proto", lua.LString(resp.Proto))
luaResp.RawSetString("url", lua.LString(resp.Request.URL.String()))
luaResp.RawSetString("request", makeReq(L, resp.Request))
}
return luaResp
}
func makeReq(L *lua.LState, req *http.Request) *lua.LTable {
luaReq := L.NewTable()
if req != nil {
luaReq.RawSetString("method", lua.LString(req.Method))
luaReq.RawSetString("url", lua.LString(req.URL.String()))
luaReq.RawSetString("scheme", lua.LString(req.URL.Scheme))
luaReq.RawSetString("proto", lua.LString(req.Proto))
luaReq.RawSetString("host", getHost(req))
luaReq.RawSetString("body", lua.LString(getReqBody(req)))
luaReq.RawSetString("headers", getHeaders(L, req.Header))
luaReq.RawSetString("raw_headers", rawHeaders(req.Header))
luaReq.RawSetString("cookies", getCookies(L, req.Cookies()))
luaReq.RawSetString("raw_cookies", rawCookies(req.Cookies()))
luaReq.RawSetString("raw", rawRequest(req))
}
return luaReq
}
func getHistory(L *lua.LState, resp *http.Response) *lua.LTable {
history := L.NewTable()
subResp := resp.Request.Response
for {
if subResp != nil {
history.Insert(1, makeResp(L, subResp))
subResp = subResp.Request.Response
} else {
break
}
}
return history
}
func getHost(req *http.Request) lua.LString {
if req.Host != "" {
return lua.LString(req.Host)
}
return lua.LString(req.URL.Host)
}
func rawHeaders(headers http.Header) lua.LString {
var rawHeader string
for name, v := range headers {
for _, vaule := range v {
rawHeader += name + ": " + vaule + "\r\n"
}
}
return lua.LString(strings.TrimSuffix(rawHeader, "\r\n"))
}
func getHeaders(L *lua.LState, headers http.Header) *lua.LTable {
table := L.NewTable()
for k, v := range headers {
var each string
for _, header := range v {
each += header + ","
}
table.RawSetString(k, lua.LString(strings.TrimSuffix(each, ",")))
}
return table
}
func rawCookies(cookies []*http.Cookie) lua.LString {
var rawCookie string
for _, cookie := range cookies {
rawCookie += cookie.Name + "=" + cookie.Value + ";"
}
return lua.LString(strings.TrimSuffix(rawCookie, ";"))
}
func getCookies(L *lua.LState, cookies []*http.Cookie) *lua.LTable {
table := L.NewTable()
for _, cookie := range cookies {
table.RawSetString(cookie.Name, lua.LString(cookie.Value))
}
return table
}
func getRespBody(resp *http.Response) string {
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
return string(body)
}
func getReqBody(req *http.Request) string {
var body string
if req.GetBody != nil {
b, err := req.GetBody()
if err == nil {
buf, err := ioutil.ReadAll(b)
b.Close()
if err == nil {
body = string(buf)
}
}
}
return body
}
func rawRequest(req *http.Request) lua.LString {
rawRequest := req.Method + " " + req.URL.RequestURI() + " " + req.Proto + "\r\n"
host := req.Host
if host == "" {
host = req.URL.Host
}
rawRequest += "Host: " + host + "\r\n"
for key, val := range req.Header {
rawRequest += key + ": " + val[0] + "\r\n"
}
rawRequest += "\r\n" + getReqBody(req)
return lua.LString(rawRequest)
}