11use arc_swap:: ArcSwap ;
22use cached:: proc_macro:: cached;
3+ use bytes:: Bytes ;
34use futures_lite:: future:: block_on;
4- use futures_lite:: { future:: Boxed , FutureExt } ;
5- use http_body_util:: BodyExt ;
5+ use futures_lite:: { future:: Boxed , FutureExt , StreamExt } ;
6+ use http_body_util:: { BodyExt , StreamBody } ;
7+ use hyper:: body:: Frame ;
68use hyper:: { header, Method , Request , Response } ;
79use log:: { error, info, trace, warn} ;
810use percent_encoding:: { percent_encode, CONTROLS } ;
@@ -13,12 +15,13 @@ use wreq_util::{Emulation, EmulationOS, EmulationOption};
1315
1416use std:: sync:: atomic:: Ordering ;
1517use std:: sync:: atomic:: { AtomicBool , AtomicU16 } ;
18+ use std:: convert:: Infallible ;
1619use std:: sync:: LazyLock ;
1720use std:: result:: Result ;
1821
1922use crate :: dbg_msg;
2023use crate :: oauth:: { force_refresh_token, token_daemon, Oauth , OauthBackendImpl } ;
21- use crate :: server:: RequestExt ;
24+ use crate :: server:: { Body , RequestExt } ;
2225use crate :: utils:: { format_url, Post } ;
2326
2427const REDDIT_URL_BASE : & str = "https://oauth.reddit.com" ;
@@ -69,8 +72,6 @@ pub fn build_client() -> WreqClient {
6972 . expect ( "Should always be able to build a client" )
7073}
7174
72- use crate :: server:: { full, Body } ;
73-
7475/// Convert a wreq Response into a hyper Response<Body>.
7576/// wreq and hyper now both use http v1.x, so header types are directly compatible.
7677trait IntoHyperResponse {
@@ -93,15 +94,18 @@ impl IntoHyperResponse for wreq::Response {
9394 }
9495 }
9596
96- let bytes = self . bytes ( ) . await . map_err ( |e| e. to_string ( ) ) ?;
97- // wreq already decompresses; remove the content-encoding header so
98- // downstream code doesn't try to decompress again.
97+ // wreq already decompresses; drop content-length since the final
98+ // size is unknown without buffering, relying on chunked encoding.
9999 if let Some ( h) = builder. headers_mut ( ) {
100100 h. remove ( header:: CONTENT_ENCODING ) ;
101- h. insert ( header:: CONTENT_LENGTH , bytes . len ( ) . into ( ) ) ;
101+ h. remove ( header:: CONTENT_LENGTH ) ;
102102 }
103103
104- builder. body ( full ( bytes) ) . map_err ( |e| e. to_string ( ) )
104+ let stream = self
105+ . bytes_stream ( )
106+ . filter_map ( |r| r. ok ( ) . map ( |b| Ok :: < Frame < Bytes > , Infallible > ( Frame :: data ( b) ) ) ) ;
107+
108+ builder. body ( BodyExt :: boxed ( StreamBody :: new ( stream) ) ) . map_err ( |e| e. to_string ( ) )
105109 }
106110}
107111
0 commit comments