Skip to content

Commit

Permalink
Merge pull request #129 from MartyHub/fix-add-tls-config-before-auth
Browse files Browse the repository at this point in the history
Add TLS Config before Auth
  • Loading branch information
danielgtaylor authored Nov 2, 2022
2 parents cff7978 + 0bcf2b5 commit 672c457
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions cli/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,50 +124,6 @@ func MakeRequest(req *http.Request, options ...requestOption) (*http.Response, e
// Save modified query string arguments.
req.URL.RawQuery = query.Encode()

// Add auth if needed.
if profile.Auth != nil && profile.Auth.Name != "" {
auth, ok := authHandlers[profile.Auth.Name]
if ok {
err := auth.OnRequest(req, name+":"+viper.GetString("rsh-profile"), profile.Auth.Params)
if err != nil {
panic(err)
}
}
}

if req.Header.Get("user-agent") == "" {
req.Header.Set("user-agent", "restish-"+Root.Version)
}

if req.Header.Get("accept") == "" {
req.Header.Set("accept", buildAcceptHeader())
}

if req.Header.Get("accept-encoding") == "" {
req.Header.Set("accept-encoding", buildAcceptEncodingHeader())
}

if req.Header.Get("content-type") == "" && req.Body != nil {
// We have a body but no content-type; default to JSON.
req.Header.Set("content-type", "application/json; charset=utf-8")
}

client := CachedTransport().Client()
if viper.GetBool("rsh-no-cache") {
client = &http.Client{Transport: InvalidateCachedTransport()}
}

log := true
for _, option := range options {
if option.client != nil {
client = option.client
}

if option.disableLog {
log = false
}
}

// The assumption is that all Transport implementations eventually use the
// default HTTP transport.
// We can therefore inject the TLS config once here, along with all the other
Expand Down Expand Up @@ -220,6 +176,50 @@ func MakeRequest(req *http.Request, options ...requestOption) (*http.Response, e
}
}

// Add auth if needed.
if profile.Auth != nil && profile.Auth.Name != "" {
auth, ok := authHandlers[profile.Auth.Name]
if ok {
err := auth.OnRequest(req, name+":"+viper.GetString("rsh-profile"), profile.Auth.Params)
if err != nil {
panic(err)
}
}
}

if req.Header.Get("user-agent") == "" {
req.Header.Set("user-agent", "restish-"+Root.Version)
}

if req.Header.Get("accept") == "" {
req.Header.Set("accept", buildAcceptHeader())
}

if req.Header.Get("accept-encoding") == "" {
req.Header.Set("accept-encoding", buildAcceptEncodingHeader())
}

if req.Header.Get("content-type") == "" && req.Body != nil {
// We have a body but no content-type; default to JSON.
req.Header.Set("content-type", "application/json; charset=utf-8")
}

client := CachedTransport().Client()
if viper.GetBool("rsh-no-cache") {
client = &http.Client{Transport: InvalidateCachedTransport()}
}

log := true
for _, option := range options {
if option.client != nil {
client = option.client
}

if option.disableLog {
log = false
}
}

if log {
LogDebugRequest(req)
}
Expand Down

0 comments on commit 672c457

Please sign in to comment.