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

network proto: revert full buffer size read #4497

Merged
merged 2 commits into from
Dec 17, 2023
Merged
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
29 changes: 27 additions & 2 deletions pkg/protocols/network/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"net/url"
"os"
"strings"
"time"

Expand Down Expand Up @@ -265,7 +266,7 @@ func (request *Request) executeRequestWithPayloads(variables map[string]interfac
}

if input.Read > 0 {
buffer, err := reader.ConnReadNWithTimeout(conn, int64(input.Read), DefaultReadTimeout)
buffer, err := ConnReadNWithTimeout(conn, int64(input.Read), DefaultReadTimeout)
if err != nil {
return errorutil.NewWithErr(err).Msgf("could not read response from connection")
}
Expand Down Expand Up @@ -315,7 +316,7 @@ func (request *Request) executeRequestWithPayloads(variables map[string]interfac
bufferSize = -1
}

final, err := reader.ConnReadNWithTimeout(conn, int64(bufferSize), DefaultReadTimeout)
final, err := ConnReadNWithTimeout(conn, int64(bufferSize), DefaultReadTimeout)
if err != nil {
request.options.Output.Request(request.options.TemplatePath, address, request.Type().String(), err)
return errors.Wrap(err, "could not read from server")
Expand Down Expand Up @@ -412,3 +413,27 @@ func getAddress(toTest string) (string, error) {
}
return toTest, nil
}

func ConnReadNWithTimeout(conn net.Conn, n int64, timeout time.Duration) ([]byte, error) {
if timeout == 0 {
timeout = DefaultReadTimeout
}
if n == -1 {
// if n is -1 then read all available data from connection
return reader.ConnReadNWithTimeout(conn, -1, timeout)
} else if n == 0 {
n = 4096 // default buffer size
}
b := make([]byte, n)
_ = conn.SetDeadline(time.Now().Add(timeout))
count, err := conn.Read(b)
_ = conn.SetDeadline(time.Time{})
if err != nil && os.IsTimeout(err) && count > 0 {
// in case of timeout with some value read, return the value
return b[:count], nil
}
if err != nil {
return nil, err
}
return b[:count], nil
}
Loading