Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
feat: set max response size for http transport (#41)
Browse files Browse the repository at this point in the history
* set max response size for http transport

* handle responze size exceeded limit

* change max size
  • Loading branch information
rikonor authored Jun 23, 2022
1 parent 76b4159 commit dcaa135
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
7 changes: 5 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ http-body = "0.4.5"
hyper = { version = "0.14", features = ["full"] }
hyper-rustls = { version = "0.23", features = [ "webpki-roots" ] }
hyper-tls = "0.5"
ic-agent = { version = "0.17" }
ic-agent = "0.17.1"
ic-utils = { version = "0.17", features = ["raw"] }
lazy-regex = "2"
opentelemetry = "0.17.0"
Expand Down
18 changes: 15 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const KB: usize = 1024;
const MB: usize = 1024 * KB;

const REQUEST_BODY_SIZE_LIMIT: usize = 10 * MB;
const RESPONSE_BODY_SIZE_LIMIT: usize = 10 * MB;

/// Resolve overrides for [`reqwest::ClientBuilder::resolve()`]
/// `ic0.app=[::1]:9090`
Expand Down Expand Up @@ -359,13 +360,15 @@ async fn forward_request(
// leak here because a user could use `dfx` to get the same reply.
match result {
Ok((http_response,)) => Ok(http_response),

Err(AgentError::ReplicaError {
reject_code,
reject_message,
}) => Err(Ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(format!(r#"Replica Error ({}): "{}""#, reject_code, reject_message).into())
.unwrap())),

Err(AgentError::HttpError(HttpErrorPayload {
status: 451,
content_type,
Expand All @@ -376,6 +379,12 @@ async fn forward_request(
.status(451)
.body(content.into())
.unwrap())),

Err(AgentError::ResponseSizeExceededLimit()) => Err(Ok(Response::builder()
.status(StatusCode::INSUFFICIENT_STORAGE)
.body("Response size exceeds limit".into())
.unwrap())),

Err(e) => Err(Err(e.into())),
}
}
Expand Down Expand Up @@ -800,14 +809,17 @@ async fn handle_request(
not_found()
}
} else {
let transport = ReqwestHttpReplicaV2Transport::create_with_client(replica_url, client)
.expect("failed to create transport")
.with_max_response_body_size(RESPONSE_BODY_SIZE_LIMIT);

let agent = Arc::new(
ic_agent::Agent::builder()
.with_transport(
ReqwestHttpReplicaV2Transport::create_with_client(replica_url, client).unwrap(),
)
.with_transport(transport)
.build()
.expect("Could not create agent..."),
);

if fetch_root_key && agent.fetch_root_key().await.is_err() {
unable_to_fetch_root_key()
} else {
Expand Down

0 comments on commit dcaa135

Please sign in to comment.