diff --git a/auth.go b/auth.go index 2e7c375..1b5cc89 100644 --- a/auth.go +++ b/auth.go @@ -1,15 +1,13 @@ package winrm import ( + "crypto/tls" "fmt" - "io/ioutil" "net" + "net/http" "strings" "time" - "github.com/masterzen/azure-sdk-for-go/core/http" - "github.com/masterzen/azure-sdk-for-go/core/tls" - "github.com/masterzen/winrm/soap" ) @@ -50,28 +48,6 @@ func (c *ClientAuthRequest) Transport(endpoint *Endpoint) error { return nil } -// parse func reads the response body and return it as a string -func parse(response *http.Response) (string, error) { - - // if we recived the content we expected - if strings.Contains(response.Header.Get("Content-Type"), "application/soap+xml") { - body, err := ioutil.ReadAll(response.Body) - defer func() { - // defer can modify the returned value before - // it is actually passed to the calling statement - if errClose := response.Body.Close(); errClose != nil && err == nil { - err = errClose - } - }() - if err != nil { - return "", fmt.Errorf("error while reading request body %s", err) - } - - return string(body), nil - } - - return "", fmt.Errorf("invalid content type") -} func (c ClientAuthRequest) Post(client *Client, request *soap.SoapMessage) (string, error) { httpClient := &http.Client{Transport: c.transport} @@ -89,18 +65,15 @@ func (c ClientAuthRequest) Post(client *Client, request *soap.SoapMessage) (stri return "", fmt.Errorf("unknown error %s", err) } - body, err := parse(resp) + // error in case on incorrect exit code + if resp.StatusCode != 200 { + return "", fmt.Errorf("http unexpected status: %s", resp.Status) + } + + body, err := ParseSoapResponse(resp) if err != nil { return "", fmt.Errorf("http response error: %d - %s", resp.StatusCode, err.Error()) } - // if we have different 200 http status code - // we must replace the error - defer func() { - if resp.StatusCode != 200 { - body, err = "", fmt.Errorf("http error %d: %s", resp.StatusCode, body) - } - }() - return body, err } diff --git a/http.go b/http.go index 929ea82..48873bb 100644 --- a/http.go +++ b/http.go @@ -3,22 +3,23 @@ package winrm import ( "crypto/tls" "fmt" - "io/ioutil" "net" "net/http" "strings" "time" "github.com/masterzen/winrm/soap" + "io/ioutil" ) var soapXML = "application/soap+xml" -// body func reads the response body and return it as a string -func body(response *http.Response) (string, error) { +// parse func reads the response body and return it as a string +func ParseSoapResponse(response *http.Response) (string, error) { - // if we recived the content we expected - if strings.Contains(response.Header.Get("Content-Type"), "application/soap+xml") { + contentType := response.Header.Get("Content-Type") + // if we received the content we expected + if strings.Contains(contentType, soapXML) { body, err := ioutil.ReadAll(response.Body) defer func() { // defer can modify the returned value before @@ -34,7 +35,7 @@ func body(response *http.Response) (string, error) { return string(body), nil } - return "", fmt.Errorf("invalid content type") + return "", fmt.Errorf("invalid content type: %s", contentType) } type clientRequest struct { @@ -84,18 +85,15 @@ func (c clientRequest) Post(client *Client, request *soap.SoapMessage) (string, return "", fmt.Errorf("unknown error %s", err) } - body, err := body(resp) + // error in case of incorrect exit code + if resp.StatusCode != 200 { + return "", fmt.Errorf("http unexpected status: %s", resp.Status) + } + + body, err := ParseSoapResponse(resp) if err != nil { return "", fmt.Errorf("http response error: %d - %s", resp.StatusCode, err.Error()) } - // if we have different 200 http status code - // we must replace the error - defer func() { - if resp.StatusCode != 200 { - body, err = "", fmt.Errorf("http error %d: %s", resp.StatusCode, body) - } - }() - return body, err }