package winrm

import (
	
	
	
	
	
	
	

	
)

//ClientAuthRequest ClientAuthRequest
type ClientAuthRequest struct {
	transport http.RoundTripper
	dial      func(network, addr string) (net.Conn, error)
}

//Transport Transport
func ( *ClientAuthRequest) ( *Endpoint) error {
	,  := tls.X509KeyPair(.Cert, .Key)
	if  != nil {
		return 
	}

	 := (&net.Dialer{
		Timeout:   30 * time.Second,
		KeepAlive: 30 * time.Second,
	}).Dial

	if .dial != nil {
		 = .dial
	}

	//nolint:gosec
	 := &http.Transport{
		Proxy: http.ProxyFromEnvironment,
		TLSClientConfig: &tls.Config{
			Renegotiation:      tls.RenegotiateOnceAsClient,
			InsecureSkipVerify: .Insecure,
			Certificates:       []tls.Certificate{},
		},
		Dial:                  ,
		ResponseHeaderTimeout: .Timeout,
	}

	if .CACert != nil && len(.CACert) > 0 {
		,  := readCACerts(.CACert)
		if  != nil {
			return 
		}

		.TLSClientConfig.RootCAs = 
	}

	.transport = 

	return nil
}

// parse func reads the response body and return it as a string
func parse( *http.Response) (string, error) {
	// if we received the content we expected
	if strings.Contains(.Header.Get("Content-Type"), "application/soap+xml") {
		,  := ioutil.ReadAll(.Body)
		defer func() {
			// defer can modify the returned value before
			// it is actually passed to the calling statement
			if  := .Body.Close();  != nil &&  == nil {
				 = 
			}
		}()
		if  != nil {
			return "", fmt.Errorf("error while reading request body %w", )
		}

		return string(), nil
	}

	return "", fmt.Errorf("invalid content type")
}

//Post Post
func ( ClientAuthRequest) ( *Client,  *soap.SoapMessage) (string, error) {
	 := &http.Client{Transport: .transport}

	,  := http.NewRequest("POST", .url, strings.NewReader(.String()))
	if  != nil {
		return "", fmt.Errorf("impossible to create http request %w", )
	}

	.Header.Set("Content-Type", soapXML+";charset=UTF-8")
	.Header.Set("Authorization", "http://schemas.dmtf.org/wbem/wsman/1/wsman/secprofile/https/mutual")

	,  := .Do()
	if  != nil {
		return "", fmt.Errorf("unknown error %w", )
	}

	,  := parse()
	if  != nil {
		return "", fmt.Errorf("http response error: %d - %w", .StatusCode, )
	}

	// if we have different 200 http status code
	// we must replace the error
	defer func() {
		if .StatusCode != 200 {
			,  = "", fmt.Errorf("http error %d: %s", .StatusCode, )
		}
	}()

	return , 
}

//NewClientAuthRequestWithDial NewClientAuthRequestWithDial
func ( func(,  string) (net.Conn, error)) *ClientAuthRequest {
	return &ClientAuthRequest{
		dial: ,
	}
}