Skip to content

Commit

Permalink
improve handleRequestBody
Browse files Browse the repository at this point in the history
Benchmarks:
```
Old:
Benchmark_parseRequestBody_string-16             2199196               550.3 ns/op           128 B/op          3 allocs/op
Benchmark_parseRequestBody_byte-16               2264421               532.9 ns/op           128 B/op          3 allocs/op
Benchmark_parseRequestBody_reader-16             8307141               141.8 ns/op            16 B/op          1 allocs/op
Benchmark_parseRequestBody_struct-16              931632              1317 ns/op             156 B/op          5 allocs/op
Benchmark_parseRequestBody_struct_xml-16          409074              2921 ns/op            4765 B/op         13 allocs/op
Benchmark_parseRequestBody_map-16                 566750              2158 ns/op             570 B/op         15 allocs/op
Benchmark_parseRequestBody_slice-16               957828              1279 ns/op             146 B/op          4 allocs/op

New:
Benchmark_parseRequestBody_string-16             5084247               237.0 ns/op            16 B/op          1 allocs/op
Benchmark_parseRequestBody_byte-16               5298362               218.0 ns/op            16 B/op          1 allocs/op
Benchmark_parseRequestBody_reader-16             8402954               141.3 ns/op            16 B/op          1 allocs/op
Benchmark_parseRequestBody_struct-16             1000000              1066 ns/op              42 B/op          3 allocs/op
Benchmark_parseRequestBody_struct_xml-16          452389              2575 ns/op            4648 B/op         11 allocs/op
Benchmark_parseRequestBody_map-16                 620391              1913 ns/op             457 B/op         13 allocs/op
Benchmark_parseRequestBody_slice-16              1207551              1203 ns/op              32 B/op          2 allocs/op
```
  • Loading branch information
SVilgelm committed Sep 29, 2023
1 parent fe95c1f commit 09b19dd
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,45 +449,43 @@ func handleContentType(c *Client, r *Request) {
}
}

func handleRequestBody(c *Client, r *Request) (err error) {
func handleRequestBody(c *Client, r *Request) error {
var bodyBytes []byte
contentType := r.Header.Get(hdrContentTypeKey)
kind := kindOf(r.Body)
releaseBuffer(r.bodyBuf)
r.bodyBuf = nil

if reader, ok := r.Body.(io.Reader); ok {
switch body := r.Body.(type) {
case io.Reader:
if c.setContentLength || r.setContentLength { // keep backward compatibility
r.bodyBuf = acquireBuffer()
_, err = r.bodyBuf.ReadFrom(reader)
if _, err := r.bodyBuf.ReadFrom(body); err != nil {
return err
}
r.Body = nil
} else {
// Otherwise buffer less processing for `io.Reader`, sounds good.
return
}
} else if b, ok := r.Body.([]byte); ok {
bodyBytes = b
} else if s, ok := r.Body.(string); ok {
bodyBytes = []byte(s)
} else if IsJSONType(contentType) &&
(kind == reflect.Struct || kind == reflect.Map || kind == reflect.Slice) {
r.bodyBuf, err = jsonMarshal(c, r, r.Body)
if err != nil {
return
return nil
}
case []byte:
bodyBytes = body
case string:
bodyBytes = []byte(body)
default:
contentType := r.Header.Get(hdrContentTypeKey)
kind := kindOf(r.Body)
var err error
if IsJSONType(contentType) && (kind == reflect.Struct || kind == reflect.Map || kind == reflect.Slice) {
r.bodyBuf, err = jsonMarshal(c, r, r.Body)
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
bodyBytes, err = c.XMLMarshal(r.Body)
}
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
bodyBytes, err = c.XMLMarshal(r.Body)
if err != nil {
return
return err
}
}

if bodyBytes == nil && r.bodyBuf == nil {
err = errors.New("unsupported 'Body' type/value")
}

// if any errors during body bytes handling, return it
if err != nil {
return
return errors.New("unsupported 'Body' type/value")
}

// []byte into Buffer
Expand All @@ -496,7 +494,7 @@ func handleRequestBody(c *Client, r *Request) (err error) {
_, _ = r.bodyBuf.Write(bodyBytes)
}

return
return nil
}

func saveResponseIntoFile(c *Client, res *Response) error {
Expand Down

0 comments on commit 09b19dd

Please sign in to comment.