summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-10-30 13:10:42 -0600
committertjpcc <tjp@ctrl-c.club>2023-10-30 13:10:42 -0600
commit3f1973b621ad10e783955244d0d9f39176feda45 (patch)
tree85ef65aa41db4ff6d5ca6585d8147f9e44796047
parent87189fafa7134f70dcdd51f1d508686573a91979 (diff)
set request body in spartan.ParseRequest
-rw-r--r--spartan/request.go22
-rw-r--r--spartan/serve.go8
2 files changed, 2 insertions, 28 deletions
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)