Skip to content
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

on the first request, avoid posting body #10

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
package digest

import (
"bytes"
"crypto/md5"
"crypto/rand"
"crypto/sha256"
Expand Down Expand Up @@ -308,18 +307,14 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
origReq.Header[k] = s
}

// We'll need the request body twice. In some cases we can use GetBody
// to obtain a fresh reader for the second request, which we do right
// before the RoundTrip(origReq) call. If GetBody is unavailable, read
// the body into a memory buffer and use it for both requests.
if req.Body != nil && req.GetBody == nil {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
origReq.Body = ioutil.NopCloser(bytes.NewBuffer(body))

// Do not send the body on the first request
if req.Body != nil || req.GetBody == nil {
req.Body = nil
req.GetBody = nil
req.ContentLength = 0
}

// Make a request to get the 401 that contains the challenge.
challenge, resp, err := t.fetchChallenge(req)
if challenge == "" || err != nil {
Expand Down