reader.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package remotecommand
  15. import (
  16. "io"
  17. )
  18. // readerWrapper delegates to an io.Reader so that only the io.Reader interface is implemented,
  19. // to keep io.Copy from doing things we don't want when copying from the reader to the data stream.
  20. //
  21. // If the Stdin io.Reader provided to remotecommand implements a WriteTo function (like bytes.Buffer does[1]),
  22. // io.Copy calls that method[2] to attempt to write the entire buffer to the stream in one call.
  23. // That results in an oversized call to spdystream.Stream#Write [3],
  24. // which results in a single oversized data frame[4] that is too large.
  25. //
  26. // [1] https://golang.org/pkg/bytes/#Buffer.WriteTo
  27. // [2] https://golang.org/pkg/io/#Copy
  28. // [3] https://github.com/kubernetes/kubernetes/blob/90295640ef87db9daa0144c5617afe889e7992b2/vendor/github.com/docker/spdystream/stream.go#L66-L73
  29. // [4] https://github.com/kubernetes/kubernetes/blob/90295640ef87db9daa0144c5617afe889e7992b2/vendor/github.com/docker/spdystream/spdy/write.go#L302-L304
  30. type readerWrapper struct {
  31. reader io.Reader
  32. }
  33. func (r readerWrapper) Read(p []byte) (int, error) {
  34. return r.reader.Read(p)
  35. }