@@ -12,8 +12,6 @@ import (
12
12
"strconv"
13
13
"strings"
14
14
"sync"
15
-
16
- "golang.org/x/xerrors"
17
15
)
18
16
19
17
const (
@@ -62,9 +60,9 @@ type Stream interface {
62
60
}
63
61
64
62
type stream struct {
65
- in * bufio.Reader
66
- out io.Writer
67
- sync.Mutex
63
+ in * bufio.Reader
64
+ out io.Writer
65
+ outMu sync.Mutex
68
66
}
69
67
70
68
func NewStream (in io.Reader , out io.Writer ) Stream {
@@ -82,61 +80,55 @@ func (s *stream) Read(ctx context.Context) ([]byte, error) {
82
80
}
83
81
84
82
var length int64
83
+ // read the header, stop on the first empty line
85
84
for {
86
85
line , err := s .in .ReadString ('\n' )
87
86
if err != nil {
88
- return nil , xerrors .Errorf ("failed reading header line: %w " , err )
87
+ return nil , fmt .Errorf ("failed reading header line %q " , err )
89
88
}
90
-
91
89
line = strings .TrimSpace (line )
92
- if line == "" { // check we have a header line
90
+ // check we have a header line
91
+ if line == "" {
93
92
break
94
93
}
95
-
96
94
colon := strings .IndexRune (line , ':' )
97
95
if colon < 0 {
98
- return nil , xerrors .Errorf ("invalid header line: %q" , line )
96
+ return nil , fmt .Errorf ("invalid header line %q" , line )
99
97
}
100
-
101
98
name , value := line [:colon ], strings .TrimSpace (line [colon + 1 :])
102
- if name != "Content-Length" {
103
- continue
104
- }
105
-
106
- if length , err = strconv . ParseInt ( value , 10 , 32 ); err != nil {
107
- return nil , xerrors . Errorf ( "failed parsing Content-Length: %v" , value )
108
- }
109
-
110
- if length <= 0 {
111
- return nil , xerrors . Errorf ( "invalid Content-Length: %v" , length )
99
+ switch name {
100
+ case "Content-Length" :
101
+ if length , err = strconv . ParseInt ( value , 10 , 32 ); err != nil {
102
+ return nil , fmt . Errorf ( "failed parsing Content-Length: %v" , value )
103
+ }
104
+ if length <= 0 {
105
+ return nil , fmt . Errorf ( "invalid Content-Length: %v" , length )
106
+ }
107
+ default :
108
+ // ignoring unknown headers
112
109
}
113
110
}
114
-
115
111
if length == 0 {
116
- return nil , xerrors . New ("missing Content-Length header" )
112
+ return nil , fmt . Errorf ("missing Content-Length header" )
117
113
}
118
-
119
114
data := make ([]byte , length )
120
115
if _ , err := io .ReadFull (s .in , data ); err != nil {
121
- return nil , xerrors . Errorf ( "failed reading data: %w" , err )
116
+ return nil , err
122
117
}
123
-
124
118
return data , nil
125
119
}
126
120
127
- func (s * stream ) Write (ctx context.Context , data []byte ) ( err error ) {
121
+ func (s * stream ) Write (ctx context.Context , data []byte ) error {
128
122
select {
129
123
case <- ctx .Done ():
130
124
return ctx .Err ()
131
125
default :
132
126
}
133
-
134
- s .Lock ()
135
- _ , err = fmt .Fprintf (s .out , HeaderContentLengthFmt + HeaderContentTypeFmt + HeaderContentSeparator , len (data ), ContentTypeJSONRPC )
127
+ s .outMu .Lock ()
128
+ _ , err := fmt .Fprintf (s .out , "Content-Length: %v\r \n \r \n " , len (data ))
136
129
if err == nil {
137
130
_ , err = s .out .Write (data )
138
131
}
139
- s .Unlock ()
140
-
132
+ s .outMu .Unlock ()
141
133
return err
142
134
}
0 commit comments