Skip to content

Commit

Permalink
Remove egctl auto change protocol (#1162)
Browse files Browse the repository at this point in the history
* udpate egctl use https protocol strategy

* fix typo
  • Loading branch information
suchen-sci committed Dec 7, 2023
1 parent 9e26c89 commit e8dc3e8
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions cmd/client/general/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,25 @@ func HandleReqWithStreamResp(httpMethod string, path string, yamlBody []byte) (i
}

if strings.HasPrefix(url, HTTPProtocol) && resp.StatusCode == http.StatusBadRequest {
resp, err = doRequest(httpMethod, HTTPSProtocol+strings.TrimPrefix(url, HTTPProtocol), jsonBody, client)
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
return nil, fmt.Errorf("read response body failed: %v", err)
}

// https://github.com/golang/go/blob/release-branch.go1.20/src/net/http/server.go#L1878-L1885
if strings.Contains(string(body), "Client sent an HTTP request to an HTTPS server") {
resp, err = doRequest(httpMethod, HTTPSProtocol+strings.TrimPrefix(url, HTTPProtocol), jsonBody, client)
if err != nil {
return nil, err
}
} else {
resp.Body = io.NopCloser(bytes.NewReader(body))
}
}

if !SuccessfulStatusCode(resp.StatusCode) {
defer func() {
resp.Body.Close()
}()
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read response body failed: %v", err)
Expand Down Expand Up @@ -198,7 +207,8 @@ func HandleRequest(httpMethod string, path string, yamlBody []byte) (body []byte
}

msg := string(body)
if strings.HasPrefix(url, HTTPProtocol) && resp.StatusCode == http.StatusBadRequest && strings.Contains(strings.ToUpper(msg), "HTTPS") {
// https://github.com/golang/go/blob/release-branch.go1.20/src/net/http/server.go#L1878-L1885
if strings.HasPrefix(url, HTTPProtocol) && resp.StatusCode == http.StatusBadRequest && strings.Contains(msg, "Client sent an HTTP request to an HTTPS server") {
resp, body, err = doRequestWithBody(httpMethod, HTTPSProtocol+strings.TrimPrefix(url, HTTPProtocol), jsonBody, client)
if err != nil {
return nil, err
Expand Down

0 comments on commit e8dc3e8

Please sign in to comment.