1
0
mirror of https://github.com/mayflower/docker-ls.git synced 2025-11-28 00:01:09 +03:00

Support authentification via basic auth.

This commit is contained in:
Christian Speckner
2016-02-26 13:25:34 +01:00
parent 17a44c1a20
commit 1eae834b66
6 changed files with 93 additions and 16 deletions

View File

@@ -0,0 +1,57 @@
package connector
import (
"net/http"
"net/url"
"strings"
)
type basicAuthConnector struct {
cfg Config
httpClient *http.Client
semaphore semaphore
stat *statistics
}
func (r *basicAuthConnector) Delete(url *url.URL, hint string) (*http.Response, error) {
return r.Request("DELETE", url, hint)
}
func (r *basicAuthConnector) Get(url *url.URL, hint string) (*http.Response, error) {
return r.Request("GET", url, hint)
}
func (r *basicAuthConnector) GetStatistics() Statistics {
return r.stat
}
func (r *basicAuthConnector) Request(method string, url *url.URL, hint string) (response *http.Response, err error) {
r.semaphore.Lock()
defer r.semaphore.Unlock()
r.stat.Request()
request, err := http.NewRequest(method, url.String(), strings.NewReader(""))
if err != nil {
return
}
credentials := r.cfg.Credentials()
if credentials.Password() != "" || credentials.User() != "" {
request.SetBasicAuth(credentials.User(), credentials.Password())
}
response, err = r.httpClient.Do(request)
return
}
func NewBasicAuthConnector(cfg Config) Connector {
return &basicAuthConnector{
cfg: cfg,
httpClient: http.DefaultClient,
semaphore: newSemaphore(cfg.MaxConcurrentRequests()),
stat: new(statistics),
}
}