-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add Request.CloneWithBody and Request.SetBody #105
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,12 +57,9 @@ type Request struct { | |
// reads may return immediately with EOF. For outgoing requests, the body is | ||
// optional. A body may only be read once. | ||
// | ||
// It is possible to assign the unread body of the incoming client request | ||
// to the body field of a different request. When that second request is | ||
// sent, the body will be efficiently streamed from the incoming request. | ||
// | ||
// It is also possible to assign the unread body of a received response to | ||
// the body field of a request, with the same results. | ||
// Prefer using the SetBody method over assigning to this value directly, | ||
// as it enables optimizations when sending outgoing requests. See the | ||
// SetBody documentation for more information. | ||
Body io.ReadCloser | ||
|
||
// Host is the hostname parsed from the incoming request URL. | ||
|
@@ -116,11 +113,16 @@ func NewRequest(method string, uri string, body io.Reader) (*Request, error) { | |
return nil, err | ||
} | ||
|
||
rc, ok := body.(io.ReadCloser) | ||
if !ok && body != nil { | ||
rc = nopCloser{body} | ||
} | ||
|
||
return &Request{ | ||
Method: method, | ||
URL: u, | ||
Header: NewHeader(), | ||
Body: makeBodyFor(body), | ||
Body: rc, | ||
Host: u.Host, | ||
}, nil | ||
} | ||
|
@@ -209,8 +211,30 @@ func newClientRequest() (*Request, error) { | |
}, nil | ||
} | ||
|
||
// SetBody sets the [Request]'s body to the provided [io.Reader]. Prefer | ||
// using this method over setting the Body field directly, as it enables | ||
// optimizations in the runtime. | ||
// | ||
// If an unread body from an incoming client request is set on an | ||
// outgoing upstream request, the body will be efficiently streamed from | ||
// the incoming request. It is also possible to set the unread body of | ||
// a received response to the body of a request, with the same results. | ||
// | ||
// If the body is set from an in-memory reader such as [bytes.Buffer], | ||
// [bytes.Reader], or [strings.Reader], the runtime will send the | ||
// request with a Content-Length header instead of Transfer-Encoding: | ||
// chunked. | ||
func (req *Request) SetBody(body io.Reader) { | ||
rc, ok := body.(io.ReadCloser) | ||
if !ok && body != nil { | ||
rc = nopCloser{body} | ||
} | ||
|
||
req.Body = rc | ||
} | ||
|
||
// Clone returns a copy of the request. The returned copy will have a nil Body | ||
// field, and it's URL will have a nil User field. | ||
// field, and its URL will have a nil User field. | ||
func (req *Request) Clone() *Request { | ||
return &Request{ | ||
Method: req.Method, | ||
|
@@ -230,6 +254,14 @@ func (req *Request) Clone() *Request { | |
} | ||
} | ||
|
||
// CloneWithBody returns a copy of the request, with the Body field set | ||
// to the provided io.Reader. Its URL will have a nil User field. | ||
func (req *Request) CloneWithBody(body io.Reader) *Request { | ||
Comment on lines
+257
to
+259
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree this could be removed now, perhaps by including the two lines of code into the doc of |
||
r := req.Clone() | ||
r.SetBody(body) | ||
return r | ||
} | ||
|
||
func cloneURL(u *url.URL) *url.URL { | ||
return &url.URL{ | ||
Scheme: u.Scheme, | ||
|
@@ -555,18 +587,6 @@ func abiBodyFrom(rc io.ReadCloser) (*fastly.HTTPBody, error) { | |
return b, nil | ||
} | ||
|
||
func makeBodyFor(r io.Reader) io.ReadCloser { | ||
if r == nil { | ||
return nil | ||
} | ||
|
||
if b, ok := r.(*fastly.HTTPBody); ok { | ||
return b | ||
} | ||
|
||
return nopCloser{r} | ||
} | ||
|
||
func safePollInterval(d time.Duration) time.Duration { | ||
const ( | ||
min = 1 * time.Millisecond | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit of a learning question here, but is it possible to have the case of an incoming request with a content-length brought to this body assignment as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realized this isn't super clear, but I think
Clone
already handles this by copying the header if present.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but the runtime will remove
Content-Length
andTransfer-Encoding
before sending the request unlessManualFramingMode
is true.