Skip to content

Commit

Permalink
feat: Plugin http module supports head requests
Browse files Browse the repository at this point in the history
  • Loading branch information
aooohan committed Feb 1, 2024
1 parent d8300ba commit 2a4fc77
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ $ brew tap version-fox/tap
$ brew install vfox
```

Hook VersionFox into your shell (pick one that works for your shell)
⚠️ **_Hook VersionFox into your shell_ (pick one that works for your shell)** ⚠️

```bash
echo 'eval "$(vfox activate bash)"' >> ~/.bashrc
echo 'eval "$(vfox activate zsh)"' >> ~/.zshrc
Expand Down
77 changes: 61 additions & 16 deletions internal/module/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
)

type Module struct {
proxy *config.Proxy
proxy *config.Proxy
client *http.Client
}

// Get performs a http get request
Expand All @@ -52,18 +53,6 @@ func (m *Module) Get(L *lua.LState) int {
L.Push(lua.LString("url is required"))
}

client := &http.Client{}
if m.proxy.Enable {
uri, err := url.Parse(m.proxy.Url)
if err == nil {
transPort := &http.Transport{
Proxy: http.ProxyURL(uri),
}
client = &http.Client{
Transport: transPort,
}
}
}
req, err := http.NewRequest("GET", urlStr.String(), nil)
if err != nil {
L.Push(lua.LNil)
Expand All @@ -79,7 +68,7 @@ func (m *Module) Get(L *lua.LState) int {
})
}
}
resp, err := client.Do(req)
resp, err := m.client.Do(req)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
Expand All @@ -102,19 +91,75 @@ func (m *Module) Get(L *lua.LState) int {
L.SetField(result, "body", lua.LString(body))
L.SetField(result, "status_code", lua.LNumber(resp.StatusCode))
L.SetField(result, "headers", headers)
L.SetField(result, "content_length", lua.LNumber(resp.ContentLength))
L.Push(result)
return 1
}

func (m *Module) Head(L *lua.LState) int {
param := L.CheckTable(1)
urlStr := param.RawGetString("url")
if urlStr == lua.LNil {
L.Push(lua.LNil)
L.Push(lua.LString("url is required"))
}

req, err := http.NewRequest("HEAD", urlStr.String(), nil)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
headersTable := param.RawGetString("headers")
if headersTable != lua.LNil {
if table, ok := headersTable.(*lua.LTable); ok {
table.ForEach(func(key lua.LValue, value lua.LValue) {
req.Header.Add(key.String(), value.String())
})
}
}
resp, err := m.client.Do(req)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
headers := L.NewTable()
for k, v := range resp.Header {
if len(v) > 0 {
headers.RawSetString(k, lua.LString(v[0]))
}
}
result := L.NewTable()
L.SetField(result, "status_code", lua.LNumber(resp.StatusCode))
L.SetField(result, "headers", headers)
L.SetField(result, "content_length", lua.LNumber(resp.ContentLength))
L.Push(result)
return 1
}

func (m *Module) luaMap() map[string]lua.LGFunction {
return map[string]lua.LGFunction{
"get": m.Get,
"get": m.Get,
"head": m.Head,
}
}

func NewModule(proxy *config.Proxy) lua.LGFunction {
return func(L *lua.LState) int {
m := &Module{proxy: proxy}
client := &http.Client{}
if proxy.Enable {
uri, err := url.Parse(proxy.Url)
if err == nil {
transPort := &http.Transport{
Proxy: http.ProxyURL(uri),
}
client = &http.Client{
Transport: transPort,
}
}
}
m := &Module{proxy: proxy, client: client}
t := L.NewTable()
L.SetFuncs(t, m.luaMap())
L.Push(t)
Expand Down
15 changes: 15 additions & 0 deletions internal/module/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ func TestGetRequest(t *testing.T) {
eval(str, t)
}

func TestHeadRequest(t *testing.T) {
const str = `
local http = require("http")
assert(type(http) == "table")
assert(type(http.get) == "function")
local resp, err = http.head({
url = "http://ip.jsontest.com/"
})
assert(err == nil)
assert(resp.status_code == 200)
assert(resp.content_length ~= 0)
`
eval(str, t)
}

func eval(str string, t *testing.T) {
s := lua.NewState()
defer s.Close()
Expand Down

0 comments on commit 2a4fc77

Please sign in to comment.