Source File
chunked.go
Belonging Package
net/http/internal
package internal
import (
)
const maxLineLength = 4096 // assumed <= bufio.defaultBufSize
var ErrLineTooLong = errors.New("header line too long")
var []byte
, .err = readChunkLine(.r)
if .err != nil {
return
}
.n, .err = parseHexUint()
if .err != nil {
return
}
if .n == 0 {
.err = io.EOF
}
}
func ( *chunkedReader) () bool {
:= .r.Buffered()
if > 0 {
, := .r.Peek()
return bytes.IndexByte(, '\n') >= 0
}
return false
}
func ( *chunkedReader) ( []uint8) ( int, error) {
for .err == nil {
if .checkEnd {
if == io.EOF {
= io.ErrUnexpectedEOF
} else if == bufio.ErrBufferFull {
= ErrLineTooLong
}
return nil,
}
if len() >= maxLineLength {
return nil, ErrLineTooLong
}
= trimTrailingWhitespace()
, = removeChunkExtension()
if != nil {
return nil,
}
return , nil
}
func ( []byte) []byte {
for len() > 0 && isASCIISpace([len()-1]) {
= [:len()-1]
}
return
}
func ( byte) bool {
return == ' ' || == '\t' || == '\n' || == '\r'
}
return [:], nil
}
func ( io.Writer) io.WriteCloser {
return &chunkedWriter{}
}
type chunkedWriter struct {
Wire io.Writer
}
func ( *chunkedWriter) ( []byte) ( int, error) {
if len() == 0 {
return 0, nil
}
if _, = fmt.Fprintf(.Wire, "%x\r\n", len()); != nil {
return 0,
}
if , = .Wire.Write(); != nil {
return
}
if != len() {
= io.ErrShortWrite
return
}
if _, = io.WriteString(.Wire, "\r\n"); != nil {
return
}
if , := .Wire.(*FlushAfterChunkWriter); {
= .Flush()
}
return
}
func ( *chunkedWriter) () error {
, := io.WriteString(.Wire, "0\r\n")
return
}
type FlushAfterChunkWriter struct {
*bufio.Writer
}
func ( []byte) ( uint64, error) {
for , := range {
switch {
case '0' <= && <= '9':
= - '0'
case 'a' <= && <= 'f':
= - 'a' + 10
case 'A' <= && <= 'F':
= - 'A' + 10
default:
return 0, errors.New("invalid byte in chunk length")
}
if == 16 {
return 0, errors.New("http chunk length too large")
}
<<= 4
|= uint64()
}
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |