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

Remove egctl auto change protocol #1162

Merged
Merged
Changes from 1 commit
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
Next Next commit
udpate egctl use https protocol strategy
  • Loading branch information
suchen-sci committed Dec 7, 2023
commit 776d48d3b739e85fcbdd28f217a662d71dad4f77
23 changes: 17 additions & 6 deletions cmd/client/general/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,26 @@ 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)
if err != nil {
return nil, err
resp.Body.Close()
return nil, fmt.Errorf("read response body failed: %v", err)
}
resp.Body.Close()

// 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 +208,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