Revert "THRIFT-3746 JSON protocol left in incorrect state on read errors"

This reverts commit 51850abb7e.
This commit is contained in:
Nobuaki Sukegawa 2016-04-09 19:46:21 +09:00
parent 8b0b7e5eb4
commit 3aa461b97b
2 changed files with 26 additions and 20 deletions

View File

@ -60,8 +60,7 @@ func NewTJSONProtocolFactory() *TJSONProtocolFactory {
} }
func (p *TJSONProtocol) WriteMessageBegin(name string, typeId TMessageType, seqId int32) error { func (p *TJSONProtocol) WriteMessageBegin(name string, typeId TMessageType, seqId int32) error {
p.resetContextStack() p.resetContextStack() // THRIFT-3735
p.writer.Reset(p.trans)
if e := p.OutputListBegin(); e != nil { if e := p.OutputListBegin(); e != nil {
return e return e
} }
@ -204,17 +203,18 @@ func (p *TJSONProtocol) WriteBinary(v []byte) error {
if e := p.OutputPreValue(); e != nil { if e := p.OutputPreValue(); e != nil {
return e return e
} }
if _, e := p.writer.Write(JSON_QUOTE_BYTES); e != nil { if _, e := p.write(JSON_QUOTE_BYTES); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
writer := base64.NewEncoder(base64.StdEncoding, p.writer) writer := base64.NewEncoder(base64.StdEncoding, p.writer)
if _, e := writer.Write(v); e != nil { if _, e := writer.Write(v); e != nil {
p.writer.Reset(p.trans) // THRIFT-3735
return NewTProtocolException(e) return NewTProtocolException(e)
} }
if e := writer.Close(); e != nil { if e := writer.Close(); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
if _, e := p.writer.Write(JSON_QUOTE_BYTES); e != nil { if _, e := p.write(JSON_QUOTE_BYTES); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
return p.OutputPostValue() return p.OutputPostValue()
@ -222,8 +222,7 @@ func (p *TJSONProtocol) WriteBinary(v []byte) error {
// Reading methods. // Reading methods.
func (p *TJSONProtocol) ReadMessageBegin() (name string, typeId TMessageType, seqId int32, err error) { func (p *TJSONProtocol) ReadMessageBegin() (name string, typeId TMessageType, seqId int32, err error) {
p.resetContextStack() p.resetContextStack() // THRIFT-3735
p.reader.Reset(p.trans)
if isNull, err := p.ParseListBegin(); isNull || err != nil { if isNull, err := p.ParseListBegin(); isNull || err != nil {
return name, typeId, seqId, err return name, typeId, seqId, err
} }

View File

@ -156,8 +156,7 @@ func mismatch(expected, actual string) error {
} }
func (p *TSimpleJSONProtocol) WriteMessageBegin(name string, typeId TMessageType, seqId int32) error { func (p *TSimpleJSONProtocol) WriteMessageBegin(name string, typeId TMessageType, seqId int32) error {
p.resetContextStack() p.resetContextStack() // THRIFT-3735
p.writer.Reset(p.trans)
if e := p.OutputListBegin(); e != nil { if e := p.OutputListBegin(); e != nil {
return e return e
} }
@ -271,17 +270,18 @@ func (p *TSimpleJSONProtocol) WriteBinary(v []byte) error {
if e := p.OutputPreValue(); e != nil { if e := p.OutputPreValue(); e != nil {
return e return e
} }
if _, e := p.writer.Write(JSON_QUOTE_BYTES); e != nil { if _, e := p.write(JSON_QUOTE_BYTES); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
writer := base64.NewEncoder(base64.StdEncoding, p.writer) writer := base64.NewEncoder(base64.StdEncoding, p.writer)
if _, e := writer.Write(v); e != nil { if _, e := writer.Write(v); e != nil {
p.writer.Reset(p.trans) // THRIFT-3735
return NewTProtocolException(e) return NewTProtocolException(e)
} }
if e := writer.Close(); e != nil { if e := writer.Close(); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
if _, e := p.writer.Write(JSON_QUOTE_BYTES); e != nil { if _, e := p.write(JSON_QUOTE_BYTES); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
return p.OutputPostValue() return p.OutputPostValue()
@ -289,8 +289,7 @@ func (p *TSimpleJSONProtocol) WriteBinary(v []byte) error {
// Reading methods. // Reading methods.
func (p *TSimpleJSONProtocol) ReadMessageBegin() (name string, typeId TMessageType, seqId int32, err error) { func (p *TSimpleJSONProtocol) ReadMessageBegin() (name string, typeId TMessageType, seqId int32, err error) {
p.resetContextStack() p.resetContextStack() // THRIFT-3735
p.reader.Reset(p.trans)
if isNull, err := p.ParseListBegin(); isNull || err != nil { if isNull, err := p.ParseListBegin(); isNull || err != nil {
return name, typeId, seqId, err return name, typeId, seqId, err
} }
@ -569,12 +568,12 @@ func (p *TSimpleJSONProtocol) OutputPreValue() error {
cxt := _ParseContext(p.dumpContext[len(p.dumpContext)-1]) cxt := _ParseContext(p.dumpContext[len(p.dumpContext)-1])
switch cxt { switch cxt {
case _CONTEXT_IN_LIST, _CONTEXT_IN_OBJECT_NEXT_KEY: case _CONTEXT_IN_LIST, _CONTEXT_IN_OBJECT_NEXT_KEY:
if _, e := p.writer.Write(JSON_COMMA); e != nil { if _, e := p.write(JSON_COMMA); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
break break
case _CONTEXT_IN_OBJECT_NEXT_VALUE: case _CONTEXT_IN_OBJECT_NEXT_VALUE:
if _, e := p.writer.Write(JSON_COLON); e != nil { if _, e := p.write(JSON_COLON); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
break break
@ -630,7 +629,7 @@ func (p *TSimpleJSONProtocol) OutputNull() error {
if e := p.OutputPreValue(); e != nil { if e := p.OutputPreValue(); e != nil {
return e return e
} }
if _, e := p.writer.Write(JSON_NULL); e != nil { if _, e := p.write(JSON_NULL); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
return p.OutputPostValue() return p.OutputPostValue()
@ -688,7 +687,7 @@ func (p *TSimpleJSONProtocol) OutputString(s string) error {
} }
func (p *TSimpleJSONProtocol) OutputStringData(s string) error { func (p *TSimpleJSONProtocol) OutputStringData(s string) error {
_, e := p.writer.Write([]byte(s)) _, e := p.write([]byte(s))
return NewTProtocolException(e) return NewTProtocolException(e)
} }
@ -696,7 +695,7 @@ func (p *TSimpleJSONProtocol) OutputObjectBegin() error {
if e := p.OutputPreValue(); e != nil { if e := p.OutputPreValue(); e != nil {
return e return e
} }
if _, e := p.writer.Write(JSON_LBRACE); e != nil { if _, e := p.write(JSON_LBRACE); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_OBJECT_FIRST)) p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_OBJECT_FIRST))
@ -704,7 +703,7 @@ func (p *TSimpleJSONProtocol) OutputObjectBegin() error {
} }
func (p *TSimpleJSONProtocol) OutputObjectEnd() error { func (p *TSimpleJSONProtocol) OutputObjectEnd() error {
if _, e := p.writer.Write(JSON_RBRACE); e != nil { if _, e := p.write(JSON_RBRACE); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
p.dumpContext = p.dumpContext[:len(p.dumpContext)-1] p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
@ -718,7 +717,7 @@ func (p *TSimpleJSONProtocol) OutputListBegin() error {
if e := p.OutputPreValue(); e != nil { if e := p.OutputPreValue(); e != nil {
return e return e
} }
if _, e := p.writer.Write(JSON_LBRACKET); e != nil { if _, e := p.write(JSON_LBRACKET); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_LIST_FIRST)) p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_LIST_FIRST))
@ -726,7 +725,7 @@ func (p *TSimpleJSONProtocol) OutputListBegin() error {
} }
func (p *TSimpleJSONProtocol) OutputListEnd() error { func (p *TSimpleJSONProtocol) OutputListEnd() error {
if _, e := p.writer.Write(JSON_RBRACKET); e != nil { if _, e := p.write(JSON_RBRACKET); e != nil {
return NewTProtocolException(e) return NewTProtocolException(e)
} }
p.dumpContext = p.dumpContext[:len(p.dumpContext)-1] p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
@ -1328,3 +1327,11 @@ func (p *TSimpleJSONProtocol) resetContextStack() {
p.parseContextStack = []int{int(_CONTEXT_IN_TOPLEVEL)} p.parseContextStack = []int{int(_CONTEXT_IN_TOPLEVEL)}
p.dumpContext = []int{int(_CONTEXT_IN_TOPLEVEL)} p.dumpContext = []int{int(_CONTEXT_IN_TOPLEVEL)}
} }
func (p *TSimpleJSONProtocol) write(b []byte) (int, error) {
n, err := p.writer.Write(b)
if err != nil {
p.writer.Reset(p.trans) // THRIFT-3735
}
return n, err
}