1
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
// The wire protocol for HTTP's "chunked" Transfer-Encoding.
7
// This code is duplicated in httputil/chunked.go.
8
// Please make any changes in both files.
20
const maxLineLength = 4096 // assumed <= bufio.defaultBufSize
22
var ErrLineTooLong = errors.New("header line too long")
24
// newChunkedReader returns a new chunkedReader that translates the data read from r
25
// out of HTTP "chunked" format before returning it.
26
// The chunkedReader returns io.EOF when the final 0-length chunk is read.
28
// newChunkedReader is not needed by normal applications. The http package
29
// automatically decodes chunking when reading response bodies.
30
func newChunkedReader(r io.Reader) io.Reader {
31
br, ok := r.(*bufio.Reader)
33
br = bufio.NewReader(r)
35
return &chunkedReader{r: br}
38
type chunkedReader struct {
40
n uint64 // unread bytes in chunk
44
func (cr *chunkedReader) beginChunk() {
47
line, cr.err = readLine(cr.r)
51
cr.n, cr.err = strconv.ParseUint(line, 16, 64)
60
func (cr *chunkedReader) Read(b []uint8) (n int, err error) {
70
if uint64(len(b)) > cr.n {
73
n, cr.err = cr.r.Read(b)
75
if cr.n == 0 && cr.err == nil {
76
// end of chunk (CRLF)
78
if _, cr.err = io.ReadFull(cr.r, b); cr.err == nil {
79
if b[0] != '\r' || b[1] != '\n' {
80
cr.err = errors.New("malformed chunked encoding")
87
// Read a line of bytes (up to \n) from b.
88
// Give up if the line exceeds maxLineLength.
89
// The returned bytes are a pointer into storage in
90
// the bufio, so they are only valid until the next bufio read.
91
func readLineBytes(b *bufio.Reader) (p []byte, err error) {
92
if p, err = b.ReadSlice('\n'); err != nil {
93
// We always know when EOF is coming.
94
// If the caller asked for a line, there should be a line.
96
err = io.ErrUnexpectedEOF
97
} else if err == bufio.ErrBufferFull {
102
if len(p) >= maxLineLength {
103
return nil, ErrLineTooLong
106
// Chop off trailing white space.
107
p = bytes.TrimRight(p, " \r\t\n")
112
// readLineBytes, but convert the bytes into a string.
113
func readLine(b *bufio.Reader) (s string, err error) {
114
p, e := readLineBytes(b)
118
return string(p), nil
121
// newChunkedWriter returns a new chunkedWriter that translates writes into HTTP
122
// "chunked" format before writing them to w. Closing the returned chunkedWriter
123
// sends the final 0-length chunk that marks the end of the stream.
125
// newChunkedWriter is not needed by normal applications. The http
126
// package adds chunking automatically if handlers don't set a
127
// Content-Length header. Using newChunkedWriter inside a handler
128
// would result in double chunking or chunking with a Content-Length
129
// length, both of which are wrong.
130
func newChunkedWriter(w io.Writer) io.WriteCloser {
131
return &chunkedWriter{w}
134
// Writing to chunkedWriter translates to writing in HTTP chunked Transfer
135
// Encoding wire format to the underlying Wire chunkedWriter.
136
type chunkedWriter struct {
140
// Write the contents of data as one chunk to Wire.
141
// NOTE: Note that the corresponding chunk-writing procedure in Conn.Write has
142
// a bug since it does not check for success of io.WriteString
143
func (cw *chunkedWriter) Write(data []byte) (n int, err error) {
145
// Don't send 0-length data. It looks like EOF for chunked encoding.
150
head := strconv.FormatInt(int64(len(data)), 16) + "\r\n"
152
if _, err = io.WriteString(cw.Wire, head); err != nil {
155
if n, err = cw.Wire.Write(data); err != nil {
159
err = io.ErrShortWrite
162
_, err = io.WriteString(cw.Wire, "\r\n")
167
func (cw *chunkedWriter) Close() error {
168
_, err := io.WriteString(cw.Wire, "0\r\n")