| 1 | package controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ |
| 2 | |
| 3 | import ( |
| 4 | "net/textprotoscip-go gomod github.com/golang/go/src go1.15 `net/textproto`/" |
| 5 | "strconvscip-go gomod github.com/golang/go/src go1.15 strconv/" |
| 6 | "stringsscip-go gomod github.com/golang/go/src go1.15 strings/" |
| 7 | ) |
| 8 | |
| 9 | // Response is a response to a control port command or an asynchronous event. |
| 10 | type Responsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/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 | Errscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Err. *textprotoscip-go gomod github.com/golang/go/src go1.15 `net/textproto`/.Errorscip-go gomod github.com/golang/go/src go1.15 `net/textproto`/Error# |
| 15 | |
| 16 | // Reply is the text on the EndReplyLine of the response. |
| 17 | Replyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#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 | Datascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Data. []string |
| 23 | |
| 24 | // RawLines is all of the lines of a response, without CRLFs. |
| 25 | RawLinesscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#RawLines. []string |
| 26 | } |
| 27 | |
| 28 | // IsOk returns true if the response status code indicates success or an |
| 29 | // asynchronous event. |
| 30 | func (r *Responsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#) IsOkscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#IsOk().() bool { |
| 31 | switch r.Errscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Err..Codescip-go gomod github.com/golang/go/src go1.15 `net/textproto`/Error#Code. { |
| 32 | case StatusOkscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/StatusOk., StatusOkUnnecessaryscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/StatusOkUnnecessary., StatusAsyncEventscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/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 *Responsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#) DataWithReplyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#DataWithReply().() []string { |
| 42 | ret := make([]string, len(r.Datascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Data.)+1) |
| 43 | copy(ret, r.Datascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Data.) |
| 44 | ret[len(ret)-1] = r.Replyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Reply. |
| 45 | return ret |
| 46 | } |
| 47 | |
| 48 | // IsAsync returns true if the response is an asynchronous event. |
| 49 | func (r *Responsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#) IsAsyncscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#IsAsync().() bool { |
| 50 | return r.Errscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Err..Codescip-go gomod github.com/golang/go/src go1.15 `net/textproto`/Error#Code. == StatusAsyncEventscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/StatusAsyncEvent. |
| 51 | } |
| 52 | |
| 53 | // ReadResponse returns the next response object. |
| 54 | func (c *Connscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#) ReadResponsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#ReadResponse().() (*Responsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#, error) { |
| 55 | var resp *Responsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response# |
| 56 | var statusCode int |
| 57 | for { |
| 58 | line, err := c.connscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#conn..ReadLinescip-go gomod github.com/golang/go/src go1.15 `net/textproto`/Reader#ReadLine().() |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | c.debugfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#debugf().("Read line: %v", line) |
| 63 | |
| 64 | // Parse the line that was just read. |
| 65 | if len(line) < 4 { |
| 66 | return nil, c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Truncated response: %v", line) |
| 67 | } |
| 68 | if code, err := strconvscip-go gomod github.com/golang/go/src go1.15 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.15 strconv/Atoi().(line[0:3]); err != nil || code < 100 { |
| 69 | return nil, c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Invalid status code: %v", line[0:3]) |
| 70 | } else if resp == nil { |
| 71 | resp = &Responsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/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.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Status code changed: %v != %v", code, statusCode) |
| 77 | } |
| 78 | resp.RawLinesscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#RawLines. = append(resp.RawLinesscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#RawLines., line) |
| 79 | switch line[3] { |
| 80 | case ' ': |
| 81 | // Final line in the response. |
| 82 | resp.Replyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Reply. = line[4:] |
| 83 | resp.Errscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Err. = statusCodeToErrorscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/statusCodeToError().(statusCode, resp.Replyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Reply.) |
| 84 | return resp, nil |
| 85 | case '-': |
| 86 | // Continuation, keep reading. |
| 87 | resp.Datascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Data. = append(resp.Datascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Data., line[4:]) |
| 88 | case '+': |
| 89 | // A "dot-encoded" payload follows. |
| 90 | dotBody, err := c.connscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#conn..ReadDotBytesscip-go gomod github.com/golang/go/src go1.15 `net/textproto`/Reader#ReadDotBytes().() |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | dotBodyStr := stringsscip-go gomod github.com/golang/go/src go1.15 strings/.TrimRightscip-go gomod github.com/golang/go/src go1.15 strings/TrimRight().(string(dotBody), "\n\r") |
| 95 | // c.debugf("Read dot body:\n---\n%v\n---", dotBodyStr) |
| 96 | resp.Datascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Data. = append(resp.Datascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Data., line[4:]+"\r\n"+dotBodyStr) |
| 97 | dotLines := stringsscip-go gomod github.com/golang/go/src go1.15 strings/.Splitscip-go gomod github.com/golang/go/src go1.15 strings/Split().(dotBodyStr, "\n") |
| 98 | for _, dotLine := range dotLines[:len(dotLines)-1] { |
| 99 | resp.RawLinesscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#RawLines. = append(resp.RawLinesscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#RawLines., dotLine) |
| 100 | } |
| 101 | resp.RawLinesscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#RawLines. = append(resp.RawLinesscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#RawLines., ".") |
| 102 | default: |
| 103 | return nil, c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Invalid separator: '%v'", line[3]) |
| 104 | } |
| 105 | } |
| 106 | } |