mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 17:13:33 +01:00
23 lines
586 B
Go
23 lines
586 B
Go
package server
|
|
|
|
import "io"
|
|
|
|
// Detect truncated responses even when upstream uses chunked transfer encoding.
|
|
// Returning a read error prevents the worker from publishing partial output.
|
|
type completeBody struct {
|
|
io.ReadCloser
|
|
expected, received int64
|
|
}
|
|
|
|
func checkedBody(body io.ReadCloser, expected int64) io.ReadCloser {
|
|
return &completeBody{ReadCloser: body, expected: expected}
|
|
}
|
|
func (b *completeBody) Read(p []byte) (int, error) {
|
|
n, e := b.ReadCloser.Read(p)
|
|
b.received += int64(n)
|
|
if e == io.EOF && b.received < b.expected {
|
|
e = io.ErrUnexpectedEOF
|
|
}
|
|
return n, e
|
|
}
|