Skip to content

Commit d4939b6

Browse files
committed
refactored body, pass all tests
1 parent 87de0ee commit d4939b6

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

roa/src/compress.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,32 @@ impl<S: State> Middleware<S> for Compress {
4848
let level = self.0;
4949
let best_encoding = parse(&ctx.req().headers)
5050
.map_err(|err| Error::new(StatusCode::BAD_REQUEST, err, true))?;
51+
let body = std::mem::take(&mut ctx.resp_mut().body);
5152
let content_encoding = match best_encoding {
5253
None | Some(Encoding::Gzip) => {
5354
ctx.resp_mut()
54-
.wrapped(move |body| GzipEncoder::with_quality(body, level));
55+
.write_stream(GzipEncoder::with_quality(body, level));
5556
Encoding::Gzip.to_header_value()
5657
}
5758
Some(Encoding::Deflate) => {
5859
ctx.resp_mut()
59-
.wrapped(move |body| ZlibEncoder::with_quality(body, level));
60+
.write_stream(ZlibEncoder::with_quality(body, level));
6061
Encoding::Deflate.to_header_value()
6162
}
6263
Some(Encoding::Brotli) => {
6364
ctx.resp_mut()
64-
.wrapped(move |body| BrotliEncoder::with_quality(body, level));
65+
.write_stream(BrotliEncoder::with_quality(body, level));
6566
Encoding::Brotli.to_header_value()
6667
}
6768
Some(Encoding::Zstd) => {
6869
ctx.resp_mut()
69-
.wrapped(move |body| ZstdEncoder::with_quality(body, level));
70+
.write_stream(ZstdEncoder::with_quality(body, level));
7071
Encoding::Zstd.to_header_value()
7172
}
72-
Some(Encoding::Identity) => Encoding::Identity.to_header_value(),
73+
Some(Encoding::Identity) => {
74+
ctx.resp_mut().body = body;
75+
Encoding::Identity.to_header_value()
76+
}
7377
};
7478
ctx.resp_mut()
7579
.headers
@@ -122,9 +126,10 @@ mod tests {
122126
fn assert_consumed(assert_counter: usize) -> impl Middleware<()> {
123127
move |mut ctx: Context<()>, next: Next| async move {
124128
next.await?;
125-
ctx.resp_mut().wrapped(move |stream| Consumer {
129+
let body = std::mem::take(&mut ctx.resp_mut().body);
130+
ctx.resp_mut().write_stream(Consumer {
126131
counter: 0,
127-
stream,
132+
stream: body,
128133
assert_counter,
129134
});
130135
Ok(())

roa/src/logger.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use log::{error, info};
3535
use roa_core::http::{Method, StatusCode};
3636
use std::io;
3737
use std::pin::Pin;
38-
use std::sync::Arc;
3938
use std::time::Instant;
4039

4140
/// A finite-state machine to log success information in each streaming response.
@@ -68,7 +67,7 @@ impl LogTask {
6867
start,
6968
exec,
7069
} = self;
71-
exec.spawn_blocking(|| {
70+
exec.spawn_blocking(move || {
7271
info!(
7372
"<-- {} {} {}ms {} {}",
7473
method,
@@ -148,7 +147,7 @@ pub async fn logger<S: State>(mut ctx: Context<S>, next: Next) -> Result {
148147
})
149148
.await
150149
}
151-
(OK(_), Body::Bytes(bytes)) => {
150+
(Ok(_), Body::Bytes(bytes)) => {
152151
let size = bytes.size_hint();
153152
ctx.exec
154153
.spawn_blocking(move || {
@@ -170,7 +169,7 @@ pub async fn logger<S: State>(mut ctx: Context<S>, next: Next) -> Result {
170169
path,
171170
status_code,
172171
start,
173-
exec: ctx.exec.clone(),
172+
exec,
174173
});
175174
let logger = StreamLogger::Polling {
176175
stream: std::mem::take(stream),

0 commit comments

Comments
 (0)