include RawLines in Response

This commit is contained in:
Leif Ryge 2016-08-19 20:41:16 +00:00
parent a7a9158e85
commit 332fc001df
1 changed files with 15 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import (
"log"
"net/textproto"
"strconv"
"strings"
)
// Response is a response to a control port command, or an asyncrhonous event.
@ -27,6 +28,9 @@ type Response struct {
// data is "decoded" and presented as a single string (terminal ".CRLF"
// removed, all intervening CRs stripped).
Data []string
// RawLines is all of the lines of a response, without CRLFs.
RawLines []string
}
// IsOk returns true if the response status code indicates success or
@ -77,11 +81,15 @@ func (c *Conn) ReadResponse() (*Response, error) {
// lines.
return nil, newProtocolError("status code changed: %03d != %03d", code, statusCode)
}
if resp.RawLines == nil {
resp.RawLines = make([]string, 0, 1)
}
if line[3] == ' ' {
// Final line in the response.
resp.Reply = line[4:]
resp.Err = statusCodeToError(statusCode, resp.Reply)
resp.RawLines = append(resp.RawLines, line)
return resp, nil
}
@ -92,9 +100,11 @@ func (c *Conn) ReadResponse() (*Response, error) {
case '-':
// Continuation, keep reading.
resp.Data = append(resp.Data, line[4:])
resp.RawLines = append(resp.RawLines, line)
case '+':
// A "dot-encoded" payload follows.
resp.Data = append(resp.Data, line[4:])
resp.RawLines = append(resp.RawLines, line)
dotBody, err := c.conn.ReadDotBytes()
if err != nil {
return nil, err
@ -103,6 +113,11 @@ func (c *Conn) ReadResponse() (*Response, error) {
log.Printf("S: [dot encoded data]")
}
resp.Data = append(resp.Data, string(dotBody))
dotLines := strings.Split(string(dotBody), "\n")
for _, dotLine := range dotLines[:len(dotLines)-1] {
resp.RawLines = append(resp.RawLines, dotLine)
}
resp.RawLines = append(resp.RawLines, ".")
default:
return nil, newProtocolError("invalid separator: '%c'", line[3])
}