Kyle, you are a genius. Thank you!
I took your advise and just returned the length of the input slice (if
not error) and it worked!
The strange thing is how this all just started happening with the
conforming to Go1. The Write methods haven't changed in this library.
There are about 5 different "transports" in this lib with Write
methods, so they all do different ops, but for instance the XHR-
POLLING transport Write looks like this:
func (s *xhrPollingSocket) Write(p []byte) (int, error) {
if !s.connected {
return 0, ErrNotConnected
}
defer s.Close()
buf := new(bytes.Buffer)
buf.WriteString("HTTP/1.0 200 OK\r\n")
buf.WriteString("Content-Type: text/plain; charset=UTF-8\r\n")
fmt.Fprintf(buf, "Content-Length: %d\r\n", len(p))
if origin := s.req.Header.Get("Origin"); origin != "" {
fmt.Fprintf(buf, "Access-Control-Allow-Origin: %s\r\n", origin)
buf.WriteString("Access-Control-Allow-Credentials: true\r\n")
}
buf.WriteString("\r\n")
buf.Write(p)
// nr, err := buf.WriteTo(s.rwc)
_, err := buf.WriteTo(s.rwc)
// return int(nr), err
return len(p), err
}
Those comments are where I applied the suggested fix that now works.
It does indeed modify the buffer before its written, causing the
length to mismatch for the Buffer.WriteTo
But strangely this never crashed it in the past.
On Apr 29, 3:36 pm, Kyle Lemons <
kev...@google.com> wrote:
> If you're doing any transformations (especially ones that might increase
> the number of bytes) on the output before you write it, you could cause
> more bytes to be written. So, in the code provided, it depends on what
> Write() does. In my transforming writers, usually I return the length of
> the input or zero instead of the number returned by the underlying write.
> Probably could confuse a writer that's trying to be smart, so maybe not
> the best solution, but it works :). I ran up against this first when I
> confused fmt.* by doing my own tab replacement on the back-end.
>