package web

import (
	"net/http"

	"github.com/concourse/go-concourse/concourse"
)

type ClientFactory interface {
	Build(request *http.Request) concourse.Client
}

type clientFactory struct {
	apiEndpoint string
}

func NewClientFactory(apiEndpoint string) ClientFactory {
	return &clientFactory{
		apiEndpoint: apiEndpoint,
	}
}

func (cf *clientFactory) Build(r *http.Request) concourse.Client {
	transport := authorizationTransport{
		Authorization: r.Header.Get("Authorization"),

		Base: &http.Transport{
			// disable connection pooling
			DisableKeepAlives: true,
		},
	}

	httpClient := &http.Client{
		Transport: transport,
	}

	return concourse.NewClient(cf.apiEndpoint, httpClient)
}

type authorizationTransport struct {
	Base          http.RoundTripper
	Authorization string
}

func (transport authorizationTransport) RoundTrip(r *http.Request) (*http.Response, error) {
	r.Header.Set("Authorization", transport.Authorization)
	return transport.Base.RoundTrip(r)
}

package web

import (
	"crypto/tls"
	"net/http"

	"github.com/concourse/go-concourse/concourse"
)

type ClientFactory interface {
	Build(request *http.Request) concourse.Client
}

type clientFactory struct {
	apiEndpoint                 string
	allowSelfSignedCertificates bool
}

func NewClientFactory(apiEndpoint string, allowSelfSignedCertificates bool) ClientFactory {
	return &clientFactory{
		apiEndpoint:                 apiEndpoint,
		allowSelfSignedCertificates: allowSelfSignedCertificates,
	}
}

func (cf *clientFactory) Build(r *http.Request) concourse.Client {
	transport := authorizationTransport{
		Authorization: r.Header.Get("Authorization"),

		Base: &http.Transport{

			DisableKeepAlives: true, // disable connection pooling
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: cf.allowSelfSignedCertificates,
			},
		},
	}

	httpClient := &http.Client{
		Transport: transport,
	}

	return concourse.NewClient(cf.apiEndpoint, httpClient)
}

type authorizationTransport struct {
	Base          http.RoundTripper
	Authorization string
}

func (transport authorizationTransport) RoundTrip(r *http.Request) (*http.Response, error) {
	r.Header.Set("Authorization", transport.Authorization)
	return transport.Base.RoundTrip(r)
}

Use external_url when TLS enabled or IP address otherwise - Allow self-signed certificates only when in development mode
Use external_url when TLS enabled or IP address otherwise
- Allow self-signed certificates only when in development mode

[#119388165]

Signed-off-by: Jacob Frank <e2b78b2ae98d5304f4676b7cd3c5892a0e3e7c9f@pivotal.io>
