github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/net/http/internal/chunked.go (about)

     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.
     4  
     5  // The wire protocol for HTTP's "chunked" Transfer-Encoding.
     6  
     7  // Package internal contains HTTP internals shared by net/http and
     8  // net/http/httputil.
     9  package internal
    10  
    11  import (
    12  	"github.com/shogo82148/std/bufio"
    13  	"github.com/shogo82148/std/errors"
    14  	"github.com/shogo82148/std/io"
    15  )
    16  
    17  var ErrLineTooLong = errors.New("header line too long")
    18  
    19  // NewChunkedReader returns a new chunkedReader that translates the data read from r
    20  // out of HTTP "chunked" format before returning it.
    21  // The chunkedReader returns [io.EOF] when the final 0-length chunk is read.
    22  //
    23  // NewChunkedReader is not needed by normal applications. The http package
    24  // automatically decodes chunking when reading response bodies.
    25  func NewChunkedReader(r io.Reader) io.Reader
    26  
    27  // NewChunkedWriter returns a new chunkedWriter that translates writes into HTTP
    28  // "chunked" format before writing them to w. Closing the returned chunkedWriter
    29  // sends the final 0-length chunk that marks the end of the stream but does
    30  // not send the final CRLF that appears after trailers; trailers and the last
    31  // CRLF must be written separately.
    32  //
    33  // NewChunkedWriter is not needed by normal applications. The http
    34  // package adds chunking automatically if handlers don't set a
    35  // Content-Length header. Using newChunkedWriter inside a handler
    36  // would result in double chunking or chunking with a Content-Length
    37  // length, both of which are wrong.
    38  func NewChunkedWriter(w io.Writer) io.WriteCloser
    39  
    40  // FlushAfterChunkWriter signals from the caller of [NewChunkedWriter]
    41  // that each chunk should be followed by a flush. It is used by the
    42  // [net/http.Transport] code to keep the buffering behavior for headers and
    43  // trailers, but flush out chunks aggressively in the middle for
    44  // request bodies which may be generated slowly. See Issue 6574.
    45  type FlushAfterChunkWriter struct {
    46  	*bufio.Writer
    47  }