aboutsummaryrefslogtreecommitdiff
path: root/streaming.go
diff options
context:
space:
mode:
authorGravatar ichx <czn16@qq.com> 2021-12-05 21:11:51 +0800
committerGravatar GitHub <noreply@github.com> 2021-12-05 14:11:51 +0100
commitda7ff7a2080953e370e8bd712f5ae8ef8b0cb4e1 (patch)
treed361832c2ab7da262ce69a646377cf3301968cca /streaming.go
parentfix: reset request after reset user values on keep-alive connections (#1162) (diff)
downloadfasthttp-da7ff7a2080953e370e8bd712f5ae8ef8b0cb4e1.tar.gz
fasthttp-da7ff7a2080953e370e8bd712f5ae8ef8b0cb4e1.tar.bz2
fasthttp-da7ff7a2080953e370e8bd712f5ae8ef8b0cb4e1.zip
Add trailer support (#1165)
* Add trailer support * fix issue and add documentation * remove redundant code * add error return for add/set trailer method * fix lint error * fix bad trailer error return issue and update bad content-length error * update errNonNumericChars * update errNonNumericChars * fix issue about error and fix typo
Diffstat (limited to 'streaming.go')
-rw-r--r--streaming.go25
1 files changed, 12 insertions, 13 deletions
diff --git a/streaming.go b/streaming.go
index 1a3d748..11750a9 100644
--- a/streaming.go
+++ b/streaming.go
@@ -10,10 +10,10 @@ import (
)
type requestStream struct {
+ header *RequestHeader
prefetchedBytes *bytes.Reader
reader *bufio.Reader
totalBytesRead int
- contentLength int
chunkLeft int
}
@@ -22,18 +22,18 @@ func (rs *requestStream) Read(p []byte) (int, error) {
n int
err error
)
- if rs.contentLength == -1 {
+ if rs.header.contentLength == -1 {
if rs.chunkLeft == 0 {
chunkSize, err := parseChunkSize(rs.reader)
if err != nil {
return 0, err
}
if chunkSize == 0 {
- err = readCrLf(rs.reader)
- if err == nil {
- err = io.EOF
+ err = rs.header.ReadTrailer(rs.reader)
+ if err != nil && err != io.EOF {
+ return 0, err
}
- return 0, err
+ return 0, io.EOF
}
rs.chunkLeft = chunkSize
}
@@ -52,7 +52,7 @@ func (rs *requestStream) Read(p []byte) (int, error) {
}
return n, err
}
- if rs.totalBytesRead == rs.contentLength {
+ if rs.totalBytesRead == rs.header.contentLength {
return 0, io.EOF
}
prefetchedSize := int(rs.prefetchedBytes.Size())
@@ -63,12 +63,12 @@ func (rs *requestStream) Read(p []byte) (int, error) {
}
n, err := rs.prefetchedBytes.Read(p)
rs.totalBytesRead += n
- if n == rs.contentLength {
+ if n == rs.header.contentLength {
return n, io.EOF
}
return n, err
} else {
- left := rs.contentLength - rs.totalBytesRead
+ left := rs.header.contentLength - rs.totalBytesRead
if len(p) > left {
p = p[:left]
}
@@ -79,18 +79,17 @@ func (rs *requestStream) Read(p []byte) (int, error) {
}
}
- if rs.totalBytesRead == rs.contentLength {
+ if rs.totalBytesRead == rs.header.contentLength {
err = io.EOF
}
return n, err
}
-func acquireRequestStream(b *bytebufferpool.ByteBuffer, r *bufio.Reader, contentLength int) *requestStream {
+func acquireRequestStream(b *bytebufferpool.ByteBuffer, r *bufio.Reader, h *RequestHeader) *requestStream {
rs := requestStreamPool.Get().(*requestStream)
rs.prefetchedBytes = bytes.NewReader(b.B)
rs.reader = r
- rs.contentLength = contentLength
-
+ rs.header = h
return rs
}