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

Add TLS Config before Auth #129

Merged
Merged
Changes from all commits
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
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