Skip to content

Commit

Permalink
consume http entity in synchronized block
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Jan 4, 2023
1 parent 7751332 commit 5ffbb74
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions src/main/java/monero/common/MoneroRpcConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,22 +279,23 @@ public Map<String, Object> sendJsonRequest(String method, Object params, Long ti
post.setConfig(getTimeoutConfig(timeoutInMs));
HttpEntity entity = new StringEntity(JsonUtils.serialize(body));
post.setEntity(entity);
Map<String, Object> respMap;
synchronized (this) {
resp = client.execute(post);
}

// validate response
validateHttpResponse(resp);

// validate response
validateHttpResponse(resp);

// deserialize response
Map<String, Object> respMap = JsonUtils.toMap(MAPPER, EntityUtils.toString(resp.getEntity(), "UTF-8"));
EntityUtils.consume(resp.getEntity());
if (MoneroUtils.getLogLevel() >= 3) {
String respStr = JsonUtils.serialize(respMap);
respStr = respStr.substring(0, Math.min(10000, respStr.length()));
MoneroUtils.log(3, "Received json response: " + respStr);
// deserialize response
respMap = JsonUtils.toMap(MAPPER, EntityUtils.toString(resp.getEntity(), "UTF-8"));
EntityUtils.consume(resp.getEntity());
if (MoneroUtils.getLogLevel() >= 3) {
String respStr = JsonUtils.serialize(respMap);
respStr = respStr.substring(0, Math.min(10000, respStr.length()));
MoneroUtils.log(3, "Received json response: " + respStr);
}
}

// check rpc response for errors
validateRpcResponse(respMap, method, params);
return respMap;
Expand Down Expand Up @@ -356,20 +357,21 @@ public Map<String, Object> sendPathRequest(String path, Map<String, Object> para
post.setEntity(entity);
}
post.setConfig(getTimeoutConfig(timeoutInMs));
Map<String, Object> respMap;
synchronized (this) {
resp = client.execute(post);
}

// validate response
validateHttpResponse(resp);

// deserialize response
Map<String, Object> respMap = JsonUtils.toMap(MAPPER, EntityUtils.toString(resp.getEntity(), "UTF-8"));
EntityUtils.consume(resp.getEntity());
if (MoneroUtils.getLogLevel() >= 3) {
String respStr = JsonUtils.serialize(respMap);
respStr = respStr.substring(0, Math.min(10000, respStr.length()));
MoneroUtils.log(3, "Received path response: " + respStr);
// validate response
validateHttpResponse(resp);
// deserialize response
respMap = JsonUtils.toMap(MAPPER, EntityUtils.toString(resp.getEntity(), "UTF-8"));
EntityUtils.consume(resp.getEntity());
if (MoneroUtils.getLogLevel() >= 3) {
String respStr = JsonUtils.serialize(respMap);
respStr = respStr.substring(0, Math.min(10000, respStr.length()));
MoneroUtils.log(3, "Received path response: " + respStr);
}
}

// check rpc response for errors
Expand Down Expand Up @@ -412,23 +414,27 @@ public byte[] sendBinaryRequest(String path, Map<String, Object> params, Long ti
CloseableHttpResponse resp = null;
try {

// send http request
// create http request
if (MoneroUtils.getLogLevel() >= 2) MoneroUtils.log(2, "Sending binary request with path '" + path + "' and params: " + JsonUtils.serialize(params));
HttpPost post = new HttpPost(uri.toString() + "/" + path);
post.setConfig(getTimeoutConfig(timeoutInMs));
if (paramsBin != null) {
HttpEntity entity = new ByteArrayEntity(paramsBin, ContentType.DEFAULT_BINARY);
post.setEntity(entity);
}

// send http request
synchronized (this) {
resp = client.execute(post);

// validate response
validateHttpResponse(resp);

// deserialize response
byte[] entity = EntityUtils.toByteArray(resp.getEntity());
EntityUtils.consume(resp.getEntity());
return entity;
}

// validate response
validateHttpResponse(resp);

// deserialize response
return EntityUtils.toByteArray(resp.getEntity());
} catch (MoneroRpcError e1) {
throw e1;
} catch (Exception e2) {
Expand Down

0 comments on commit 5ffbb74

Please sign in to comment.