|
| 1 | +// TINYGO: Copied verbatim from the Go 1.26.2 official implementation to provide |
| 2 | +// the ResponseController API used by net/http/httputil (reverse proxy). The |
| 3 | +// underlying methods degrade to ErrNotSupported when the ResponseWriter does |
| 4 | +// not implement them, which is the standard behavior. |
| 5 | + |
| 6 | +// Copyright 2022 The Go Authors. All rights reserved. |
| 7 | +// Use of this source code is governed by a BSD-style |
| 8 | +// license that can be found in the LICENSE file. |
| 9 | + |
| 10 | +package http |
| 11 | + |
| 12 | +import ( |
| 13 | + "bufio" |
| 14 | + "fmt" |
| 15 | + "net" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +// A ResponseController is used by an HTTP handler to control the response. |
| 20 | +// |
| 21 | +// A ResponseController may not be used after the [Handler.ServeHTTP] method has returned. |
| 22 | +type ResponseController struct { |
| 23 | + rw ResponseWriter |
| 24 | +} |
| 25 | + |
| 26 | +// NewResponseController creates a [ResponseController] for a request. |
| 27 | +// |
| 28 | +// The ResponseWriter should be the original value passed to the [Handler.ServeHTTP] method, |
| 29 | +// or have an Unwrap method returning the original ResponseWriter. |
| 30 | +// |
| 31 | +// If the ResponseWriter implements any of the following methods, the ResponseController |
| 32 | +// will call them as appropriate: |
| 33 | +// |
| 34 | +// Flush() |
| 35 | +// FlushError() error // alternative Flush returning an error |
| 36 | +// Hijack() (net.Conn, *bufio.ReadWriter, error) |
| 37 | +// SetReadDeadline(deadline time.Time) error |
| 38 | +// SetWriteDeadline(deadline time.Time) error |
| 39 | +// EnableFullDuplex() error |
| 40 | +// |
| 41 | +// If the ResponseWriter does not support a method, ResponseController returns |
| 42 | +// an error matching [ErrNotSupported]. |
| 43 | +func NewResponseController(rw ResponseWriter) *ResponseController { |
| 44 | + return &ResponseController{rw} |
| 45 | +} |
| 46 | + |
| 47 | +type rwUnwrapper interface { |
| 48 | + Unwrap() ResponseWriter |
| 49 | +} |
| 50 | + |
| 51 | +// Flush flushes buffered data to the client. |
| 52 | +func (c *ResponseController) Flush() error { |
| 53 | + rw := c.rw |
| 54 | + for { |
| 55 | + switch t := rw.(type) { |
| 56 | + case interface{ FlushError() error }: |
| 57 | + return t.FlushError() |
| 58 | + case Flusher: |
| 59 | + t.Flush() |
| 60 | + return nil |
| 61 | + case rwUnwrapper: |
| 62 | + rw = t.Unwrap() |
| 63 | + default: |
| 64 | + return errNotSupported() |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Hijack lets the caller take over the connection. |
| 70 | +// See the [Hijacker] interface for details. |
| 71 | +func (c *ResponseController) Hijack() (net.Conn, *bufio.ReadWriter, error) { |
| 72 | + rw := c.rw |
| 73 | + for { |
| 74 | + switch t := rw.(type) { |
| 75 | + case Hijacker: |
| 76 | + return t.Hijack() |
| 77 | + case rwUnwrapper: |
| 78 | + rw = t.Unwrap() |
| 79 | + default: |
| 80 | + return nil, nil, errNotSupported() |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// SetReadDeadline sets the deadline for reading the entire request, including the body. |
| 86 | +// Reads from the request body after the deadline has been exceeded will return an error. |
| 87 | +// A zero value means no deadline. |
| 88 | +// |
| 89 | +// Setting the read deadline after it has been exceeded will not extend it. |
| 90 | +func (c *ResponseController) SetReadDeadline(deadline time.Time) error { |
| 91 | + rw := c.rw |
| 92 | + for { |
| 93 | + switch t := rw.(type) { |
| 94 | + case interface{ SetReadDeadline(time.Time) error }: |
| 95 | + return t.SetReadDeadline(deadline) |
| 96 | + case rwUnwrapper: |
| 97 | + rw = t.Unwrap() |
| 98 | + default: |
| 99 | + return errNotSupported() |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// SetWriteDeadline sets the deadline for writing the response. |
| 105 | +// Writes to the response body after the deadline has been exceeded will not block, |
| 106 | +// but may succeed if the data has been buffered. |
| 107 | +// A zero value means no deadline. |
| 108 | +// |
| 109 | +// Setting the write deadline after it has been exceeded will not extend it. |
| 110 | +func (c *ResponseController) SetWriteDeadline(deadline time.Time) error { |
| 111 | + rw := c.rw |
| 112 | + for { |
| 113 | + switch t := rw.(type) { |
| 114 | + case interface{ SetWriteDeadline(time.Time) error }: |
| 115 | + return t.SetWriteDeadline(deadline) |
| 116 | + case rwUnwrapper: |
| 117 | + rw = t.Unwrap() |
| 118 | + default: |
| 119 | + return errNotSupported() |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// EnableFullDuplex indicates that the request handler will interleave reads from [Request.Body] |
| 125 | +// with writes to the [ResponseWriter]. |
| 126 | +// |
| 127 | +// For HTTP/1 requests, the Go HTTP server by default consumes any unread portion of |
| 128 | +// the request body before beginning to write the response, preventing handlers from |
| 129 | +// concurrently reading from the request and writing the response. |
| 130 | +// Calling EnableFullDuplex disables this behavior and permits handlers to continue to read |
| 131 | +// from the request while concurrently writing the response. |
| 132 | +// |
| 133 | +// For HTTP/2 requests, the Go HTTP server always permits concurrent reads and responses. |
| 134 | +func (c *ResponseController) EnableFullDuplex() error { |
| 135 | + rw := c.rw |
| 136 | + for { |
| 137 | + switch t := rw.(type) { |
| 138 | + case interface{ EnableFullDuplex() error }: |
| 139 | + return t.EnableFullDuplex() |
| 140 | + case rwUnwrapper: |
| 141 | + rw = t.Unwrap() |
| 142 | + default: |
| 143 | + return errNotSupported() |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// errNotSupported returns an error that Is ErrNotSupported, |
| 149 | +// but is not == to it. |
| 150 | +func errNotSupported() error { |
| 151 | + return fmt.Errorf("%w", ErrNotSupported) |
| 152 | +} |
0 commit comments