From 0ed968c841abc9a1914af5fb52dd7c91e38c9131 Mon Sep 17 00:00:00 2001 From: thedae Date: Wed, 15 Jan 2025 12:41:21 +0100 Subject: [PATCH] Exposing httpResponse for external usage --- decorator/http.go | 53 +++++++++-------------------------------------- types.go | 35 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/decorator/http.go b/decorator/http.go index 9013bf0..049cbc3 100644 --- a/decorator/http.go +++ b/decorator/http.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "errors" - "io" "net/http" "sync" @@ -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 } @@ -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 } @@ -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 } @@ -180,7 +147,7 @@ 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 } @@ -188,7 +155,7 @@ func httpClose(c *binder.Context) error { return nil } resp.Close() - resp.r = nil + resp.R = nil resp = nil return nil } diff --git a/types.go b/types.go index 94f8ddc..9606594 100644 --- a/types.go +++ b/types.go @@ -2,8 +2,11 @@ package lua import ( "errors" + "io" + "net/http" "sort" "strconv" + "sync" glua "github.com/yuin/gopher-lua" ) @@ -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