aboutsummaryrefslogtreecommitdiffstats
path: root/clients/http/common.go
blob: 483308a0a5d71e58b5dd24a1ef82609432a5f0de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Package http implements a HTTP client for go-git.
package http

import (
	"fmt"
	"net/http"

	"gopkg.in/src-d/go-git.v4/clients/common"
	"gopkg.in/src-d/go-git.v4/core"
)

// 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 core.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,
	)
}