diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-11-08 23:46:38 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-08 23:46:38 +0100 |
commit | ac095bb12c4d29722b60ba9f20590fa7cfa6bc7d (patch) | |
tree | 223f36f336ba3414b1e45cac8af6c4744a5d7ef6 /plumbing/client/http/common.go | |
parent | e523701393598f4fa241dd407af9ff8925507a1a (diff) | |
download | go-git-ac095bb12c4d29722b60ba9f20590fa7cfa6bc7d.tar.gz |
new plumbing package (#118)
* plumbing: now core was renamed to core, and formats and clients moved inside
Diffstat (limited to 'plumbing/client/http/common.go')
-rw-r--r-- | plumbing/client/http/common.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/plumbing/client/http/common.go b/plumbing/client/http/common.go new file mode 100644 index 0000000..4c07876 --- /dev/null +++ b/plumbing/client/http/common.go @@ -0,0 +1,77 @@ +// Package http implements a HTTP client for go-git. +package http + +import ( + "fmt" + "net/http" + + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/client/common" +) + +// HTTPAuthMethod concrete implementation of common.AuthMethod for HTTP services +type HTTPAuthMethod interface { + common.AuthMethod + setAuth(r *http.Request) +} + +// BasicAuth represent a HTTP basic auth +type BasicAuth struct { + username, password string +} + +// NewBasicAuth returns a BasicAuth base on the given user and password +func NewBasicAuth(username, password string) *BasicAuth { + return &BasicAuth{username, password} +} + +func (a *BasicAuth) setAuth(r *http.Request) { + r.SetBasicAuth(a.username, a.password) +} + +// Name name of the auth +func (a *BasicAuth) Name() string { + return "http-basic-auth" +} + +func (a *BasicAuth) String() string { + masked := "*******" + if a.password == "" { + masked = "<empty>" + } + + return fmt.Sprintf("%s - %s:%s", a.Name(), a.username, masked) +} + +// HTTPError a dedicated error to return errors bases on status codes +type HTTPError struct { + Response *http.Response +} + +// NewHTTPError returns a new HTTPError based on a http response +func NewHTTPError(r *http.Response) error { + if r.StatusCode >= 200 && r.StatusCode < 300 { + return nil + } + + switch r.StatusCode { + case 401: + return common.ErrAuthorizationRequired + case 404: + return common.ErrRepositoryNotFound + } + + err := &HTTPError{r} + return plumbing.NewUnexpectedError(err) +} + +// StatusCode returns the status code of the response +func (e *HTTPError) StatusCode() int { + return e.Response.StatusCode +} + +func (e *HTTPError) Error() string { + return fmt.Sprintf("unexpected requesting %q status code: %d", + e.Response.Request.URL, e.Response.StatusCode, + ) +} |