<commit_before>package gogobosh

import (
	"bytes"
	"crypto/tls"
	"encoding/json"
	"fmt"
	"golang.org/x/net/context"
	"golang.org/x/oauth2"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"strings"
)

//Client used to communicate with BOSH
type Client struct {
	config   Config
	Endpoint Endpoint
}

//Config is used to configure the creation of a client
type Config struct {
	BOSHAddress       string
	Port              string
	Username          string
	Password          string
	UAAAuth           bool
	HttpClient        *http.Client
	SkipSslValidation bool
	TokenSource       oauth2.TokenSource
}

type Endpoint struct {
	URL string `json:"doppler_logging_endpoint"`
}

// request is used to help build up a request
type request struct {
	method string
	url    string
	header map[string]string
	params url.Values
	body   io.Reader
	obj    interface{}
}

//DefaultConfig configuration for client
func DefaultConfig() *Config {
	return &Config{
		BOSHAddress:       "https://192.168.50.4:25555",
		Username:          "admin",
		Password:          "<PASSWORD>",
		HttpClient:        http.DefaultClient,
		SkipSslValidation: true,
	}
}

func DefaultEndpoint() *Endpoint {
	return &Endpoint{
		URL: "https://192.168.50.4:8443",
	}
}

// NewClient returns a new client
func NewClient(config *Config) (*Client, error) {
	// bootstrap the config
	defConfig := DefaultConfig()

	if len(config.BOSHAddress) == 0 {
		config.BOSHAddress = defConfig.BOSHAddress
	}

	if len(config.Username) == 0 {
		config.Username = defConfig.Username
	}

	if len(config.Password) == 0 {
		config.Password = <PASSWORD>
	}

	endpoint := &Endpoint{}
	config.HttpClient = &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: config.SkipSslValidation,
			},
		},
	}
	authType, err := getAuthType(config.BOSHAddress, config.HttpClient)
	if err != nil {
		return nil, fmt.Errorf("Could not get auth type: %v", err)
	}
	if authType != "uaa" {
		config.HttpClient = &http.Client{
			CheckRedirect: func(req *http.Request, via []*http.Request) error {
				if len(via) > 10 {
					return fmt.Errorf("stopped after 10 redirects")
				}
				req.URL.Host = strings.TrimPrefix(config.BOSHAddress, req.URL.Scheme+"://")
				req.SetBasicAuth(config.Username, config.Password)
				return nil
			},
		}
	} else {
		ctx := oauth2.NoContext
		if config.SkipSslValidation == false {
			ctx = context.WithValue(ctx, oauth2.HTTPClient, defConfig.HttpClient)
		} else {
			tr := &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			}
			ctx = context.WithValue(ctx, oauth2.HTTPClient, &http.Client{Transport: tr})
		}

		endpoint, err := getUAAEndpoint(config.BOSHAddress, oauth2.NewClient(ctx, nil))

		if err != nil {
			return nil, fmt.Errorf("Could not get api /info: %v", err)
		}

		authConfig := &oauth2.Config{
			ClientID: "cf",<commit_msg>remove referer header when redirected to prevent bosh returning http forbidden<commit_after>package gogobosh

import (
	"bytes"
	"crypto/tls"
	"encoding/json"
	"fmt"
	"golang.org/x/net/context"
	"golang.org/x/oauth2"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"strings"
)

//Client used to communicate with BOSH
type Client struct {
	config   Config
	Endpoint Endpoint
}

//Config is used to configure the creation of a client
type Config struct {
	BOSHAddress       string
	Port              string
	Username          string
	Password          string
	UAAAuth           bool
	HttpClient        *http.Client
	SkipSslValidation bool
	TokenSource       oauth2.TokenSource
}

type Endpoint struct {
	URL string `json:"doppler_logging_endpoint"`
}

// request is used to help build up a request
type request struct {
	method string
	url    string
	header map[string]string
	params url.Values
	body   io.Reader
	obj    interface{}
}

//DefaultConfig configuration for client
func DefaultConfig() *Config {
	return &Config{
		BOSHAddress:       "https://192.168.50.4:25555",
		Username:          "admin",
		Password:          "<PASSWORD>",
		HttpClient:        http.DefaultClient,
		SkipSslValidation: true,
	}
}

func DefaultEndpoint() *Endpoint {
	return &Endpoint{
		URL: "https://192.168.50.4:8443",
	}
}

// NewClient returns a new client
func NewClient(config *Config) (*Client, error) {
	// bootstrap the config
	defConfig := DefaultConfig()

	if len(config.BOSHAddress) == 0 {
		config.BOSHAddress = defConfig.BOSHAddress
	}

	if len(config.Username) == 0 {
		config.Username = defConfig.Username
	}

	if len(config.Password) == 0 {
		config.Password = <PASSWORD>
	}

	endpoint := &Endpoint{}
	config.HttpClient = &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: config.SkipSslValidation,
			},
		},
	}
	authType, err := getAuthType(config.BOSHAddress, config.HttpClient)
	if err != nil {
		return nil, fmt.Errorf("Could not get auth type: %v", err)
	}
	if authType != "uaa" {
		config.HttpClient = &http.Client{
			CheckRedirect: func(req *http.Request, via []*http.Request) error {
				if len(via) > 10 {
					return fmt.Errorf("stopped after 10 redirects")
				}
				req.URL.Host = strings.TrimPrefix(config.BOSHAddress, req.URL.Scheme+"://")
				req.SetBasicAuth(config.Username, config.Password)
				req.Header.Add("User-Agent", "gogo-bosh")
				req.Header.Del("Referer")
				return nil
			},
		}
	} else {
		ctx := oauth2.NoContext
		if config.SkipSslValidation == false {
			ctx = context.WithValue(ctx, oauth2.HTTPClient, defConfig.HttpClient)
		} else {
			tr := &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			}
			ctx = context.WithValue(ctx, oauth2.HTTPClient, &http.Client{Transport: tr})
		}

		endpoint, err := getUAAEndpoint(config.BOSHAddress, oauth2.NewClient(ctx, nil))

		if err != nil {
			return nil, fmt.Errorf("Could not get api /info: %v", err)
		}

		authConfig := &oauth2.Config{
			ClientID: "cf",
{'fraction_non_alphanumeric': 0.12264631043256997, 'fraction_numerical': 0.013910093299406276, 'mean_word_length': 3.3545051698670605, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6358377', 'n_tokens_mistral': 2206, 'n_tokens_neox': 1996, 'n_words': 486}