streaming.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package fasthttp
  2. import (
  3. "bufio"
  4. "bytes"
  5. "io"
  6. "sync"
  7. "github.com/valyala/bytebufferpool"
  8. )
  9. type headerInterface interface {
  10. ContentLength() int
  11. ReadTrailer(r *bufio.Reader) error
  12. }
  13. type requestStream struct {
  14. header headerInterface
  15. prefetchedBytes *bytes.Reader
  16. reader *bufio.Reader
  17. totalBytesRead int
  18. chunkLeft int
  19. }
  20. func (rs *requestStream) Read(p []byte) (int, error) {
  21. var (
  22. n int
  23. err error
  24. )
  25. if rs.header.ContentLength() == -1 {
  26. if rs.chunkLeft == 0 {
  27. chunkSize, err := parseChunkSize(rs.reader)
  28. if err != nil {
  29. return 0, err
  30. }
  31. if chunkSize == 0 {
  32. err = rs.header.ReadTrailer(rs.reader)
  33. if err != nil && err != io.EOF {
  34. return 0, err
  35. }
  36. return 0, io.EOF
  37. }
  38. rs.chunkLeft = chunkSize
  39. }
  40. bytesToRead := min(rs.chunkLeft, len(p))
  41. n, err = rs.reader.Read(p[:bytesToRead])
  42. rs.totalBytesRead += n
  43. rs.chunkLeft -= n
  44. if err == io.EOF {
  45. err = io.ErrUnexpectedEOF
  46. }
  47. if err == nil && rs.chunkLeft == 0 {
  48. err = readCrLf(rs.reader)
  49. }
  50. return n, err
  51. }
  52. if rs.totalBytesRead == rs.header.ContentLength() {
  53. return 0, io.EOF
  54. }
  55. prefetchedSize := int(rs.prefetchedBytes.Size())
  56. if prefetchedSize > rs.totalBytesRead {
  57. left := prefetchedSize - rs.totalBytesRead
  58. if len(p) > left {
  59. p = p[:left]
  60. }
  61. n, err := rs.prefetchedBytes.Read(p)
  62. rs.totalBytesRead += n
  63. if n == rs.header.ContentLength() {
  64. return n, io.EOF
  65. }
  66. return n, err
  67. }
  68. left := rs.header.ContentLength() - rs.totalBytesRead
  69. if left > 0 && len(p) > left {
  70. p = p[:left]
  71. }
  72. n, err = rs.reader.Read(p)
  73. rs.totalBytesRead += n
  74. if err != nil {
  75. return n, err
  76. }
  77. if rs.totalBytesRead == rs.header.ContentLength() {
  78. err = io.EOF
  79. }
  80. return n, err
  81. }
  82. func acquireRequestStream(b *bytebufferpool.ByteBuffer, r *bufio.Reader, h headerInterface) *requestStream {
  83. rs := requestStreamPool.Get().(*requestStream)
  84. rs.prefetchedBytes = bytes.NewReader(b.B)
  85. rs.reader = r
  86. rs.header = h
  87. return rs
  88. }
  89. func releaseRequestStream(rs *requestStream) {
  90. rs.prefetchedBytes = nil
  91. rs.totalBytesRead = 0
  92. rs.chunkLeft = 0
  93. rs.reader = nil
  94. rs.header = nil
  95. requestStreamPool.Put(rs)
  96. }
  97. var requestStreamPool = sync.Pool{
  98. New: func() any {
  99. return &requestStream{}
  100. },
  101. }