Fix unmarshaling of HTTP POST responses.

If connecting to a bitcoin RPC server as an HTTP POST client, full
response objects rather than just the raw result bytes were being
passed to the specific result unmarshalers, causing unmarshal errors
for the incorrect JSON types.  To fix this, first unmarshal the
response body into a rawResponse, and pass only the raw result bytes
(or an error) to the specific handlers.

This was reported by nskelsey on IRC.

ok @davecgh
This commit is contained in:
Josh Rickmar 2014-06-18 10:16:09 -05:00
parent aceaed82c5
commit 22b6af1400

View file

@ -271,7 +271,7 @@ func (r rawResponse) result() (result []byte, err error) {
// handleMessage is the main handler for incoming notifications and responses.
func (c *Client) handleMessage(msg []byte) {
// Attempt to unmarshal the message as either a notifiation or response.
in := inMessage{}
var in inMessage
err := json.Unmarshal(msg, &in)
if err != nil {
log.Warnf("Remote server sent invalid message: %v", err)
@ -618,12 +618,19 @@ func (c *Client) handleSendPostMessage(details *sendPostDetails) {
}
// Read the raw bytes and close the response.
resp, err := btcjson.GetRaw(httpResponse.Body)
respBytes, err := btcjson.GetRaw(httpResponse.Body)
if err != nil {
details.responseChan <- &response{result: nil, err: err}
return
}
details.responseChan <- &response{result: resp, err: nil}
var resp rawResponse
err = json.Unmarshal(respBytes, &resp)
if err != nil {
details.responseChan <- &response{result: nil, err: err}
return
}
res, err := resp.result()
details.responseChan <- &response{result: res, err: err}
}
// sendPostHandler handles all outgoing messages when the client is running