package winrm

import (
	
	
	
	
	
	
	
	

	
)

var soapXML = "application/soap+xml"

// body func reads the response body and return it as a string
func body( *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")
}

type clientRequest struct {
	transport http.RoundTripper
	dial      func(network, addr string) (net.Conn, error)
	proxyfunc func(req *http.Request) (*url.URL, error)
}

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

	if .dial != nil {
		 = .dial
	}

	 := http.ProxyFromEnvironment
	if .proxyfunc != nil {
		 = .proxyfunc
	}

	//nolint:gosec
	 := &http.Transport{
		Proxy: ,
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: .Insecure,
			ServerName:         .TLSServerName,
		},
		Dial:                  ,
		ResponseHeaderTimeout: .Timeout,
	}

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

		.TLSClientConfig.RootCAs = 
	}

	.transport = 

	return nil
}

// Post make post to the winrm soap service
func ( clientRequest) ( *Client,  *soap.SoapMessage) (string, error) {
	 := &http.Client{Transport: .transport}

	//nolint:noctx
	,  := 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")
	.SetBasicAuth(.username, .password)
	,  := .Do()
	if  != nil {
		return "", fmt.Errorf("unknown error %w", )
	}

	,  := body()
	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 , 
}

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

//NewClientWithProxyFunc NewClientWithProxyFunc
func ( func( *http.Request) (*url.URL, error)) *clientRequest {
	return &clientRequest{
		proxyfunc: ,
	}
}