mirror of
https://github.com/golang/go
synced 2024-11-17 20:04:47 -07:00
net/textproto: properly write terminating sequence if DotWriter is closed with no writes
Fixed textproto.Writer.DotWriter() to properly write \r\n.\r\n to the buffer when Close() is called without any bytes written. This properly writes the terminating sequence outlined in RFC 5321 section 4.1.1.4 and RFC 3977 section 3.1.1, even when no other bytes are written. Change-Id: I262fd2963ee76fff7ffae8e3cb0e86255694b361 Reviewed-on: https://go-review.googlesource.com/c/go/+/77350 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
4d23cbc671
commit
93af677837
@ -58,7 +58,8 @@ type dotWriter struct {
|
||||
}
|
||||
|
||||
const (
|
||||
wstateBeginLine = iota // beginning of line; initial state; must be zero
|
||||
wstateBegin = iota // initial state; must be zero
|
||||
wstateBeginLine // beginning of line
|
||||
wstateCR // wrote \r (possibly at end of line)
|
||||
wstateData // writing data in middle of line
|
||||
)
|
||||
@ -68,7 +69,7 @@ func (d *dotWriter) Write(b []byte) (n int, err error) {
|
||||
for n < len(b) {
|
||||
c := b[n]
|
||||
switch d.state {
|
||||
case wstateBeginLine:
|
||||
case wstateBegin, wstateBeginLine:
|
||||
d.state = wstateData
|
||||
if c == '.' {
|
||||
// escape leading dot
|
||||
|
@ -33,3 +33,29 @@ func TestDotWriter(t *testing.T) {
|
||||
t.Fatalf("wrote %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDotWriterCloseEmptyWrite(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
w := NewWriter(bufio.NewWriter(&buf))
|
||||
d := w.DotWriter()
|
||||
n, err := d.Write([]byte{})
|
||||
if n != 0 || err != nil {
|
||||
t.Fatalf("Write: %d, %s", n, err)
|
||||
}
|
||||
d.Close()
|
||||
want := "\r\n.\r\n"
|
||||
if s := buf.String(); s != want {
|
||||
t.Fatalf("wrote %q; want %q", s, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDotWriterCloseNoWrite(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
w := NewWriter(bufio.NewWriter(&buf))
|
||||
d := w.DotWriter()
|
||||
d.Close()
|
||||
want := "\r\n.\r\n"
|
||||
if s := buf.String(); s != want {
|
||||
t.Fatalf("wrote %q; want %q", s, want)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user