Skip to content

Commit bb74298

Browse files
committed
Don't try to parse body if HTTP status is not 200
1 parent 7e40f93 commit bb74298

2 files changed

Lines changed: 21 additions & 50 deletions

File tree

auth.go

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package winrm
22

33
import (
4+
"crypto/tls"
45
"fmt"
5-
"io/ioutil"
66
"net"
7+
"net/http"
78
"strings"
89
"time"
910

10-
"github.com/masterzen/azure-sdk-for-go/core/http"
11-
"github.com/masterzen/azure-sdk-for-go/core/tls"
12-
1311
"github.com/masterzen/winrm/soap"
1412
)
1513

@@ -50,28 +48,6 @@ func (c *ClientAuthRequest) Transport(endpoint *Endpoint) error {
5048
return nil
5149
}
5250

53-
// parse func reads the response body and return it as a string
54-
func parse(response *http.Response) (string, error) {
55-
56-
// if we recived the content we expected
57-
if strings.Contains(response.Header.Get("Content-Type"), "application/soap+xml") {
58-
body, err := ioutil.ReadAll(response.Body)
59-
defer func() {
60-
// defer can modify the returned value before
61-
// it is actually passed to the calling statement
62-
if errClose := response.Body.Close(); errClose != nil && err == nil {
63-
err = errClose
64-
}
65-
}()
66-
if err != nil {
67-
return "", fmt.Errorf("error while reading request body %s", err)
68-
}
69-
70-
return string(body), nil
71-
}
72-
73-
return "", fmt.Errorf("invalid content type")
74-
}
7551

7652
func (c ClientAuthRequest) Post(client *Client, request *soap.SoapMessage) (string, error) {
7753
httpClient := &http.Client{Transport: c.transport}
@@ -89,18 +65,15 @@ func (c ClientAuthRequest) Post(client *Client, request *soap.SoapMessage) (stri
8965
return "", fmt.Errorf("unknown error %s", err)
9066
}
9167

92-
body, err := parse(resp)
68+
// error in case on incorrect exit code
69+
if resp.StatusCode != 200 {
70+
return "", fmt.Errorf("http unexpected status: %s", resp.Status)
71+
}
72+
73+
body, err := ParseSoapResponse(resp)
9374
if err != nil {
9475
return "", fmt.Errorf("http response error: %d - %s", resp.StatusCode, err.Error())
9576
}
9677

97-
// if we have different 200 http status code
98-
// we must replace the error
99-
defer func() {
100-
if resp.StatusCode != 200 {
101-
body, err = "", fmt.Errorf("http error %d: %s", resp.StatusCode, body)
102-
}
103-
}()
104-
10578
return body, err
10679
}

http.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@ package winrm
33
import (
44
"crypto/tls"
55
"fmt"
6-
"io/ioutil"
76
"net"
87
"net/http"
98
"strings"
109
"time"
1110

1211
"github.com/masterzen/winrm/soap"
12+
"io/ioutil"
1313
)
1414

1515
var soapXML = "application/soap+xml"
1616

17-
// body func reads the response body and return it as a string
18-
func body(response *http.Response) (string, error) {
17+
// parse func reads the response body and return it as a string
18+
func ParseSoapResponse(response *http.Response) (string, error) {
1919

20-
// if we recived the content we expected
21-
if strings.Contains(response.Header.Get("Content-Type"), "application/soap+xml") {
20+
contentType := response.Header.Get("Content-Type")
21+
// if we received the content we expected
22+
if strings.Contains(contentType, soapXML) {
2223
body, err := ioutil.ReadAll(response.Body)
2324
defer func() {
2425
// defer can modify the returned value before
@@ -34,7 +35,7 @@ func body(response *http.Response) (string, error) {
3435
return string(body), nil
3536
}
3637

37-
return "", fmt.Errorf("invalid content type")
38+
return "", fmt.Errorf("invalid content type: %s", contentType)
3839
}
3940

4041
type clientRequest struct {
@@ -84,18 +85,15 @@ func (c clientRequest) Post(client *Client, request *soap.SoapMessage) (string,
8485
return "", fmt.Errorf("unknown error %s", err)
8586
}
8687

87-
body, err := body(resp)
88+
// error in case on incorrect exit code
89+
if resp.StatusCode != 200 {
90+
return "", fmt.Errorf("http unexpected status %s", resp.Status)
91+
}
92+
93+
body, err := ParseSoapResponse(resp)
8894
if err != nil {
8995
return "", fmt.Errorf("http response error: %d - %s", resp.StatusCode, err.Error())
9096
}
9197

92-
// if we have different 200 http status code
93-
// we must replace the error
94-
defer func() {
95-
if resp.StatusCode != 200 {
96-
body, err = "", fmt.Errorf("http error %d: %s", resp.StatusCode, body)
97-
}
98-
}()
99-
10098
return body, err
10199
}

0 commit comments

Comments
 (0)