From 330f79b40e45e989078e4eca05bc40c3bce87f4b Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 22 Sep 2017 14:03:31 -0700 Subject: [PATCH] Remove pre go1.8 code and cleanup (#4933) We don't need certain go1.7.x custom code anymore, since we have migrated to go1.8 --- cmd/notify-webhook.go | 56 +++++++++----------------------------- cmd/notify-webhook_test.go | 2 +- 2 files changed, 14 insertions(+), 44 deletions(-) diff --git a/cmd/notify-webhook.go b/cmd/notify-webhook.go index 10cb80909..d3da2b8ea 100644 --- a/cmd/notify-webhook.go +++ b/cmd/notify-webhook.go @@ -111,7 +111,7 @@ func lookupEndpoint(urlStr string) error { client := &http.Client{ Timeout: 1 * time.Second, Transport: &http.Transport{ - // need to close connection after usage. + // Need to close connection after usage. DisableKeepAlives: true, }, } @@ -122,50 +122,20 @@ func lookupEndpoint(urlStr string) error { // Set proper server user-agent. req.Header.Set("User-Agent", globalServerUserAgent) - // Retry if the request needs to be re-directed. - // This code is necessary since Go 1.7.x do not - // support retrying for http 307 for POST operation. - // https://github.com/golang/go/issues/7912 - // - // FIXME: Remove this when we move to Go 1.8. - for { - resp, derr := client.Do(req) - if derr != nil { - if isNetErrorIgnored(derr) { - errorIf(derr, "Unable to lookup webhook endpoint %s", urlStr) - return nil - } - return derr + resp, err := client.Do(req) + if err != nil { + if isNetErrorIgnored(err) { + errorIf(err, "Unable to lookup webhook endpoint %s", urlStr) + return nil } - if resp == nil { - return fmt.Errorf("No response from server to download URL %s", urlStr) - } - resp.Body.Close() - - // Redo the request with the new redirect url if http 307 - // is returned, quit the loop otherwise - if resp.StatusCode == http.StatusTemporaryRedirect { - newURL, uerr := url.Parse(resp.Header.Get("Location")) - if uerr != nil { - return uerr - } - req.URL = newURL - continue - } - - // For any known successful http status, return quickly. - for _, httpStatus := range successStatus { - if httpStatus == resp.StatusCode { - return nil - } - } - - err = fmt.Errorf("Unexpected response from webhook server %s: (%s)", urlStr, resp.Status) - break + return err } - - // Succes. - return err + defer resp.Body.Close() + // HTTP status OK/NoContent. + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent { + return fmt.Errorf("Unable to lookup webhook endpoint %s response(%s)", urlStr, resp.Status) + } + return nil } // Initializes new webhook logrus notifier. diff --git a/cmd/notify-webhook_test.go b/cmd/notify-webhook_test.go index 18dd2bc01..55899464d 100644 --- a/cmd/notify-webhook_test.go +++ b/cmd/notify-webhook_test.go @@ -106,7 +106,7 @@ func TestLookupEndpoint(t *testing.T) { }, { endpoint: server.URL, - err: fmt.Errorf("Unexpected response from webhook server %s: (400 Bad Request)", server.URL), + err: fmt.Errorf("Unable to lookup webhook endpoint %s response(400 Bad Request)", server.URL), }, } for _, test := range testCases {