aboutsummaryrefslogblamecommitdiffstats
path: root/plumbing/client/http/common.go
blob: 24479958afcaff1e76d47209399d690af3e2ce59 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                    




                  
 

                                                         

 

                                                                               



                                
                                        



                                 
                                                                       







                                                         
                           












                                                                      

                                                                 


                               


                                                                                       


                          
                             
                                     
                                                      
                                 
                                                   

         
                                                   

 
                                                     
                                


                                    
                              
                                                                      


                                                              
// 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"
)

// AuthMethod is concrete implementation of common.AuthMethod for HTTP services
type AuthMethod 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 is 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)
}

// Err is a dedicated error to return errors based on status code
type Err struct {
	Response *http.Response
}

// NewErr returns a new Err based on a http response
func NewErr(r *http.Response) error {
	if r.StatusCode >= http.StatusOK && r.StatusCode < http.StatusMultipleChoices {
		return nil
	}

	switch r.StatusCode {
	case http.StatusUnauthorized:
		return common.ErrAuthorizationRequired
	case http.StatusNotFound:
		return common.ErrRepositoryNotFound
	}

	return plumbing.NewUnexpectedError(&Err{r})
}

// StatusCode returns the status code of the response
func (e *Err) StatusCode() int {
	return e.Response.StatusCode
}

func (e *Err) Error() string {
	return fmt.Sprintf("unexpected requesting %q status code: %d",
		e.Response.Request.URL, e.Response.StatusCode,
	)
}