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

Bypass retryablehttp library error #22

Merged
merged 1 commit into from
Jan 13, 2020
Merged
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
65 changes: 61 additions & 4 deletions dx_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"strings"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -211,22 +212,71 @@ func NewHttpClient(pooled bool) *retryablehttp.Client {
}
}

// Bypass the retryablehttp library, it is causing an error.
//
// The error happens when doing an http PUT for a zero sized byte array.
// The library somehow mangles the aws signature key, causing AWS to
// reject the request with a 403 Forbidden code.
func dxHttpRequestCoreBypass(
ctx context.Context,
client *retryablehttp.Client,
requestType string,
url string,
headers map[string]string,
data []byte) ( []byte, error) {
var dataReader io.Reader
if data != nil {
dataReader = bytes.NewReader(data)
}
req, err := http.NewRequest(requestType, url, dataReader)
if err != nil {
return nil, err
}
for key, value := range headers {
req.Header.Set(key, value)
}
//log.Printf("dxHttpRequestCoreBypass req=%v", req)

resp, err := client.HTTPClient.Do(req)
if err != nil {
return nil, err
}
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
statusCode, statusHumanReadable := parseStatus(resp.Status)

// If the status is not in the 200-299 range, an error occured.
if !(isGood(statusCode)) {
httpError := HttpError{
Message : body,
StatusCode : statusCode,
StatusHumanReadable : statusHumanReadable,
}
return nil, &httpError
}

// good status
return body, nil
}


func dxHttpRequestCore(
ctx context.Context,
client *retryablehttp.Client,
requestType string,
url string,
headers map[string]string,
data []byte) ( []byte, error) {
req, err := retryablehttp.NewRequest(requestType, url, bytes.NewReader(data))
req, err := retryablehttp.NewRequest(requestType, url, data)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
for header, value := range headers {
req.Header.Set(header, value)
for key, value := range headers {
req.Header.Set(key, value)
}
resp, err := client.Do(req)

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -270,7 +320,14 @@ func DxHttpRequest(
attemptTimeout = minInt(2 * attemptTimeout, attemptTimeoutMax)
}

body, err := dxHttpRequestCore(ctx, client, requestType, url, headers, data)
var body []byte
var err error
if requestType == "PUT" && (data == nil || len(data) == 0) {
// circumvent an error in the retryablehttp library
body, err = dxHttpRequestCoreBypass(ctx, client, requestType, url, headers, data)
} else {
body, err = dxHttpRequestCore(ctx, client, requestType, url, headers, data)
}
if err == nil {
// http request went well, return the body
return body, nil
Expand Down