package winrm
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/masterzen/winrm/soap"
)
var soapXML = "application/soap+xml"
func body(response *http .Response ) (string , error ) {
if strings .Contains (response .Header .Get ("Content-Type" ), "application/soap+xml" ) {
body , err := ioutil .ReadAll (response .Body )
defer func () {
if errClose := response .Body .Close (); errClose != nil && err == nil {
err = errClose
}
}()
if err != nil {
return "" , fmt .Errorf ("error while reading request body %w" , err )
}
return string (body ), 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 (c *clientRequest ) Transport (endpoint *Endpoint ) error {
dial := (&net .Dialer {
Timeout : 30 * time .Second ,
KeepAlive : 30 * time .Second ,
}).Dial
if c .dial != nil {
dial = c .dial
}
proxyfunc := http .ProxyFromEnvironment
if c .proxyfunc != nil {
proxyfunc = c .proxyfunc
}
transport := &http .Transport {
Proxy : proxyfunc ,
TLSClientConfig : &tls .Config {
InsecureSkipVerify : endpoint .Insecure ,
ServerName : endpoint .TLSServerName ,
},
Dial : dial ,
ResponseHeaderTimeout : endpoint .Timeout ,
}
if endpoint .CACert != nil && len (endpoint .CACert ) > 0 {
certPool , err := readCACerts (endpoint .CACert )
if err != nil {
return err
}
transport .TLSClientConfig .RootCAs = certPool
}
c .transport = transport
return nil
}
func (c clientRequest ) Post (client *Client , request *soap .SoapMessage ) (string , error ) {
httpClient := &http .Client {Transport : c .transport }
req , err := http .NewRequest ("POST" , client .url , strings .NewReader (request .String ()))
if err != nil {
return "" , fmt .Errorf ("impossible to create http request %w" , err )
}
req .Header .Set ("Content-Type" , soapXML +";charset=UTF-8" )
req .SetBasicAuth (client .username , client .password )
resp , err := httpClient .Do (req )
if err != nil {
return "" , fmt .Errorf ("unknown error %w" , err )
}
body , err := body (resp )
if err != nil {
return "" , fmt .Errorf ("http response error: %d - %w" , resp .StatusCode , err )
}
defer func () {
if resp .StatusCode != 200 {
body , err = "" , fmt .Errorf ("http error %d: %s" , resp .StatusCode , body )
}
}()
return body , err
}
func NewClientWithDial (dial func (network , addr string ) (net .Conn , error )) *clientRequest {
return &clientRequest {
dial : dial ,
}
}
func NewClientWithProxyFunc (proxyfunc func (req *http .Request ) (*url .URL , error )) *clientRequest {
return &clientRequest {
proxyfunc : proxyfunc ,
}
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .