From 3f1973b621ad10e783955244d0d9f39176feda45 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Mon, 30 Oct 2023 13:10:42 -0600 Subject: set request body in spartan.ParseRequest --- spartan/request.go | 22 +--------------------- spartan/serve.go | 8 +------- 2 files changed, 2 insertions(+), 28 deletions(-) (limited to 'spartan') diff --git a/spartan/request.go b/spartan/request.go index c88a20b..36dd225 100644 --- a/spartan/request.go +++ b/spartan/request.go @@ -2,7 +2,6 @@ package spartan import ( "bufio" - "bytes" "errors" "io" "net/url" @@ -59,6 +58,7 @@ func ParseRequest(rdr io.Reader) (*types.Request, int, error) { Path: path, RawPath: path, }, + Meta: &io.LimitedReader{R: bufrdr, N: int64(contentlen)}, }, contentlen, nil } @@ -81,23 +81,3 @@ func GetRequestBody(request *types.Request) io.Reader { } return nil } - -// SetRequestBody adds an io.Reader as a request body. -// -// It is for use in clients, preparing the request to be sent. -// -// This function will read the entire contents into memory unless -// the reader is already an *io.LimitedReader. -func SetRequestBody(request *types.Request, body io.Reader) error { - if rdr, ok := body.(*io.LimitedReader); ok { - request.Meta = rdr - return nil - } - - buf, err := io.ReadAll(body) - if err != nil { - return err - } - request.Meta = io.LimitReader(bytes.NewBuffer(buf), int64(len(buf))) - return nil -} diff --git a/spartan/serve.go b/spartan/serve.go index 0fb4939..f2155ec 100644 --- a/spartan/serve.go +++ b/spartan/serve.go @@ -47,19 +47,13 @@ func (ss *spartanServer) handleConn(conn net.Conn) { buf := bufio.NewReader(conn) var response *types.Response - request, clen, err := ParseRequest(buf) + request, _, err := ParseRequest(buf) if err != nil { response = ClientError(err) } else { request.Server = ss request.RemoteAddr = conn.RemoteAddr() - var body io.Reader = nil - if clen > 0 { - body = io.LimitReader(buf, int64(clen)) - } - request.Meta = body - defer func() { if r := recover(); r != nil { err := fmt.Errorf("%s", r) -- cgit v1.2.3