Skip to content

Commit

Permalink
Merge pull request #210 from palantir/backport-4.7-content-length
Browse files Browse the repository at this point in the history
[backport] Propagate fixed size request body sizes
  • Loading branch information
sfackler authored May 6, 2024
2 parents 53e0231 + 1c1d81a commit 90868f1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ version: 2
jobs:
build:
docker:
- image: rust:1.65.0
- image: rust:1.75.0
environment:
RUSTFLAGS: -D warnings
steps:
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-209.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Fixed-size request bodies now set a `Content-Length`.
links:
- https://github.com/palantir/conjure-rust-runtime/pull/209
9 changes: 9 additions & 0 deletions conjure-runtime/src/raw/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use conjure_error::Error;
use conjure_http::client::{AsyncRequestBody, AsyncWriteBody};
use futures::channel::{mpsc, oneshot};
use futures::{pin_mut, Stream};
use http_body::SizeHint;
use hyper::HeaderMap;
use pin_project::pin_project;
use std::io::Cursor;
Expand Down Expand Up @@ -146,6 +147,14 @@ impl http_body::Body for RawBody {
fn is_end_stream(&self) -> bool {
matches!(self.inner, RawBodyInner::Empty)
}

fn size_hint(&self) -> SizeHint {
match &self.inner {
RawBodyInner::Empty => SizeHint::with_exact(0),
RawBodyInner::Single(buf) => SizeHint::with_exact(buf.len() as u64),
RawBodyInner::Stream { .. } => SizeHint::new(),
}
}
}

pub(crate) enum Writer<'a> {
Expand Down
2 changes: 1 addition & 1 deletion conjure-runtime/src/service/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
use crate::config;
pub use crate::service::proxy::connector::{ProxyConnectorLayer, ProxyConnectorService};
pub use crate::service::proxy::request::{ProxyLayer, ProxyService};
pub use crate::service::proxy::request::ProxyLayer;
use base64::display::Base64Display;
use base64::engine::general_purpose::STANDARD;
use conjure_error::Error;
Expand Down
55 changes: 55 additions & 0 deletions conjure-runtime/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use conjure_runtime_config::ServiceConfig;
use flate2::write::GzEncoder;
use flate2::Compression;
use futures::{join, pin_mut};
use http::header::{CONTENT_LENGTH, TRANSFER_ENCODING};
use http::{request, Method};
use hyper::body;
use hyper::header::{ACCEPT_ENCODING, CONTENT_ENCODING};
Expand Down Expand Up @@ -753,3 +754,57 @@ security:
)
.await
}

#[tokio::test]
async fn empty_body_has_no_transfer_encoding() {
test(
STOCK_CONFIG,
1,
|req| async move {
assert_eq!(req.headers().get(CONTENT_LENGTH), None);
assert_eq!(req.headers().get(TRANSFER_ENCODING), None);
Ok(Response::new(hyper::Body::empty()))
},
|builder| async move {
builder
.build()
.unwrap()
.send(
req()
.method(Method::POST)
.body(AsyncRequestBody::Empty)
.unwrap(),
)
.await
.unwrap();
},
)
.await
}

#[tokio::test]
async fn fixed_body_has_content_length() {
test(
STOCK_CONFIG,
1,
|req| async move {
assert_eq!(req.headers().get(CONTENT_LENGTH).unwrap(), "4");
assert_eq!(req.headers().get(TRANSFER_ENCODING), None);
Ok(Response::new(hyper::Body::empty()))
},
|builder| async move {
builder
.build()
.unwrap()
.send(
req()
.method(Method::POST)
.body(AsyncRequestBody::Fixed(Bytes::from_static(b"1234")))
.unwrap(),
)
.await
.unwrap();
},
)
.await
}

0 comments on commit 90868f1

Please sign in to comment.