-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't try to parse body if HTTP status is not 200 #81
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem here is that if you get an operation timeout (which is also an HTTP 500 error), you'll return an error here that the upper layer will not "ignore" anymore (see Note that So, I think we first have to check if the body is a |
||
} | ||
|
||
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this means we're breaking the compatibility with Go < 1.8.
But maybe we don't care anymore nowadays...