Skip to content

Commit

Permalink
Use length in header when receiving response data
Browse files Browse the repository at this point in the history
  • Loading branch information
vbatoufflet committed Feb 26, 2018
1 parent 8a8d040 commit 96db018
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 0 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"net"
)

const bufferSize = 1024

// Client represents a Livestatus client instance.
type Client struct {
network string
Expand Down
13 changes: 9 additions & 4 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,17 @@ func (q Query) handle(conn net.Conn) (*Response, error) {
return nil, err
}

length, err := strconv.Atoi(string(bytes.TrimSpace(data[5:15])))
if err != nil {
return nil, err
}
remainder := length

// Receive response data
buf := bytes.NewBuffer(nil)

for {
data = make([]byte, bufferSize)
data = make([]byte, remainder)

n, err := conn.Read(data)
if err == io.EOF {
Expand All @@ -164,9 +170,8 @@ func (q Query) handle(conn net.Conn) (*Response, error) {

buf.Write(bytes.TrimRight(data, "\x00"))

// New line signals the end of content. This check helps
// if the connection is not forcibly closed
if n != bufferSize && data[n-1] == byte('\n') {
remainder -= n
if remainder <= 0 {
break
}
}
Expand Down

0 comments on commit 96db018

Please sign in to comment.