Skip to content

Commit

Permalink
CLI | Add redirected request mechanism (AST-37864) (#698)
Browse files Browse the repository at this point in the history
* Add redirected request mechanism
* try fix sast vulnerabilities
---------

Co-authored-by: AlvoBen <[email protected]>
  • Loading branch information
AlvoBen and BenAlvo1 authored Apr 8, 2024
1 parent 5b44ada commit 253b380
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions internal/wrappers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,21 @@ func request(client *http.Client, req *http.Request, responseBody bool) (*http.R
logger.PrintIfVerbose(err.Error())
}
if resp != nil && err == nil {
if hasRedirectStatusCode(resp) {
redirectURL := resp.Header.Get("Location")
if redirectURL == "" {
return nil, fmt.Errorf("redirect URL not found in response")
}
method := GetHTTPMethod(req)
if method == "" {
return nil, fmt.Errorf("method not found in request")
}
req, err = http.NewRequest(method, redirectURL, bytes.NewReader(body))
if err != nil {
return nil, err
}
continue
}
logger.PrintResponse(resp, responseBody)
return resp, nil
}
Expand All @@ -588,6 +603,29 @@ func request(client *http.Client, req *http.Request, responseBody bool) (*http.R
return nil, err
}

func GetHTTPMethod(req *http.Request) string {
switch req.Method {
case http.MethodGet:
return http.MethodGet
case http.MethodPost:
return http.MethodPost
case http.MethodPut:
return http.MethodPut
case http.MethodDelete:
return http.MethodDelete
case http.MethodOptions:
return http.MethodOptions
case http.MethodPatch:
return http.MethodPatch
default:
return ""
}
}

func hasRedirectStatusCode(resp *http.Response) bool {
return resp.StatusCode == http.StatusTemporaryRedirect || resp.StatusCode == http.StatusMovedPermanently
}

func getAuthURI() (string, error) {
var authURI string
var err error
Expand Down

0 comments on commit 253b380

Please sign in to comment.