Skip to content

Commit

Permalink
更改部分lib_http接口
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Sep 30, 2022
1 parent d0973e9 commit 5296f13
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"type": "go",
"request": "launch",
"mode": "debug",
"program": "main.go",
"args": ["-f", "test/re.lk"]
"program": ".",
"args": ["-f", "test/basic.lk"]
}
]
}
15 changes: 9 additions & 6 deletions state/auxlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ func (self *luaState) ToString2(idx int) string {
}
case LUA_TNIL:
self.PushString("nil")
case LUA_TTABLE:
tb, ok := self.ToPointer(idx).(*luaTable)
if ok {
s, err := tb.String()
if err == nil {
self.PushString(s)
}
}
default:
tt := self.GetMetafield(idx, "__name") /* try name */
var kind string
Expand All @@ -223,12 +231,7 @@ func (self *luaState) ToString2(idx int) string {
kind = self.TypeName2(idx)
}

tb, err := json.MarshalToString(self.ToPointer(idx))
if err != nil {
self.PushString(fmt.Sprintf("%s: %p", kind, self.ToPointer(idx)))
} else {
self.PushString(fmt.Sprintf("%s: %s", kind, tb))
}
self.PushString(fmt.Sprintf("%s: %v", kind, self.ToPointer(idx)))

if tt != LUA_TNIL {
self.Remove(-2) /* remove '__name' */
Expand Down
8 changes: 8 additions & 0 deletions state/lua_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ type luaTable struct {
changed bool // used by next()
}

func (self *luaTable) String() (string, error) {
m := map[string]any{
"arr": self.arr,
"map": self._map,
}
return json.MarshalToString(m)
}

func newLuaTable(nArr, nRec int) *luaTable {
t := &luaTable{}
if nArr > 0 {
Expand Down
4 changes: 2 additions & 2 deletions stdlib/lib_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func baseType(ls LkState) int {
return 1
}

// tostring (v)
// str (v)
// http://www.lua.org/manual/5.3/manual.html#pdf-tostring
// lua-5.3.4/src/lbaselib.c#luaB_tostring()
func baseToString(ls LkState) int {
Expand All @@ -395,7 +395,7 @@ func baseToString(ls LkState) int {
return 1
}

// tonumber (e [, base])
// num (e [, base])
// http://www.lua.org/manual/5.3/manual.html#pdf-tonumber
// lua-5.3.4/src/lbaselib.c#luaB_tonumber()
func baseToNumber(ls LkState) int {
Expand Down
57 changes: 37 additions & 20 deletions stdlib/lib_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,27 @@ func OpenHttpLib(ls LkState) int {
return 1
}

func httpDo(method, url string, headers luaMap, body io.Reader) (int, string, error) {
func genHeaderMap(h *http.Header) luaMap {
m := luaMap{}
for k := range *h {
v := strings.Join((*h)[k], ";")
m[k] = v
}
return m
}

func genReturn(code int, body string, header *http.Header) luaMap {
return luaMap{
"code": code,
"body": body,
"headers": genHeaderMap(header),
}
}

func httpDo(method, url string, headers luaMap, body io.Reader) (int, string, http.Header, error) {
request, err := http.NewRequest(method, url, body)
if err != nil {
return 0, "", err
return 0, "", nil, err
}

request.Header.Set("user-agent", "lk-http/"+consts.VERSION)
Expand All @@ -43,27 +60,27 @@ func httpDo(method, url string, headers luaMap, body io.Reader) (int, string, er

resp, err := client.Do(request)
if err != nil {
return 0, "", err
return 0, "", nil, err
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, "", err
return 0, "", nil, err
}
defer resp.Body.Close()
return resp.StatusCode, string(respBody), nil
return resp.StatusCode, string(respBody), resp.Header, nil
}

func httpGet(ls LkState) int {
url := ls.CheckString(1)
headers := OptTable(ls, 2, luaMap{})
code, data, err := httpDo("GET", url, headers, nil)
code, data, respHeader, err := httpDo("GET", url, headers, nil)
if err != nil {
ls.PushInteger(0)
ls.PushNil()
ls.PushString(err.Error())
return 2
}
ls.PushInteger(int64(code))
ls.PushString(data)
pushTable(ls, genReturn(code, data, &respHeader))
ls.PushNil()
return 2
}

Expand All @@ -79,14 +96,14 @@ func httpPost(ls LkState) int {
return nil
}()

code, data, err := httpDo("POST", url, headers, body)
code, data, respHeader, err := httpDo("POST", url, headers, body)
if err != nil {
ls.PushInteger(0)
ls.PushNil()
ls.PushString(err.Error())
return 2
}
ls.PushInteger(int64(code))
ls.PushString(data)
pushTable(ls, genReturn(code, data, &respHeader))
ls.PushNil()
return 2
}

Expand All @@ -105,27 +122,27 @@ func httpReq(ls LkState) int {
return nil
}()

code, data, err := httpDo(method, url, headers, body)
code, data, respHeader, err := httpDo(method, url, headers, body)
if err != nil {
ls.PushInteger(0)
ls.PushNil()
ls.PushString(err.Error())
return 2
}

ls.PushInteger(int64(code))
pushTable(ls, genReturn(code, data, &respHeader))
ls.PushString(data)
return 2
}




func genReqTable(r *http.Request) (luaMap, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
headers := luaMap{}
for k := range r.Header {
headers[k] = r.Header[k]
}
headers := genHeaderMap(&r.Header)
return luaMap{
"method": r.Method,
"url": r.URL.String(),
Expand Down
2 changes: 2 additions & 0 deletions stdlib/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ func pushValue(ls LkState, item any) {
ls.PushString(i)
case int64:
ls.PushInteger(i)
case int:
ls.PushInteger(int64(i))
case float64:
ls.PushNumber(i)
case bool:
Expand Down
3 changes: 2 additions & 1 deletion test/basic.lk
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ if #long >= 0 and '' {
}

print(6 ~/ 2, 6 & 2, 6 / 2)
print({'a'} .. '', 1 .. 2, _VERSION, math.pi)
print({'a'} .. '', 1 .. 2, _VERSION, math.pi)
print(str({5, 'd': false}))
6 changes: 1 addition & 5 deletions test/http_listen.lk
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ shy headers = {}
headers.__str = fn(a) {
shy s = ''
for k, v in a {
shy ss = ''
for _, vv in v {
ss = ss .. vv .. ';'
}
s = s .. k .. ': ' .. ss .. '\n'
s = s .. k .. ': ' .. v .. '\n'
}
rt s
}
Expand Down
14 changes: 10 additions & 4 deletions test/http_req.lk
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
shy _, resp = http.post(
shy resp, err = http.post(
'http://httpbin.org/post',
{'accept': 'application/json'},
'{"foo": "bar"}'
)
print(resp)
if err == nil {
for k, v in resp.headers {
print(k, v)
}
}

shy code, resp = http.req(
shy resp, _ = http.req(
'delete',
'http://httpbin.org/delete',
{'accept': 'application/json'},
'{"foo": "bar"}'
)
print(code, json.get(resp, 'json.foo'))
if err == nil {
print(resp.code, json.get(resp.body, 'json.foo'))
}

0 comments on commit 5296f13

Please sign in to comment.