| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "net/textproto" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // Response is a response to a control port command or an asynchronous event. |
| 10 | type Response struct { |
| 11 | // Err is the status code and string representation associated with a |
| 12 | // response. Responses that have completed successfully will also have Err |
| 13 | // set to indicate such. |
| 14 | Err *textproto.Error |
| 15 | |
| 16 | // Reply is the text on the EndReplyLine of the response. |
| 17 | Reply string |
| 18 | |
| 19 | // Data is the MidReplyLines/DataReplyLines of the response. Dot encoded |
| 20 | // data is "decoded" and presented as a single string (terminal ".CRLF" |
| 21 | // removed, all intervening CRs stripped). |
| 22 | Data []string |
| 23 | |
| 24 | // RawLines is all of the lines of a response, without CRLFs. |
| 25 | RawLines []string |
| 26 | } |
| 27 | |
| 28 | // IsOk returns true if the response status code indicates success or an |
| 29 | // asynchronous event. |
| 30 | func (r *Response) IsOk() bool { |
| 31 | switch r.Err.Code { |
| 32 | case StatusOk, StatusOkUnnecessary, StatusAsyncEvent: |
| 33 | return true |
| 34 | default: |
| 35 | return false |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // DataWithReply returns a combination of Data and Reply to give a full set of |
| 40 | // the lines of the response. |
| 41 | func (r *Response) DataWithReply() []string { |
| 42 | ret := make([]string, len(r.Data)+1) |
| 43 | copy(ret, r.Data) |
| 44 | ret[len(ret)-1] = r.Reply |
| 45 | return ret |
| 46 | } |
| 47 | |
| 48 | // IsAsync returns true if the response is an asynchronous event. |
| 49 | func (r *Response) IsAsync() bool { |
| 50 | return r.Err.Code == StatusAsyncEvent |
| 51 | } |
| 52 | |
| 53 | // ReadResponse returns the next response object. |
| 54 | func (c *Conn) ReadResponse() (*Response, error) { |
| 55 | var resp *Response |
| 56 | var statusCode int |
| 57 | for { |
| 58 | line, err := c.conn.ReadLine() |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | c.debugf("Read line: %v", line) |
| 63 | |
| 64 | // Parse the line that was just read. |
| 65 | if len(line) < 4 { |
| 66 | return nil, c.protoErr("Truncated response: %v", line) |
| 67 | } |
| 68 | if code, err := strconv.Atoi(line[0:3]); err != nil || code < 100 { |
| 69 | return nil, c.protoErr("Invalid status code: %v", line[0:3]) |
| 70 | } else if resp == nil { |
| 71 | resp = &Response{} |
| 72 | statusCode = code |
| 73 | } else if code != statusCode { |
| 74 | // The status code should stay fixed for all lines of the response, since events can't be interleaved with |
| 75 | // response lines. |
| 76 | return nil, c.protoErr("Status code changed: %v != %v", code, statusCode) |
| 77 | } |
| 78 | resp.RawLines = append(resp.RawLines, line) |
| 79 | switch line[3] { |
| 80 | case ' ': |
| 81 | // Final line in the response. |
| 82 | resp.Reply = line[4:] |
| 83 | resp.Err = statusCodeToError(statusCode, resp.Reply) |
| 84 | return resp, nil |
| 85 | case '-': |
| 86 | // Continuation, keep reading. |
| 87 | resp.Data = append(resp.Data, line[4:]) |
| 88 | case '+': |
| 89 | // A "dot-encoded" payload follows. |
| 90 | dotBody, err := c.conn.ReadDotBytes() |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | dotBodyStr := strings.TrimRight(string(dotBody), "\n\r") |
| 95 | // c.debugf("Read dot body:\n---\n%v\n---", dotBodyStr) |
| 96 | resp.Data = append(resp.Data, line[4:]+"\r\n"+dotBodyStr) |
| 97 | dotLines := strings.Split(dotBodyStr, "\n") |
| 98 | for _, dotLine := range dotLines[:len(dotLines)-1] { |
| 99 | resp.RawLines = append(resp.RawLines, dotLine) |
| 100 | } |
| 101 | resp.RawLines = append(resp.RawLines, ".") |
| 102 | default: |
| 103 | return nil, c.protoErr("Invalid separator: '%v'", line[3]) |
| 104 | } |
| 105 | } |
| 106 | } |