Skip to content

Commit fd9fe8b

Browse files
left-armArtem Mironov
and
Artem Mironov
authored
Avoid HTTP request body cloning. (#574)
Consume body passed to post_message(body, url) and update callers. Fold some long lines while I'm here to improve code readability. Signed-off-by: Artem Mironov <artem.mironov@absa.africa> Signed-off-by: Artem Mironov <artem.mironov@absa.africa> Co-authored-by: Artem Mironov <artem.mironov@absa.africa>
1 parent ad10687 commit fd9fe8b

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

agency_client/src/api/agent.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl AgencyClient {
2626
agent_vk,
2727
)
2828
.await?;
29-
let response = self.post_to_agency(&data).await?;
29+
let response = self.post_to_agency(data).await?;
3030
let mut response = self.parse_response_from_agency(&response).await?;
3131

3232
match response.remove(0) {
@@ -56,7 +56,7 @@ impl AgencyClient {
5656
let data = self
5757
.prepare_message_for_agent(&Client2AgencyMessage::CreateKey(message), &agent_pwdid)
5858
.await?;
59-
let response = self.post_to_agency(&data).await?;
59+
let response = self.post_to_agency(data).await?;
6060
let mut response = self.parse_response_from_agency(&response).await?;
6161

6262
match response.remove(0) {
@@ -81,7 +81,7 @@ impl AgencyClient {
8181
let agent_did = self.get_agent_pwdid();
8282
let message = Client2AgencyMessage::UpdateComMethod(UpdateComMethod::build(com_method));
8383
let data = self.prepare_message_for_agent(&message, &agent_did).await?;
84-
let response = self.post_to_agency(&data).await?;
84+
let response = self.post_to_agency(data).await?;
8585
self.parse_response_from_agency(&response).await?;
8686

8787
Ok(())

agency_client/src/api/messaging.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl AgencyClient {
3131
&agent_did,
3232
)
3333
.await?;
34-
let response = self.post_to_agency(&data).await?;
34+
let response = self.post_to_agency(data).await?;
3535
let mut response = self.parse_response_from_agency(&response).await?;
3636

3737
match response.remove(0) {
@@ -69,7 +69,7 @@ impl AgencyClient {
6969
let data = self
7070
.prepare_message_for_connection_agent(vec![message], to_pw_vk, agent_did, agent_vk)
7171
.await?;
72-
let response = self.post_to_agency(&data).await?;
72+
let response = self.post_to_agency(data).await?;
7373
let mut response = self.parse_response_from_agency(&response).await?;
7474

7575
match response.remove(0) {

agency_client/src/httpclient.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ lazy_static! {
2323
.unwrap();
2424
}
2525

26-
pub async fn post_message(body_content: &Vec<u8>, url: &str) -> AgencyClientResult<Vec<u8>> {
26+
pub async fn post_message(body_content: Vec<u8>, url: &str) -> AgencyClientResult<Vec<u8>> {
2727
if mocking::agency_mocks_enabled() {
2828
if HttpClientMockResponse::has_response() {
2929
warn!("post_message >> mocking response for POST {}", url);
@@ -42,7 +42,8 @@ pub async fn post_message(body_content: &Vec<u8>, url: &str) -> AgencyClientResu
4242
return Ok(mocked_response);
4343
}
4444

45-
//Setting SSL Certs location. This is needed on android platform. Or openssl will fail to verify the certs
45+
// Setting SSL Certs location. This is needed on android
46+
// platform. Or openssl will fail to verify the certs
4647
if cfg!(target_os = "android") {
4748
info!("::Android code");
4849
set_ssl_cert_location();
@@ -52,7 +53,7 @@ pub async fn post_message(body_content: &Vec<u8>, url: &str) -> AgencyClientResu
5253

5354
let response = HTTP_CLIENT
5455
.post(url)
55-
.body(body_content.to_owned())
56+
.body(body_content)
5657
.header(CONTENT_TYPE, "application/ssi-agent-wire")
5758
.header(USER_AGENT, "reqwest")
5859
.send()
@@ -71,19 +72,32 @@ pub async fn post_message(body_content: &Vec<u8>, url: &str) -> AgencyClientResu
7172
if response_status.is_success() {
7273
Ok(payload.into_bytes())
7374
} else {
74-
Err(AgencyClientError::from_msg(AgencyClientErrorKind::PostMessageFailed, format!("POST {} failed due to non-success HTTP status: {}, response body: {}", url, response_status, payload)))
75+
Err(AgencyClientError::from_msg(
76+
AgencyClientErrorKind::PostMessageFailed,
77+
format!("POST {} failed due to non-success HTTP status: {}, response body: {}",
78+
url, response_status, payload)))
7579
}
7680
}
77-
Err(error) => Err(AgencyClientError::from_msg(AgencyClientErrorKind::PostMessageFailed, format!("POST {} failed because response could not be decoded as utf-8, HTTP status: {}, content-length header: {:?}, error: {:?}", url, response_status, content_length, error))),
81+
Err(error) => Err(AgencyClientError::from_msg(
82+
AgencyClientErrorKind::PostMessageFailed,
83+
format!("POST {} failed because response could not be decoded as utf-8, HTTP status: {}, \
84+
content-length header: {:?}, error: {:?}",
85+
url, response_status, content_length, error))),
7886
}
7987
}
8088

8189
fn set_ssl_cert_location() {
8290
let ssl_cert_file = "SSL_CERT_FILE";
83-
env::set_var(ssl_cert_file, env::var("EXTERNAL_STORAGE").unwrap() + "/cacert.pem"); //TODO: CHANGE ME, HARDCODING FOR TESTING ONLY
91+
92+
// TODO: CHANGE ME, HARDCODING FOR TESTING ONLY
93+
env::set_var(ssl_cert_file, env::var("EXTERNAL_STORAGE").unwrap() + "/cacert.pem");
94+
8495
match env::var(ssl_cert_file) {
8596
Ok(val) => info!("{}:: {:?}", ssl_cert_file, val),
86-
Err(e) => error!("couldn't find var in env {}:: {}. This needs to be set on Android to make https calls.\n See https://github.com/seanmonstar/reqwest/issues/70 for more info", ssl_cert_file, e),
97+
Err(e) => error!("couldn't find var in env {}:: {}. \
98+
This needs to be set on Android to make https calls.\n\
99+
See https://github.com/seanmonstar/reqwest/issues/70 for more info",
100+
ssl_cert_file, e),
87101
}
88102
info!("::SSL_CERT_FILE has been set");
89103
}

agency_client/src/internal/messaging.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::u8;
99
use serde_json::Value;
1010

1111
impl AgencyClient {
12-
pub async fn post_to_agency(&self, body_content: &Vec<u8>) -> AgencyClientResult<Vec<u8>> {
12+
pub async fn post_to_agency(&self, body_content: Vec<u8>) -> AgencyClientResult<Vec<u8>> {
1313
let url = self.get_agency_url_full();
1414
httpclient::post_message(body_content, &url).await
1515
}
@@ -203,7 +203,7 @@ impl AgencyClient {
203203
) -> AgencyClientResult<Vec<Client2AgencyMessage>> {
204204
trace!("send_message_to_agency >>> message: ..., did: {}", did);
205205
let data = self.prepare_message_for_agency(message, did, verkey).await?;
206-
let response = self.post_to_agency(&data).await?;
206+
let response = self.post_to_agency(data).await?;
207207
self.parse_response_from_agency(&response).await
208208
}
209209
}

aries_vcx/src/utils/mod.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,16 @@ pub async fn send_message(
7777
message: A2AMessage,
7878
) -> VcxResult<()> {
7979
trace!("send_message >>> message: {:?}, did_doc: {:?}", message, &did_doc);
80-
let envelope = EncryptionEnvelope::create(wallet_handle, &message, Some(&sender_verkey), &did_doc).await?;
81-
agency_client::httpclient::post_message(&envelope.0, &did_doc.get_endpoint()).await?;
80+
81+
let EncryptionEnvelope(envelope) = EncryptionEnvelope::create(
82+
wallet_handle,
83+
&message,
84+
Some(&sender_verkey),
85+
&did_doc)
86+
.await?;
87+
88+
agency_client::httpclient::post_message(envelope, &did_doc.get_endpoint()).await?;
89+
8290
Ok(())
8391
}
8492

@@ -92,7 +100,15 @@ pub async fn send_message_anonymously(
92100
message,
93101
&did_doc
94102
);
95-
let envelope = EncryptionEnvelope::create(wallet_handle, message, None, did_doc).await?;
96-
agency_client::httpclient::post_message(&envelope.0, &did_doc.get_endpoint()).await?;
103+
104+
let EncryptionEnvelope(envelope) = EncryptionEnvelope::create(
105+
wallet_handle,
106+
message,
107+
None,
108+
did_doc)
109+
.await?;
110+
111+
agency_client::httpclient::post_message(envelope, &did_doc.get_endpoint()).await?;
112+
97113
Ok(())
98114
}

0 commit comments

Comments
 (0)