Merge pull request #20 from yunginnanet/master
Connector: close response bodies
This commit is contained in:
commit
ce1163658f
|
@ -25,40 +25,50 @@ func NewHTTPConnector(url string) *HTTPConnector {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request encodes and executes a command on the HSM and returns the binary response
|
// Request encodes and executes a command on the HSM and returns the binary response
|
||||||
func (c *HTTPConnector) Request(command *commands.CommandMessage) ([]byte, error) {
|
func (c *HTTPConnector) Request(command *commands.CommandMessage) (data []byte, err error) {
|
||||||
requestData, err := command.Serialize()
|
var requestData []byte
|
||||||
|
requestData, err = command.Serialize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := http.DefaultClient.Post("http://"+c.URL+"/connector/api", "application/octet-stream", bytes.NewReader(requestData))
|
var res *http.Response
|
||||||
|
res, err = http.DefaultClient.Post("http://"+c.URL+"/connector/api", "application/octet-stream", bytes.NewReader(requestData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
closeErr := res.Body.Close()
|
||||||
|
if err == nil {
|
||||||
|
err = closeErr
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if res.StatusCode != http.StatusOK {
|
if res.StatusCode != http.StatusOK {
|
||||||
return nil, fmt.Errorf("server returned non OK status code %d", res.StatusCode)
|
err = fmt.Errorf("server returned non OK status code %d", res.StatusCode)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := ioutil.ReadAll(res.Body)
|
data, err = ioutil.ReadAll(res.Body)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return data, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStatus requests the status of the HSM connector route /connector/status
|
// GetStatus requests the status of the HSM connector route /connector/status
|
||||||
func (c *HTTPConnector) GetStatus() (*StatusResponse, error) {
|
func (c *HTTPConnector) GetStatus() (status *StatusResponse, err error) {
|
||||||
res, err := http.DefaultClient.Get("http://" + c.URL + "/connector/status")
|
var res *http.Response
|
||||||
|
res, err = http.DefaultClient.Get("http://" + c.URL + "/connector/status")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := ioutil.ReadAll(res.Body)
|
var data []byte
|
||||||
|
data, err = ioutil.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
bodyString := string(data)
|
bodyString := string(data)
|
||||||
pairs := strings.Split(bodyString, "\n")
|
pairs := strings.Split(bodyString, "\n")
|
||||||
|
|
||||||
|
@ -67,7 +77,7 @@ func (c *HTTPConnector) GetStatus() (*StatusResponse, error) {
|
||||||
values = append(values, strings.Split(pair, "=")...)
|
values = append(values, strings.Split(pair, "=")...)
|
||||||
}
|
}
|
||||||
|
|
||||||
status := &StatusResponse{}
|
status = &StatusResponse{}
|
||||||
status.Status = Status(values[1])
|
status.Status = Status(values[1])
|
||||||
status.Serial = values[3]
|
status.Serial = values[3]
|
||||||
status.Version = values[5]
|
status.Version = values[5]
|
||||||
|
@ -75,5 +85,7 @@ func (c *HTTPConnector) GetStatus() (*StatusResponse, error) {
|
||||||
status.Address = values[9]
|
status.Address = values[9]
|
||||||
status.Port = values[11]
|
status.Port = values[11]
|
||||||
|
|
||||||
return status, nil
|
err = res.Body.Close()
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue