Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing httpResponse for external usage #63

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 10 additions & 43 deletions decorator/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"errors"
"io"
"net/http"
"sync"

Expand Down Expand Up @@ -85,60 +84,28 @@ func executeHttpRequest(r *http.Request) (*http.Response, error) {
return http.DefaultClient.Do(r)
}

type httpResponse struct {
once *sync.Once
r *http.Response
body string
}

func (h *httpResponse) Close() {
if h == nil || h.r == nil || h.r.Body == nil {
return
}

h.r.Body.Close()
h.r.Body = nil
}

func (h *httpResponse) Body() string {
h.once.Do(func() {
b, _ := io.ReadAll(h.r.Body)
h.Close()
h.body = string(b)
})
return h.body
}

func (h *httpResponse) Header(k string) string {
return h.r.Header.Get(k)
}

func (h *httpResponse) Headers(k string) []string {
return h.r.Header.Values(k)
}

func pushHTTPResponse(c *binder.Context, r *http.Response) {
c.Push().Data(
&httpResponse{
once: new(sync.Once),
r: r,
&lua.HttpResponse{
Once: new(sync.Once),
R: r,
},
"http_response",
)
}

func httpStatus(c *binder.Context) error {
resp, ok := c.Arg(1).Data().(*httpResponse)
resp, ok := c.Arg(1).Data().(*lua.HttpResponse)
if !ok {
return ErrResponseExpected
}
c.Push().Number(float64(resp.r.StatusCode))
c.Push().Number(float64(resp.R.StatusCode))

return nil
}

func httpHeaders(c *binder.Context) error {
resp, ok := c.Arg(1).Data().(*httpResponse)
resp, ok := c.Arg(1).Data().(*lua.HttpResponse)
if !ok {
return ErrResponseExpected
}
Expand All @@ -151,7 +118,7 @@ func httpHeaders(c *binder.Context) error {
}

func httpHeaderList(c *binder.Context) error {
resp, ok := c.Arg(1).Data().(*httpResponse)
resp, ok := c.Arg(1).Data().(*lua.HttpResponse)
if !ok {
return ErrResponseExpected
}
Expand All @@ -170,7 +137,7 @@ func httpHeaderList(c *binder.Context) error {
}

func httpBody(c *binder.Context) error {
resp, ok := c.Arg(1).Data().(*httpResponse)
resp, ok := c.Arg(1).Data().(*lua.HttpResponse)
if !ok {
return ErrResponseExpected
}
Expand All @@ -180,15 +147,15 @@ func httpBody(c *binder.Context) error {
}

func httpClose(c *binder.Context) error {
resp, ok := c.Arg(1).Data().(*httpResponse)
resp, ok := c.Arg(1).Data().(*lua.HttpResponse)
if !ok {
return ErrResponseExpected
}
if resp == nil {
return nil
}
resp.Close()
resp.r = nil
resp.R = nil
resp = nil
return nil
}
35 changes: 35 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package lua

import (
"errors"
"io"
"net/http"
"sort"
"strconv"
"sync"

glua "github.com/yuin/gopher-lua"
)
Expand All @@ -16,6 +19,38 @@ type List struct {
Data []interface{}
}

type HttpResponse struct {
Once *sync.Once
R *http.Response
body string
}

func (h *HttpResponse) Close() {
if h == nil || h.R == nil || h.R.Body == nil {
return
}

h.R.Body.Close()
h.R.Body = nil
}

func (h *HttpResponse) Body() string {
h.Once.Do(func() {
b, _ := io.ReadAll(h.R.Body)
h.Close()
h.body = string(b)
})
return h.body
}

func (h *HttpResponse) Header(k string) string {
return h.R.Header.Get(k)
}

func (h *HttpResponse) Headers(k string) []string {
return h.R.Header.Values(k)
}

type NativeValue = glua.LValue
type NativeNumber = glua.LNumber
type NativeString = glua.LString
Expand Down
Loading